Bey007 commited on
Commit
8430ee5
·
verified ·
1 Parent(s): 4839250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -35,12 +35,11 @@ def generate_story(theme):
35
  story = gpt2_tokenizer.decode(story_ids[0], skip_special_tokens=True)
36
  return story
37
 
38
-
39
  def generate_response(user_input):
40
- # Enhanced empathetic prompt
41
- response_prompt = f"User shares: '{user_input}'. Respond with empathy and understanding. Acknowledge their feelings and offer words of comfort and encouragement. Your response should focus on offering reassurance and emotional support, not practical advice."
42
 
43
- # Generate the response using the model
44
  input_ids = gpt2_tokenizer.encode(response_prompt, return_tensors='pt')
45
  response_ids = gpt2_model.generate(
46
  input_ids,
@@ -51,18 +50,16 @@ def generate_response(user_input):
51
  num_return_sequences=1
52
  )
53
 
54
- # Decode and clean up the response
55
  response = gpt2_tokenizer.decode(response_ids[0], skip_special_tokens=True)
56
 
57
- # Clean response from repeating the prompt
58
- cleaned_response = response.replace(f"User shares: '{user_input}'. Respond with empathy and understanding. Acknowledge their feelings and offer words of comfort and encouragement. Your response should focus on offering reassurance and emotional support, not practical advice.", "").strip()
59
 
60
- # Return the cleaned response
61
  return cleaned_response
62
 
63
 
64
 
65
-
66
  # Analyze user input for emotional tone
67
  def get_emotion(user_input):
68
  emotions = emotion_classifier(user_input)
 
35
  story = gpt2_tokenizer.decode(story_ids[0], skip_special_tokens=True)
36
  return story
37
 
 
38
  def generate_response(user_input):
39
+ # Empathy-focused prompt to guide the bot
40
+ response_prompt = f"The user has shared the following: '{user_input}'. Respond with empathy, compassion, and understanding. Acknowledge their sadness and offer comforting, reassuring words. Show that you care and validate their feelings without giving unsolicited advice."
41
 
42
+ # Generate the response using the GPT-2 model
43
  input_ids = gpt2_tokenizer.encode(response_prompt, return_tensors='pt')
44
  response_ids = gpt2_model.generate(
45
  input_ids,
 
50
  num_return_sequences=1
51
  )
52
 
53
+ # Decode the response and clean it up by removing the prompt
54
  response = gpt2_tokenizer.decode(response_ids[0], skip_special_tokens=True)
55
 
56
+ # Strip out the prompt portion to get a clean, empathetic message
57
+ cleaned_response = response.replace(f"The user has shared the following: '{user_input}'. Respond with empathy, compassion, and understanding. Acknowledge their sadness and offer comforting, reassuring words. Show that you care and validate their feelings without giving unsolicited advice.", "").strip()
58
 
 
59
  return cleaned_response
60
 
61
 
62
 
 
63
  # Analyze user input for emotional tone
64
  def get_emotion(user_input):
65
  emotions = emotion_classifier(user_input)