Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,105 +49,290 @@ def infer(instruction, input_text="", max_new_tokens=512):
|
|
| 49 |
|
| 50 |
# ----------------------- UI -----------------------
|
| 51 |
with gr.Blocks(
|
| 52 |
-
theme=gr.themes.Soft(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
css="""
|
| 54 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
h1,h2,h3,h4,h5 {text-align:center;}
|
|
|
|
| 56 |
#title-bar {
|
| 57 |
-
background:linear-gradient(
|
| 58 |
color:white;
|
| 59 |
-
padding:
|
| 60 |
-
border-radius:
|
| 61 |
-
margin-bottom:
|
| 62 |
-
box-shadow:0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
with gr.Row(equal_height=
|
| 74 |
-
with gr.Column(scale=
|
| 75 |
instruction = gr.Textbox(
|
| 76 |
-
label="
|
| 77 |
-
placeholder="
|
| 78 |
-
lines=
|
|
|
|
| 79 |
)
|
|
|
|
| 80 |
input_text = gr.Textbox(
|
| 81 |
-
label="Additional Context
|
| 82 |
-
placeholder="අමතර තොරතුරු ඇතුළත්
|
| 83 |
lines=3,
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
-
with gr.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
status = gr.Markdown("", elem_id="status-text")
|
| 93 |
|
| 94 |
-
with gr.Column(scale=
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# --- Example prompts ---
|
| 98 |
-
with gr.Accordion("
|
| 99 |
-
with gr.Tab("Maths"):
|
| 100 |
gr.Examples(
|
| 101 |
examples=[
|
| 102 |
["රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්රතිශතයකින් කීයද?", ""],
|
| 103 |
["සියයට 10 ක වර්ධනයක් තිබේ නම් අලුත් අගය කීයද?", ""],
|
|
|
|
| 104 |
],
|
| 105 |
-
inputs=[instruction],
|
|
|
|
| 106 |
)
|
| 107 |
-
|
|
|
|
| 108 |
gr.Examples(
|
| 109 |
examples=[
|
| 110 |
["ඔබ ගුරුවරයෙකු ලෙස ක්රියාකරන්න. ශිෂ්යයාට ගණිතය උගන්වන්න.",
|
| 111 |
"රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්රතිශතයකින් කීයද?"],
|
| 112 |
["ඔබ පරිසර විද්යාඥයෙකු ලෙස වායු මණ්ඩලය පැහැදිලි කරන්න.", ""],
|
|
|
|
| 113 |
],
|
| 114 |
-
inputs=[instruction],
|
|
|
|
| 115 |
)
|
| 116 |
-
|
|
|
|
| 117 |
gr.Examples(
|
| 118 |
examples=[
|
| 119 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 120 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
|
|
|
| 121 |
],
|
| 122 |
-
inputs=[instruction],
|
|
|
|
| 123 |
)
|
| 124 |
-
|
|
|
|
| 125 |
gr.Examples(
|
| 126 |
examples=[
|
| 127 |
["මෙම වාක්යය සිංහලයට පරිවර්තනය කරන්න: 'The sun rises in the east.'", ""],
|
| 128 |
-
["වචන 'ආදරය' සඳහා
|
|
|
|
| 129 |
],
|
| 130 |
-
inputs=[instruction],
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
-
# --- Button feedback ---
|
| 134 |
-
def process_with_status(instruction,input_text,max_new_tokens):
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
-
demo.launch()
|
|
|
|
| 49 |
|
| 50 |
# ----------------------- UI -----------------------
|
| 51 |
with gr.Blocks(
|
| 52 |
+
theme=gr.themes.Soft(
|
| 53 |
+
primary_hue="purple",
|
| 54 |
+
secondary_hue="pink",
|
| 55 |
+
neutral_hue="slate",
|
| 56 |
+
),
|
| 57 |
css="""
|
| 58 |
+
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Sinhala:wght@400;500;600;700&display=swap');
|
| 59 |
+
|
| 60 |
+
.gradio-container {
|
| 61 |
+
max-width:1200px !important;
|
| 62 |
+
margin:auto;
|
| 63 |
+
font-family:'Noto Sans Sinhala','Inter',sans-serif !important;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
h1,h2,h3,h4,h5 {text-align:center;}
|
| 67 |
+
|
| 68 |
#title-bar {
|
| 69 |
+
background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
|
| 70 |
color:white;
|
| 71 |
+
padding:2rem 1rem;
|
| 72 |
+
border-radius:1.5rem;
|
| 73 |
+
margin-bottom:2rem;
|
| 74 |
+
box-shadow:0 8px 32px rgba(102,126,234,0.3);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
#title-bar h1 {
|
| 78 |
+
font-size:2.5rem;
|
| 79 |
+
font-weight:700;
|
| 80 |
+
margin-bottom:0.5rem;
|
| 81 |
+
text-shadow:2px 2px 4px rgba(0,0,0,0.2);
|
| 82 |
}
|
| 83 |
+
|
| 84 |
+
#title-bar h4 {
|
| 85 |
+
font-size:1.2rem;
|
| 86 |
+
font-weight:400;
|
| 87 |
+
opacity:0.95;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
textarea,input,.gr-text-input,.gr-textbox {
|
| 91 |
font-family:'Noto Sans Sinhala',sans-serif !important;
|
| 92 |
+
font-size:1.05rem !important;
|
| 93 |
+
line-height:1.8 !important;
|
| 94 |
+
border-radius:0.75rem !important;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
#output-box {
|
| 98 |
+
background:#f8f9fa;
|
| 99 |
+
border:2px solid #e9ecef;
|
| 100 |
+
border-radius:1rem;
|
| 101 |
+
padding:1.5rem;
|
| 102 |
+
min-height:200px;
|
| 103 |
+
font-size:1.1rem !important;
|
| 104 |
+
line-height:1.9 !important;
|
| 105 |
+
box-shadow:inset 0 2px 8px rgba(0,0,0,0.05);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
#status-text {
|
| 109 |
+
text-align:center;
|
| 110 |
+
font-size:1.1rem;
|
| 111 |
+
font-weight:600;
|
| 112 |
+
padding:0.75rem;
|
| 113 |
+
border-radius:0.5rem;
|
| 114 |
+
margin:0.5rem 0;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
#status-text.generating {
|
| 118 |
+
background:linear-gradient(90deg,#667eea,#764ba2);
|
| 119 |
+
color:white;
|
| 120 |
+
animation:pulse 1.5s ease-in-out infinite;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
@keyframes pulse {
|
| 124 |
+
0%, 100% {opacity:1;}
|
| 125 |
+
50% {opacity:0.7;}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
.primary-btn {
|
| 129 |
+
background:linear-gradient(135deg,#667eea 0%,#764ba2 100%) !important;
|
| 130 |
+
border:none !important;
|
| 131 |
+
font-size:1.1rem !important;
|
| 132 |
+
font-weight:600 !important;
|
| 133 |
+
padding:0.75rem 2rem !important;
|
| 134 |
+
border-radius:0.75rem !important;
|
| 135 |
+
box-shadow:0 4px 15px rgba(102,126,234,0.4) !important;
|
| 136 |
+
transition:all 0.3s ease !important;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.primary-btn:hover {
|
| 140 |
+
transform:translateY(-2px);
|
| 141 |
+
box-shadow:0 6px 20px rgba(102,126,234,0.5) !important;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.secondary-btn {
|
| 145 |
+
background:#6c757d !important;
|
| 146 |
+
border:none !important;
|
| 147 |
+
font-size:1rem !important;
|
| 148 |
+
border-radius:0.75rem !important;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.label-text {
|
| 152 |
+
font-size:1rem !important;
|
| 153 |
+
font-weight:600 !important;
|
| 154 |
+
color:#2d3748 !important;
|
| 155 |
+
margin-bottom:0.5rem !important;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.example-box {
|
| 159 |
+
border-radius:0.75rem;
|
| 160 |
+
padding:0.5rem;
|
| 161 |
+
transition:all 0.2s ease;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.example-box:hover {
|
| 165 |
+
background:#f8f9fa;
|
| 166 |
+
transform:translateX(4px);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
#footer-info {
|
| 170 |
+
text-align:center;
|
| 171 |
+
padding:2rem 1rem;
|
| 172 |
+
margin-top:3rem;
|
| 173 |
+
border-top:2px solid #e9ecef;
|
| 174 |
+
color:#6c757d;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.token-counter {
|
| 178 |
+
font-size:0.9rem;
|
| 179 |
+
color:#6c757d;
|
| 180 |
+
text-align:right;
|
| 181 |
+
padding:0.25rem 0.5rem;
|
| 182 |
}
|
|
|
|
| 183 |
""",
|
| 184 |
) as demo:
|
| 185 |
|
| 186 |
+
gr.HTML("""
|
| 187 |
+
<div id='title-bar'>
|
| 188 |
+
<h1>🧠 Gamunu 4B Instruct Alpha</h1>
|
| 189 |
+
<h4>සිංහල භාෂාව සඳහා නිර්මාණය කළ අධිකරණ භාෂා ආකෘතියක්</h4>
|
| 190 |
+
</div>
|
| 191 |
+
""")
|
| 192 |
|
| 193 |
+
with gr.Row(equal_height=False):
|
| 194 |
+
with gr.Column(scale=5, min_width=400):
|
| 195 |
instruction = gr.Textbox(
|
| 196 |
+
label="📝 උපදෙස / ප්රශ්නය (Instruction / Question)",
|
| 197 |
+
placeholder="උදාහරණ: හායි! මම සමන්. ඔයාට කොහොමද?\nया: ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.",
|
| 198 |
+
lines=5,
|
| 199 |
+
elem_classes="label-text"
|
| 200 |
)
|
| 201 |
+
|
| 202 |
input_text = gr.Textbox(
|
| 203 |
+
label="📄 අමතර සන්දර්භය (Additional Context) - විකල්ප",
|
| 204 |
+
placeholder="අමතර තොරතුරු හෝ සන්දර්භය මෙහි ඇතුළත් කරන්න...",
|
| 205 |
lines=3,
|
| 206 |
+
elem_classes="label-text"
|
| 207 |
)
|
| 208 |
|
| 209 |
+
with gr.Row():
|
| 210 |
+
with gr.Column(scale=2):
|
| 211 |
+
with gr.Accordion("⚙️ උසස් සැකසීම් (Advanced Settings)", open=False):
|
| 212 |
+
max_new_tokens = gr.Slider(
|
| 213 |
+
64, 1024, value=512, step=32,
|
| 214 |
+
label="🔢 උපරිම නව සලකුණු (Max New Tokens)",
|
| 215 |
+
info="දිගු ප්රතිචාර සඳහා වැඩි කරන්න"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
with gr.Column(scale=1):
|
| 219 |
+
token_count = gr.Markdown("", elem_classes="token-counter")
|
| 220 |
|
| 221 |
+
with gr.Row():
|
| 222 |
+
run_btn = gr.Button("🚀 ප්රතිචාරය උත්පාදනය කරන්න", variant="primary", elem_classes="primary-btn", scale=3)
|
| 223 |
+
clear_btn = gr.Button("🗑️ මකන්න", elem_classes="secondary-btn", scale=1)
|
| 224 |
+
|
| 225 |
status = gr.Markdown("", elem_id="status-text")
|
| 226 |
|
| 227 |
+
with gr.Column(scale=5, min_width=450):
|
| 228 |
+
gr.Markdown("### 💬 ගැමුණු ප්රතිචාරය (Gamunu Response)")
|
| 229 |
+
output = gr.Markdown("", elem_id="output-box")
|
| 230 |
+
|
| 231 |
+
with gr.Row():
|
| 232 |
+
copy_btn = gr.Button("📋 පිටපත් කරන්න (Copy)", size="sm")
|
| 233 |
|
| 234 |
# --- Example prompts ---
|
| 235 |
+
with gr.Accordion("📚 උදාහරණ ප්රශ්න (Example Prompts by Category)", open=True):
|
| 236 |
+
with gr.Tab("🔢 ගණිතය (Maths)"):
|
| 237 |
gr.Examples(
|
| 238 |
examples=[
|
| 239 |
["රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්රතිශතයකින් කීයද?", ""],
|
| 240 |
["සියයට 10 ක වර්ධනයක් තිබේ නම් අලුත් අගය කීයද?", ""],
|
| 241 |
+
["2x + 5 = 15 සමීකරණය විසඳන්න.", ""],
|
| 242 |
],
|
| 243 |
+
inputs=[instruction, input_text],
|
| 244 |
+
elem_classes="example-box"
|
| 245 |
)
|
| 246 |
+
|
| 247 |
+
with gr.Tab("🎭 භූමිකා (Roleplay)"):
|
| 248 |
gr.Examples(
|
| 249 |
examples=[
|
| 250 |
["ඔබ ගුරුවරයෙකු ලෙස ක්රියාකරන්න. ශිෂ්යයාට ගණිතය උගන්වන්න.",
|
| 251 |
"රු. 30 කින් මිලදී ගත් දේ රු. 60 නම් මිල වෙනස ප්රතිශතයකින් කීයද?"],
|
| 252 |
["ඔබ පරිසර විද්යාඥයෙකු ලෙස වායු මණ්ඩලය පැහැදිලි කරන්න.", ""],
|
| 253 |
+
["ඔබ කාර්මික විශේෂඥයෙකු ලෙස ක්රියා කරමින් මෘදුකාංග සංවර්ධනය පැහැදිලි කරන්න.", ""],
|
| 254 |
],
|
| 255 |
+
inputs=[instruction, input_text],
|
| 256 |
+
elem_classes="example-box"
|
| 257 |
)
|
| 258 |
+
|
| 259 |
+
with gr.Tab("❓ ප්රශ්න සහ පිළිතුරු (Q&A)"):
|
| 260 |
gr.Examples(
|
| 261 |
examples=[
|
| 262 |
["ෆොටෝසින්තසිස් ක්රියාවලිය පැහැදිලි කරන්න.", ""],
|
| 263 |
["ජලයේ රසායනික සූත්රය කුමක්ද?", ""],
|
| 264 |
+
["ශ්රී ලංකාවේ පළමු අගමැති කවුද?", ""],
|
| 265 |
],
|
| 266 |
+
inputs=[instruction, input_text],
|
| 267 |
+
elem_classes="example-box"
|
| 268 |
)
|
| 269 |
+
|
| 270 |
+
with gr.Tab("🔤 භාෂා සැකසුම (NLP)"):
|
| 271 |
gr.Examples(
|
| 272 |
examples=[
|
| 273 |
["මෙම වාක්යය සිංහලයට පරිවර්තනය කරන්න: 'The sun rises in the east.'", ""],
|
| 274 |
+
["වචන 'ආදරය' සඳහා සමානාර්ථ පද 3ක් දෙන්න.", ""],
|
| 275 |
+
["මෙම වාක්යය සරල කරන්න: 'අධිකාරිය විසින් නිකුත් කරන ලද නියෝගය අනුව ක්රියාත්මක වේ.'", ""],
|
| 276 |
],
|
| 277 |
+
inputs=[instruction, input_text],
|
| 278 |
+
elem_classes="example-box"
|
| 279 |
)
|
| 280 |
|
| 281 |
+
# --- Button feedback with improved status ---
|
| 282 |
+
def process_with_status(instruction, input_text, max_new_tokens):
|
| 283 |
+
if not instruction.strip():
|
| 284 |
+
return "⚠️ කරුණාකර උපදෙසක් ඇතුළත් කරන්න", gr.update(interactive=True), "", ""
|
| 285 |
+
|
| 286 |
+
yield (
|
| 287 |
+
"<div class='generating'>⏳ ප්රතිචාරය උත්පාදනය වෙමින්...</div>",
|
| 288 |
+
gr.update(interactive=False, value="⏳ ප්රතිචාරය උත්පාදනය වෙමින්..."),
|
| 289 |
+
"",
|
| 290 |
+
""
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
try:
|
| 294 |
+
result = infer(instruction, input_text, max_new_tokens)
|
| 295 |
+
approx_tokens = len(result.split())
|
| 296 |
+
token_info = f"📊 ආසන්න වශයෙන් වචන {approx_tokens}ක්"
|
| 297 |
+
yield (
|
| 298 |
+
"✅ සාර්ථකයි!",
|
| 299 |
+
gr.update(interactive=True, value="🚀 ප්රතිචාරය උත්පාදනය කරන්න"),
|
| 300 |
+
result,
|
| 301 |
+
token_info
|
| 302 |
+
)
|
| 303 |
+
except Exception as e:
|
| 304 |
+
yield (
|
| 305 |
+
f"❌ දෝෂයක්: {str(e)}",
|
| 306 |
+
gr.update(interactive=True, value="🚀 ප්රතිචාරය උත්පාදනය කරන්න"),
|
| 307 |
+
"",
|
| 308 |
+
""
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
def clear_all():
|
| 312 |
+
return "", "", 512, "", gr.update(value="🚀 ප්රතිචාරය උත්පාදනය කරන්න", interactive=True), ""
|
| 313 |
|
| 314 |
run_btn.click(
|
| 315 |
process_with_status,
|
| 316 |
+
inputs=[instruction, input_text, max_new_tokens],
|
| 317 |
+
outputs=[status, run_btn, output, token_count],
|
| 318 |
show_progress=True,
|
| 319 |
)
|
| 320 |
|
| 321 |
+
clear_btn.click(
|
| 322 |
+
clear_all,
|
| 323 |
+
outputs=[instruction, input_text, max_new_tokens, output, run_btn, status]
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
gr.HTML("""
|
| 327 |
+
<div id='footer-info'>
|
| 328 |
+
<p style='font-size:1.1rem; margin-bottom:0.5rem;'>
|
| 329 |
+
🪶 <strong>Model:</strong> <a href='https://huggingface.co/manthilaffs/Gamunu-4B-Instruct-Alpha' target='_blank'>manthilaffs/Gamunu-4B-Instruct-Alpha</a>
|
| 330 |
+
</p>
|
| 331 |
+
<p style='font-size:0.95rem; color:#868e96;'>
|
| 332 |
+
© 2025 Gamunu Project | Experimental Release | Made with ❤️ for Sinhala NLP
|
| 333 |
+
</p>
|
| 334 |
+
</div>
|
| 335 |
+
""")
|
| 336 |
|
| 337 |
if __name__ == "__main__":
|
| 338 |
+
demo.launch()
|