import gradio as gr from transformers import pipeline # Load sentiment analysis models english_sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") arabic_sentiment_model = pipeline("text-classification", model="CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment") # Define label meanings for Arabic sentiment analysis arabic_labels = { "positive": "إيجابي", "negative": "سلبي", "neutral": "محايد" } def analyze_sentiment(text, language): if language == "English": result = english_sentiment_model(text) return result[0]['label'], result[0]['score'] else: result = arabic_sentiment_model(text) label = result[0]['label'] arabic_label = arabic_labels.get(label, "غير معروف") # Default to "Unknown" if label not found return arabic_label, result[0]['score'] # custom CSS css = """ body { background-color: #f4f7f6; font-family: 'Arial', sans-serif; } h1, h2 { color: #3e606f; } .gradio-container { border-radius: 10px; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); } button { background-color: #3e606f; color: white; border: none; border-radius: 5px; padding: 10px 15px; cursor: pointer; } button:hover { background-color: #2c4d4f; } .result { font-weight: bold; color: #3e606f; } """ iface = gr.Interface( fn=analyze_sentiment, inputs=[ gr.Textbox(label="Enter text", placeholder="Type your text here..."), gr.Radio(choices=["English", "Arabic"], label="Select Language") # Default style retained ], outputs=[ gr.Label(label="Sentiment"), gr.Number(label="Confidence Score") ], title="Sentiment Analysis", description="Analyze the sentiment of text in English and Arabic.", examples=[ ["I love this product!", "English"], ["This is the worst experience I've ever had.", "English"], ["أنا سعيد جدًا بهذا!", "Arabic"], ["هذا المكان سيء للغاية.", "Arabic"] ], css=css ) iface.launch()