loubnabnl HF staff commited on
Commit
9716d2d
1 Parent(s): 7a717f8

update files

Browse files
Files changed (3) hide show
  1. README.md +4 -4
  2. app.py +62 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
  title: Python To Text
3
- emoji: 🔥
4
- colorFrom: yellow
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.0.26
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
1
  ---
2
  title: Python To Text
3
+ emoji: 🪞
4
+ colorFrom: red
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.0.24
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
3
+
4
+
5
+ title = "Python to Text Converter [WIP]"
6
+ description = "This is a space to convert Python code into english text explaining what it does using [codeparrot-small-code-to-text](codeparrot-small-code-to-text),\
7
+ a code generation model for Python finetuned on [github-jupyter-code-to-text](https://huggingface.co/datasets/codeparrot/github-jupyter-text) a dataset Python code followed by a doctring explaining it, the data was extracted from Jupyter notebooks."
8
+ example = [
9
+ ["example1", 65, 0.6, 42],
10
+ ["example2", 60, 0.6, 42],
11
+ ["example3", 87, 0.6, 42],
12
+ ]
13
+
14
+ # change model to the finetuned one
15
+ tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small")
16
+ model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small")
17
+
18
+ def make_doctring(gen_prompt):
19
+ return gen_prompt + f"\n\n\"\"\"\nExplanation:"
20
+
21
+ def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
22
+ set_seed(seed)
23
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
24
+ prompt = make_doctring(gen_prompt)
25
+ generated_text = pipe(prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[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="Python code"),
33
+ gr.inputs.Slider(
34
+ minimum=8,
35
+ maximum=256,
36
+ step=1,
37
+ default=8,
38
+ label="Number of tokens to generate",
39
+ ),
40
+ gr.inputs.Slider(
41
+ minimum=0,
42
+ maximum=2.5,
43
+ step=0.1,
44
+ default=0.6,
45
+ label="Temperature",
46
+ ),
47
+ gr.inputs.Slider(
48
+ minimum=0,
49
+ maximum=1000,
50
+ step=1,
51
+ default=42,
52
+ label="Random seed to use for the generation"
53
+ )
54
+ ],
55
+ outputs=gr.Textbox(label="Predicted explanation", lines=10),
56
+ examples=example,
57
+ layout="horizontal",
58
+ theme="peach",
59
+ description=description,
60
+ title=title
61
+ )
62
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers==4.19.0
2
+ torch==1.11.0