Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Select a model
|
| 5 |
+
model_name = "Salesforce/codegen-2B-mono" # Ensure this model is available
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Function to generate code based on a prompt
|
| 10 |
+
def generate_code(prompt):
|
| 11 |
+
# Adjust parameters to improve output quality
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
outputs = model.generate(
|
| 14 |
+
**inputs,
|
| 15 |
+
max_new_tokens=100, # Adjust as needed for code length
|
| 16 |
+
temperature=0.3, # Lower temperature for more deterministic output
|
| 17 |
+
top_p=0.9, # Top-p filtering to focus on more likely completions
|
| 18 |
+
repetition_penalty=1.2, # Penalizes repetitive phrases
|
| 19 |
+
do_sample=True # Enables sampling for a creative touch
|
| 20 |
+
)
|
| 21 |
+
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
return generated_code
|
| 23 |
+
|
| 24 |
+
# Create a Gradio interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=generate_code,
|
| 27 |
+
inputs=gr.Textbox(lines=5, label="Enter your prompt"),
|
| 28 |
+
outputs=gr.Code(language="python", label="Generated Code"),
|
| 29 |
+
title="Python Code Generator",
|
| 30 |
+
description="Enter a description of the Python code you want to generate."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the interface
|
| 34 |
+
iface.launch()
|