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