Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +79 -31
src/streamlit_app.py
CHANGED
|
@@ -1,31 +1,79 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from textblob import TextBlob
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# === Load Quotes ===
|
| 8 |
+
QUOTES_PATH = os.path.join(os.path.dirname(__file__), "quotes.json")
|
| 9 |
+
with open(QUOTES_PATH, "r") as f:
|
| 10 |
+
quotes_data = json.load(f)
|
| 11 |
+
|
| 12 |
+
# === Emotion Detection via TextBlob ===
|
| 13 |
+
def detect_emotion(text):
|
| 14 |
+
blob = TextBlob(text)
|
| 15 |
+
polarity = blob.sentiment.polarity
|
| 16 |
+
|
| 17 |
+
if polarity > 0.2:
|
| 18 |
+
return "positive", polarity
|
| 19 |
+
elif polarity < -0.2:
|
| 20 |
+
return "negative", polarity
|
| 21 |
+
else:
|
| 22 |
+
return "neutral", polarity
|
| 23 |
+
|
| 24 |
+
# === Quotes Retrieval ===
|
| 25 |
+
def get_quote(emotion):
|
| 26 |
+
if isinstance(quotes_data, dict):
|
| 27 |
+
return quotes_data.get(emotion, ["Stay strong, you're doing great!"])
|
| 28 |
+
else:
|
| 29 |
+
return ["Stay strong, you're doing great!"]
|
| 30 |
+
|
| 31 |
+
# === Page Layout ===
|
| 32 |
+
st.set_page_config(page_title="DilBot - Emotionally Intelligent AI Companion", layout="centered")
|
| 33 |
+
st.title("π DilBot: Your Emotional Companion")
|
| 34 |
+
st.markdown("---")
|
| 35 |
+
|
| 36 |
+
# === Input Section ===
|
| 37 |
+
user_input = st.text_area("π£οΈ How are you feeling today?", height=150, placeholder="Type your thoughts here...")
|
| 38 |
+
|
| 39 |
+
if st.button("Analyze"):
|
| 40 |
+
if user_input.strip():
|
| 41 |
+
# Detect Emotion
|
| 42 |
+
emotion, polarity = detect_emotion(user_input)
|
| 43 |
+
confidence = abs(polarity) * 100
|
| 44 |
+
|
| 45 |
+
# Display Emotion
|
| 46 |
+
st.subheader("π§ Detected Emotion:")
|
| 47 |
+
st.success(f"**{emotion.capitalize()}** with a confidence of **{confidence:.2f}%**")
|
| 48 |
+
|
| 49 |
+
# Show a personal quote
|
| 50 |
+
st.markdown("### π¬ DilBot's Message:")
|
| 51 |
+
quote = get_quote(emotion)[0]
|
| 52 |
+
st.info(f"_{quote}_")
|
| 53 |
+
|
| 54 |
+
# === Personal Dashboard ===
|
| 55 |
+
st.markdown("### π Personal Dashboard")
|
| 56 |
+
|
| 57 |
+
# Bar & Doughnut Chart
|
| 58 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4), dpi=100)
|
| 59 |
+
|
| 60 |
+
labels = ['Positive', 'Neutral', 'Negative']
|
| 61 |
+
values = [confidence if emotion == lbl.lower() else 100 - confidence if lbl.lower() == 'neutral' else 0 for lbl in labels]
|
| 62 |
+
colors = ['#00cc99', '#b0bec5', '#ff6f61']
|
| 63 |
+
|
| 64 |
+
# Bar Chart
|
| 65 |
+
ax1.bar(labels, values, color=colors)
|
| 66 |
+
ax1.set_title("Confidence by Emotion")
|
| 67 |
+
ax1.set_ylabel("Percentage")
|
| 68 |
+
ax1.set_ylim(0, 100)
|
| 69 |
+
|
| 70 |
+
# Doughnut Chart
|
| 71 |
+
wedges, texts, autotexts = ax2.pie(
|
| 72 |
+
values, labels=labels, autopct='%1.1f%%',
|
| 73 |
+
colors=colors, startangle=90, wedgeprops=dict(width=0.4)
|
| 74 |
+
)
|
| 75 |
+
ax2.set_title("Emotion Distribution")
|
| 76 |
+
|
| 77 |
+
st.pyplot(fig)
|
| 78 |
+
else:
|
| 79 |
+
st.warning("Please write something to analyze.")
|