File size: 1,517 Bytes
3964b4c
 
 
 
b11f128
3964b4c
b11f128
 
 
 
3964b4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()