zainali9091 commited on
Commit
875374f
Β·
verified Β·
1 Parent(s): 5c363a1

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +79 -31
src/streamlit_app.py CHANGED
@@ -1,31 +1,79 @@
1
- # Page layout
2
- st.set_page_config(page_title="DilBot", layout="centered")
3
-
4
- # Centered and styled title
5
- st.markdown("<h1 style='text-align: center; color: white;'>πŸ€– DilBot – Your Emotional AI Companion</h1>", unsafe_allow_html=True)
6
- st.markdown("<h3 style='text-align: center; color: white;'>πŸ“ What's on your mind today?</h3>", unsafe_allow_html=True)
7
-
8
- # Rest of your app logic (Text Input etc...)
9
-
10
- # AFTER prediction and confidence calculation
11
- if emotion:
12
- st.markdown("---")
13
- st.markdown("### 🎯 PERSONAL DASHBOARD")
14
- st.markdown(f"**Detected Emotion:** `{emotion.capitalize()}`")
15
- st.markdown(f"**Confidence Score:** `{confidence * 100:.1f}%`")
16
-
17
- # Proper Graph like reference image
18
- st.markdown("#### Emotion Confidence Graph")
19
- fig, ax = plt.subplots()
20
- ax.bar(["Confidence"], [confidence], color="#1f77b4", width=0.4)
21
- ax.set_ylim(0, 1)
22
- ax.set_ylabel("Confidence (0-1)")
23
- ax.set_title("Emotion Detection Confidence")
24
- for i, v in enumerate([confidence]):
25
- ax.text(i, v + 0.02, f"{v*100:.1f}%", ha='center', fontweight='bold')
26
- st.pyplot(fig)
27
-
28
- # QUOTES section
29
- quotes = quotes_data.get(emotion, ["Stay strong, you're doing great!"])
30
- selected_quote = random.choice(quotes)
31
- st.markdown(f"> πŸ’¬ *{selected_quote}*")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")