Not-Grim-Refer commited on
Commit
e9cd936
·
1 Parent(s): 8e98aef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -44
app.py CHANGED
@@ -2,62 +2,42 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
3
 
4
 
5
- title = "Code Explainer"
6
- description = "This is a space to convert Python code into english text explaining what it does using [codeparrot-small-code-to-text](https://huggingface.co/codeparrot/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-code-to-text) a dataset of Python code followed by a docstring explaining it, the data was originally extracted from Jupyter notebooks."
8
-
9
- EXAMPLE_1 = "def sort_function(arr):\n n = len(arr)\n \n # Traverse through all array elements\n for i in range(n):\n \n # Last i elements are already in place\n for j in range(0, n-i-1):\n \n # traverse the array from 0 to n-i-1\n # Swap if the element found is greater\n # than the next element\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]"
10
- EXAMPLE_2 = "from sklearn import model_selection\nX_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.2)"
11
- EXAMPLE_3 = "def load_text(file)\n with open(filename, 'r') as f:\n text = f.read()\n return text"
12
- example = [
13
- [EXAMPLE_1, 32, 0.6, 42],
14
- [EXAMPLE_2, 16, 0.6, 42],
15
- [EXAMPLE_3, 11, 0.2, 42],
16
- ]
17
-
18
- # change model to the finetuned one
19
  tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small-code-to-text")
20
  model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small-code-to-text")
 
 
21
 
22
  def make_doctring(gen_prompt):
23
  return gen_prompt + f"\n\n\"\"\"\nExplanation:"
24
 
25
- def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
 
26
  set_seed(seed)
27
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
28
- prompt = make_doctring(gen_prompt)
29
- generated_text = pipe(prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
30
- return generated_text
31
 
 
 
 
32
 
 
 
 
 
 
 
33
  iface = gr.Interface(
34
- fn=code_generation,
35
  inputs=[
36
- gr.Code(lines=10, label="Python code"),
37
- gr.inputs.Slider(
38
- minimum=8,
39
- maximum=256,
40
- step=1,
41
- default=8,
42
- label="Number of tokens to generate",
43
- ),
44
- gr.inputs.Slider(
45
- minimum=0,
46
- maximum=2.5,
47
- step=0.1,
48
- default=0.6,
49
- label="Temperature",
50
- ),
51
- gr.inputs.Slider(
52
- minimum=0,
53
- maximum=1000,
54
- step=1,
55
- default=42,
56
- label="Random seed to use for the generation"
57
- )
58
  ],
59
- outputs=gr.Code(label="Predicted explanation", lines=10),
60
- examples=example,
61
  layout="horizontal",
62
  theme="peach",
63
  description=description,
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
3
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small-code-to-text")
6
  model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small-code-to-text")
7
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, num_return_sequences=1, device=0)
8
+
9
 
10
  def make_doctring(gen_prompt):
11
  return gen_prompt + f"\n\n\"\"\"\nExplanation:"
12
 
13
+
14
+ def code_generation(gen_prompts, max_tokens=8, temperature=0.6, seed=42):
15
  set_seed(seed)
16
+ prompts = [make_doctring(p) for p in gen_prompts]
17
+ generated_text = pipe(prompts, do_sample=True, top_p=0.95, temperature=temperature, max_length=max_tokens)[0]
18
+ return generated_text["generated_text"]
19
+
20
 
21
+ title = "Code Explainer"
22
+ description = "This is a space to convert Python code into english text explaining what it does using [codeparrot-small-code-to-text](https://huggingface.co/codeparrot/codeparrot-small-code-to-text),\
23
+ a code generation model for Python finetuned on [github-jupyter-code-to-text](https://huggingface.co/datasets/codeparrot/github-jupyter-code-to-text) a dataset of Python code followed by a docstring explaining it, the data was originally extracted from Jupyter notebooks."
24
 
25
+ EXAMPLES = [
26
+ ["def sort_function(arr):\n n = len(arr)\n \n # Traverse through all array elements\n for i in range(n):\n \n # Last i elements are already in place\n for j in range(0, n-i-1):\n \n # traverse the array from 0 to n-i-1\n # Swap if the element found is greater\n # than the next element\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]"],
27
+ ["from sklearn import model_selection\nX_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.2)"],
28
+ ["def load_text(filename):\n with open(filename, 'r') as f:\n text = f.read()\n return text"]
29
+ ]
30
+
31
  iface = gr.Interface(
32
+ fn=code_generation,
33
  inputs=[
34
+ gr.inputs.Code(language="python", label="Python code snippet", lines=10),
35
+ gr.inputs.Slider(minimum=8, maximum=256, step=1, default=8, label="Number of tokens to generate"),
36
+ gr.inputs.Slider(minimum=0, maximum=2.5, step=0.1, default=0.6, label="Temperature"),
37
+ gr.inputs.Slider(minimum=0, maximum=1000, step=1, default=42, label="Random seed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  ],
39
+ outputs=gr.outputs.Code(language="text", label="Generated explanation", lines=10),
40
+ examples=EXAMPLES,
41
  layout="horizontal",
42
  theme="peach",
43
  description=description,