tahamueed23 commited on
Commit
6743c3d
·
verified ·
1 Parent(s): f0abce9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
3
  import pandas as pd
4
  import os
5
  import re
@@ -12,7 +12,6 @@ english_model = pipeline(
12
  model="siebert/sentiment-roberta-large-english"
13
  )
14
 
15
- # Replace with your own fine-tuned models
16
  urdu_model = pipeline(
17
  "sentiment-analysis",
18
  model="tahamueed23/fine_tuned_cardiffnlp_urdu_and_roman-urdu"
@@ -28,8 +27,9 @@ roman_urdu_model = pipeline(
28
  # -----------------------------
29
  SAVE_FILE = "sentiment_logs.csv"
30
  if not os.path.exists(SAVE_FILE):
31
- df = pd.DataFrame(columns=["Sentence", "Language", "Sentiment", "Confidence"])
32
- df.to_csv(SAVE_FILE, index=False)
 
33
 
34
  # -----------------------------
35
  # Language Detection (simple rule-based)
@@ -48,9 +48,9 @@ def detect_language(text):
48
  # -----------------------------
49
  def normalize_label(label):
50
  label = label.lower()
51
- if "positive" in label:
52
  return "Positive"
53
- elif "negative" in label:
54
  return "Negative"
55
  else:
56
  return "Neutral"
@@ -70,38 +70,41 @@ def sentiment_with_tips(sentiment):
70
  # Main Sentiment Function
71
  # -----------------------------
72
  def analyze_sentiment(text, lang_hint):
73
- if not text.strip():
74
- return "⚠️ Please enter a sentence.", "", "", SAVE_FILE
75
-
76
- # Auto detect if language hint is not clear
77
- lang = lang_hint if lang_hint != "Auto Detect" else detect_language(text)
78
-
79
- # Select model
80
- if lang == "English":
81
- result = english_model(text)[0]
82
- elif lang == "Urdu":
83
- result = urdu_model(text)[0]
84
- else:
85
- result = roman_urdu_model(text)[0]
86
-
87
- # Process results
88
- sentiment = normalize_label(result["label"])
89
- score = round(result["score"], 3)
90
- explanation = sentiment_with_tips(sentiment)
91
-
92
- # Save to CSV
93
  try:
94
- df = pd.read_csv(SAVE_FILE, encoding="utf-8-sig")
95
- except:
96
- df = pd.DataFrame(columns=["Sentence", "Language", "Sentiment", "Confidence"])
97
-
98
- new_row = pd.DataFrame([[text, lang, sentiment, score]],
99
- columns=["Sentence", "Language", "Sentiment", "Confidence"])
100
- df = pd.concat([df, new_row], ignore_index=True)
101
-
102
- # Save with proper encoding for Urdu
103
- df.to_csv(SAVE_FILE, index=False, encoding="utf-8-sig")
104
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  # -----------------------------
107
  # Gradio UI
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import pandas as pd
4
  import os
5
  import re
 
12
  model="siebert/sentiment-roberta-large-english"
13
  )
14
 
 
15
  urdu_model = pipeline(
16
  "sentiment-analysis",
17
  model="tahamueed23/fine_tuned_cardiffnlp_urdu_and_roman-urdu"
 
27
  # -----------------------------
28
  SAVE_FILE = "sentiment_logs.csv"
29
  if not os.path.exists(SAVE_FILE):
30
+ pd.DataFrame(columns=["Sentence", "Language", "Sentiment", "Confidence"]).to_csv(
31
+ SAVE_FILE, index=False, encoding="utf-8-sig"
32
+ )
33
 
34
  # -----------------------------
35
  # Language Detection (simple rule-based)
 
48
  # -----------------------------
49
  def normalize_label(label):
50
  label = label.lower()
51
+ if "pos" in label or "positive" in label:
52
  return "Positive"
53
+ elif "neg" in label or "negative" in label:
54
  return "Negative"
55
  else:
56
  return "Neutral"
 
70
  # Main Sentiment Function
71
  # -----------------------------
72
  def analyze_sentiment(text, lang_hint):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  try:
74
+ if not text.strip():
75
+ return "⚠️ Please enter a sentence.", "", "", SAVE_FILE
76
+
77
+ # Auto detect if language hint is not selected
78
+ lang = lang_hint if lang_hint != "Auto Detect" else detect_language(text)
79
+
80
+ # Select model
81
+ if lang == "English":
82
+ result = english_model(text)[0]
83
+ elif lang == "Urdu":
84
+ result = urdu_model(text)[0]
85
+ else:
86
+ result = roman_urdu_model(text)[0]
87
+
88
+ # Process results
89
+ sentiment = normalize_label(result["label"])
90
+ score = round(float(result["score"]), 3)
91
+ explanation = sentiment_with_tips(sentiment)
92
+
93
+ # Save to CSV (UTF-8 safe)
94
+ try:
95
+ df = pd.read_csv(SAVE_FILE, encoding="utf-8-sig")
96
+ except:
97
+ df = pd.DataFrame(columns=["Sentence", "Language", "Sentiment", "Confidence"])
98
+
99
+ new_row = pd.DataFrame([[text, lang, sentiment, score]],
100
+ columns=["Sentence", "Language", "Sentiment", "Confidence"])
101
+ df = pd.concat([df, new_row], ignore_index=True)
102
+ df.to_csv(SAVE_FILE, index=False, encoding="utf-8-sig")
103
+
104
+ return sentiment, str(score), explanation, SAVE_FILE
105
+
106
+ except Exception as e:
107
+ return f"⚠️ Error: {str(e)}", "", "", SAVE_FILE
108
 
109
  # -----------------------------
110
  # Gradio UI