Bey007 commited on
Commit
56b5bd3
·
verified ·
1 Parent(s): 30d702b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -1,15 +1,16 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
  from gtts import gTTS
4
  from pytube import Search
5
  import os
6
  import random
7
 
8
- # Initialize conversational model for empathetic dialogue
9
- conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
 
10
 
11
- # Initialize sentiment analysis
12
- sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
13
 
14
  # Set up Streamlit page
15
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
@@ -37,14 +38,11 @@ if 'previous_responses' not in st.session_state:
37
 
38
  # Check if user has entered text
39
  if user_input:
40
- # Run sentiment analysis to check for distress
41
- sentiment = sentiment_analysis(user_input)[0]
42
-
43
- # Generate multiple responses with increased randomness (top_k_sampling)
44
- responses = conversational_bot(user_input, max_length=300, temperature=1.0, top_k=50, num_return_sequences=3, do_sample=True)
45
 
46
  # Filter out any responses that are too similar to previous responses or user input
47
- new_responses = [response['generated_text'] for response in responses]
48
  new_responses = [resp for resp in new_responses if resp.lower() not in [prev.lower() for prev in st.session_state.previous_responses] and resp.lower() != user_input.lower()]
49
 
50
  # If there are valid new responses, pick one, otherwise fallback
@@ -80,11 +78,22 @@ if user_input:
80
  st.audio(audio_file, format="audio/mp3")
81
 
82
  # Suggest a productive activity based on detected keywords
83
- if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
84
  st.info("Here's a suggestion to help you cope:")
85
- hobbies = ["journaling", "yoga", "painting"]
86
- activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
87
-
 
 
 
 
 
 
 
 
 
 
 
88
  # Search YouTube for videos related to the selected activity
89
  search = Search(activity)
90
  search_results = search.results[:3] # limit results to 3 videos
 
1
  import streamlit as st
2
+ from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
3
  from gtts import gTTS
4
  from pytube import Search
5
  import os
6
  import random
7
 
8
+ # Initialize GPT-2 model and tokenizer from Hugging Face
9
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
10
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
11
 
12
+ # Create a text generation pipeline using GPT-2
13
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
14
 
15
  # Set up Streamlit page
16
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
 
38
 
39
  # Check if user has entered text
40
  if user_input:
41
+ # Run the text generation model to generate a response based on user input
42
+ generated_responses = generator(user_input, max_length=300, num_return_sequences=3, temperature=0.9)
 
 
 
43
 
44
  # Filter out any responses that are too similar to previous responses or user input
45
+ new_responses = [response['generated_text'] for response in generated_responses]
46
  new_responses = [resp for resp in new_responses if resp.lower() not in [prev.lower() for prev in st.session_state.previous_responses] and resp.lower() != user_input.lower()]
47
 
48
  # If there are valid new responses, pick one, otherwise fallback
 
78
  st.audio(audio_file, format="audio/mp3")
79
 
80
  # Suggest a productive activity based on detected keywords
81
+ if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "overwhelmed", "academic", "exam"]):
82
  st.info("Here's a suggestion to help you cope:")
83
+
84
+ # Providing a variety of activities based on user mood and needs
85
+ activities = {
86
+ "journaling": "Express your feelings in writing. Journaling is a great way to process emotions.",
87
+ "yoga": "Yoga helps you relax and refocus. Try some deep breathing exercises or light stretching.",
88
+ "painting": "Creative expression through painting or drawing can be soothing and help you release pent-up emotions.",
89
+ "meditation": "Take a moment to calm your mind. Guided meditation can help reduce stress and anxiety.",
90
+ "exercise": "Physical activity can lift your mood. Even a short walk in nature can make a big difference."
91
+ }
92
+
93
+ # Randomly select an activity category to suggest
94
+ activity = random.choice(list(activities.keys()))
95
+ st.write(f"How about {activity}? {activities[activity]}")
96
+
97
  # Search YouTube for videos related to the selected activity
98
  search = Search(activity)
99
  search_results = search.results[:3] # limit results to 3 videos