Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import torch
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
-
# Global model/tokenizer cache
|
| 7 |
model = None
|
| 8 |
tokenizer = None
|
| 9 |
|
|
@@ -19,8 +18,8 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
|
|
| 19 |
{}"""
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def infer(instruction, input_text="",
|
| 23 |
-
"""
|
| 24 |
global model, tokenizer
|
| 25 |
if model is None:
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
|
|
@@ -40,13 +39,7 @@ def infer(instruction, input_text="", temperature=0.5, top_p=0.95, repetition_pe
|
|
| 40 |
|
| 41 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 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:
|
|
@@ -58,8 +51,8 @@ def infer(instruction, input_text="", temperature=0.5, top_p=0.95, repetition_pe
|
|
| 58 |
with gr.Blocks(
|
| 59 |
theme=gr.themes.Soft(),
|
| 60 |
css="""
|
| 61 |
-
.gradio-container {max-width:
|
| 62 |
-
h1,
|
| 63 |
#title-bar {
|
| 64 |
background:linear-gradient(90deg,#764de6,#e36cee);
|
| 65 |
color:white;
|
|
@@ -68,17 +61,17 @@ with gr.Blocks(
|
|
| 68 |
margin-bottom:0.8rem;
|
| 69 |
box-shadow:0 2px 8px rgba(0,0,0,0.15);
|
| 70 |
}
|
| 71 |
-
textarea,
|
| 72 |
font-family:'Noto Sans Sinhala',sans-serif !important;
|
| 73 |
}
|
| 74 |
-
#status-text {text-align:center;
|
| 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,
|
| 82 |
instruction = gr.Textbox(
|
| 83 |
label="🧾 Instruction / Question",
|
| 84 |
placeholder="උදා: හායි! මම සමන්. ඔයාට කොහොමද?",
|
|
@@ -90,16 +83,15 @@ with gr.Blocks(
|
|
| 90 |
lines=3,
|
| 91 |
)
|
| 92 |
|
| 93 |
-
with gr.Accordion("⚙️ Advanced
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 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 |
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
with gr.Column(scale=1,
|
| 103 |
output = gr.Markdown(label="🧩 Gamunu Response", elem_id="output-box")
|
| 104 |
|
| 105 |
# --- Example prompts ---
|
|
@@ -115,7 +107,8 @@ with gr.Blocks(
|
|
| 115 |
with gr.Tab("Roleplay"):
|
| 116 |
gr.Examples(
|
| 117 |
examples=[
|
| 118 |
-
["ඔබ ගුරුවරයෙකු ලෙස ක්රියාකරන්න. ශිෂ්යයාට ගණිතය උගන්වන්න.",
|
|
|
|
| 119 |
["ඔබ පරිසර විද්යාඥයෙකු ලෙස වායු මණ්ඩලය පැහැදිලි කරන්න.", ""],
|
| 120 |
],
|
| 121 |
inputs=[instruction],
|
|
@@ -125,7 +118,6 @@ with gr.Blocks(
|
|
| 125 |
examples=[
|
| 126 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 127 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
| 128 |
-
["ජලය සහ සනීපාරක්ෂාව පිළිබඳ සංකල්පය SMART PRICES WASH", ""],
|
| 129 |
],
|
| 130 |
inputs=[instruction],
|
| 131 |
)
|
|
@@ -138,16 +130,16 @@ with gr.Blocks(
|
|
| 138 |
inputs=[instruction],
|
| 139 |
)
|
| 140 |
|
| 141 |
-
# ---
|
| 142 |
-
def process_with_status(instruction,
|
| 143 |
-
yield "⏳ Generating response...", gr.update(interactive=False,
|
| 144 |
-
result = infer(instruction,
|
| 145 |
-
yield "", gr.update(interactive=True,
|
| 146 |
|
| 147 |
run_btn.click(
|
| 148 |
process_with_status,
|
| 149 |
-
inputs=[instruction,
|
| 150 |
-
outputs=[status,
|
| 151 |
show_progress=True,
|
| 152 |
)
|
| 153 |
|
|
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
|
|
|
| 6 |
model = None
|
| 7 |
tokenizer = None
|
| 8 |
|
|
|
|
| 18 |
{}"""
|
| 19 |
|
| 20 |
@spaces.GPU
|
| 21 |
+
def infer(instruction, input_text="", max_new_tokens=512):
|
| 22 |
+
"""Pure Transformers generation — lets model defaults decide behavior."""
|
| 23 |
global model, tokenizer
|
| 24 |
if model is None:
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
|
|
|
|
| 39 |
|
| 40 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 41 |
with torch.inference_mode():
|
| 42 |
+
outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
if "### ප්රතිචාරය:" in text:
|
|
|
|
| 51 |
with gr.Blocks(
|
| 52 |
theme=gr.themes.Soft(),
|
| 53 |
css="""
|
| 54 |
+
.gradio-container {max-width:1080px !important; margin:auto;}
|
| 55 |
+
h1,h2,h3,h4,h5 {text-align:center;}
|
| 56 |
#title-bar {
|
| 57 |
background:linear-gradient(90deg,#764de6,#e36cee);
|
| 58 |
color:white;
|
|
|
|
| 61 |
margin-bottom:0.8rem;
|
| 62 |
box-shadow:0 2px 8px rgba(0,0,0,0.15);
|
| 63 |
}
|
| 64 |
+
textarea,input,.gr-text-input,.gr-textbox {
|
| 65 |
font-family:'Noto Sans Sinhala',sans-serif !important;
|
| 66 |
}
|
| 67 |
+
#status-text {text-align:center;color:#555;}
|
| 68 |
""",
|
| 69 |
) as demo:
|
| 70 |
|
| 71 |
gr.HTML("<div id='title-bar'><h1>🧠 Gamunu 4B Instruct Alpha</h1><h4>සිංහල Instruct LLM</h4></div>")
|
| 72 |
|
| 73 |
with gr.Row(equal_height=True):
|
| 74 |
+
with gr.Column(scale=1,min_width=350):
|
| 75 |
instruction = gr.Textbox(
|
| 76 |
label="🧾 Instruction / Question",
|
| 77 |
placeholder="උදා: හායි! මම සමන්. ඔයාට කොහොමද?",
|
|
|
|
| 83 |
lines=3,
|
| 84 |
)
|
| 85 |
|
| 86 |
+
with gr.Accordion("⚙️ Advanced Option", open=False):
|
| 87 |
+
max_new_tokens = gr.Slider(
|
| 88 |
+
64, 1024, value=512, step=32, label="🔢 Max New Tokens"
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
run_btn = gr.Button("🔮 Generate Response", variant="primary")
|
| 92 |
+
status = gr.Markdown("", elem_id="status-text")
|
| 93 |
|
| 94 |
+
with gr.Column(scale=1,min_width=400):
|
| 95 |
output = gr.Markdown(label="🧩 Gamunu Response", elem_id="output-box")
|
| 96 |
|
| 97 |
# --- Example prompts ---
|
|
|
|
| 107 |
with gr.Tab("Roleplay"):
|
| 108 |
gr.Examples(
|
| 109 |
examples=[
|
| 110 |
+
["ඔබ ගුරුවරයෙකු ලෙස ක්රියාකරන්න. ශිෂ්යයාට ගණිතය උගන්වන්න.",
|
| 111 |
+
"රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්රතිශතයකින් කීයද?"],
|
| 112 |
["ඔබ පරිසර විද්යාඥයෙකු ලෙස වායු මණ්ඩලය පැහැදිලි කරන්න.", ""],
|
| 113 |
],
|
| 114 |
inputs=[instruction],
|
|
|
|
| 118 |
examples=[
|
| 119 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 120 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
|
|
|
| 121 |
],
|
| 122 |
inputs=[instruction],
|
| 123 |
)
|
|
|
|
| 130 |
inputs=[instruction],
|
| 131 |
)
|
| 132 |
|
| 133 |
+
# --- Button feedback ---
|
| 134 |
+
def process_with_status(instruction,input_text,max_new_tokens):
|
| 135 |
+
yield "⏳ Generating response...", gr.update(interactive=False,value="⏳ Generating..."), ""
|
| 136 |
+
result = infer(instruction,input_text,max_new_tokens)
|
| 137 |
+
yield "", gr.update(interactive=True,value="🔮 Generate Response"), result
|
| 138 |
|
| 139 |
run_btn.click(
|
| 140 |
process_with_status,
|
| 141 |
+
inputs=[instruction,input_text,max_new_tokens],
|
| 142 |
+
outputs=[status,run_btn,output],
|
| 143 |
show_progress=True,
|
| 144 |
)
|
| 145 |
|