Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
title = "Santa Model Generator"
|
| 7 |
+
description = "Demo"
|
| 8 |
+
example = [
|
| 9 |
+
["def print_hello_world():", 8, 0.6, 42],
|
| 10 |
+
["def get_file_size(filepath):", 24, 0.6, 42],
|
| 11 |
+
["def count_lines(filename):", 40, 0.6, 42],
|
| 12 |
+
["def count_words(filename):", 40, 0.6, 42]]
|
| 13 |
+
|
| 14 |
+
token = os.environ["HUB_TOKEN"]
|
| 15 |
+
device="cpu" #"cuda:0"
|
| 16 |
+
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained("bigcode/christmas-models", use_auth_token=token)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained("bigcode/christmas-models", revision="dedeup-alt", trust_remote_code=True, use_auth_token=token)
|
| 19 |
+
|
| 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 |
+
generated_text = pipe(gen_prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
|
| 25 |
+
return generated_text
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=code_generation,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(lines=10, label="Input code"),
|
| 32 |
+
gr.inputs.Slider(
|
| 33 |
+
minimum=8,
|
| 34 |
+
maximum=1000,
|
| 35 |
+
step=1,
|
| 36 |
+
default=8,
|
| 37 |
+
label="Number of tokens to generate",
|
| 38 |
+
),
|
| 39 |
+
gr.inputs.Slider(
|
| 40 |
+
minimum=0,
|
| 41 |
+
maximum=2.5,
|
| 42 |
+
step=0.1,
|
| 43 |
+
default=0.6,
|
| 44 |
+
label="Temperature",
|
| 45 |
+
),
|
| 46 |
+
gr.inputs.Slider(
|
| 47 |
+
minimum=0,
|
| 48 |
+
maximum=1000,
|
| 49 |
+
step=1,
|
| 50 |
+
default=42,
|
| 51 |
+
label="Random seed to use for the generation"
|
| 52 |
+
)
|
| 53 |
+
],
|
| 54 |
+
outputs=gr.Textbox(label="Predicted code", lines=10),
|
| 55 |
+
examples=example,
|
| 56 |
+
layout="horizontal",
|
| 57 |
+
theme="peach",
|
| 58 |
+
description=description,
|
| 59 |
+
title=title
|
| 60 |
+
)
|
| 61 |
+
iface.launch()
|