Bey007 commited on
Commit
c017ee5
·
verified ·
1 Parent(s): 30a16a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
  from gtts import gTTS
4
  import torch
5
 
6
- # Load GPT-2 model and tokenizer from Hugging Face
7
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
8
- model = GPT2LMHeadModel.from_pretrained("gpt2")
9
 
10
  # Set up Streamlit page configuration
11
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
@@ -31,46 +31,27 @@ user_input = st.text_input("Share what's on your mind...", placeholder="Type her
31
  if 'previous_responses' not in st.session_state:
32
  st.session_state.previous_responses = []
33
 
34
- # Function to generate a more empathetic and focused response
35
  def generate_response(user_input):
36
- # Tokenize input and set parameters for text generation
37
- inputs = tokenizer.encode(user_input, return_tensors="pt")
38
- outputs = model.generate(
39
- inputs,
40
- max_length=150,
41
- temperature=0.7,
42
- top_k=50,
43
- repetition_penalty=1.2, # Adds a penalty for repeated phrases
44
- num_return_sequences=1,
45
- pad_token_id=tokenizer.eos_token_id
46
- )
47
- response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
 
49
- # Trim the output to remove extraneous content
50
- response_text = response_text.split("Here's something")[0].strip()
 
51
 
52
- # Add a suggestion for coping activity based on keywords in user input
53
- if "crying" in user_input.lower() or "overload" in user_input.lower():
54
- activity_suggestion = (
55
- "Work overload can be overwhelming, and it's completely understandable to feel like you're reaching your limit. "
56
- "Sometimes, taking a short break can help you gain some clarity and calm your mind. "
57
- "If possible, try stepping outside for a few minutes to breathe deeply or do some light stretching to release tension."
58
- )
59
- elif "angry" in user_input.lower() or "frustrated" in user_input.lower():
60
- activity_suggestion = (
61
- "When feeling overwhelmed, anger can sometimes mask deeper emotions like stress or exhaustion. "
62
- "Taking deep breaths or doing a short meditation can help you ground yourself and regain focus. "
63
- "Even small moments of calm can make a huge difference."
64
- )
65
- else:
66
- activity_suggestion = (
67
- "Sometimes, a creative outlet can be a great way to cope with overwhelming feelings. "
68
- "Art, whether it’s drawing, painting, or even coloring, allows you to express what you might not be able to say out loud."
69
- )
70
-
71
- # Add a closing empathetic sentence
72
- response = f"{response_text}\n\nHere's something you could try to help cope with how you're feeling:\n{activity_suggestion}"
73
 
 
 
 
 
 
 
 
74
  return response
75
 
76
  # Check if the user has typed something
 
1
  import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  from gtts import gTTS
4
  import torch
5
 
6
+ # Load DialoGPT model and tokenizer from Hugging Face
7
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
8
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
9
 
10
  # Set up Streamlit page configuration
11
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
 
31
  if 'previous_responses' not in st.session_state:
32
  st.session_state.previous_responses = []
33
 
34
+ # Function to generate a more empathetic and focused response using DialoGPT
35
  def generate_response(user_input):
36
+ # Encode the input text and generate a response
37
+ new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
38
+ bot_input_ids = new_user_input_ids
39
+ chat_history_ids = model.generate(bot_input_ids, max_length=150, pad_token_id=tokenizer.eos_token_id, temperature=0.7, top_k=50, repetition_penalty=1.2)
 
 
 
 
 
 
 
 
40
 
41
+ # Decode the response to text
42
+ chat_history_ids = chat_history_ids[:, bot_input_ids.shape[-1]:] # remove the input from the response
43
+ bot_output = tokenizer.decode(chat_history_ids[0], skip_special_tokens=True)
44
 
45
+ # Add empathetic and coping suggestions
46
+ response = f"{bot_output}\n\nHere's something you could try to help cope with how you're feeling:\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ if "crying" in user_input.lower():
49
+ response += "Sometimes letting yourself cry is a necessary release. It’s okay to feel what you're feeling. Also, trying to reach out to a friend or family member for support can be really comforting."
50
+ elif "overwhelmed" in user_input.lower():
51
+ response += "Overwhelm can often feel like too much to handle, but try taking it one step at a time. Break your tasks down into small pieces and remember to take breaks. You don't have to do it all at once."
52
+ else:
53
+ response += "Taking a moment to breathe deeply, meditate, or even take a short walk can help you regain some balance in this difficult moment."
54
+
55
  return response
56
 
57
  # Check if the user has typed something