mostafasmart commited on
Commit
c64b520
·
1 Parent(s): 9a4de5c

Add StarCoder2 Flutter generator

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4
+
5
+ # ----------------------
6
+ # 1. تحميل المودل
7
+ # ----------------------
8
+ model_name = "bigcode/starcoder2-7b"
9
+
10
+ bnb_config = BitsAndBytesConfig(load_in_8bit=True) # 8-bit لتوفير VRAM وتسريع التوليد
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ model_name,
15
+ device_map="auto",
16
+ quantization_config=bnb_config,
17
+ trust_remote_code=True
18
+ )
19
+ model.eval()
20
+
21
+ # ----------------------
22
+ # 2. دالة التوليد
23
+ # ----------------------
24
+ def generate_code(prompt):
25
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
26
+ with torch.no_grad():
27
+ output_ids = model.generate(
28
+ input_ids,
29
+ max_new_tokens=300,
30
+ do_sample=False,
31
+ temperature=0.2,
32
+ repetition_penalty=1.05,
33
+ use_cache=True,
34
+ pad_token_id=tokenizer.eos_token_id
35
+ )
36
+ text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
37
+ return text
38
+
39
+ # ----------------------
40
+ # 3. واجهة Gradio
41
+ # ----------------------
42
+ title = "StarCoder2 Flutter Code Generator"
43
+ description = """
44
+ Generate Dart / Flutter code using StarCoder2.
45
+ Type your prompt describing the widget or functionality you want.
46
+ """
47
+
48
+ demo = gr.Interface(
49
+ fn=generate_code,
50
+ inputs=gr.Textbox(lines=8, placeholder="Write your Flutter prompt here..."),
51
+ outputs=gr.Textbox(lines=20),
52
+ title=title,
53
+ description=description,
54
+ allow_flagging="never"
55
+ )
56
+
57
+ demo.launch()