Bey007 commited on
Commit
ce9d087
Β·
verified Β·
1 Parent(s): 9bdc1f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -8,22 +8,33 @@ import os
8
  # Load pretrained models
9
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
10
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
 
 
 
11
  emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", return_all_scores=True)
12
 
13
- # Function to generate a comforting story using the pretrained model
14
  def generate_story(theme):
15
- story_prompt = f"Tell a detailed, comforting, and heartwarming story about {theme}. Include a character facing a tough challenge, showing courage, and overcoming it with a positive resolution."
16
- input_ids = tokenizer.encode(story_prompt, return_tensors='pt')
17
- story_ids = model.generate(
 
 
 
 
18
  input_ids,
19
- max_length=500,
20
- temperature=0.9,
21
- repetition_penalty=1.1,
 
22
  num_return_sequences=1
23
  )
24
- story = tokenizer.decode(story_ids[0], skip_special_tokens=True)
 
 
25
  return story
26
 
 
27
  # Function to generate an empathetic response
28
  def generate_response(user_input):
29
  response_prompt = f"You are a compassionate support bot. A user has shared: '{user_input}'. Respond with empathy and encouragement."
@@ -72,6 +83,8 @@ with st.sidebar:
72
  tts.save(meditation_audio)
73
  st.audio(meditation_audio, format="audio/mp3")
74
 
 
 
75
  st.header("πŸ“– Short Comforting Story")
76
  story_theme = st.selectbox("Choose a theme for your story:", ["courage", "healing", "hope"])
77
  if st.button("Generate Story"):
@@ -79,6 +92,7 @@ with st.sidebar:
79
  story = generate_story(story_theme)
80
  st.text_area("Here's your story:", story, height=300)
81
 
 
82
  # User input section
83
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
84
 
 
8
  # Load pretrained models
9
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
10
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
11
+ # Load GPT-2 model and tokenizer for story generation
12
+ gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2-medium")
13
+ gpt2_model = AutoModelForCausalLM.from_pretrained("gpt2-medium")
14
  emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", return_all_scores=True)
15
 
16
+ # Function to generate a comforting story using GPT-2
17
  def generate_story(theme):
18
+ # A detailed prompt for generating a comforting story about the selected theme
19
+ story_prompt = f"Write a comforting, detailed, and heartwarming story about {theme}. The story should include a character who faces a tough challenge, finds hope, and ultimately overcomes the situation with a positive resolution."
20
+
21
+ # Generate story using GPT-2
22
+ input_ids = gpt2_tokenizer.encode(story_prompt, return_tensors='pt')
23
+
24
+ story_ids = gpt2_model.generate(
25
  input_ids,
26
+ max_length=500, # Generate longer stories
27
+ temperature=0.8, # Balanced creativity
28
+ top_p=0.9,
29
+ repetition_penalty=1.2,
30
  num_return_sequences=1
31
  )
32
+
33
+ # Decode the generated text
34
+ story = gpt2_tokenizer.decode(story_ids[0], skip_special_tokens=True)
35
  return story
36
 
37
+
38
  # Function to generate an empathetic response
39
  def generate_response(user_input):
40
  response_prompt = f"You are a compassionate support bot. A user has shared: '{user_input}'. Respond with empathy and encouragement."
 
83
  tts.save(meditation_audio)
84
  st.audio(meditation_audio, format="audio/mp3")
85
 
86
+ # Sidebar for additional features
87
+ with st.sidebar:
88
  st.header("πŸ“– Short Comforting Story")
89
  story_theme = st.selectbox("Choose a theme for your story:", ["courage", "healing", "hope"])
90
  if st.button("Generate Story"):
 
92
  story = generate_story(story_theme)
93
  st.text_area("Here's your story:", story, height=300)
94
 
95
+
96
  # User input section
97
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
98