mrgmd01 commited on
Commit
0f459d4
Β·
verified Β·
1 Parent(s): 5e58202

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -17,7 +17,7 @@ urdu_model = pipeline(
17
 
18
  roman_urdu_model = pipeline(
19
  "sentiment-analysis",
20
- model="mrgmd01/sentiment_model_FineTune_cardiffnlp" # Replace with your Roman Urdu model if available
21
  )
22
 
23
  # File to store only sentences
@@ -48,16 +48,22 @@ def normalize_label(label):
48
  else:
49
  return "Neutral"
50
 
51
- # Prediction function
 
 
 
 
 
 
 
 
 
52
  def analyze_single(text, lang_hint):
53
  if not text.strip():
54
  return "Please enter a sentence.", "", "", SAVE_FILE
55
 
56
- # If user gives hint, use it; else auto-detect
57
- if lang_hint and lang_hint != "Auto":
58
- lang = lang_hint
59
- else:
60
- lang = detect_language(text)
61
 
62
  if lang == "English":
63
  result = english_model(text)[0]
@@ -67,34 +73,36 @@ def analyze_single(text, lang_hint):
67
  result = roman_urdu_model(text)[0]
68
 
69
  sentiment = normalize_label(result["label"])
 
70
  score = round(result["score"], 3)
71
- polarity = "Positive" if sentiment == "Positive" else ("Negative" if sentiment == "Negative" else "Neutral")
72
 
73
- # Save only sentence
74
  df = pd.read_csv(SAVE_FILE)
75
  new_row = pd.DataFrame([[text]], columns=["Sentence"])
76
  df = pd.concat([df, new_row], ignore_index=True)
77
  df.to_csv(SAVE_FILE, index=False)
78
 
79
- return sentiment, str(score), polarity, SAVE_FILE
80
-
81
 
82
  # Gradio UI
83
  with gr.Blocks() as demo:
84
- gr.Markdown("## 🌍 Multilingual Sentiment Analysis (Positive β€’ Neutral β€’ Negative)")
85
- gr.Markdown("**Languages:** English, Urdu, Roman Urdu \n"
86
- "Model: `cardiffnlp/twitter-roberta-base-sentiment-latest (English)` \n"
87
- "`mrgmd01/sentiment_model_FineTune_cardiffnlp (Urdu & Roman Urdu)`")
88
-
89
- with gr.Tab("Sentiment Analysis"):
 
 
 
90
  user_text = gr.Textbox(label="Enter text", placeholder="Type in English, Urdu, or Roman Urdu...")
91
- lang_dropdown = gr.Dropdown(["Auto", "English", "Urdu", "Roman Urdu"], label="Language Hint", value="Auto")
92
- btn = gr.Button("Analyze")
93
 
94
  out_sent = gr.Textbox(label="Sentiment")
95
  out_conf = gr.Textbox(label="Confidence (0–1)")
96
- out_pol = gr.Textbox(label="Polarity")
97
- out_file = gr.File(label="Download logs (.csv)", type="filepath")
98
 
99
  btn.click(analyze_single, inputs=[user_text, lang_dropdown],
100
  outputs=[out_sent, out_conf, out_pol, out_file])
 
17
 
18
  roman_urdu_model = pipeline(
19
  "sentiment-analysis",
20
+ model="mrgmd01/sentiment_model_FineTune_cardiffnlp" # replace with roman urdu model if available
21
  )
22
 
23
  # File to store only sentences
 
48
  else:
49
  return "Neutral"
50
 
51
+ # Add emojis for polarity
52
+ def add_emoji(label):
53
+ mapping = {
54
+ "Positive": "πŸ˜ŠπŸ‘",
55
+ "Negative": "πŸ˜žπŸ‘Ž",
56
+ "Neutral": "😐"
57
+ }
58
+ return f"{label} {mapping.get(label, '')}"
59
+
60
+ # Prediction + Save sentence
61
  def analyze_single(text, lang_hint):
62
  if not text.strip():
63
  return "Please enter a sentence.", "", "", SAVE_FILE
64
 
65
+ # Auto detect if user keeps default "English"
66
+ lang = detect_language(text) if lang_hint == "English" else lang_hint
 
 
 
67
 
68
  if lang == "English":
69
  result = english_model(text)[0]
 
73
  result = roman_urdu_model(text)[0]
74
 
75
  sentiment = normalize_label(result["label"])
76
+ sentiment_with_emoji = add_emoji(sentiment)
77
  score = round(result["score"], 3)
 
78
 
79
+ # Save only the sentence
80
  df = pd.read_csv(SAVE_FILE)
81
  new_row = pd.DataFrame([[text]], columns=["Sentence"])
82
  df = pd.concat([df, new_row], ignore_index=True)
83
  df.to_csv(SAVE_FILE, index=False)
84
 
85
+ return sentiment, str(score), sentiment_with_emoji, SAVE_FILE
 
86
 
87
  # Gradio UI
88
  with gr.Blocks() as demo:
89
+ gr.Markdown(
90
+ "## 🌍 Multilingual Sentiment Analysis (Positive β€’ Neutral β€’ Negative)\n"
91
+ "**Languages:** English, Urdu, Roman Urdu \n"
92
+ "πŸ“Œ Models: \n"
93
+ "- `cardiffnlp/twitter-roberta-base-sentiment-latest (English)` \n"
94
+ "- `mrgmd01/sentiment_model_FineTune_cardiffnlp (Urdu & Roman Urdu)`"
95
+ )
96
+
97
+ with gr.Tab("Sentiment Analyzer"):
98
  user_text = gr.Textbox(label="Enter text", placeholder="Type in English, Urdu, or Roman Urdu...")
99
+ lang_dropdown = gr.Dropdown(["English", "Urdu", "Roman Urdu"], label="Language Hint", value="English")
100
+ btn = gr.Button("πŸ” Analyze")
101
 
102
  out_sent = gr.Textbox(label="Sentiment")
103
  out_conf = gr.Textbox(label="Confidence (0–1)")
104
+ out_pol = gr.Textbox(label="Polarity + Emoji")
105
+ out_file = gr.File(label="⬇️ Download Sentences (.csv)", type="filepath")
106
 
107
  btn.click(analyze_single, inputs=[user_text, lang_dropdown],
108
  outputs=[out_sent, out_conf, out_pol, out_file])