Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load the model
|
| 5 |
+
model_name = "Salesforce/codegen-350M-mono"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, pad_token_id=tokenizer.eos_token_id)
|
| 9 |
+
|
| 10 |
+
# Function to generate code
|
| 11 |
+
def generate_code(prompt):
|
| 12 |
+
output = generator(prompt, max_new_tokens=256, do_sample=True, temperature=0.3, top_p=0.95)
|
| 13 |
+
return output[0]["generated_text"]
|
| 14 |
+
|
| 15 |
+
# Gradio UI
|
| 16 |
+
ui = gr.Interface(
|
| 17 |
+
fn=generate_code,
|
| 18 |
+
inputs=gr.Textbox(lines=4, label="💬 Enter your Python prompt"),
|
| 19 |
+
outputs=gr.Code(label="🧠 Generated Python Code"),
|
| 20 |
+
title="🤖 AI Python Code Generator",
|
| 21 |
+
description="Type a task like 'write a function to reverse a list', and get Python code.",
|
| 22 |
+
theme="default"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
ui.launch()
|