Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# تحميل الموديل
|
| 6 |
+
model_name = "Salesforce/codegen-350M-mono"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to("cpu")
|
| 9 |
+
|
| 10 |
+
# دالة التوليد
|
| 11 |
+
def generate_code(prompt):
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
outputs = model.generate(**inputs, max_length=128, num_return_sequences=1)
|
| 14 |
+
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 15 |
+
return code
|
| 16 |
+
|
| 17 |
+
# واجهة Gradio
|
| 18 |
+
gr.Interface(
|
| 19 |
+
fn=generate_code,
|
| 20 |
+
inputs=gr.Textbox(lines=5, placeholder="Describe what code you want...", label="Prompt"),
|
| 21 |
+
outputs=gr.Textbox(label="Generated Code"),
|
| 22 |
+
title="Code Generator - Mono Model",
|
| 23 |
+
description="Generate Python code from a text description using CodeGen-350M-Mono model"
|
| 24 |
+
).launch()
|