izhan001 commited on
Commit
6c0b407
·
verified ·
1 Parent(s): 2337bc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -13
app.py CHANGED
@@ -1,19 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def virtual_psychologist(input_text):
2
  # Check for empty or invalid input
3
  if not input_text.strip():
4
  return "Please provide some input about how you're feeling.", None, None
5
 
6
- # Sentiment analysis
7
  sentiment = sentiment_model(input_text)[0]
8
  label = sentiment['label']
9
  confidence = sentiment['score']
10
-
 
11
  sentiment_feedback = f"Your input sentiment is detected as **{label}** with confidence {confidence:.2f}.\n\n"
12
 
13
- if confidence > 0.7: # Confident sentiment analysis
 
14
  if label == 'POSITIVE':
15
  response = "I'm glad you're feeling positive! Tell me more about what’s bringing you joy, and let’s keep this energy up together."
16
  elif label == 'NEGATIVE':
 
17
  if "suicide" in input_text.lower() or "worthless" in input_text.lower():
18
  response = ("I'm really sorry you're feeling this way, but please know you're not alone. "
19
  "It's really important to talk to someone who can provide support. Would you like to share more "
@@ -25,18 +41,30 @@ def virtual_psychologist(input_text):
25
  else:
26
  response = "I'm not quite sure I understand. Could you elaborate a bit more? I'm here to listen."
27
 
28
- # Generate response
29
  generated_text = text_gen_model(response, max_length=100, num_return_sequences=1)[0]['generated_text']
30
-
 
31
  full_response = sentiment_feedback + generated_text
32
-
33
  # Convert response to speech using gTTS
34
  tts = gTTS(text=full_response, lang='en')
35
- # Save the audio to a temporary location
36
- audio_path = "/tmp/response.mp3"
37
- tts.save(audio_path)
38
-
39
  response_type = "Supportive Response" if "suicide" in input_text.lower() or "worthless" in input_text.lower() else "General Response"
40
-
41
- # Return audio file path
42
- return full_response, response_type, audio_path
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Step 2: Import Libraries and Set Up Models
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+ from gtts import gTTS
6
+ import os
7
+ import IPython.display as ipd
8
+
9
+ # Load sentiment analysis and text generation models
10
+ sentiment_model = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
11
+ text_gen_model = pipeline("text-generation", model="microsoft/DialoGPT-medium")
12
+
13
+ # Step 3: Define the Response Function
14
  def virtual_psychologist(input_text):
15
  # Check for empty or invalid input
16
  if not input_text.strip():
17
  return "Please provide some input about how you're feeling.", None, None
18
 
19
+ # Step 1: Sentiment Analysis
20
  sentiment = sentiment_model(input_text)[0]
21
  label = sentiment['label']
22
  confidence = sentiment['score']
23
+
24
+ # Step 2: Display Sentiment Information
25
  sentiment_feedback = f"Your input sentiment is detected as **{label}** with confidence {confidence:.2f}.\n\n"
26
 
27
+ # Step 3: Generate a Response Based on Sentiment
28
+ if confidence > 0.7: # Threshold for confident sentiment analysis
29
  if label == 'POSITIVE':
30
  response = "I'm glad you're feeling positive! Tell me more about what’s bringing you joy, and let’s keep this energy up together."
31
  elif label == 'NEGATIVE':
32
+ # Check for sensitive keywords like "suicide" or "worthless"
33
  if "suicide" in input_text.lower() or "worthless" in input_text.lower():
34
  response = ("I'm really sorry you're feeling this way, but please know you're not alone. "
35
  "It's really important to talk to someone who can provide support. Would you like to share more "
 
41
  else:
42
  response = "I'm not quite sure I understand. Could you elaborate a bit more? I'm here to listen."
43
 
44
+ # Step 4: Generate a Longer Response for the User
45
  generated_text = text_gen_model(response, max_length=100, num_return_sequences=1)[0]['generated_text']
46
+
47
+ # Combine sentiment feedback and generated text into the final output
48
  full_response = sentiment_feedback + generated_text
49
+
50
  # Convert response to speech using gTTS
51
  tts = gTTS(text=full_response, lang='en')
52
+ tts.save("response.mp3")
53
+
54
+ # Return the response text and audio file path
 
55
  response_type = "Supportive Response" if "suicide" in input_text.lower() or "worthless" in input_text.lower() else "General Response"
56
+
57
+ # Return response as text, response type, and audio file path for Gradio
58
+ return full_response, response_type, "response.mp3"
59
+
60
+ # Step 5: Create a Gradio interface for the app
61
+ interface = gr.Interface(
62
+ fn=virtual_psychologist,
63
+ inputs=gr.Textbox(lines=5, placeholder="How are you feeling today?"),
64
+ outputs=[gr.Textbox(), gr.Textbox(), gr.Audio()],
65
+ title="Virtual Psychologist Assistant",
66
+ description="Share your feelings, and this assistant will analyze your sentiment and respond as a supportive psychologist."
67
+ )
68
+
69
+ # Step 6: Launch the Gradio interface
70
+ interface.launch()