Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def analyze_sentiment(text):
|
|
|
|
|
|
|
|
|
|
| 9 |
if not text or not text.strip():
|
| 10 |
return "No text provided."
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
iface = gr.Interface(
|
| 17 |
fn=analyze_sentiment,
|
| 18 |
-
inputs=gr.Textbox(lines=5, label="
|
| 19 |
-
outputs=gr.Textbox(label="
|
| 20 |
-
title="🧠
|
| 21 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load a pre-trained Arabic sentiment analysis model
|
| 6 |
+
try:
|
| 7 |
+
# This model is specifically trained for Arabic sentiment analysis
|
| 8 |
+
sentiment_pipeline = pipeline(
|
| 9 |
+
"sentiment-analysis",
|
| 10 |
+
model="CAMeL-Lab/bert-base-arabic-camelbert-mix-sentiment",
|
| 11 |
+
return_all_scores=True
|
| 12 |
+
)
|
| 13 |
+
MODEL_LOADED = True
|
| 14 |
+
except Exception as e:
|
| 15 |
+
MODEL_LOADED = False
|
| 16 |
+
error_msg = str(e)
|
| 17 |
|
| 18 |
def analyze_sentiment(text):
|
| 19 |
+
if not MODEL_LOADED:
|
| 20 |
+
return f"Model loading failed: {error_msg}"
|
| 21 |
+
|
| 22 |
if not text or not text.strip():
|
| 23 |
return "No text provided."
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Get sentiment prediction
|
| 27 |
+
results = sentiment_pipeline(text)
|
| 28 |
+
|
| 29 |
+
# Extract the result with highest confidence
|
| 30 |
+
best_result = max(results[0], key=lambda x: x['score'])
|
| 31 |
+
label = best_result['label']
|
| 32 |
+
confidence = best_result['score']
|
| 33 |
+
|
| 34 |
+
# Map labels to Arabic
|
| 35 |
+
label_map = {
|
| 36 |
+
'POSITIVE': 'إيجابي',
|
| 37 |
+
'NEGATIVE': 'سلبي',
|
| 38 |
+
'NEUTRAL': 'محايد'
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
arabic_label = label_map.get(label, label)
|
| 42 |
+
|
| 43 |
+
return f"المشاعر: {arabic_label} (الثقة: {confidence:.2f})"
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error in analysis: {str(e)}"
|
| 47 |
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=analyze_sentiment,
|
| 50 |
+
inputs=gr.Textbox(lines=5, label="أدخل النص العربي هنا", placeholder="اكتب أي نص عربي لتحليل مشاعره..."),
|
| 51 |
+
outputs=gr.Textbox(label="نتيجة التحليل"),
|
| 52 |
+
title="🧠 العقل العربي لمشروع كيان 🧠",
|
| 53 |
+
description="محرك تحليل المشاعر العربية السحابي لمشروع كيان. برمجة: أنور ومانوس AI",
|
| 54 |
+
examples=[
|
| 55 |
+
["أنا سعيد جداً اليوم"],
|
| 56 |
+
["هذا اليوم سيء للغاية"],
|
| 57 |
+
["الطقس جميل اليوم"]
|
| 58 |
+
]
|
| 59 |
)
|
| 60 |
|
| 61 |
iface.launch()
|