Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
if st.button("Analyze"):
|
14 |
-
if user_input.strip():
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
else:
|
32 |
st.warning("Please enter some text to analyze.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
st.set_page_config(page_title="Sentiment & Emotion Analyzer", layout="centered")
|
5 |
+
st.title("π§ Text Analyzer (Streamlit)")
|
6 |
+
|
7 |
+
# Load models
|
8 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
9 |
+
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
10 |
+
toxicity_pipeline = pipeline("text-classification", model="martin-ha/toxic-comment-model")
|
11 |
+
|
12 |
+
# Refinement function
|
13 |
+
def refine_predictions(sentiment, sentiment_conf, emotions, toxicity):
|
14 |
+
if emotions.get("fear", 0) > 0.8 or emotions.get("anger", 0) > 0.8 or emotions.get("sadness", 0) > 0.8:
|
15 |
+
sentiment = "negative"
|
16 |
+
if emotions.get("joy", 0) > 0.7:
|
17 |
+
sentiment = "positive"
|
18 |
+
max_emotion = max(emotions, key=emotions.get)
|
19 |
+
if sentiment == "neutral" and emotions[max_emotion] > 0.7:
|
20 |
+
if max_emotion == "joy":
|
21 |
+
sentiment = "positive"
|
22 |
+
else:
|
23 |
+
sentiment = "negative"
|
24 |
+
return sentiment
|
25 |
|
26 |
+
# UI
|
27 |
+
user_input = st.text_area("Enter text here:")
|
28 |
|
29 |
if st.button("Analyze"):
|
30 |
+
if user_input.strip() != "":
|
31 |
+
# Sentiment
|
32 |
+
sent_res = sentiment_pipeline(user_input)[0]
|
33 |
+
sentiment = sent_res["label"].lower()
|
34 |
+
sentiment_conf = sent_res["score"]
|
35 |
+
|
36 |
+
# Emotions
|
37 |
+
emo_res = emotion_pipeline(user_input)[0]
|
38 |
+
emotions = {e["label"].lower(): e["score"] for e in emo_res}
|
39 |
+
|
40 |
+
# Toxicity
|
41 |
+
tox_res = toxicity_pipeline(user_input)[0]
|
42 |
+
toxicity = {t["label"].lower(): t["score"] for t in tox_res}
|
43 |
+
|
44 |
+
# Refine sentiment based on emotion
|
45 |
+
refined_sentiment = refine_predictions(sentiment, sentiment_conf, emotions, toxicity)
|
46 |
+
|
47 |
+
# Display results
|
48 |
+
st.subheader("π Results")
|
49 |
+
st.write(f"**Sentiment:** {refined_sentiment} (confidence: {sentiment_conf:.2f})")
|
50 |
+
st.write("### Emotions")
|
51 |
+
for emo, score in emotions.items():
|
52 |
+
st.write(f"- {emo}: {score:.2f}")
|
53 |
+
st.write("### Toxicity")
|
54 |
+
for tox, score in toxicity.items():
|
55 |
+
st.write(f"- {tox}: {score:.2f}")
|
56 |
else:
|
57 |
st.warning("Please enter some text to analyze.")
|