Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ def analyze_multilingual_sentences(text):
|
|
15 |
# Splits de input op nieuwe regels (elke regel is een aparte zin)
|
16 |
sentences = [s.strip() for s in text.split("\n") if s.strip()]
|
17 |
|
18 |
-
# Detecteer de taal van de eerste zin als referentie
|
19 |
detected_lang = langdetect.detect(sentences[0]) if sentences else "en"
|
20 |
|
21 |
# Vertaal alleen als de tekst NIET in het Engels is
|
@@ -32,27 +32,27 @@ def analyze_multilingual_sentences(text):
|
|
32 |
negative_count = sum(1 for r in results if r['label'] == "LABEL_0") # NEGATIVE
|
33 |
neutral_count = len(results) - (positive_count + negative_count) # NEUTRAL
|
34 |
|
35 |
-
# Maak een overzichtelijke output
|
36 |
-
output = "
|
37 |
for original, translated, result in zip(sentences, translated_sentences, results):
|
38 |
sentiment_label = result['label']
|
39 |
score = result['score']
|
40 |
|
41 |
-
#
|
42 |
if sentiment_label == "LABEL_2":
|
43 |
sentiment = "POSITIVE"
|
44 |
-
|
45 |
elif sentiment_label == "LABEL_0":
|
46 |
sentiment = "NEGATIVE"
|
47 |
-
|
48 |
else:
|
49 |
sentiment = "NEUTRAL"
|
50 |
-
|
51 |
|
52 |
-
output += f"
|
53 |
if detected_lang != "en": # Alleen vertaling tonen als invoer niet in het Engels is
|
54 |
-
output += f"
|
55 |
-
output += f"
|
56 |
|
57 |
# Maak een grafiek met de sentimentverdeling
|
58 |
labels = ["Positive", "Neutral", "Negative"]
|
@@ -63,44 +63,57 @@ def analyze_multilingual_sentences(text):
|
|
63 |
ax.pie(values, labels=labels, autopct='%1.1f%%', startangle=90, colors=colors)
|
64 |
ax.axis("equal") # Gelijke assen voor een ronde taartdiagram
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
examples_top = [
|
70 |
-
["I just aced my exam. My cat knocked over my coffee. Pineapple on pizza is the best debate of our generation."],
|
71 |
-
["Woke up feeling like the main character. Spilled my cereal and questioned my life choices. This app better not judge me."],
|
72 |
-
["Mondays should be illegal. I found a sock thatβs been missing for six months. The WiFi went out, total meltdown mode."]
|
73 |
-
]
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
# Gradio-interface met
|
82 |
demo = gr.Interface(
|
83 |
fn=analyze_multilingual_sentences,
|
84 |
inputs=gr.Textbox(
|
85 |
lines=5,
|
86 |
placeholder="Hey there! Drop some sentences (one per line) and get instant sentiment vibesβpositive, neutral, or negative...",
|
87 |
),
|
88 |
-
outputs=["
|
89 |
title="π Multilingual Sentiment Analysis with Graph Feedback",
|
90 |
-
description="""
|
91 |
<div style='font-size: 18px; font-weight: bold; text-align: center; color: #333;'>
|
92 |
-
Enter sentences in any language
|
|
|
93 |
</div>
|
|
|
94 |
""",
|
95 |
-
examples=examples_top, # Eerste drie voorbeelden boven
|
96 |
theme="default",
|
97 |
allow_flagging="never"
|
98 |
)
|
99 |
|
100 |
-
# Extra voorbeeldsectie onder het invoerveld
|
101 |
-
extra_examples = gr.Examples(examples_bottom, inputs=demo.input_components)
|
102 |
-
|
103 |
# Start de app
|
104 |
demo.launch()
|
105 |
-
extra_examples.launch()
|
106 |
|
|
|
15 |
# Splits de input op nieuwe regels (elke regel is een aparte zin)
|
16 |
sentences = [s.strip() for s in text.split("\n") if s.strip()]
|
17 |
|
18 |
+
# Detecteer de taal van de eerste zin als referentie
|
19 |
detected_lang = langdetect.detect(sentences[0]) if sentences else "en"
|
20 |
|
21 |
# Vertaal alleen als de tekst NIET in het Engels is
|
|
|
32 |
negative_count = sum(1 for r in results if r['label'] == "LABEL_0") # NEGATIVE
|
33 |
neutral_count = len(results) - (positive_count + negative_count) # NEUTRAL
|
34 |
|
35 |
+
# Maak een overzichtelijke output met kleur per sentiment
|
36 |
+
output = "<h3>π Sentiment Analysis Results:</h3><br>"
|
37 |
for original, translated, result in zip(sentences, translated_sentences, results):
|
38 |
sentiment_label = result['label']
|
39 |
score = result['score']
|
40 |
|
41 |
+
# Kleurcodes per sentiment
|
42 |
if sentiment_label == "LABEL_2":
|
43 |
sentiment = "POSITIVE"
|
44 |
+
color = "green"
|
45 |
elif sentiment_label == "LABEL_0":
|
46 |
sentiment = "NEGATIVE"
|
47 |
+
color = "red"
|
48 |
else:
|
49 |
sentiment = "NEUTRAL"
|
50 |
+
color = "blue"
|
51 |
|
52 |
+
output += f"<p><b>π '{original}'</b></p>"
|
53 |
if detected_lang != "en": # Alleen vertaling tonen als invoer niet in het Engels is
|
54 |
+
output += f"<p>π <i>Translation:</i> {translated}</p>"
|
55 |
+
output += f"<p style='color: {color}; font-weight: bold;'>π Sentiment: {sentiment} ({score:.2f})</p><hr>"
|
56 |
|
57 |
# Maak een grafiek met de sentimentverdeling
|
58 |
labels = ["Positive", "Neutral", "Negative"]
|
|
|
63 |
ax.pie(values, labels=labels, autopct='%1.1f%%', startangle=90, colors=colors)
|
64 |
ax.axis("equal") # Gelijke assen voor een ronde taartdiagram
|
65 |
|
66 |
+
# Extra voorbeeldzinnen direct zichtbaar na de output
|
67 |
+
output += """
|
68 |
+
<h3>π‘ Try More Sentences:</h3>
|
69 |
+
<ul>
|
70 |
+
<li>I woke up at 5 AM and went for a run.</li>
|
71 |
+
<li>This is the worst movie I have ever seen.</li>
|
72 |
+
<li>Just got a puppy, and I'm in love!</li>
|
73 |
+
<li>The weather is so boring today.</li>
|
74 |
+
<li>I forgot my homework and now I'm panicking.</li>
|
75 |
+
<li>This song makes me feel amazing.</li>
|
76 |
+
<li>My internet just crashed, life is over.</li>
|
77 |
+
</ul>
|
78 |
+
"""
|
79 |
|
80 |
+
return output, fig
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# Lijst met voorbeelden als HTML-lijst voor duidelijke weergave
|
83 |
+
example_sentences = """
|
84 |
+
<div style='font-size: 16px; font-weight: bold; text-align: left; color: #333; margin-top: 10px;'>
|
85 |
+
<p>πΉ Enter sentences line by line:</p>
|
86 |
+
<ul>
|
87 |
+
<li>I just aced my exam.</li>
|
88 |
+
<li>My cat knocked over my coffee.</li>
|
89 |
+
<li>Pineapple on pizza is the best debate of our generation.</li>
|
90 |
+
<li>Woke up feeling like the main character.</li>
|
91 |
+
<li>Spilled my cereal and questioned my life choices.</li>
|
92 |
+
<li>This app better not judge me.</li>
|
93 |
+
</ul>
|
94 |
+
</div>
|
95 |
+
"""
|
96 |
|
97 |
+
# Gradio-interface met duidelijke instructies en styling
|
98 |
demo = gr.Interface(
|
99 |
fn=analyze_multilingual_sentences,
|
100 |
inputs=gr.Textbox(
|
101 |
lines=5,
|
102 |
placeholder="Hey there! Drop some sentences (one per line) and get instant sentiment vibesβpositive, neutral, or negative...",
|
103 |
),
|
104 |
+
outputs=["html", "plot"], # HTML output zodat kleuren en opmaak goed werken
|
105 |
title="π Multilingual Sentiment Analysis with Graph Feedback",
|
106 |
+
description=f"""
|
107 |
<div style='font-size: 18px; font-weight: bold; text-align: center; color: #333;'>
|
108 |
+
Enter sentences in any language, <b>one per line</b>.
|
109 |
+
This AI-powered app translates, analyzes the vibe, and shows the results in a cool summary & chart.
|
110 |
</div>
|
111 |
+
{example_sentences}
|
112 |
""",
|
|
|
113 |
theme="default",
|
114 |
allow_flagging="never"
|
115 |
)
|
116 |
|
|
|
|
|
|
|
117 |
# Start de app
|
118 |
demo.launch()
|
|
|
119 |
|