File size: 1,673 Bytes
c64b520
 
58518f0
c64b520
 
 
 
 
 
5d75ab3
 
 
 
 
c64b520
 
58518f0
 
c64b520
 
 
 
 
 
 
 
58518f0
c64b520
58518f0
 
 
c64b520
58518f0
 
 
 
c64b520
58518f0
 
c64b520
 
58518f0
c64b520
 
 
 
 
 
 
 
 
 
 
 
58518f0
c64b520
 
58518f0
 
 
c64b520
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# ----------------------
# 1. تحميل المودل
# ----------------------
model_name = "bigcode/starcoder2-7b"

tokenizer = AutoTokenizer.from_pretrained(
    model_name,
    trust_remote_code=True
)

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16,   # FP16 لتقليل استهلاك VRAM
    device_map="auto",
    trust_remote_code=True
)
model.eval()

# ----------------------
# 2. دالة التوليد
# ----------------------
def generate_code(prompt):
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=700,
            temperature=0.2,
            do_sample=False,
            eos_token_id=tokenizer.eos_token_id,
            pad_token_id=tokenizer.eos_token_id,
            repetition_penalty=1.1
        )
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

# ----------------------
# 3. واجهة Gradio بدون allow_flagging
# ----------------------
title = "StarCoder2 Flutter Code Generator"
description = """
Generate Dart / Flutter code using StarCoder2.
Type your prompt describing the widget or functionality you want.
"""

demo = gr.Interface(
    fn=generate_code,
    inputs=gr.Textbox(lines=8, placeholder="Write your Flutter prompt here..."),
    outputs=gr.Textbox(lines=20),
    title=title,
    description=description
)

# ----------------------
# 4. تشغيل الواجهة
# ----------------------
demo.launch()