H-Layba commited on
Commit
04a7dac
·
verified ·
1 Parent(s): 450e3a3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +175 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ MODEL_NAME = "deepset/xlm-roberta-base-squad2"
5
+ qa = pipeline("question-answering", model=MODEL_NAME)
6
+
7
+ EXAMPLES = [
8
+ [
9
+ "پاکستان جنوبی ایشیا میں واقع ایک ملک ہے۔ اس کا دارالحکومت اسلام آباد ہے۔ پاکستان کی آبادی تقریباً 22 کروڑ ہے۔ یہ دنیا کا پانچواں سب سے زیادہ آبادی والا ملک ہے۔ پاکستان 1947 میں برطانوی راج سے آزاد ہوا۔",
10
+ "پاکستان کا دارالحکومت کیا ہے؟"
11
+ ],
12
+ [
13
+ "کرکٹ پاکستان کا سب سے مقبول کھیل ہے۔ پاکستان کرکٹ ٹیم نے 1992 میں عمران خان کی قیادت میں ورلڈ کپ جیتا۔ پاکستان سپر لیگ ملک کی سب سے بڑی کرکٹ لیگ ہے۔",
14
+ "پاکستان نے ورلڈ کپ کب جیتا؟"
15
+ ],
16
+ [
17
+ "اسلام آباد پاکستان کا دارالحکومت ہے۔ یہ شہر 1960 کی دہائی میں تعمیر کیا گیا۔ فیصل مسجد اسلام آباد کی سب سے مشہور عمارت ہے۔ اسلام آباد کی آبادی تقریباً 20 لاکھ ہے۔",
18
+ "فیصل مسجد کہاں ہے؟"
19
+ ],
20
+ ]
21
+
22
+ def answer_question(context, question):
23
+ if not context.strip() or not question.strip():
24
+ return "", 0, ""
25
+
26
+ result = qa(question=question, context=context)
27
+ answer = result["answer"]
28
+ confidence = round(result["score"] * 100, 2)
29
+
30
+ # Highlight answer in context
31
+ highlighted = context.replace(
32
+ answer,
33
+ f"**{answer}**"
34
+ )
35
+
36
+ return answer, confidence, highlighted
37
+
38
+
39
+ css = """
40
+ @import url('https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu&family=Inter:wght@400;500;600&display=swap');
41
+
42
+ body, .gradio-container {
43
+ background: #0a0a0f !important;
44
+ font-family: 'Inter', sans-serif;
45
+ }
46
+
47
+ #title {
48
+ text-align: center;
49
+ padding: 2rem 1rem 1rem;
50
+ }
51
+
52
+ #title h1 {
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
+ direction: rtl !important;
68
+ text-align: right !important;
69
+ line-height: 2.2 !important;
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
+ direction: rtl !important;
85
+ text-align: right !important;
86
+ background: #12121e !important;
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>
113
+ <p>اردو متن سے جوابات نکالیں — XLM-RoBERTa fine-tuned on SQuAD2</p>
114
+ </div>
115
+ """)
116
+
117
+ with gr.Row():
118
+ with gr.Column(scale=2):
119
+ context_input = gr.Textbox(
120
+ label="متن / سیاق — Context (paste your Urdu text here)",
121
+ placeholder="یہاں اردو متن پیسٹ کریں جس سے آپ سوال پوچھنا چاہتے ہیں...",
122
+ lines=8,
123
+ elem_classes="urdu-box"
124
+ )
125
+ question_input = gr.Textbox(
126
+ label="سوال — Question",
127
+ placeholder="یہاں اردو میں سوال لکھیں...",
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
+ label="جواب — Answer",
136
+ interactive=False,
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 base · Fine-tuned on SQuAD2
161
+ </div>
162
+ """)
163
+
164
+ ask_btn.click(
165
+ fn=answer_question,
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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers>=4.40.0
2
+ torch>=2.0.0
3
+ gradio>=4.0.0
4
+ sentencepiece>=0.1.99