manthilaffs commited on
Commit
165e807
·
verified ·
1 Parent(s): 9b9ba13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -62
app.py CHANGED
@@ -3,9 +3,6 @@ import torch
3
  import spaces
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
- # ----------------------------------------------------
7
- # Globals
8
- # ----------------------------------------------------
9
  model = None
10
  tokenizer = None
11
 
@@ -20,20 +17,14 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
20
  ### ප්‍රතිචාරය:
21
  {}"""
22
 
23
- # ----------------------------------------------------
24
- # GPU inference — official ZeroGPU style
25
- # ----------------------------------------------------
26
  @spaces.GPU
27
  def infer(instruction, input_text=""):
28
  global model, tokenizer
29
-
30
  if model is None:
31
- tokenizer = AutoTokenizer.from_pretrained(
32
- "manthilaffs/Gamunu-4B-Instruct-Alpha"
33
- )
34
  model = AutoModelForCausalLM.from_pretrained(
35
  "manthilaffs/Gamunu-4B-Instruct-Alpha",
36
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
37
  device_map="auto",
38
  )
39
  model.eval()
@@ -46,72 +37,86 @@ def infer(instruction, input_text=""):
46
  )
47
 
48
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
49
-
50
  with torch.inference_mode():
51
  outputs = model.generate(
52
  **inputs,
53
  max_new_tokens=256,
54
- temperature=0.4,
55
- top_k=64,
56
- top_p=0.95,
57
  repetition_penalty=1.05,
58
  )
59
-
60
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
  if "### ප්‍රතිචාරය:" in text:
62
  text = text.split("### ප්‍රතිචාරය:")[-1].strip()
63
  return text
64
 
65
 
66
- # ----------------------------------------------------
67
- # Gradio UI
68
- # ----------------------------------------------------
69
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
- gr.Markdown(
71
- """
72
- # 🧠 Gamunu 4B Instruct Alpha
73
- *Sinhala Instruct LLM ZeroGPU Demo*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- ⚙️ Pure Transformers Inference | 💠 ZeroGPU GPU Burst
76
- """
77
- )
78
-
79
- with gr.Row():
80
- instruction = gr.Textbox(
81
- label="🧾 Instruction / Question",
82
- placeholder="උදා: ඊයේ රු. 30 ක ඇපල් ගෙඩියක් අද රු. 60 නම් මිල වෙනස කීයද?",
83
- lines=2,
84
- )
85
-
86
- with gr.Row():
87
- input_text = gr.Textbox(
88
- label="📥 Additional Context (Optional)",
89
- placeholder="අමතර තොරතුරු (ඇත්නම්) එහි සටහන් කරන්න",
90
- lines=2,
91
- )
92
-
93
- output = gr.Markdown(label="🧩 Gamunu Response")
94
- run_btn = gr.Button("🔮 Generate Response")
95
  run_btn.click(infer, inputs=[instruction, input_text], outputs=output)
96
 
97
- gr.Examples(
98
- examples=[
99
- ["ඉන්දියානු මහා සමුද්‍රය යනු කොහෙද?", ""],
100
- ["කාලගුණය අවතැන් වන්නේ මොනවක් නිසාද?", ""],
101
- ["මුල්ම අගමැති කවුද?", ""],
102
- ],
103
- inputs=[instruction],
104
- label="🪄 Example Questions (Click to try)",
105
- )
106
-
107
- gr.Markdown(
108
- """
109
- ---
110
- 🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
111
- 🧰 **Stack:** Transformers + Torch + Gradio + ZeroGPU
112
- © 2025 Gamunu Project | Experimental Release
113
- """
114
- )
115
 
116
  if __name__ == "__main__":
117
  demo.launch()
 
3
  import spaces
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
 
 
 
6
  model = None
7
  tokenizer = None
8
 
 
17
  ### ප්‍රතිචාරය:
18
  {}"""
19
 
 
 
 
20
  @spaces.GPU
21
  def infer(instruction, input_text=""):
22
  global model, tokenizer
 
23
  if model is None:
24
+ tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
 
 
25
  model = AutoModelForCausalLM.from_pretrained(
26
  "manthilaffs/Gamunu-4B-Instruct-Alpha",
27
+ dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
28
  device_map="auto",
29
  )
30
  model.eval()
 
37
  )
38
 
39
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
40
  with torch.inference_mode():
