Code generator
#19
by
Samuelblue
- opened
- codegenerator.py +57 -0
codegenerator.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# change model to the finetuned one
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small-text-to-code")
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small-text-to-code")
|
| 12 |
+
|
| 13 |
+
def make_doctring(gen_prompt):
|
| 14 |
+
return "\"\"\"\n" + gen_prompt + "\n\"\"\"\n\n"
|
| 15 |
+
|
| 16 |
+
def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
|
| 17 |
+
set_seed(seed)
|
| 18 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 19 |
+
prompt = make_doctring(gen_prompt)
|
| 20 |
+
generated_text = pipe(prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
|
| 21 |
+
return generated_text
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=code_generation,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Textbox(lines=10, label="Text"),
|
| 28 |
+
gr.inputs.Slider(
|
| 29 |
+
minimum=8,
|
| 30 |
+
maximum=1000,
|
| 31 |
+
step=1,
|
| 32 |
+
default=8,
|
| 33 |
+
label="Number of tokens to generate",
|
| 34 |
+
),
|
| 35 |
+
gr.inputs.Slider(
|
| 36 |
+
minimum=0,
|
| 37 |
+
maximum=2.5,
|
| 38 |
+
step=0.1,
|
| 39 |
+
default=0.6,
|
| 40 |
+
label="Temperature",
|
| 41 |
+
),
|
| 42 |
+
gr.inputs.Slider(
|
| 43 |
+
minimum=0,
|
| 44 |
+
maximum=1000,
|
| 45 |
+
step=1,
|
| 46 |
+
default=42,
|
| 47 |
+
label="Random seed to use for the generation"
|
| 48 |
+
)
|
| 49 |
+
],
|
| 50 |
+
outputs=gr.Textbox(label="Python code", lines=10),
|
| 51 |
+
examples=example,
|
| 52 |
+
layout="horizontal",
|
| 53 |
+
theme="peach",
|
| 54 |
+
description=description,
|
| 55 |
+
title=title
|
| 56 |
+
)
|
| 57 |
+
iface.launch()
|