Update app.py
#2
by arifa-batool - opened
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
model_path = "./best_model"
|
| 7 |
-
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 10 |
|
|
@@ -12,30 +11,28 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 12 |
model.to(device)
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
-
|
| 16 |
def predict_sentiment(text):
|
|
|
|
|
|
|
|
|
|
| 17 |
inputs = tokenizer(
|
| 18 |
text,
|
| 19 |
return_tensors="pt",
|
| 20 |
truncation=True,
|
| 21 |
-
padding=True
|
|
|
|
| 22 |
).to(device)
|
| 23 |
-
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model(**inputs)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return label, f"{confidence:.2f}"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
custom_css = """
|
| 40 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');
|
| 41 |
|
|
@@ -44,252 +41,154 @@ custom_css = """
|
|
| 44 |
--panel: rgba(255, 255, 255, 0.04);
|
| 45 |
--panel-strong: rgba(255, 255, 255, 0.07);
|
| 46 |
--border: rgba(255, 255, 255, 0.08);
|
| 47 |
-
|
| 48 |
--primary: #7C3AED;
|
| 49 |
--secondary: #22D3EE;
|
| 50 |
--text: #E5E7EB;
|
| 51 |
--muted: #9CA3AF;
|
| 52 |
}
|
| 53 |
|
| 54 |
-
/* GLOBAL */
|
| 55 |
body {
|
| 56 |
background: radial-gradient(circle at top, #0B1020, #05060A);
|
| 57 |
font-family: 'Inter', sans-serif;
|
| 58 |
}
|
| 59 |
|
| 60 |
-
/* MAIN CONTAINER */
|
| 61 |
.gradio-container {
|
| 62 |
-
max-width:
|
| 63 |
margin: auto !important;
|
| 64 |
-
padding: 40px 20px
|
| 65 |
}
|
| 66 |
|
| 67 |
-
/*
|
| 68 |
.block {
|
| 69 |
background: var(--panel) !important;
|
| 70 |
border: 1px solid var(--border) !important;
|
| 71 |
border-radius: 24px !important;
|
| 72 |
-
padding:
|
| 73 |
-
backdrop-filter: blur(
|
| 74 |
box-shadow: 0 20px 60px rgba(0,0,0,0.6);
|
| 75 |
}
|
| 76 |
|
| 77 |
-
/*
|
| 78 |
h1 {
|
| 79 |
-
font-size:
|
| 80 |
font-weight: 800 !important;
|
| 81 |
text-align: center;
|
| 82 |
-
color: white
|
| 83 |
-
|
| 84 |
-
letter-spacing: -0.5px;
|
| 85 |
}
|
| 86 |
-
|
| 87 |
h1 span {
|
| 88 |
-
background: linear-gradient(90deg,
|
| 89 |
-webkit-background-clip: text;
|
| 90 |
-webkit-text-fill-color: transparent;
|
| 91 |
}
|
| 92 |
|
| 93 |
-
/*
|
| 94 |
.subtitle {
|
| 95 |
text-align: center;
|
| 96 |
color: var(--muted);
|
| 97 |
-
font-size:
|
| 98 |
margin-bottom: 30px;
|
| 99 |
}
|
| 100 |
|
| 101 |
-
/*
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
padding:
|
| 108 |
-
border-radius:
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
-
/*
|
| 114 |
textarea {
|
| 115 |
-
background: rgba(0,0,0,0.
|
| 116 |
border: 1px solid var(--border) !important;
|
| 117 |
border-radius: 16px !important;
|
| 118 |
color: var(--text) !important;
|
| 119 |
-
font-size:
|
| 120 |
-
padding:
|
| 121 |
-
min-height:
|
| 122 |
-
transition: all 0.3s ease;
|
| 123 |
}
|
| 124 |
-
|
| 125 |
textarea:focus {
|
| 126 |
border-color: var(--secondary) !important;
|
| 127 |
-
box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.15);
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
/* BUTTON */
|
| 131 |
-
button {
|
| 132 |
-
background: linear-gradient(135deg, var(--primary), var(--secondary)) !important;
|
| 133 |
-
color: white !important;
|
| 134 |
-
font-weight: 700 !important;
|
| 135 |
-
font-size: 16px !important;
|
| 136 |
-
padding: 14px !important;
|
| 137 |
-
border-radius: 14px !important;
|
| 138 |
-
border: none !important;
|
| 139 |
-
width: 100%;
|
| 140 |
-
transition: all 0.25s ease;
|
| 141 |
-
box-shadow: 0 10px 25px rgba(124, 58, 237, 0.25);
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
button:hover {
|
| 145 |
-
transform: translateY(-2px);
|
| 146 |
-
box-shadow: 0 15px 35px rgba(34, 211, 238, 0.25);
|
| 147 |
}
|
| 148 |
|
| 149 |
-
/*
|
| 150 |
label {
|
| 151 |
color: var(--secondary) !important;
|
| 152 |
font-weight: 600 !important;
|
|
|
|
| 153 |
}
|
| 154 |
|
| 155 |
-
/*
|
| 156 |
.output-text {
|
| 157 |
background: rgba(0,0,0,0.35) !important;
|
| 158 |
border: 1px solid var(--border) !important;
|
| 159 |
border-radius: 16px !important;
|
| 160 |
-
padding:
|
| 161 |
font-size: 18px !important;
|
| 162 |
color: white !important;
|
| 163 |
font-weight: 600;
|
| 164 |
text-align: center;
|
| 165 |
}
|
| 166 |
-
|
| 167 |
-
/* EXAMPLES */
|
| 168 |
-
.gr-examples {
|
| 169 |
-
margin-top: 20px;
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
.gr-examples button {
|
| 173 |
-
background: rgba(255,255,255,0.06) !important;
|
| 174 |
-
border: 1px solid var(--border) !important;
|
| 175 |
-
color: var(--text) !important;
|
| 176 |
-
font-weight: 500 !important;
|
| 177 |
-
border-radius: 10px !important;
|
| 178 |
-
}
|
| 179 |
-
|
| 180 |
-
.gr-examples button:hover {
|
| 181 |
-
background: rgba(255,255,255,0.1) !important;
|
| 182 |
-
}
|
| 183 |
"""
|
| 184 |
|
| 185 |
-
|
| 186 |
examples = [
|
| 187 |
["یہ بہت اچھا پروڈکٹ ہے"],
|
| 188 |
["مجھے یہ بالکل پسند نہیں آیا"],
|
| 189 |
-
["یہ ٹھیک تھا"],
|
| 190 |
["زبردست تجربہ تھا"],
|
| 191 |
-
["خدمات بہت خراب تھیں"]
|
|
|
|
| 192 |
]
|
| 193 |
|
| 194 |
-
|
| 195 |
with gr.Blocks(css=custom_css, title="Urdu Sentiment Analysis") as interface:
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
# FULL HERO HEADER
|
| 198 |
-
gr.Markdown("""
|
| 199 |
-
<div style="
|
| 200 |
-
width:100%;
|
| 201 |
-
text-align:center;
|
| 202 |
-
padding:30px 0 10px 0;
|
| 203 |
-
">
|
| 204 |
-
<h1 style="
|
| 205 |
-
font-size:46px;
|
| 206 |
-
font-weight:900;
|
| 207 |
-
margin:0;
|
| 208 |
-
color:white;
|
| 209 |
-
">
|
| 210 |
-
🇵🇰 Urdu Sentiment <span style="
|
| 211 |
-
background: linear-gradient(90deg,#7C3AED,#22D3EE);
|
| 212 |
-
-webkit-background-clip:text;
|
| 213 |
-
-webkit-text-fill-color:transparent;
|
| 214 |
-
">Analyzer</span>
|
| 215 |
-
</h1>
|
| 216 |
-
|
| 217 |
-
<p style="
|
| 218 |
-
color:#9CA3AF;
|
| 219 |
-
font-size:16px;
|
| 220 |
-
margin-top:10px;
|
| 221 |
-
">
|
| 222 |
-
Production-grade NLP system for Urdu text understanding
|
| 223 |
-
</p>
|
| 224 |
-
</div>
|
| 225 |
-
""")
|
| 226 |
-
|
| 227 |
-
# FULL WIDTH MAIN AREA (NO CONDENSED BOX FEEL)
|
| 228 |
with gr.Row():
|
| 229 |
-
|
| 230 |
-
# INPUT AREA (FULL HEIGHT PANEL STYLE)
|
| 231 |
-
with gr.Column(scale=6):
|
| 232 |
-
|
| 233 |
text_input = gr.Textbox(
|
| 234 |
-
label="
|
| 235 |
placeholder="یہاں اردو جملہ لکھیں...",
|
| 236 |
-
lines=
|
| 237 |
)
|
| 238 |
-
|
| 239 |
analyze_btn = gr.Button("Analyze Sentiment")
|
| 240 |
-
|
| 241 |
gr.Examples(
|
| 242 |
examples=examples,
|
| 243 |
inputs=text_input,
|
| 244 |
-
label="
|
| 245 |
)
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
gr.Markdown("### Prediction")
|
| 251 |
-
|
| 252 |
sentiment = gr.Textbox(
|
| 253 |
-
label="
|
| 254 |
interactive=False,
|
| 255 |
elem_classes="output-text"
|
| 256 |
)
|
| 257 |
-
|
| 258 |
confidence = gr.Textbox(
|
| 259 |
label="Confidence Score",
|
| 260 |
interactive=False,
|
| 261 |
elem_classes="output-text"
|
| 262 |
)
|
| 263 |
|
| 264 |
-
|
| 265 |
-
<div style="
|
| 266 |
-
margin-top:20px;
|
| 267 |
-
padding:16px;
|
| 268 |
-
border-radius:16px;
|
| 269 |
-
background: rgba(255,255,255,0.03);
|
| 270 |
-
border: 1px solid rgba(255,255,255,0.08);
|
| 271 |
-
color:#9CA3AF;
|
| 272 |
-
font-size:13px;
|
| 273 |
-
line-height:1.6;
|
| 274 |
-
">
|
| 275 |
-
⚡ Model uses fine-tuned mBERT architecture<br>
|
| 276 |
-
📊 Output is softmax probability distribution<br>
|
| 277 |
-
🧠 Trained on Urdu sentiment dataset
|
| 278 |
-
</div>
|
| 279 |
-
""")
|
| 280 |
-
|
| 281 |
-
# FULL WIDTH FOOTER
|
| 282 |
gr.Markdown("""
|
| 283 |
-
<div style="
|
| 284 |
-
|
| 285 |
-
padding:25px;
|
| 286 |
-
text-align:center;
|
| 287 |
-
color:#6B7280;
|
| 288 |
-
font-size:13px;
|
| 289 |
-
border-top:1px solid rgba(255,255,255,0.08);
|
| 290 |
-
">
|
| 291 |
-
Built with Transformers + Gradio • Deployed NLP Demo •
|
| 292 |
-
<b>Urdu Sentiment AI System</b>
|
| 293 |
</div>
|
| 294 |
""")
|
| 295 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# ====================== MODEL ======================
|
| 6 |
model_path = "./best_model"
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 9 |
|
|
|
|
| 11 |
model.to(device)
|
| 12 |
model.eval()
|
| 13 |
|
|
|
|
| 14 |
def predict_sentiment(text):
|
| 15 |
+
if not text or text.strip() == "":
|
| 16 |
+
return "Please enter text", "0.00"
|
| 17 |
+
|
| 18 |
inputs = tokenizer(
|
| 19 |
text,
|
| 20 |
return_tensors="pt",
|
| 21 |
truncation=True,
|
| 22 |
+
padding=True,
|
| 23 |
+
max_length=512
|
| 24 |
).to(device)
|
| 25 |
+
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model(**inputs)
|
| 28 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 29 |
+
pred_id = torch.argmax(probs, dim=1).item()
|
| 30 |
+
confidence = torch.max(probs).item()
|
| 31 |
+
label = model.config.id2label[pred_id]
|
| 32 |
+
|
| 33 |
+
return label, f"{confidence:.2%}"
|
| 34 |
+
|
| 35 |
+
# ====================== PROFESSIONAL CSS ======================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
custom_css = """
|
| 37 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');
|
| 38 |
|
|
|
|
| 41 |
--panel: rgba(255, 255, 255, 0.04);
|
| 42 |
--panel-strong: rgba(255, 255, 255, 0.07);
|
| 43 |
--border: rgba(255, 255, 255, 0.08);
|
|
|
|
| 44 |
--primary: #7C3AED;
|
| 45 |
--secondary: #22D3EE;
|
| 46 |
--text: #E5E7EB;
|
| 47 |
--muted: #9CA3AF;
|
| 48 |
}
|
| 49 |
|
|
|
|
| 50 |
body {
|
| 51 |
background: radial-gradient(circle at top, #0B1020, #05060A);
|
| 52 |
font-family: 'Inter', sans-serif;
|
| 53 |
}
|
| 54 |
|
|
|
|
| 55 |
.gradio-container {
|
| 56 |
+
max-width: 100% !important;
|
| 57 |
margin: auto !important;
|
| 58 |
+
padding: 40px 20px;
|
| 59 |
}
|
| 60 |
|
| 61 |
+
/* Main Card */
|
| 62 |
.block {
|
| 63 |
background: var(--panel) !important;
|
| 64 |
border: 1px solid var(--border) !important;
|
| 65 |
border-radius: 24px !important;
|
| 66 |
+
padding: 40px !important;
|
| 67 |
+
backdrop-filter: blur(12px);
|
| 68 |
box-shadow: 0 20px 60px rgba(0,0,0,0.6);
|
| 69 |
}
|
| 70 |
|
| 71 |
+
/* Title */
|
| 72 |
h1 {
|
| 73 |
+
font-size: 46px !important;
|
| 74 |
font-weight: 800 !important;
|
| 75 |
text-align: center;
|
| 76 |
+
color: white;
|
| 77 |
+
letter-spacing: -1px;
|
|
|
|
| 78 |
}
|
|
|
|
| 79 |
h1 span {
|
| 80 |
+
background: linear-gradient(90deg, #7C3AED, #22D3EE);
|
| 81 |
-webkit-background-clip: text;
|
| 82 |
-webkit-text-fill-color: transparent;
|
| 83 |
}
|
| 84 |
|
| 85 |
+
/* Subtitle */
|
| 86 |
.subtitle {
|
| 87 |
text-align: center;
|
| 88 |
color: var(--muted);
|
| 89 |
+
font-size: 17px;
|
| 90 |
margin-bottom: 30px;
|
| 91 |
}
|
| 92 |
|
| 93 |
+
/* Button */
|
| 94 |
+
button {
|
| 95 |
+
background: linear-gradient(135deg, var(--primary), var(--secondary)) !important;
|
| 96 |
+
color: white !important;
|
| 97 |
+
font-weight: 700 !important;
|
| 98 |
+
font-size: 17px !important;
|
| 99 |
+
padding: 16px 0 !important;
|
| 100 |
+
border-radius: 16px !important;
|
| 101 |
+
width: 100%;
|
| 102 |
+
margin: 15px 0 25px 0;
|
| 103 |
+
box-shadow: 0 10px 30px rgba(124, 58, 237, 0.3);
|
| 104 |
+
}
|
| 105 |
+
button:hover {
|
| 106 |
+
transform: translateY(-3px);
|
| 107 |
+
box-shadow: 0 15px 40px rgba(34, 211, 238, 0.3);
|
| 108 |
}
|
| 109 |
|
| 110 |
+
/* Input */
|
| 111 |
textarea {
|
| 112 |
+
background: rgba(0,0,0,0.35) !important;
|
| 113 |
border: 1px solid var(--border) !important;
|
| 114 |
border-radius: 16px !important;
|
| 115 |
color: var(--text) !important;
|
| 116 |
+
font-size: 16.5px !important;
|
| 117 |
+
padding: 18px !important;
|
| 118 |
+
min-height: 160px !important;
|
|
|
|
| 119 |
}
|
|
|
|
| 120 |
textarea:focus {
|
| 121 |
border-color: var(--secondary) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
+
/* Output Labels (Cyan) */
|
| 125 |
label {
|
| 126 |
color: var(--secondary) !important;
|
| 127 |
font-weight: 600 !important;
|
| 128 |
+
font-size: 15px !important;
|
| 129 |
}
|
| 130 |
|
| 131 |
+
/* Output Boxes - Subtle Dark */
|
| 132 |
.output-text {
|
| 133 |
background: rgba(0,0,0,0.35) !important;
|
| 134 |
border: 1px solid var(--border) !important;
|
| 135 |
border-radius: 16px !important;
|
| 136 |
+
padding: 20px !important;
|
| 137 |
font-size: 18px !important;
|
| 138 |
color: white !important;
|
| 139 |
font-weight: 600;
|
| 140 |
text-align: center;
|
| 141 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
"""
|
| 143 |
|
|
|
|
| 144 |
examples = [
|
| 145 |
["یہ بہت اچھا پروڈکٹ ہے"],
|
| 146 |
["مجھے یہ بالکل پسند نہیں آیا"],
|
|
|
|
| 147 |
["زبردست تجربہ تھا"],
|
| 148 |
+
["خدمات بہت خراب تھیں"],
|
| 149 |
+
["یہ ٹھیک تھا"]
|
| 150 |
]
|
| 151 |
|
|
|
|
| 152 |
with gr.Blocks(css=custom_css, title="Urdu Sentiment Analysis") as interface:
|
| 153 |
+
|
| 154 |
+
gr.Markdown("# 🇵🇰 Urdu Sentiment <span>Analyzer</span>")
|
| 155 |
+
gr.Markdown('<p class="subtitle">Fine-tuned mBERT for Accurate Urdu Sentiment Classification</p>')
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
with gr.Row():
|
| 158 |
+
with gr.Column(scale=7):
|
|
|
|
|
|
|
|
|
|
| 159 |
text_input = gr.Textbox(
|
| 160 |
+
label="Enter Urdu Text",
|
| 161 |
placeholder="یہاں اردو جملہ لکھیں...",
|
| 162 |
+
lines=9
|
| 163 |
)
|
| 164 |
+
|
| 165 |
analyze_btn = gr.Button("Analyze Sentiment")
|
| 166 |
+
|
| 167 |
gr.Examples(
|
| 168 |
examples=examples,
|
| 169 |
inputs=text_input,
|
| 170 |
+
label="Example Sentences"
|
| 171 |
)
|
| 172 |
|
| 173 |
+
with gr.Column(scale=5):
|
| 174 |
+
gr.Markdown("### Prediction Results")
|
| 175 |
+
|
|
|
|
|
|
|
| 176 |
sentiment = gr.Textbox(
|
| 177 |
+
label="Predicted Sentiment",
|
| 178 |
interactive=False,
|
| 179 |
elem_classes="output-text"
|
| 180 |
)
|
| 181 |
+
|
| 182 |
confidence = gr.Textbox(
|
| 183 |
label="Confidence Score",
|
| 184 |
interactive=False,
|
| 185 |
elem_classes="output-text"
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# Footer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
gr.Markdown("""
|
| 190 |
+
<div style="text-align:center; margin-top:50px; color:#6B7280; font-size:14px;">
|
| 191 |
+
Built with Transformers + Gradio • Production-ready Urdu NLP Demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
</div>
|
| 193 |
""")
|
| 194 |
|