File size: 1,520 Bytes
3964b4c
 
 
 
a2c79e6
3964b4c
a2c79e6
 
 
 
3964b4c
 
 
 
 
 
a2c79e6
 
 
 
 
3964b4c
 
a2c79e6
3964b4c
 
 
 
a2c79e6
3964b4c
 
 
 
 
 
 
 
 
a2c79e6
 
3964b4c
 
 
a2c79e6
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
42
43
44
45
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# ู†ู…ุงุฐุฌ ุฃูƒูŠุฏุฉ ูˆู…ูุชูˆุญุฉ
models = {
    "CodeGen 2B": "Salesforce/codegen-2B-multi",
    "CodeParrot": "codeparrot/codeparrot-small",
    "GPT-J-6B": "EleutherAI/gpt-j-6B",
    "GPT2": "gpt2"  # ู†ู…ูˆุฐุฌ ุจุณูŠุท ูƒู€ fallback
}

# ุชุญู…ูŠู„ ุงู„ู†ู…ุงุฐุฌ
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 if torch.cuda.is_available() else torch.float32
    )
    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)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# ูˆุงุฌู‡ุฉ 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 Open AI Models",
    description="ุงุฎุชุฑ ู†ู…ูˆุฐุฌู‹ุง ู…ูุชูˆุญู‹ุง ูˆุฃุฏุฎู„ ูˆุตูู‹ุง ู„ูŠุชู… ุชูˆู„ูŠุฏ ุงู„ูƒูˆุฏ ุชู„ู‚ุงุฆูŠู‹ุง"
)

demo.launch()