Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- README.md +14 -7
- app.py +114 -0
- assets/fig_data.png +0 -0
- assets/fig_domains.png +0 -0
- assets/fig_overall.png +0 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
---
|
| 2 |
-
title: Urdu
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Urdu Education & Reasoning
|
| 3 |
+
emoji: ⚖️
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.49.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: gemma
|
| 11 |
+
models:
|
| 12 |
+
- abdullah693/gemma-3-4b-it-urdu-edu-reasoning
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# Urdu Education & Reasoning — Gemma-3-4B adapted via Adaption AutoScientist
|
| 16 |
+
|
| 17 |
+
A results page + live demo for a Gemma-3-4B model adapted to Urdu by translating English
|
| 18 |
+
knowledge corpora into Urdu with Adaption AutoScientist, benchmarked on UrduMMLU (46.2%, +1.3 vs base).
|
| 19 |
+
|
| 20 |
+
**Hardware:** set Space → Settings → Hardware → **ZeroGPU** (PRO). The model loads bf16 on the H200 per request.
|
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Urdu Education & Reasoning — Gemma-3-4B adapted to Urdu via Adaption AutoScientist.
|
| 3 |
+
A compelling results page + live demo, served on HF ZeroGPU.
|
| 4 |
+
"""
|
| 5 |
+
import os, spaces, gradio as gr, torch
|
| 6 |
+
from threading import Thread
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 8 |
+
|
| 9 |
+
MODEL = os.environ.get("MODEL_REPO", "abdullah693/gemma-3-4b-it-urdu-edu-reasoning")
|
| 10 |
+
|
| 11 |
+
print("loading model on cuda (module level for ZeroGPU)...", flush=True)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL, device_map="cuda", torch_dtype=torch.bfloat16)
|
| 14 |
+
model.eval()
|
| 15 |
+
print("ready", flush=True)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@spaces.GPU(duration=120)
|
| 19 |
+
def respond(message, history, max_tokens, temperature):
|
| 20 |
+
msgs = []
|
| 21 |
+
for u, a in history:
|
| 22 |
+
msgs.append({"role": "user", "content": u})
|
| 23 |
+
if a: msgs.append({"role": "assistant", "content": a})
|
| 24 |
+
msgs.append({"role": "user", "content": message})
|
| 25 |
+
enc = tokenizer.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt",
|
| 26 |
+
return_dict=True).to(model.device)
|
| 27 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 28 |
+
Thread(target=model.generate, kwargs=dict(**enc, streamer=streamer, max_new_tokens=int(max_tokens),
|
| 29 |
+
do_sample=temperature > 0, temperature=temperature if temperature > 0 else None,
|
| 30 |
+
repetition_penalty=1.1, pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id)).start()
|
| 31 |
+
out = ""
|
| 32 |
+
for t in streamer:
|
| 33 |
+
out += t; yield out
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
CSS = """
|
| 37 |
+
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Inter:wght@400;500;600&display=swap');
|
| 38 |
+
.gradio-container{max-width:1000px!important;margin:0 auto!important}
|
| 39 |
+
#hero{position:relative;border-radius:20px;overflow:hidden;margin-bottom:14px;
|
| 40 |
+
background:linear-gradient(135deg,#01411C 0%,#0a7a43 55%,#01411C 100%);box-shadow:0 14px 40px rgba(1,40,20,.35)}
|
| 41 |
+
#hero .in{padding:38px 26px;text-align:center;color:#fff}
|
| 42 |
+
#hero h1{font-family:'Playfair Display',serif;font-size:2.4rem;margin:.1em 0;color:#fff;line-height:1.1}
|
| 43 |
+
#hero h1 .a{color:#e9c75a}
|
| 44 |
+
#hero p{font-family:'Inter',sans-serif;font-size:1.05rem;color:#fff;opacity:1;max-width:680px;margin:10px auto;text-shadow:0 1px 4px rgba(0,0,0,.4)}
|
| 45 |
+
#hero .badges{display:flex;gap:8px;justify-content:center;flex-wrap:wrap;margin-top:14px}
|
| 46 |
+
#hero .b{font-family:'Inter';font-size:.8rem;color:#06301c;background:#e9c75a;font-weight:600;padding:5px 13px;border-radius:999px}
|
| 47 |
+
.sec{font-family:'Inter',sans-serif}
|
| 48 |
+
.sec h2{font-family:'Playfair Display',serif;color:#01411C;font-size:1.5rem;margin:.2em 0 .1em}
|
| 49 |
+
.kpi{display:flex;gap:10px;flex-wrap:wrap;justify-content:center;margin:6px 0 2px}
|
| 50 |
+
.kpi .c{background:#f3f8f4;border:1px solid #d8e6dc;border-radius:14px;padding:12px 18px;text-align:center;min-width:120px}
|
| 51 |
+
.kpi .v{font-size:1.7rem;font-weight:700;color:#0a7a43;font-family:'Inter'}
|
| 52 |
+
.kpi .l{font-size:.78rem;color:#555}
|
| 53 |
+
.note{background:#fbf6e9;border:1px solid #ecdfb8;border-radius:12px;padding:12px 16px;font-family:'Inter';font-size:.92rem;color:#4a4a4a}
|
| 54 |
+
#foot{font-family:'Inter';font-size:.8rem;color:#888;text-align:center;margin-top:16px;line-height:1.7}
|
| 55 |
+
#foot a{color:#0a7a43;text-decoration:none;font-weight:500}
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
HERO = """<div id="hero"><div class="in">
|
| 59 |
+
<h1>اردو <span class="a">Education & Reasoning</span></h1>
|
| 60 |
+
<p>A Gemma-3-4B model adapted to Urdu by translating English knowledge corpora into Urdu with
|
| 61 |
+
<b>Adaption AutoScientist</b> — and benchmarked on UrduMMLU.</p>
|
| 62 |
+
<div class="badges"><span class="b">Gemma-3-4B</span><span class="b">Adaption AutoScientist</span>
|
| 63 |
+
<span class="b">UrduMMLU 46.2%</span><span class="b">+1.3 vs base</span></div></div></div>"""
|
| 64 |
+
|
| 65 |
+
KPI = """<div class="kpi">
|
| 66 |
+
<div class="c"><div class="v">+5.9</div><div class="l">STEM (pts)</div></div>
|
| 67 |
+
<div class="c"><div class="v">+3.6</div><div class="l">Profession</div></div>
|
| 68 |
+
<div class="c"><div class="v">+2.7</div><div class="l">Social Sci.</div></div>
|
| 69 |
+
<div class="c"><div class="v">46.2%</div><div class="l">UrduMMLU overall</div></div>
|
| 70 |
+
<div class="c"><div class="v">beats</div><div class="l">base Gemma (44.96%)</div></div></div>"""
|
| 71 |
+
|
| 72 |
+
APPROACH = """<div class="sec"><h2>How it was built</h2>
|
| 73 |
+
<p>Most UrduMMLU domains test <b>transferable knowledge</b> (science, math, social science, reasoning) that
|
| 74 |
+
exists in abundant English datasets. We assembled ~40K examples from open English corpora
|
| 75 |
+
(MMLU, GSM8K, MATH, ARC, AQuA, commonsense) plus native-Urdu instruction data, then used
|
| 76 |
+
<b>Adaption AutoScientist</b> and the <b>Adaptive Data</b> pipeline to adapt each example into natural
|
| 77 |
+
Pakistani Urdu — adding richer prompts and English reasoning traces — and supervised-fine-tuned Gemma-3-4B on the result.</p></div>"""
|
| 78 |
+
|
| 79 |
+
FINDING = """<div class="note">📌 <b>The honest finding.</b> Cross-lingual adaptation lifted <i>every transferable
|
| 80 |
+
domain</i> and beat the base model. The one domain that didn't improve was <b>Urdu literature / Humanities</b>
|
| 81 |
+
(��2.5) — knowledge that is intrinsic to the Urdu language and <i>cannot</i> be sourced by adapting English.
|
| 82 |
+
Closing it needs <b>better native Urdu-literature datasets</b>, not better adaptation — a data-availability
|
| 83 |
+
problem, not a method problem.</div>"""
|
| 84 |
+
|
| 85 |
+
FOOT = """<div id="foot">Model: <a href="https://huggingface.co/abdullah693/gemma-3-4b-it-urdu-edu-reasoning" target="_blank">abdullah693/gemma-3-4b-it-urdu-edu-reasoning</a>
|
| 86 |
+
· Adapted with <b>Adaption AutoScientist</b> · Eval: <a href="https://huggingface.co/datasets/MBZUAI/UrduMMLU" target="_blank">UrduMMLU</a> (Urdu, 0-shot)<br>
|
| 87 |
+
<i>Research/educational use — not authoritative for exams or religious rulings.</i></div>"""
|
| 88 |
+
|
| 89 |
+
EXAMPLES = [
|
| 90 |
+
"پاکستان کا قومی پھول کون سا ہے اور اس کی کیا خصوصیات ہیں؟",
|
| 91 |
+
"اگر ایک ٹرین 60 کلومیٹر فی گھنٹہ کی رفتار سے 2.5 گھنٹے چلے تو کتنا فاصلہ طے کرے گی؟",
|
| 92 |
+
"تعلیم کسی معاشرے کی ترقی میں کیا کردار ادا کرتی ہے؟ مختصر وضاحت کریں۔",
|
| 93 |
+
"نظامِ شمسی میں کتنے سیارے ہیں اور سب سے بڑا سیارہ کون سا ہے؟",
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(title="Urdu Education & Reasoning", theme=gr.themes.Soft(primary_hue="emerald"), css=CSS) as demo:
|
| 97 |
+
gr.HTML(HERO)
|
| 98 |
+
gr.HTML('<div class="sec"><h2>The result</h2></div>')
|
| 99 |
+
gr.HTML(KPI)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
gr.Image("assets/fig_overall.png", show_label=False, container=False)
|
| 102 |
+
gr.Image("assets/fig_domains.png", show_label=False, container=False)
|
| 103 |
+
gr.HTML(APPROACH)
|
| 104 |
+
gr.Image("assets/fig_data.png", show_label=False, container=False)
|
| 105 |
+
gr.HTML(FINDING)
|
| 106 |
+
gr.HTML('<div class="sec"><h2>Try it — اردو میں سوال پوچھیں</h2></div>')
|
| 107 |
+
with gr.Accordion("⚙️ settings", open=False):
|
| 108 |
+
mt = gr.Slider(64, 512, value=256, step=32, label="Max new tokens")
|
| 109 |
+
tp = gr.Slider(0.0, 1.0, value=0.0, step=0.1, label="Temperature")
|
| 110 |
+
gr.ChatInterface(respond, additional_inputs=[mt, tp], examples=[[e] for e in EXAMPLES], cache_examples=False)
|
| 111 |
+
gr.HTML(FOOT)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.queue().launch()
|
assets/fig_data.png
ADDED
|
assets/fig_domains.png
ADDED
|
assets/fig_overall.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers>=4.50
|
| 3 |
+
accelerate
|
| 4 |
+
sentencepiece
|
| 5 |
+
gradio>=5.0
|
| 6 |
+
spaces
|