loubnabnl HF staff commited on
Commit
f224c77
·
1 Parent(s): 593193e

add app.py

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