File size: 2,128 Bytes
2e810d8 50b5fb0 078b91e 50b5fb0 236c9ff 3567fb1 236c9ff d2104f8 50b5fb0 236c9ff d2104f8 50b5fb0 236c9ff 50b5fb0 3567fb1 cb7b592 e12aa82 cb7b592 50b5fb0 cb7b592 eee68b0 50b5fb0 b886a36 50b5fb0 f4ab359 cb7b592 3567fb1 50b5fb0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
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() |