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
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio App for Multilingual Sentiment Analysis
|
| 3 |
+
Deploy this to Hugging Face Spaces
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from sentiment_analyzer import MultilingualSentimentAnalyzer
|
| 8 |
+
|
| 9 |
+
def analyze_sentiment(text, language, method):
|
| 10 |
+
"""Analyze sentiment and return formatted results"""
|
| 11 |
+
if not text or not text.strip():
|
| 12 |
+
return "Please enter some text to analyze."
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
analyzer = MultilingualSentimentAnalyzer(language=language, method=method)
|
| 16 |
+
result = analyzer.analyze(text)
|
| 17 |
+
|
| 18 |
+
# Format the output nicely
|
| 19 |
+
output = f"""
|
| 20 |
+
## Sentiment Analysis Results
|
| 21 |
+
|
| 22 |
+
**Polarity:** {result['polarity'].upper()}
|
| 23 |
+
**Confidence:** {result['confidence']*100:.1f}%
|
| 24 |
+
|
| 25 |
+
**Scores:**
|
| 26 |
+
- Positive: {result['positive_score']:.2f}
|
| 27 |
+
- Negative: {result['negative_score']:.2f}
|
| 28 |
+
|
| 29 |
+
**Details:**
|
| 30 |
+
- Method: {result['method']}
|
| 31 |
+
- Language: {result['language']}
|
| 32 |
+
- Words analyzed: {result.get('word_count', 0)}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
return output
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
def batch_analyze(texts, language, method):
|
| 40 |
+
"""Analyze multiple texts"""
|
| 41 |
+
if not texts:
|
| 42 |
+
return "Please enter texts to analyze (one per line)."
|
| 43 |
+
|
| 44 |
+
text_list = [t.strip() for t in texts.split('\n') if t.strip()]
|
| 45 |
+
if not text_list:
|
| 46 |
+
return "No valid texts found."
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
analyzer = MultilingualSentimentAnalyzer(language=language, method=method)
|
| 50 |
+
results = analyzer.analyze_batch(text_list)
|
| 51 |
+
stats = analyzer.get_statistics(text_list)
|
| 52 |
+
|
| 53 |
+
output = f"""
|
| 54 |
+
## Batch Analysis Results
|
| 55 |
+
|
| 56 |
+
**Statistics:**
|
| 57 |
+
- Total texts: {stats['total_texts']}
|
| 58 |
+
- Average confidence: {stats['average_confidence']*100:.1f}%
|
| 59 |
+
|
| 60 |
+
**Polarity Distribution:**
|
| 61 |
+
"""
|
| 62 |
+
for polarity, percentage in stats['polarity_percentages'].items():
|
| 63 |
+
output += f"- {polarity.capitalize()}: {percentage}%\n"
|
| 64 |
+
|
| 65 |
+
output += "\n**Individual Results:**\n"
|
| 66 |
+
for i, (text, result) in enumerate(zip(text_list, results), 1):
|
| 67 |
+
output += f"\n{i}. \"{text[:50]}...\" → {result['polarity']} ({result['confidence']*100:.1f}%)\n"
|
| 68 |
+
|
| 69 |
+
return output
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error: {str(e)}"
|
| 72 |
+
|
| 73 |
+
# Create Gradio interface
|
| 74 |
+
with gr.Blocks(title="Multilingual Sentiment Analysis", theme=gr.themes.Soft()) as demo:
|
| 75 |
+
gr.Markdown("""
|
| 76 |
+
# 🌍 Multilingual Sentiment Analysis Tool
|
| 77 |
+
|
| 78 |
+
Analyze sentiment in **English**, **Turkish**, and **Persian** text using non-deep-learning approaches.
|
| 79 |
+
|
| 80 |
+
This tool uses lexicon-based, rule-based, and hybrid methods for interpretable sentiment analysis.
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
with gr.Tabs():
|
| 84 |
+
with gr.TabItem("Single Text Analysis"):
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column():
|
| 87 |
+
text_input = gr.Textbox(
|
| 88 |
+
label="Enter Text",
|
| 89 |
+
placeholder="Type your text here...",
|
| 90 |
+
lines=5
|
| 91 |
+
)
|
| 92 |
+
language = gr.Dropdown(
|
| 93 |
+
choices=["english", "turkish", "persian"],
|
| 94 |
+
value="english",
|
| 95 |
+
label="Language"
|
| 96 |
+
)
|
| 97 |
+
method = gr.Dropdown(
|
| 98 |
+
choices=["lexicon", "rule", "hybrid"],
|
| 99 |
+
value="hybrid",
|
| 100 |
+
label="Analysis Method"
|
| 101 |
+
)
|
| 102 |
+
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
output = gr.Markdown(label="Results")
|
| 106 |
+
|
| 107 |
+
analyze_btn.click(
|
| 108 |
+
fn=analyze_sentiment,
|
| 109 |
+
inputs=[text_input, language, method],
|
| 110 |
+
outputs=output
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
with gr.TabItem("Batch Analysis"):
|
| 114 |
+
with gr.Row():
|
| 115 |
+
with gr.Column():
|
| 116 |
+
batch_texts = gr.Textbox(
|
| 117 |
+
label="Enter Texts (one per line)",
|
| 118 |
+
placeholder="Enter multiple texts, one per line...",
|
| 119 |
+
lines=10
|
| 120 |
+
)
|
| 121 |
+
batch_language = gr.Dropdown(
|
| 122 |
+
choices=["english", "turkish", "persian"],
|
| 123 |
+
value="english",
|
| 124 |
+
label="Language"
|
| 125 |
+
)
|
| 126 |
+
batch_method = gr.Dropdown(
|
| 127 |
+
choices=["lexicon", "rule", "hybrid"],
|
| 128 |
+
value="hybrid",
|
| 129 |
+
label="Analysis Method"
|
| 130 |
+
)
|
| 131 |
+
batch_btn = gr.Button("Analyze Batch", variant="primary")
|
| 132 |
+
|
| 133 |
+
with gr.Column():
|
| 134 |
+
batch_output = gr.Markdown(label="Batch Results")
|
| 135 |
+
|
| 136 |
+
batch_btn.click(
|
| 137 |
+
fn=batch_analyze,
|
| 138 |
+
inputs=[batch_texts, batch_language, batch_method],
|
| 139 |
+
outputs=batch_output
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.TabItem("Examples"):
|
| 143 |
+
gr.Markdown("""
|
| 144 |
+
### Example Texts to Try:
|
| 145 |
+
|
| 146 |
+
**English:**
|
| 147 |
+
- "I love this product! It's absolutely amazing!!! 😊"
|
| 148 |
+
- "This is terrible. I hate it."
|
| 149 |
+
- "Not bad, actually it's quite good!"
|
| 150 |
+
|
| 151 |
+
**Turkish:**
|
| 152 |
+
- "Bu ürünü çok seviyorum! Harika!"
|
| 153 |
+
- "Berbat bir deneyim. Hiç beğenmedim."
|
| 154 |
+
|
| 155 |
+
**Persian:**
|
| 156 |
+
- "این محصول عالی است!"
|
| 157 |
+
- "خیلی بد بود"
|
| 158 |
+
""")
|
| 159 |
+
|
| 160 |
+
gr.Markdown("""
|
| 161 |
+
---
|
| 162 |
+
**About:** This tool uses lexicon-based, rule-based, and hybrid approaches (without deep learning)
|
| 163 |
+
for interpretable sentiment analysis. Supports English, Turkish, and Persian languages.
|
| 164 |
+
""")
|
| 165 |
+
|
| 166 |
+
if __name__ == "__main__":
|
| 167 |
+
demo.launch()
|