Bey007 commited on
Commit
8f34be7
β€’
1 Parent(s): 615fb1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -30
app.py CHANGED
@@ -1,70 +1,74 @@
1
  import streamlit as st
2
- from llama_cpp import Llama
3
  from gtts import gTTS
4
  from pytube import Search
5
  import random
6
  import os
7
 
8
- # Initialize the Llama model by specifying the model path
9
- llm = Llama(model_path="HyunCello-KULLM3-empathy-v1.0-IQ4_XS.gguf")
 
10
 
11
- # Configure Streamlit page
12
- st.set_page_config(page_title="Grief & Loss Support Bot", page_icon="🌱", layout="centered")
13
  st.markdown("""
14
  <style>
15
- .main { background-color: #F4F9F9; }
16
- h1, h2, h3, h4 { color: #4C6D7D; }
17
  .stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; }
18
- .stButton>button { background-color: #A4D4AE; color: white; }
19
- .stButton>button:hover { background-color: #92C8A6; }
 
20
  </style>
21
  """, unsafe_allow_html=True)
22
 
23
  # Title and description
24
- st.title("Grief & Loss Support Bot 🌱")
25
  st.subheader("Your compassionate companion in tough times πŸ’š")
26
 
27
  # User input
28
  user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
29
 
30
- # Generate empathetic response using Llama model
31
  if user_input:
32
- response = llm(
33
- messages=[
34
- {"role": "user", "content": user_input}
35
- ]
36
- )['choices'][0]['message']['content']
37
-
38
- # Display empathetic response
39
  st.text_area("Bot's Response:", response, height=150)
40
 
41
- # Convert text to speech for added comfort
42
  tts = gTTS(response, lang='en')
43
  audio_file = "response.mp3"
44
  tts.save(audio_file)
45
  st.audio(audio_file, format="audio/mp3")
46
 
47
- # Suggested activities based on user input
48
- activity_suggestions = {
49
  "positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
50
  "neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
51
  "negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
52
  }
53
- activities = activity_suggestions["neutral"] # Default to "neutral" for simplicity
54
 
55
- # Show dynamic activity suggestion
56
- st.info("Here’s something you could try to lift your spirits:")
57
- selected_activity = random.choice(activities)
58
- st.write(f"**Activity Suggestion:** {selected_activity}")
 
 
 
 
 
59
 
60
  # YouTube search for video resources
61
- search = Search(selected_activity)
62
  st.write("Recommended Videos:")
63
  for video in search.results[:2]: # Display top 2 videos
64
  st.write(f"[{video.title}]({video.watch_url})")
65
 
66
- # Crisis response if critical keywords are detected
67
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
68
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
69
- st.error("It sounds like you're going through a very tough time. Please don't hesitate to reach out for help.")
70
- st.write("[Click here for emergency resources](https://www.helpguide.org/find-help.htm)")
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
  from gtts import gTTS
4
  from pytube import Search
5
  import random
6
  import os
7
 
8
+ # Initialize conversational and sentiment analysis models
9
+ conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
10
+ sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
11
 
12
+ # Streamlit page configuration
13
+ st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
14
  st.markdown("""
15
  <style>
16
+ .css-1d391kg { background-color: #F3F7F6; }
17
+ .css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
18
  .stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; }
19
+ .stButton>button { background-color: #A9D0B6; color: white; }
20
+ .stButton>button:hover { background-color: #8FB79A; }
21
+ .stTextInput>div>label { color: #4C6D7D; }
22
  </style>
23
  """, unsafe_allow_html=True)
24
 
25
  # Title and description
26
+ st.title("Grief and Loss Support Bot 🌿")
27
  st.subheader("Your compassionate companion in tough times πŸ’š")
28
 
29
  # User input
30
  user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
31
 
32
+ # Generate response
33
  if user_input:
34
+ # Analyze sentiment
35
+ sentiment = sentiment_analysis(user_input)[0]
36
+
37
+ # Use conversational bot for response
38
+ response = conversational_bot(user_input, max_length=150)[0]['generated_text']
 
 
39
  st.text_area("Bot's Response:", response, height=150)
40
 
41
+ # Text-to-speech conversion
42
  tts = gTTS(response, lang='en')
43
  audio_file = "response.mp3"
44
  tts.save(audio_file)
45
  st.audio(audio_file, format="audio/mp3")
46
 
47
+ # Suggest activity based on mood keywords
48
+ mood_keywords = {
49
  "positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
50
  "neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
51
  "negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
52
  }
 
53
 
54
+ mood_type = "neutral" # Default category
55
+ if any(word in user_input.lower() for word in ["lonely", "lost", "sad", "stressed"]):
56
+ mood_type = "negative"
57
+ elif sentiment["label"] == "POSITIVE":
58
+ mood_type = "positive"
59
+
60
+ suggested_activity = random.choice(mood_keywords[mood_type])
61
+ st.info("Here's a suggestion to help lift your spirits:")
62
+ st.write(f"**Activity Suggestion:** {suggested_activity}")
63
 
64
  # YouTube search for video resources
65
+ search = Search(suggested_activity)
66
  st.write("Recommended Videos:")
67
  for video in search.results[:2]: # Display top 2 videos
68
  st.write(f"[{video.title}]({video.watch_url})")
69
 
70
+ # Crisis intervention resources
71
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
72
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
73
+ st.warning("It sounds like you're going through a very tough time. Please reach out to someone you trust or a professional for support.")
74
+ st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")