mrgmd01 commited on
Commit
5e58202
·
verified ·
1 Parent(s): a68e8c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
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
21
  )
22
 
23
  # File to store only sentences
@@ -33,7 +33,6 @@ def detect_language(text):
33
  urdu_chars = set("ابتثجحخدذرزسشصضطظعغفقکلمنوہیءآؤئۀ")
34
  if any(ch in urdu_chars for ch in text):
35
  return "Urdu"
36
- # Heuristic: Roman Urdu often has "hai, kia, kaisa, bohot, acha" etc.
37
  roman_urdu_pattern = r"\b(hai|kia|kyun|nahi|bohot|acha|galat|sahi|parhai|ustad)\b"
38
  if re.search(roman_urdu_pattern, text.lower()):
39
  return "Roman Urdu"
@@ -49,48 +48,56 @@ def normalize_label(label):
49
  else:
50
  return "Neutral"
51
 
52
- # Prediction + Save sentence
53
- def predict_and_save(text):
54
  if not text.strip():
55
- return "Please enter a sentence.", SAVE_FILE
56
 
57
- lang = detect_language(text)
 
 
 
 
58
 
59
  if lang == "English":
60
  result = english_model(text)[0]
61
  elif lang == "Urdu":
62
  result = urdu_model(text)[0]
63
- else: # Roman Urdu
64
  result = roman_urdu_model(text)[0]
65
 
66
  sentiment = normalize_label(result["label"])
67
  score = round(result["score"], 3)
 
68
 
69
- # Save only the sentence
70
  df = pd.read_csv(SAVE_FILE)
71
  new_row = pd.DataFrame([[text]], columns=["Sentence"])
72
  df = pd.concat([df, new_row], ignore_index=True)
73
  df.to_csv(SAVE_FILE, index=False)
74
 
75
- return f"Sentiment: {sentiment} (Confidence: {score}, Language: {lang})", SAVE_FILE
 
76
 
77
  # Gradio UI
78
  with gr.Blocks() as demo:
79
- "## 🌍 Multilingual Sentiment Analysis (Positive • Neutral • Negative)\n"
80
- "**Languages:** English, Urdu, Roman Urdu \n"
81
- "Model: `siebert/sentiment-roberta-large-english (For English)`\n `mrgmd01/sentiment_model_FineTune_cardiffnlp(For Urdu & Roman Urdu)`"
 
82
 
83
- with gr.Tab():
84
  user_text = gr.Textbox(label="Enter text", placeholder="Type in English, Urdu, or Roman Urdu...")
85
- lang_dropdown = gr.Dropdown(["English", "Urdu", "Roman Urdu"], label="Language Hint", value="English")
86
  btn = gr.Button("Analyze")
87
 
88
  out_sent = gr.Textbox(label="Sentiment")
89
  out_conf = gr.Textbox(label="Confidence (0–1)")
90
  out_pol = gr.Textbox(label="Polarity")
91
- out_file = gr.File(label="Download logs (.xlsx)")
92
 
93
  btn.click(analyze_single, inputs=[user_text, lang_dropdown],
94
  outputs=[out_sent, out_conf, out_pol, out_file])
 
95
  if __name__ == "__main__":
96
- demo.launch()
 
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
 
33
  urdu_chars = set("ابتثجحخدذرزسشصضطظعغفقکلمنوہیءآؤئۀ")
34
  if any(ch in urdu_chars for ch in text):
35
  return "Urdu"
 
36
  roman_urdu_pattern = r"\b(hai|kia|kyun|nahi|bohot|acha|galat|sahi|parhai|ustad)\b"
37
  if re.search(roman_urdu_pattern, text.lower()):
38
  return "Roman Urdu"
 
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]
64
  elif lang == "Urdu":
65
  result = urdu_model(text)[0]
66
+ else:
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])
101
+
102
  if __name__ == "__main__":
103
+ demo.launch()