anwerhu395 commited on
Commit
3001380
·
verified ·
1 Parent(s): 93856c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -12
app.py CHANGED
@@ -1,24 +1,61 @@
1
  import gradio as gr
2
- from camel_tools.sentiment import SentimentAnalyzer
3
- import torch # Add torch as it's a key dependency
4
 
5
- # This will run on the Hugging Face server
6
- sa = SentimentAnalyzer.from_pretrained()
 
 
 
 
 
 
 
 
 
 
7
 
8
  def analyze_sentiment(text):
 
 
 
9
  if not text or not text.strip():
10
  return "No text provided."
11
- # sa.predict expects a list of strings
12
- prediction = sa.predict([text])
13
- # It returns a list with one result, e.g., ['positive']
14
- return prediction[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  iface = gr.Interface(
17
  fn=analyze_sentiment,
18
- inputs=gr.Textbox(lines=5, label="Enter Arabic Text Here"),
19
- outputs=gr.Textbox(label="Sentiment Result"),
20
- title="🧠 Kayan's Arabic Sentiment Brain 🧠",
21
- description="This is the cloud-based sentiment analysis engine for Project Kayan. Coded by Anwar & Manus AI."
 
 
 
 
 
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()