Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import spaces
|
| 4 |
+
from unsloth import FastLanguageModel
|
| 5 |
+
from transformers import TextStreamer
|
| 6 |
+
|
| 7 |
+
# ----------------------------------------------------
|
| 8 |
+
# Lazy model loading (GPU-accelerated on demand)
|
| 9 |
+
# ----------------------------------------------------
|
| 10 |
+
model = None
|
| 11 |
+
tokenizer = None
|
| 12 |
+
|
| 13 |
+
# Sinhala Alpaca-style prompt
|
| 14 |
+
alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්රතිචාරයක් සපයන්න.
|
| 15 |
+
|
| 16 |
+
### උපදෙස:
|
| 17 |
+
{}
|
| 18 |
+
|
| 19 |
+
### ආදානය:
|
| 20 |
+
{}
|
| 21 |
+
|
| 22 |
+
### ප්රතිචාරය:
|
| 23 |
+
{}"""
|
| 24 |
+
|
| 25 |
+
# ----------------------------------------------------
|
| 26 |
+
# GPU inference function — executed on ZeroGPU
|
| 27 |
+
# ----------------------------------------------------
|
| 28 |
+
@spaces.GPU
|
| 29 |
+
def infer(instruction, input_text=""):
|
| 30 |
+
global model, tokenizer
|
| 31 |
+
|
| 32 |
+
if model is None:
|
| 33 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 34 |
+
"manthilaffs/Gamunu-4B-Instruct-Alpha"
|
| 35 |
+
)
|
| 36 |
+
FastLanguageModel.for_inference(model)
|
| 37 |
+
|
| 38 |
+
prompt = alpaca_prompt.format(
|
| 39 |
+
"ඔබ ගැමුණු නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 40 |
+
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
| 41 |
+
instruction.strip(),
|
| 42 |
+
input_text.strip(),
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
inputs = tokenizer(text=prompt, return_tensors="pt").to(model.device)
|
| 46 |
+
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
outputs = model.generate(
|
| 49 |
+
**inputs,
|
| 50 |
+
max_new_tokens=256,
|
| 51 |
+
temperature=0.4,
|
| 52 |
+
top_k=64,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
min_p=0.75,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 58 |
+
if "### ප්රතිචාරය:" in text:
|
| 59 |
+
text = text.split("### ප්රතිචාරය:")[-1].strip()
|
| 60 |
+
|
| 61 |
+
return text
|
| 62 |
+
|
| 63 |
+
# ----------------------------------------------------
|
| 64 |
+
# Gradio Interface
|
| 65 |
+
# ----------------------------------------------------
|
| 66 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown(
|
| 68 |
+
"""
|
| 69 |
+
# 🧠 Gamunu 4B Instruct Alpha
|
| 70 |
+
*Sinhala Instruct LLM — ZeroGPU Demo*
|
| 71 |
+
|
| 72 |
+
⚙️ Runs via **Unsloth FastLanguageModel**
|
| 73 |
+
💠 Accelerated temporarily with `@spaces.GPU`
|
| 74 |
+
"""
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
instruction = gr.Textbox(
|
| 79 |
+
label="🧾 Instruction / Question",
|
| 80 |
+
placeholder="උදා: ඊයේ ඇපල් මිල 30ක් නම් අද 60ක් නම් ප්රතිශත වෙනස කීයද?",
|
| 81 |
+
lines=2,
|
| 82 |
+
)
|
| 83 |
+
with gr.Row():
|
| 84 |
+
input_text = gr.Textbox(
|
| 85 |
+
label="📥 Additional Context (Optional)",
|
| 86 |
+
placeholder="අමතර තොරතුරු ඇතුළත් කරන්න",
|
| 87 |
+
lines=2,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
output = gr.Markdown(label="🧩 Gamunu Response")
|
| 91 |
+
|
| 92 |
+
run_btn = gr.Button("🔮 Generate Response")
|
| 93 |
+
run_btn.click(infer, inputs=[instruction, input_text], outputs=output)
|
| 94 |
+
|
| 95 |
+
gr.Markdown(
|
| 96 |
+
"""
|
| 97 |
+
---
|
| 98 |
+
🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
|
| 99 |
+
🧰 **Built with:** Unsloth + Gradio + ZeroGPU
|
| 100 |
+
© 2025 Gamunu Project | Experimental Release
|
| 101 |
+
"""
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
demo.launch()
|