Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"sentiment-analysis",
|
| 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"
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
roman_urdu_model = pipeline(
|
| 22 |
-
"sentiment-analysis",
|
| 23 |
-
model="tahamueed23/fine_tuned_cardiffnlp_urdu_and_roman-urdu"
|
| 24 |
-
)
|
| 25 |
|
|
|
|
| 26 |
SAVE_FILE = "sentiment_logs.csv"
|
| 27 |
if not os.path.exists(SAVE_FILE):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
|
|
|
| 31 |
def detect_language(text):
|
| 32 |
urdu_chars = set("ابتثجحخدذرزسشصضطظعغفقکلمنوہیءآؤئۀ")
|
| 33 |
if any(ch in urdu_chars for ch in text):
|
|
@@ -48,20 +36,20 @@ def normalize_label(label):
|
|
| 48 |
|
| 49 |
def sentiment_with_tips(sentiment):
|
| 50 |
tips = {
|
| 51 |
-
"Positive": "😊 Great! Keep
|
| 52 |
-
"Negative": "😞
|
| 53 |
-
"Neutral": "😐 Neutral
|
| 54 |
}
|
| 55 |
return tips.get(sentiment, "")
|
| 56 |
|
|
|
|
| 57 |
def analyze_sentiment(text, lang_hint):
|
| 58 |
-
if not text.strip():
|
| 59 |
return "⚠️ Please enter a sentence.", "", "", SAVE_FILE
|
| 60 |
|
| 61 |
-
# Auto detect if language hint is not clear
|
| 62 |
lang = lang_hint if lang_hint != "Auto Detect" else detect_language(text)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
if lang == "English":
|
| 66 |
result = english_model(text)[0]
|
| 67 |
elif lang == "Urdu":
|
|
@@ -69,45 +57,15 @@ def analyze_sentiment(text, lang_hint):
|
|
| 69 |
else:
|
| 70 |
result = roman_urdu_model(text)[0]
|
| 71 |
|
| 72 |
-
# Process results
|
| 73 |
sentiment = normalize_label(result["label"])
|
| 74 |
score = round(result["score"], 3)
|
| 75 |
explanation = sentiment_with_tips(sentiment)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
new_row = pd.DataFrame([[text, lang, sentiment, score]],
|
| 80 |
columns=["Sentence", "Language", "Sentiment", "Confidence"])
|
| 81 |
-
df = pd.concat([df, new_row], ignore_index=True)
|
| 82 |
-
df.to_csv(SAVE_FILE, index=False)
|
| 83 |
-
|
| 84 |
-
return f"{sentiment}", f"{score}", f"{explanation}", SAVE_FILE
|
| 85 |
-
|
| 86 |
-
with gr.Blocks() as demo:
|
| 87 |
-
gr.Markdown(
|
| 88 |
-
"## 🌍 Multilingual Sentiment Analysis (English • Urdu • Roman Urdu)\n"
|
| 89 |
-
"Detect sentiment as **Positive, Neutral, or Negative** with confidence score.\n\n"
|
| 90 |
-
"📌 Features:\n"
|
| 91 |
-
"- Choose language (or Auto Detect)\n"
|
| 92 |
-
"- Download all results as CSV\n"
|
| 93 |
-
"- Emojis + Tips for better understanding 🎯"
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
with gr.Row():
|
| 97 |
-
with gr.Column():
|
| 98 |
-
user_text = gr.Textbox(label="✍️ Enter text", placeholder="Type in English, Urdu, or Roman Urdu...")
|
| 99 |
-
lang_dropdown = gr.Dropdown(["Auto Detect", "English", "Urdu", "Roman Urdu"],
|
| 100 |
-
label="🌐 Language", value="Auto Detect")
|
| 101 |
-
btn = gr.Button("🔍 Analyze")
|
| 102 |
-
|
| 103 |
-
with gr.Column():
|
| 104 |
-
out_sent = gr.Textbox(label="Sentiment")
|
| 105 |
-
out_conf = gr.Textbox(label="Confidence (0–1)")
|
| 106 |
-
out_exp = gr.Textbox(label="Explanation")
|
| 107 |
-
out_file = gr.File(label="⬇️ Download Logs (.csv)", type="filepath")
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# --- models (keep yours) ---
|
| 8 |
+
english_model = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english")
|
| 9 |
+
urdu_model = pipeline("sentiment-analysis", model="tahamueed23/fine_tuned_cardiffnlp_urdu_and_roman-urdu")
|
| 10 |
+
roman_urdu_model = pipeline("sentiment-analysis", model="tahamueed23/fine_tuned_cardiffnlp_urdu_and_roman-urdu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# --- CSV setup (UTF-8 with BOM) ---
|
| 13 |
SAVE_FILE = "sentiment_logs.csv"
|
| 14 |
if not os.path.exists(SAVE_FILE):
|
| 15 |
+
df_init = pd.DataFrame(columns=["Sentence", "Language", "Sentiment", "Confidence"])
|
| 16 |
+
df_init.to_csv(SAVE_FILE, index=False, encoding="utf-8-sig") # write BOM + UTF-8
|
| 17 |
|
| 18 |
+
# --- helper functions (keep yours) ---
|
| 19 |
def detect_language(text):
|
| 20 |
urdu_chars = set("ابتثجحخدذرزسشصضطظعغفقکلمنوہیءآؤئۀ")
|
| 21 |
if any(ch in urdu_chars for ch in text):
|
|
|
|
| 36 |
|
| 37 |
def sentiment_with_tips(sentiment):
|
| 38 |
tips = {
|
| 39 |
+
"Positive": "😊 Great! Keep it up.",
|
| 40 |
+
"Negative": "😞 Looks negative. Consider constructive changes.",
|
| 41 |
+
"Neutral": "😐 Neutral — neither strongly positive nor negative."
|
| 42 |
}
|
| 43 |
return tips.get(sentiment, "")
|
| 44 |
|
| 45 |
+
# --- main analyze function (writes UTF-8 rows safely) ---
|
| 46 |
def analyze_sentiment(text, lang_hint):
|
| 47 |
+
if not text or not text.strip():
|
| 48 |
return "⚠️ Please enter a sentence.", "", "", SAVE_FILE
|
| 49 |
|
|
|
|
| 50 |
lang = lang_hint if lang_hint != "Auto Detect" else detect_language(text)
|
| 51 |
|
| 52 |
+
# Choose model
|
| 53 |
if lang == "English":
|
| 54 |
result = english_model(text)[0]
|
| 55 |
elif lang == "Urdu":
|
|
|
|
| 57 |
else:
|
| 58 |
result = roman_urdu_model(text)[0]
|
| 59 |
|
|
|
|
| 60 |
sentiment = normalize_label(result["label"])
|
| 61 |
score = round(result["score"], 3)
|
| 62 |
explanation = sentiment_with_tips(sentiment)
|
| 63 |
|
| 64 |
+
# Prepare row and append using utf-8-sig (Excel-friendly)
|
| 65 |
+
new_row = pd.DataFrame([[text, lang, sentiment, score]],
|
|
|
|
| 66 |
columns=["Sentence", "Language", "Sentiment", "Confidence"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# Append: header only if file missing (safe even if file exists)
|
| 69 |
+
new_row.to_csv(SAVE_FILE, mode="a", index=False, header=not os.path.exists(SAVE_FILE), encoding="utf-8-sig")
|
| 70 |
|
| 71 |
+
return sentiment, str(score), explanation, SAVE_FILE
|
|
|