manthilaffs commited on
Commit
726fa01
·
verified ·
1 Parent(s): bd0068f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -4,12 +4,12 @@ import spaces
4
  from transformers import AutoTokenizer
5
 
6
  # ----------------------------------------------------
7
- # Global placeholders (lazy-load later)
8
  # ----------------------------------------------------
9
  model = None
10
  tokenizer = None
11
 
12
- # Sinhala Alpaca-style prompt
13
  alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්‍රතිචාරයක් සපයන්න.
14
 
15
  ### උපදෙස:
@@ -22,13 +22,14 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
22
  {}"""
23
 
24
  # ----------------------------------------------------
25
- # GPU inference function — executed on ZeroGPU
26
  # ----------------------------------------------------
27
  @spaces.GPU
28
  def infer(instruction, input_text=""):
 
29
  global model, tokenizer
30
 
31
- # ✅ Import Unsloth *only after GPU is available*
32
  from unsloth import FastLanguageModel
33
 
34
  if model is None:
@@ -37,6 +38,7 @@ def infer(instruction, input_text=""):
37
  )
38
  FastLanguageModel.for_inference(model)
39
 
 
40
  prompt = alpaca_prompt.format(
41
  "ඔබ ගැමුණු නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
42
  "ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ ප්‍රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
@@ -46,6 +48,11 @@ def infer(instruction, input_text=""):
46
 
47
  inputs = tokenizer(text=prompt, return_tensors="pt").to(model.device)
48
 
 
 
 
 
 
49
  with torch.no_grad():
50
  outputs = model.generate(
51
  **inputs,
@@ -64,7 +71,7 @@ def infer(instruction, input_text=""):
64
 
65
 
66
  # ----------------------------------------------------
67
- # Gradio Interface
68
  # ----------------------------------------------------
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
  gr.Markdown(
@@ -72,21 +79,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
72
  # 🧠 Gamunu 4B Instruct Alpha
73
  *Sinhala Instruct LLM — ZeroGPU Demo*
74
 
75
- ⚙️ Runs via **Unsloth FastLanguageModel**
76
- 💠 Accelerated temporarily with `@spaces.GPU`
77
  """
78
  )
79
 
80
  with gr.Row():
81
  instruction = gr.Textbox(
82
  label="🧾 Instruction / Question",
83
- placeholder="උදා: ඊයේ ඇපල් මිල 30ක් නම් අද 60ක් නම් ප්‍රතිශත වෙනස කීයද?",
84
  lines=2,
85
  )
 
86
  with gr.Row():
87
  input_text = gr.Textbox(
88
  label="📥 Additional Context (Optional)",
89
- placeholder="අමතර තොරතුරු ඇතුළත් කරන්න",
90
  lines=2,
91
  )
92
 
@@ -95,12 +103,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
95
  run_btn = gr.Button("🔮 Generate Response")
96
  run_btn.click(infer, inputs=[instruction, input_text], outputs=output)
97
 
 
 
 
 
 
 
 
 
 
 
 
98
  gr.Markdown(
99
  """
100
  ---
101
  🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
102
- 🧰 **Built with:** Unsloth + Gradio + ZeroGPU
103
- © 2025 Gamunu Project | Experimental Release
104
  """
105
  )
106
 
 
4
  from transformers import AutoTokenizer
5
 
6
  # ----------------------------------------------------
7
+ # Globals (lazy-loaded later)
8
  # ----------------------------------------------------
9
  model = None
10
  tokenizer = None
11
 
12
+ # Sinhala Alpaca-style prompt template
13
  alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්‍රතිචාරයක් සපයන්න.
14
 
15
  ### උපදෙස:
 
22
  {}"""
23
 
24
  # ----------------------------------------------------
25
+ # GPU inference — executed only when ZeroGPU allocates GPU
26
  # ----------------------------------------------------
27
  @spaces.GPU
28
  def infer(instruction, input_text=""):
29
+ """Run Gamunu inference on GPU (ZeroGPU burst)."""
30
  global model, tokenizer
31
 
32
+ # ✅ Lazy import Unsloth *after* GPU is available
33
  from unsloth import FastLanguageModel
34
 
35
  if model is None:
 
38
  )
39
  FastLanguageModel.for_inference(model)
40
 
41
+ # Build Alpaca-style prompt
42
  prompt = alpaca_prompt.format(
43
  "ඔබ ගැමුණු නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
44
  "ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ ප්‍රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
 
48
 
49
  inputs = tokenizer(text=prompt, return_tensors="pt").to(model.device)
50
 
51
+ # ✅ Disable TorchDynamo (fix Gemma3 compile bug on Torch 2.8)
52
+ import torch._dynamo
53
+ torch._dynamo.config.suppress_errors = True
54
+ torch._dynamo.disable()
55
+
56
  with torch.no_grad():
57
  outputs = model.generate(
58
  **inputs,
 
71
 
72
 
73
  # ----------------------------------------------------
74
+ # Gradio Interface — ZeroGPU ready
75
  # ----------------------------------------------------
76
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
77
  gr.Markdown(
 
79
  # 🧠 Gamunu 4B Instruct Alpha
80
  *Sinhala Instruct LLM — ZeroGPU Demo*
81
 
82
+ ⚙️ Built with Unsloth FastLanguageModel
83
+ 💠 Temporary GPU acceleration via `@spaces.GPU`
84
  """
85
  )
86
 
87
  with gr.Row():
88
  instruction = gr.Textbox(
89
  label="🧾 Instruction / Question",
90
+ placeholder="උදා: ඊයේ රු. 30 ඇපල් ගෙඩියක් අද රු. 60 නම් මිල වෙනස කීයද?",
91
  lines=2,
92
  )
93
+
94
  with gr.Row():
95
  input_text = gr.Textbox(
96
  label="📥 Additional Context (Optional)",
97
+ placeholder="අමතර තොරතුරු ඇතුළත් කරන්න (උදා: කාල පරිච්ඡේදය හෝ තත්ත්වය)",
98
  lines=2,
99
  )
100
 
 
103
  run_btn = gr.Button("🔮 Generate Response")
104
  run_btn.click(infer, inputs=[instruction, input_text], outputs=output)
105
 
106
+ # 🪄 Example questions for visitors
107
+ gr.Examples(
108
+ examples=[
109
+ ["ඉන්දියානු මහා සමුද්‍රය යනු කොහෙද?", ""],
110
+ ["කාලගුණය අවතැන් වන්නේ මොනවක් නිසාද?", ""],
111
+ ["මුල්ම අගමැති කවුද?", ""],
112
+ ],
113
+ inputs=[instruction],
114
+ label="🪄 Example Questions (Click to try)",
115
+ )
116
+
117
  gr.Markdown(
118
  """
119
  ---
120
  🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
121
+ 🧰 **Stack:** Unsloth + Transformers + Gradio + ZeroGPU
122
+ © 2025 Gamunu Project | Experimental Research Release
123
  """
124
  )
125