Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
-
from transformers import AutoTokenizer
|
| 5 |
|
| 6 |
# ----------------------------------------------------
|
| 7 |
-
# Globals
|
| 8 |
# ----------------------------------------------------
|
| 9 |
model = None
|
| 10 |
tokenizer = None
|
| 11 |
|
| 12 |
-
# Sinhala Alpaca-style prompt template
|
| 13 |
alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්රතිචාරයක් සපයන්න.
|
| 14 |
|
| 15 |
### උපදෙස:
|
|
@@ -22,23 +21,23 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
|
|
| 22 |
{}"""
|
| 23 |
|
| 24 |
# ----------------------------------------------------
|
| 25 |
-
# GPU inference —
|
| 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:
|
| 36 |
-
|
| 37 |
"manthilaffs/Gamunu-4B-Instruct-Alpha"
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Build Alpaca-style prompt
|
| 42 |
prompt = alpaca_prompt.format(
|
| 43 |
"ඔබ ගැමුණු නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 44 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
|
@@ -46,32 +45,26 @@ def infer(instruction, input_text=""):
|
|
| 46 |
input_text.strip(),
|
| 47 |
)
|
| 48 |
|
| 49 |
-
inputs = tokenizer(
|
| 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.
|
| 57 |
outputs = model.generate(
|
| 58 |
**inputs,
|
| 59 |
max_new_tokens=256,
|
| 60 |
temperature=0.4,
|
| 61 |
top_k=64,
|
| 62 |
top_p=0.95,
|
| 63 |
-
|
| 64 |
)
|
| 65 |
|
| 66 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
if "### ප්රතිචාරය:" in text:
|
| 68 |
text = text.split("### ප්රතිචාරය:")[-1].strip()
|
| 69 |
-
|
| 70 |
return text
|
| 71 |
|
| 72 |
|
| 73 |
# ----------------------------------------------------
|
| 74 |
-
# Gradio
|
| 75 |
# ----------------------------------------------------
|
| 76 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 77 |
gr.Markdown(
|
|
@@ -79,8 +72,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 79 |
# 🧠 Gamunu 4B Instruct Alpha
|
| 80 |
*Sinhala Instruct LLM — ZeroGPU Demo*
|
| 81 |
|
| 82 |
-
⚙️
|
| 83 |
-
💠 Temporary GPU acceleration via `@spaces.GPU`
|
| 84 |
"""
|
| 85 |
)
|
| 86 |
|
|
@@ -94,16 +86,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 94 |
with gr.Row():
|
| 95 |
input_text = gr.Textbox(
|
| 96 |
label="📥 Additional Context (Optional)",
|
| 97 |
-
placeholder="අමතර තොරතුරු
|
| 98 |
lines=2,
|
| 99 |
)
|
| 100 |
|
| 101 |
output = gr.Markdown(label="🧩 Gamunu Response")
|
| 102 |
-
|
| 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 |
["ඉන්දියානු මහා සමුද්රය යනු කොහෙද?", ""],
|
|
@@ -118,8 +108,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 118 |
"""
|
| 119 |
---
|
| 120 |
🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
|
| 121 |
-
🧰 **Stack:**
|
| 122 |
-
© 2025 Gamunu Project | Experimental
|
| 123 |
"""
|
| 124 |
)
|
| 125 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
# ----------------------------------------------------
|
| 7 |
+
# Globals
|
| 8 |
# ----------------------------------------------------
|
| 9 |
model = None
|
| 10 |
tokenizer = None
|
| 11 |
|
|
|
|
| 12 |
alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්රතිචාරයක් සපයන්න.
|
| 13 |
|
| 14 |
### උපදෙස:
|
|
|
|
| 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()
|
| 40 |
|
|
|
|
| 41 |
prompt = alpaca_prompt.format(
|
| 42 |
"ඔබ ගැමුණු නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 43 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
|
|
|
| 45 |
input_text.strip(),
|
| 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(
|
|
|
|
| 72 |
# 🧠 Gamunu 4B Instruct Alpha
|
| 73 |
*Sinhala Instruct LLM — ZeroGPU Demo*
|
| 74 |
|
| 75 |
+
⚙️ Pure Transformers Inference | 💠 ZeroGPU GPU Burst
|
|
|
|
| 76 |
"""
|
| 77 |
)
|
| 78 |
|
|
|
|
| 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 |
["ඉන්දියානු මහා සමුද්රය යනු කොහෙද?", ""],
|
|
|
|
| 108 |
"""
|
| 109 |
---
|
| 110 |
🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
|
| 111 |
+
🧰 **Stack:** Transformers + Torch + Gradio + ZeroGPU
|
| 112 |
+
© 2025 Gamunu Project | Experimental Release
|
| 113 |
"""
|
| 114 |
)
|
| 115 |
|