loubnabnl HF staff commited on
Commit
a77e67e
1 Parent(s): 4aade44
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
3
+ from transformers import pipeline
4
+
5
+
6
+ title = "InCoder Generator"
7
+ description = "This is a subspace to make code generation with InCoder, it is used in a larger space for model comparison."
8
+ example = [
9
+ ["def print_hello_world():", "Sample", 8, 42],
10
+ ["def get_file_size(filepath):", "Sample", 22, 42]]
11
+ tokenizer = AutoTokenizer.from_pretrained("facebook/incoder-6B")
12
+ model = AutoModelForCausalLM.from_pretrained("facebook/incoder-6B", low_cpu_mem_usage=True)
13
+
14
+
15
+ def code_generation(gen_prompt, strategy, max_tokens, seed=42):
16
+ set_seed(seed)
17
+ gen_kwargs = {}
18
+ gen_kwargs["do_sample"] = strategy == "Sample"
19
+ gen_kwargs["max_new_tokens"] = max_tokens
20
+ if gen_kwargs["do_sample"]:
21
+ gen_kwargs["temperature"] = 0.2
22
+ gen_kwargs["top_k"] = 0
23
+ gen_kwargs["top_p"] = 0.95
24
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
25
+ generated_text = pipe(gen_prompt, **gen_kwargs)[0]['generated_text']
26
+ return generated_text
27
+
28
+
29
+ iface = gr.Interface(
30
+ fn=code_generation,
31
+ inputs=[
32
+ gr.Textbox(lines=10, label="Input code"),
33
+ gr.Dropdown(choices=["Greedy", "Sample"], value="Greedy"),
34
+ gr.inputs.Slider(
35
+ minimum=8,
36
+ maximum=256,
37
+ step=1,
38
+ default=8,
39
+ label="Number of tokens to generate",
40
+ ),
41
+ gr.inputs.Slider(
42
+ minimum=0,
43
+ maximum=1000,
44
+ step=1,
45
+ default=42,
46
+ label="Random seed to use for the generation"
47
+ )
48
+ ],
49
+ outputs=gr.Textbox(label="Predicted code", lines=10),
50
+ examples=example,
51
+ layout="horizontal",
52
+ theme="peach",
53
+ description=description,
54
+ title=title
55
+ )
56
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ accelerate
3
+ torch