import gradio as gr from transformers import pipeline # Load model from HuggingFace Hub MODEL_ID = "H-Layba/urdu-sentiment-classifier" classifier = pipeline( "text-classification", model=MODEL_ID ) # Example sentences EXAMPLES = [ ["یہ فلم بہت اچھی تھی"], # This film was very good ["آج کا دن بہت برا تھا"], # Today was a very bad day ["کھانا لاجواب تھا"], # The food was excellent ["خدمت بہت خراب تھی"], # The service was very bad ["یہ کتاب بہت مفید ہے"], # This book is very useful ["بہت مایوسی ہوئی"], # Very disappointed ] def analyze(text): if not text.strip(): return "", "", 0 result = classifier(text)[0] label = result["label"] score = result["score"] emoji = "🟢 مثبت (Positive)" if label == "positive" else "🔴 منفی (Negative)" confidence = round(score * 100, 2) bar = score return emoji, f"{confidence}%", bar # Custom CSS for a clean dark UI with Urdu-friendly typography css = """ @import url('https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu&family=Inter:wght@400;500;600&display=swap'); body, .gradio-container { background: #0a0a0f !important; font-family: 'Inter', sans-serif; } #title { text-align: center; padding: 2rem 1rem 0.5rem; } #title h1 { font-size: 2rem; font-weight: 600; color: #e4e4f0; letter-spacing: -0.02em; margin-bottom: 0.4rem; } #title p { color: #6b6b8a; font-size: 0.95rem; } .urdu-input textarea { font-family: 'Noto Nastaliq Urdu', serif !important; font-size: 1.3rem !important; direction: rtl !important; text-align: right !important; line-height: 2.2 !important; background: #12121e !important; border: 1px solid #2a2a3e !important; color: #e4e4f0 !important; border-radius: 10px !important; padding: 1rem !important; } .urdu-input textarea:focus { border-color: #7c6dfa !important; box-shadow: 0 0 0 2px rgba(124, 109, 250, 0.15) !important; } .result-label { font-size: 1.4rem !important; font-weight: 600 !important; text-align: center !important; padding: 1rem !important; background: #12121e !important; border: 1px solid #2a2a3e !important; border-radius: 10px !important; color: #e4e4f0 !important; } .confidence-text { font-size: 1.1rem !important; text-align: center !important; color: #7c6dfa !important; font-weight: 500 !important; background: #12121e !important; border: 1px solid #2a2a3e !important; border-radius: 10px !important; } button.primary { background: #7c6dfa !important; border: none !important; color: white !important; font-weight: 500 !important; border-radius: 8px !important; padding: 0.6rem 1.5rem !important; } button.primary:hover { background: #6b5ce7 !important; } .example-btn { font-family: 'Noto Nastaliq Urdu', serif !important; direction: rtl !important; font-size: 1rem !important; } #stats { display: flex; justify-content: center; gap: 2rem; padding: 1rem; margin-top: 1rem; } .stat-card { background: #12121e; border: 1px solid #2a2a3e; border-radius: 10px; padding: 0.8rem 1.5rem; text-align: center; } .stat-num { font-size: 1.4rem; font-weight: 600; color: #7c6dfa; font-family: 'JetBrains Mono', monospace; } .stat-label { font-size: 0.75rem; color: #6b6b8a; margin-top: 2px; } footer { display: none !important; } """ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo: gr.HTML("""
اردو متن کا جذباتی تجزیہ — Fine-tuned mBERT on 50,000 Urdu reviews