Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,45 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# Load the Starcoder2 model and tokenizer
|
| 6 |
+
model_name = "bigcode/starcoder2-3b"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Load a sample dataset (you can change this to any dataset you prefer)
|
| 11 |
+
dataset = load_dataset("code_search_net", "python", split="train[:100]")
|
| 12 |
+
|
| 13 |
+
def generate_code(prompt, max_length=100):
|
| 14 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 15 |
+
outputs = model.generate(**inputs, max_length=max_length)
|
| 16 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
+
|
| 18 |
+
def get_random_sample():
|
| 19 |
+
random_sample = dataset[int(len(dataset) * gr.Random().random())]
|
| 20 |
+
return random_sample['func_code_string']
|
| 21 |
+
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("# Starcoder2 Code Generation Demo")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
input_text = gr.Textbox(label="Input Prompt", lines=5)
|
| 28 |
+
max_length = gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Max Output Length")
|
| 29 |
+
submit_btn = gr.Button("Generate Code")
|
| 30 |
+
random_btn = gr.Button("Get Random Sample")
|
| 31 |
+
|
| 32 |
+
with gr.Column():
|
| 33 |
+
output_text = gr.Textbox(label="Generated Code", lines=10)
|
| 34 |
+
|
| 35 |
+
submit_btn.click(generate_code, inputs=[input_text, max_length], outputs=output_text)
|
| 36 |
+
random_btn.click(get_random_sample, outputs=input_text)
|
| 37 |
+
|
| 38 |
+
gr.Markdown("""
|
| 39 |
+
## How to use:
|
| 40 |
+
1. Enter a prompt in the input box or click 'Get Random Sample' to load a random code snippet.
|
| 41 |
+
2. Adjust the max output length if needed.
|
| 42 |
+
3. Click 'Generate Code' to see the model's output.
|
| 43 |
+
""")
|
| 44 |
+
|
| 45 |
+
demo.launch()
|