Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import torch
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
|
|
|
| 6 |
model = None
|
| 7 |
tokenizer = None
|
| 8 |
|
|
@@ -18,19 +19,20 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
|
|
| 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 |
-
|
| 28 |
device_map="auto",
|
| 29 |
)
|
| 30 |
model.eval()
|
| 31 |
|
| 32 |
prompt = alpaca_prompt.format(
|
| 33 |
-
"ඔබ ගැමුණු (Gamunu) නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි."
|
| 34 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ උපදෙස් නිවැරදිව පිලිපැදීම හා අසා ඇති ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
| 35 |
instruction.strip(),
|
| 36 |
input_text.strip(),
|
|
@@ -40,23 +42,40 @@ def infer(instruction, input_text=""):
|
|
| 40 |
with torch.inference_mode():
|
| 41 |
outputs = model.generate(
|
| 42 |
**inputs,
|
| 43 |
-
max_new_tokens=
|
| 44 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 53 |
-
.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
gr.HTML("<div id='title-bar'><h1>🧠 Gamunu 4B Instruct Alpha</h1><h4>සිංහල Instruct LLM</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):
|
|
@@ -70,12 +89,21 @@ textarea, input {font-family:'Noto Sans Sinhala',sans-serif;}
|
|
| 70 |
placeholder="අමතර තොරතුරු ඇතුළත් කරන්න (ඇත්නම්)",
|
| 71 |
lines=3,
|
| 72 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
run_btn = gr.Button("🔮 Generate Response", variant="primary", scale=1)
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
output = gr.Markdown(label="🧩 Gamunu Response", elem_id="output-box")
|
| 76 |
|
| 77 |
-
# Example
|
| 78 |
-
with gr.Accordion("🧮 Example Prompts by Category", open=
|
| 79 |
with gr.Tab("Maths"):
|
| 80 |
gr.Examples(
|
| 81 |
examples=[
|
|
@@ -97,8 +125,7 @@ textarea, input {font-family:'Noto Sans Sinhala',sans-serif;}
|
|
| 97 |
examples=[
|
| 98 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 99 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
| 100 |
-
["
|
| 101 |
-
i. SMART ii. PRICES iii. WASH""", ""]
|
| 102 |
],
|
| 103 |
inputs=[instruction],
|
| 104 |
)
|
|
@@ -111,12 +138,23 @@ i. SMART ii. PRICES iii. WASH""", ""]
|
|
| 111 |
inputs=[instruction],
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
gr.Markdown("""
|
| 117 |
---
|
| 118 |
-
🪶 **Model:** `manthilaffs/Gamunu-4B-Instruct-Alpha`
|
| 119 |
-
© 2025 Gamunu Project | Experimental Release
|
| 120 |
""")
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# Global model/tokenizer cache
|
| 7 |
model = None
|
| 8 |
tokenizer = None
|
| 9 |
|
|
|
|
| 19 |
{}"""
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
+
def infer(instruction, input_text="", temperature=0.5, top_p=0.95, repetition_penalty=1.05, max_new_tokens=256):
|
| 23 |
+
"""Main inference function with adjustable generation parameters."""
|
| 24 |
global model, tokenizer
|
| 25 |
if model is None:
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
"manthilaffs/Gamunu-4B-Instruct-Alpha",
|
| 29 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 30 |
device_map="auto",
|
| 31 |
)
|
| 32 |
model.eval()
|
| 33 |
|
| 34 |
prompt = alpaca_prompt.format(
|
| 35 |
+
"ඔබ ගැමුණු (Gamunu) නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 36 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ උපදෙස් නිවැරදිව පිලිපැදීම හා අසා ඇති ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
| 37 |
instruction.strip(),
|
| 38 |
input_text.strip(),
|
|
|
|
| 42 |
with torch.inference_mode():
|
| 43 |
outputs = model.generate(
|
| 44 |
**inputs,
|
| 45 |
+
max_new_tokens=max_new_tokens,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
top_p=top_p,
|
| 48 |
+
repetition_penalty=repetition_penalty,
|
| 49 |
)
|
| 50 |
+
|
| 51 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
if "### ප්රතිචාරය:" in text:
|
| 53 |
text = text.split("### ප්රතිචාරය:")[-1].strip()
|
| 54 |
return text
|
| 55 |
|
| 56 |
|
| 57 |
+
# ----------------------- UI -----------------------
|
| 58 |
+
with gr.Blocks(
|
| 59 |
+
theme=gr.themes.Soft(),
|
| 60 |
+
css="""
|
| 61 |
+
.gradio-container {max-width: 1080px !important; margin:auto;}
|
| 62 |
+
h1, h2, h3, h4, h5 {text-align:center;}
|
| 63 |
+
#title-bar {
|
| 64 |
+
background:linear-gradient(90deg,#764de6,#e36cee);
|
| 65 |
+
color:white;
|
| 66 |
+
padding:0.7rem;
|
| 67 |
+
border-radius:1rem;
|
| 68 |
+
margin-bottom:0.8rem;
|
| 69 |
+
box-shadow:0 2px 8px rgba(0,0,0,0.15);
|
| 70 |
+
}
|
| 71 |
+
textarea, input, .gr-text-input, .gr-textbox {
|
| 72 |
+
font-family:'Noto Sans Sinhala',sans-serif !important;
|
| 73 |
+
}
|
| 74 |
+
#status-text {text-align:center; color:#555;}
|
| 75 |
+
""",
|
| 76 |
+
) as demo:
|
| 77 |
+
|
| 78 |
gr.HTML("<div id='title-bar'><h1>🧠 Gamunu 4B Instruct Alpha</h1><h4>සිංහල Instruct LLM</h4></div>")
|
|
|
|
| 79 |
|
| 80 |
with gr.Row(equal_height=True):
|
| 81 |
with gr.Column(scale=1, min_width=350):
|
|
|
|
| 89 |
placeholder="අමතර තොරතුරු ඇතුළත් කරන්න (ඇත්නම්)",
|
| 90 |
lines=3,
|
| 91 |
)
|
| 92 |
+
|
| 93 |
+
with gr.Accordion("⚙️ Advanced Options", open=False):
|
| 94 |
+
temperature = gr.Slider(0.1, 1.5, value=0.5, step=0.05, label="🌡 Temperature (0 = more focused)")
|
| 95 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.01, label="🎯 Top-p (Nucleus Sampling)")
|
| 96 |
+
repetition_penalty = gr.Slider(0.8, 2.0, value=1.05, step=0.05, label="♻️ Repetition Penalty")
|
| 97 |
+
max_new_tokens = gr.Slider(32, 1024, value=256, step=32, label="🔢 Max New Tokens")
|
| 98 |
+
|
| 99 |
run_btn = gr.Button("🔮 Generate Response", variant="primary", scale=1)
|
| 100 |
+
status = gr.Markdown("", elem_id="status-text")
|
| 101 |
+
|
| 102 |
+
with gr.Column(scale=1, min_width=400):
|
| 103 |
output = gr.Markdown(label="🧩 Gamunu Response", elem_id="output-box")
|
| 104 |
|
| 105 |
+
# --- Example prompts ---
|
| 106 |
+
with gr.Accordion("🧮 Example Prompts by Category", open=False):
|
| 107 |
with gr.Tab("Maths"):
|
| 108 |
gr.Examples(
|
| 109 |
examples=[
|
|
|
|
| 125 |
examples=[
|
| 126 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 127 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
| 128 |
+
["ජලය සහ සනීපාරක්ෂාව පිළිබඳ සංකල්පය SMART PRICES WASH", ""],
|
|
|
|
| 129 |
],
|
| 130 |
inputs=[instruction],
|
| 131 |
)
|
|
|
|
| 138 |
inputs=[instruction],
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# --- Loading feedback ---
|
| 142 |
+
def process_with_status(instruction, input_text, temperature, top_p, repetition_penalty, max_new_tokens):
|
| 143 |
+
yield "⏳ Generating response...", gr.update(interactive=False, value="⏳ Generating..."), ""
|
| 144 |
+
result = infer(instruction, input_text, temperature, top_p, repetition_penalty, max_new_tokens)
|
| 145 |
+
yield "", gr.update(interactive=True, value="🔮 Generate Response"), result
|
| 146 |
+
|
| 147 |
+
run_btn.click(
|
| 148 |
+
process_with_status,
|
| 149 |
+
inputs=[instruction, input_text, temperature, top_p, repetition_penalty, max_new_tokens],
|
| 150 |
+
outputs=[status, run_btn, output],
|
| 151 |
+
show_progress=True,
|
| 152 |
+
)
|
| 153 |
|
| 154 |
gr.Markdown("""
|
| 155 |
---
|
| 156 |
+
🪶 **Model:** [`manthilaffs/Gamunu-4B-Instruct-Alpha`](https://huggingface.co/manthilaffs/Gamunu-4B-Instruct-Alpha)
|
| 157 |
+
© 2025 Gamunu Project | Experimental Release
|
| 158 |
""")
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|