ai-codegen-app / app.py
LeenAnabtawe's picture
Update app.py
b11f128 verified
raw
history blame
1.52 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# ู†ู…ุงุฐุฌ ู…ูุชูˆุญุฉ ู„ุชูˆู„ูŠุฏ ุงู„ูƒูˆุฏ
models = {
"CodeGen 2B (Salesforce)": "Salesforce/codegen-2B-multi",
"WizardCoder 1B": "WizardLM/WizardCoder-1B-V1.0",
"CodeParrot Small": "codeparrot/codeparrot-small",
"GPT-J-6B (Python)": "EleutherAI/gpt-j-6B" # ุจุฏูŠู„ ู…ูุชูˆุญ ู„ู€ Phind LLaMA
}
# ุชุญู…ูŠู„ ุงู„ู†ู…ุงุฐุฌ
loaded_models = {}
for name, model_id in models.items():
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.float16)
loaded_models[name] = (tokenizer, model)
# ุฏุงู„ุฉ ุชูˆู„ูŠุฏ ุงู„ูƒูˆุฏ
def generate_code(prompt, model_name):
tokenizer, model = loaded_models[model_name]
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=150)
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
return code
# ูˆุงุฌู‡ุฉ Gradio
demo = gr.Interface(
fn=generate_code,
inputs=[
gr.Textbox(lines=5, label="ุงูƒุชุจ ูˆุตู ุงู„ูƒูˆุฏ (ุจุงู„ุฅู†ุฌู„ูŠุฒูŠุฉ)"),
gr.Radio(choices=list(models.keys()), label="ุงุฎุชุฑ ุงู„ู†ู…ูˆุฐุฌ")
],
outputs=gr.Code(label="ุงู„ูƒูˆุฏ ุงู„ู†ุงุชุฌ"),
title="Code Generation with AI Models",
description="ุงุฎุชุฑ ู†ู…ูˆุฐุฌ AI ูˆุงุฏุฎู„ ูˆุตู ุงู„ูƒูˆุฏ ู„ูŠุชู… ุชูˆู„ูŠุฏู‡ ุชู„ู‚ุงุฆูŠู‹ุง"
)
demo.launch()