Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +28 -114
src/streamlit_app.py
CHANGED
|
@@ -1,117 +1,31 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
st.markdown(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
.main-title {{
|
| 27 |
-
text-align: center;
|
| 28 |
-
font-size: 3em;
|
| 29 |
-
font-weight: bold;
|
| 30 |
-
color: white;
|
| 31 |
-
}}
|
| 32 |
-
.sub-title {{
|
| 33 |
-
font-size: 1.3em;
|
| 34 |
-
color: white;
|
| 35 |
-
}}
|
| 36 |
-
.quote-box {{
|
| 37 |
-
padding: 15px;
|
| 38 |
-
border-radius: 10px;
|
| 39 |
-
font-weight: bold;
|
| 40 |
-
font-size: 1.2em;
|
| 41 |
-
margin-top: 15px;
|
| 42 |
-
}}
|
| 43 |
-
</style>
|
| 44 |
-
""",
|
| 45 |
-
unsafe_allow_html=True
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# Title
|
| 49 |
-
st.markdown('<h1 class="main-title">π€ DilBot β Your Emotional AI Companion</h1>', unsafe_allow_html=True)
|
| 50 |
-
st.markdown('<div class="sub-title">π What\'s on your mind today?</div>', unsafe_allow_html=True)
|
| 51 |
-
|
| 52 |
-
# User input
|
| 53 |
-
user_input = st.text_area("", height=150, placeholder="Type here...")
|
| 54 |
-
|
| 55 |
-
# Load quotes based on emotion
|
| 56 |
-
def load_quotes():
|
| 57 |
-
json_path = os.path.join(os.path.dirname(__file__), "quotes.json")
|
| 58 |
-
with open(json_path, "r") as f:
|
| 59 |
-
return json.load(f)
|
| 60 |
-
|
| 61 |
-
quotes_data = load_quotes()
|
| 62 |
-
|
| 63 |
-
# Sentiment analyzer
|
| 64 |
-
def analyze_sentiment(text):
|
| 65 |
-
blob = TextBlob(text)
|
| 66 |
-
polarity = blob.sentiment.polarity
|
| 67 |
-
if polarity > 0.2:
|
| 68 |
-
return "happy", polarity
|
| 69 |
-
elif polarity < -0.2:
|
| 70 |
-
return "sad", abs(polarity)
|
| 71 |
-
elif 0 <= polarity <= 0.2:
|
| 72 |
-
return "neutral", polarity
|
| 73 |
-
else:
|
| 74 |
-
return "angry", abs(polarity)
|
| 75 |
-
|
| 76 |
-
# Emotion colors
|
| 77 |
-
emotion_colors = {
|
| 78 |
-
"happy": "#FFD700", # Yellow
|
| 79 |
-
"sad": "#000000", # Black
|
| 80 |
-
"angry": "#FF0000", # Red
|
| 81 |
-
"neutral": "#800080" # Purple
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
if user_input:
|
| 85 |
-
emotion, score = analyze_sentiment(user_input)
|
| 86 |
-
percent_score = round(score * 100, 1)
|
| 87 |
-
|
| 88 |
-
# Styled sentiment result
|
| 89 |
-
st.markdown(f"""
|
| 90 |
-
<div style='margin-top: 25px;'>
|
| 91 |
-
<p style='font-size: 28px; font-weight: bold; font-style: italic; color: white;'>
|
| 92 |
-
Sentiment: {emotion.upper()}<br>
|
| 93 |
-
Confidence Score: {percent_score}%
|
| 94 |
-
</p>
|
| 95 |
-
</div>
|
| 96 |
-
""", unsafe_allow_html=True)
|
| 97 |
-
|
| 98 |
-
# Emotion indicator graph
|
| 99 |
-
fig, ax = plt.subplots(figsize=(8, 1.2))
|
| 100 |
-
ax.barh([0], [score], color=emotion_colors.get(emotion, "#800080"))
|
| 101 |
-
ax.set_xlim(0, 1)
|
| 102 |
-
ax.set_xticks([])
|
| 103 |
-
ax.set_yticks([])
|
| 104 |
-
ax.set_title("Emotion Indicator", fontsize=14, color='white')
|
| 105 |
-
ax.spines['top'].set_visible(False)
|
| 106 |
-
ax.spines['right'].set_visible(False)
|
| 107 |
-
ax.spines['left'].set_visible(False)
|
| 108 |
-
ax.spines['bottom'].set_visible(False)
|
| 109 |
st.pyplot(fig)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
quotes = quotes_data.get(emotion, ["Stay strong, you're doing great!"])
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
f"<div class='quote-box' style='background-color: {emotion_colors.get(emotion, '#800080')}; color: white;'>{quote}</div>",
|
| 116 |
-
unsafe_allow_html=True
|
| 117 |
-
)
|
|
|
|
| 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}*")
|
|
|
|
|
|
|
|
|