Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
MODEL_NAME = "deepset/xlm-roberta-base-squad2"
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
EXAMPLES = [
|
| 8 |
[
|
| 9 |
-
"پاکستان جنوبی ایشیا میں واقع ایک ملک ہے۔ اس کا دارالحکومت اسلام آباد ہے۔ پاکستان کی آبادی تقریباً 22 کروڑ ہے۔
|
| 10 |
"پاکستان کا دارالحکومت کیا ہے؟"
|
| 11 |
],
|
| 12 |
[
|
| 13 |
-
"کرکٹ پاکستان کا سب سے مقبول کھیل ہے۔ پاکستان کرکٹ ٹیم نے 1992 میں عمران خان کی قیادت میں ورلڈ کپ جیتا۔
|
| 14 |
"پاکستان نے ورلڈ کپ کب جیتا؟"
|
| 15 |
],
|
| 16 |
[
|
| 17 |
-
"اسلام آباد
|
| 18 |
"فیصل مسجد کہاں ہے؟"
|
| 19 |
],
|
| 20 |
]
|
| 21 |
|
| 22 |
def answer_question(context, question):
|
| 23 |
if not context.strip() or not question.strip():
|
| 24 |
-
return "", 0, ""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
answer,
|
| 33 |
-
f"**{answer}**"
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
background: #0a0a0f !important;
|
| 44 |
-
font-family: 'Inter', sans-serif;
|
| 45 |
-
}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
text-align: center;
|
| 49 |
-
padding: 2rem 1rem 1rem;
|
| 50 |
-
}
|
| 51 |
|
| 52 |
-
|
| 53 |
-
font-size: 2rem;
|
| 54 |
-
font-weight: 600;
|
| 55 |
-
color: #e4e4f0;
|
| 56 |
-
margin-bottom: 0.4rem;
|
| 57 |
-
}
|
| 58 |
|
| 59 |
-
#title p {
|
| 60 |
-
color: #6b6b8a;
|
| 61 |
-
font-size: 0.9rem;
|
| 62 |
-
}
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
.urdu-box textarea {
|
| 65 |
font-family: 'Noto Nastaliq Urdu', serif !important;
|
| 66 |
-
font-size: 1.2rem !important;
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
background: #12121e !important;
|
| 71 |
-
border: 1px solid #2a2a3e !important;
|
| 72 |
-
color: #e4e4f0 !important;
|
| 73 |
-
border-radius: 10px !important;
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
.urdu-box textarea:focus {
|
| 77 |
-
border-color: #f59e0b !important;
|
| 78 |
-
box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.15) !important;
|
| 79 |
}
|
| 80 |
-
|
| 81 |
.answer-box textarea {
|
| 82 |
font-family: 'Noto Nastaliq Urdu', serif !important;
|
| 83 |
-
font-size: 1.4rem !important;
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
border: 1px solid #2a2a3e !important;
|
| 88 |
-
color: #f59e0b !important;
|
| 89 |
-
border-radius: 10px !important;
|
| 90 |
-
font-weight: 600 !important;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
button.primary {
|
| 94 |
-
background: #f59e0b !important;
|
| 95 |
-
border: none !important;
|
| 96 |
-
color: #0a0a0f !important;
|
| 97 |
-
font-weight: 600 !important;
|
| 98 |
-
border-radius: 8px !important;
|
| 99 |
}
|
| 100 |
-
|
| 101 |
-
button.primary:hover {
|
| 102 |
-
background: #d97706 !important;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
footer { display: none !important; }
|
| 106 |
"""
|
| 107 |
|
| 108 |
with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
| 109 |
-
|
| 110 |
gr.HTML("""
|
| 111 |
<div id="title">
|
| 112 |
<h1>❓ Urdu Question Answering</h1>
|
|
@@ -117,59 +86,30 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column(scale=2):
|
| 119 |
context_input = gr.Textbox(
|
| 120 |
-
label="متن
|
| 121 |
-
|
| 122 |
-
lines=8,
|
| 123 |
-
elem_classes="urdu-box"
|
| 124 |
)
|
| 125 |
question_input = gr.Textbox(
|
| 126 |
-
label="سوال — Question",
|
| 127 |
-
|
| 128 |
-
lines=2,
|
| 129 |
-
elem_classes="urdu-box"
|
| 130 |
)
|
| 131 |
ask_btn = gr.Button("جواب تلاش کریں — Find Answer", variant="primary")
|
| 132 |
|
| 133 |
with gr.Column(scale=1):
|
| 134 |
-
answer_out = gr.Textbox(
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
lines=3,
|
| 138 |
-
elem_classes="answer-box"
|
| 139 |
-
)
|
| 140 |
-
confidence_out = gr.Number(
|
| 141 |
-
label="Confidence %",
|
| 142 |
-
interactive=False
|
| 143 |
-
)
|
| 144 |
-
highlighted_out = gr.Textbox(
|
| 145 |
-
label="Answer highlighted in context",
|
| 146 |
-
interactive=False,
|
| 147 |
-
lines=6,
|
| 148 |
-
elem_classes="urdu-box"
|
| 149 |
-
)
|
| 150 |
|
| 151 |
-
gr.Examples(
|
| 152 |
-
examples=EXAMPLES,
|
| 153 |
-
inputs=[context_input, question_input],
|
| 154 |
-
label="مثالیں — Try these examples"
|
| 155 |
-
)
|
| 156 |
|
| 157 |
gr.HTML("""
|
| 158 |
<div style="text-align:center; padding:1.5rem; color:#3a3a5a; font-size:0.8rem;">
|
| 159 |
Built by <a href="https://huggingface.co/H-Layba" style="color:#f59e0b">H-Layba</a> ·
|
| 160 |
-
Model: XLM-RoBERTa
|
| 161 |
</div>
|
| 162 |
""")
|
| 163 |
|
| 164 |
-
ask_btn.click(
|
| 165 |
-
|
| 166 |
-
inputs=[context_input, question_input],
|
| 167 |
-
outputs=[answer_out, confidence_out, highlighted_out]
|
| 168 |
-
)
|
| 169 |
-
question_input.submit(
|
| 170 |
-
fn=answer_question,
|
| 171 |
-
inputs=[context_input, question_input],
|
| 172 |
-
outputs=[answer_out, confidence_out, highlighted_out]
|
| 173 |
-
)
|
| 174 |
|
| 175 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
MODEL_NAME = "deepset/xlm-roberta-base-squad2"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForQuestionAnswering.from_pretrained(MODEL_NAME)
|
| 8 |
|
| 9 |
EXAMPLES = [
|
| 10 |
[
|
| 11 |
+
"پاکستان جنوبی ایشیا میں واقع ایک ملک ہے۔ اس کا دارالحکومت اسلام آباد ہے۔ پاکستان کی آبادی تقریباً 22 کروڑ ہے۔ پاکستان 1947 میں برطانوی راج سے آزاد ہوا۔",
|
| 12 |
"پاکستان کا دارالحکومت کیا ہے؟"
|
| 13 |
],
|
| 14 |
[
|
| 15 |
+
"کرکٹ پاکستان کا سب سے مقبول کھیل ہے۔ پاکستان کرکٹ ٹیم نے 1992 میں عمران خان کی قیادت میں ورلڈ کپ جیتا۔",
|
| 16 |
"پاکستان نے ورلڈ کپ کب جیتا؟"
|
| 17 |
],
|
| 18 |
[
|
| 19 |
+
"فیصل مسجد اسلام آباد میں واقع ہے اور یہ پاکستان کی سب سے بڑی مسجد ہے۔ اس کی تعمیر 1986 میں مکمل ہوئی۔",
|
| 20 |
"فیصل مسجد کہاں ہے؟"
|
| 21 |
],
|
| 22 |
]
|
| 23 |
|
| 24 |
def answer_question(context, question):
|
| 25 |
if not context.strip() or not question.strip():
|
| 26 |
+
return "", 0.0, ""
|
| 27 |
+
|
| 28 |
+
inputs = tokenizer(
|
| 29 |
+
question,
|
| 30 |
+
context,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
truncation=True,
|
| 33 |
+
max_length=512
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
|
| 39 |
+
start = torch.argmax(outputs.start_logits)
|
| 40 |
+
end = torch.argmax(outputs.end_logits) + 1
|
| 41 |
+
tokens = inputs["input_ids"][0][start:end]
|
| 42 |
+
answer = tokenizer.decode(tokens, skip_special_tokens=True)
|
| 43 |
|
| 44 |
+
# Confidence from logits
|
| 45 |
+
start_score = torch.softmax(outputs.start_logits, dim=1).max().item()
|
| 46 |
+
end_score = torch.softmax(outputs.end_logits, dim=1).max().item()
|
| 47 |
+
confidence = round((start_score + end_score) / 2 * 100, 2)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
highlighted = context.replace(answer, f"**{answer}**") if answer in context else context
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
return answer, confidence, highlighted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
css = """
|
| 55 |
+
@import url('https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu&family=Inter:wght@400;500;600&display=swap');
|
| 56 |
+
body, .gradio-container { background: #0a0a0f !important; font-family: 'Inter', sans-serif; }
|
| 57 |
+
#title { text-align: center; padding: 2rem 1rem 1rem; }
|
| 58 |
+
#title h1 { font-size: 2rem; font-weight: 600; color: #e4e4f0; margin-bottom: 0.4rem; }
|
| 59 |
+
#title p { color: #6b6b8a; font-size: 0.9rem; }
|
| 60 |
.urdu-box textarea {
|
| 61 |
font-family: 'Noto Nastaliq Urdu', serif !important;
|
| 62 |
+
font-size: 1.2rem !important; direction: rtl !important;
|
| 63 |
+
text-align: right !important; line-height: 2.2 !important;
|
| 64 |
+
background: #12121e !important; border: 1px solid #2a2a3e !important;
|
| 65 |
+
color: #e4e4f0 !important; border-radius: 10px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
|
|
|
| 67 |
.answer-box textarea {
|
| 68 |
font-family: 'Noto Nastaliq Urdu', serif !important;
|
| 69 |
+
font-size: 1.4rem !important; direction: rtl !important;
|
| 70 |
+
text-align: right !important; background: #12121e !important;
|
| 71 |
+
border: 1px solid #2a2a3e !important; color: #f59e0b !important;
|
| 72 |
+
border-radius: 10px !important; font-weight: 600 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
+
button.primary { background: #f59e0b !important; border: none !important; color: #0a0a0f !important; font-weight: 600 !important; border-radius: 8px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
footer { display: none !important; }
|
| 76 |
"""
|
| 77 |
|
| 78 |
with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
|
|
| 79 |
gr.HTML("""
|
| 80 |
<div id="title">
|
| 81 |
<h1>❓ Urdu Question Answering</h1>
|
|
|
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column(scale=2):
|
| 88 |
context_input = gr.Textbox(
|
| 89 |
+
label="متن — Context", placeholder="یہاں اردو متن پیسٹ کریں...",
|
| 90 |
+
lines=8, elem_classes="urdu-box"
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
question_input = gr.Textbox(
|
| 93 |
+
label="سوال — Question", placeholder="یہاں سوال لکھیں...",
|
| 94 |
+
lines=2, elem_classes="urdu-box"
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
ask_btn = gr.Button("جواب تلاش کریں — Find Answer", variant="primary")
|
| 97 |
|
| 98 |
with gr.Column(scale=1):
|
| 99 |
+
answer_out = gr.Textbox(label="جواب — Answer", interactive=False, lines=3, elem_classes="answer-box")
|
| 100 |
+
confidence_out = gr.Number(label="Confidence %", interactive=False)
|
| 101 |
+
highlighted_out = gr.Textbox(label="Answer in context", interactive=False, lines=6, elem_classes="urdu-box")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
gr.Examples(examples=EXAMPLES, inputs=[context_input, question_input], label="مثالیں — Examples")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
gr.HTML("""
|
| 106 |
<div style="text-align:center; padding:1.5rem; color:#3a3a5a; font-size:0.8rem;">
|
| 107 |
Built by <a href="https://huggingface.co/H-Layba" style="color:#f59e0b">H-Layba</a> ·
|
| 108 |
+
Model: XLM-RoBERTa · Fine-tuned on SQuAD2
|
| 109 |
</div>
|
| 110 |
""")
|
| 111 |
|
| 112 |
+
ask_btn.click(fn=answer_question, inputs=[context_input, question_input], outputs=[answer_out, confidence_out, highlighted_out])
|
| 113 |
+
question_input.submit(fn=answer_question, inputs=[context_input, question_input], outputs=[answer_out, confidence_out, highlighted_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
demo.launch()
|