41
  outputs = model.generate(
42
  **inputs,
43
  max_new_tokens=256,
 
 
 
44
  repetition_penalty=1.05,
45
  )
 
46
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
  if "### ප්‍රතිචාරය:" in text:
48
  text = text.split("### ප්‍රතිචාරය:")[-1].strip()
49
  return text
50
 
51
 
52
+ with gr.Blocks(css="""
53
+ .gradio-container {max-width: 1100px !important; margin:auto;}
54
+ h1, h2, h3, h4, h5 {text-align:center;}
55
+ #title-bar {background:linear-gradient(90deg,#804dee,#ee82ee);color:white;padding:0.6rem;border-radius:1rem;margin-bottom:0.6rem;}
56
+ textarea, input {font-family:'Noto Sans Sinhala',sans-serif;}
57
+ """, theme=gr.themes.Soft()) as demo:
58
+ gr.HTML("<div id='title-bar'><h1>🧠 Gamunu 4B Instruct Alpha</h1><h4>Sinhala Instruct LLM — ZeroGPU Demo</h4></div>")
59
+ gr.Markdown("⚙️ **Pure Transformers Inference** | 💠 ZeroGPU GPU Burst")
60
+
61
+ with gr.Row(equal_height=True):
62
+ with gr.Column(scale=1, min_width=350):
63
+ instruction = gr.Textbox(
64
+ label="🧾 Instruction / Question",
65
+ placeholder="උදා: ඊයේ රු. 30 ක ඇපල් ගෙඩියක් අද රු. 60 නම් මිල වෙනස කීයද?",
66
+ lines=4,
67
+ )
68
+ input_text = gr.Textbox(
69
+ label="📥 Additional Context (Optional)",
70
+ placeholder="අමතර තොරතුරු ඇතුළත් කරන්න (ඇත්නම්)",
71
+ lines=3,
72
+ )
73
+ run_btn = gr.Button("🔮 Generate Response", variant="primary", scale=1)
74
+ with gr.Column(scale=1, min_width=350):
75
+ output = gr.Markdown(label="🧩 Gamunu Response", elem_id="output-box")
76
+
77
+ # Example categories
78
+ with gr.Accordion("🧮 Example Prompts by Category", open=False):
79
+ with gr.Tab("Maths"):
80
+ gr.Examples(
81
+ examples=[
82
+ ["රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්‍රතිශතයකින් කීයද?", ""],
83
+ ["සියයට 10 ක වර්ධනයක් තිබේ නම් අලුත් අගය කීයද?", ""],
84
+ ],
85
+ inputs=[instruction],
86
+ )
87
+ with gr.Tab("Roleplay"):
88
+ gr.Examples(
89
+ examples=[
90
+ ["ඔබ ගුරුවරයෙකු ලෙස ක්‍රියාකරන්න. ශිෂ්‍යයාට ගණිතය උගන්වන්න.", ""],
91
+ ["ඔබ පරිසර විද්‍යාඥයෙකු ලෙස වායු මණ්ඩලය පැහැදිලි කරන්න.", ""],
92
+ ],
93
+ inputs=[instruction],
94
+ )
95
+ with gr.Tab("QA"):
96
+ gr.Examples(
97
+ examples=[
98
+ ["ශ්‍රී ලංකාවේ මුල්ම අගමැති කවුද?", ""],
99
+ ["ඉන්දියානු මහා සමුද්‍රය යනු කොහෙද?", ""],
100
+ ],
101
+ inputs=[instruction],
102
+ )
103
+ with gr.Tab("NLP"):
104
+ gr.Examples(
105
+ examples=[
106
+ ["මෙම වාක්‍යය සිංහලයට පරිවර්තනය කරන්න: 'The sun rises in the east.'", ""],
107
+ ["වචන 'ආදරය' සඳහා පරිවර්තන 3ක් දෙන්න.", ""],
108
+ ],
109
+ inputs=[instruction],
110
+ )
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  run_btn.click(infer, inputs=[instruction, input_text], outputs=output)
113
 
114
+ gr.Markdown("""
115
+ ---
116
+ 🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
117
+ 🧰 **Stack:** Transformers + Torch + Gradio + ZeroGPU
118
+ © 2025 Gamunu Project | Experimental Release
119
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  if __name__ == "__main__":
122
  demo.launch()