Nikhil SST commited on
Commit
7085a87
·
1 Parent(s): 2e5c439

Add AI Virtual Therapist application

Browse files
Files changed (2) hide show
  1. app.py +121 -0
  2. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastapi import FastAPI, UploadFile, File, HTTPException
3
+ import librosa
4
+ import openai
5
+ from transformers import pipeline
6
+ import requests
7
+ import os
8
+ from pydantic import BaseModel
9
+ import numpy as np
10
+
11
+ # Initialize FastAPI
12
+ app = FastAPI()
13
+
14
+ # Initialize emotion classifier
15
+ text_emotion_classifier = pipeline("text-classification",
16
+ model="bhadresh-savani/distilbert-base-uncased-emotion",
17
+ device=-1) # Use CPU
18
+
19
+ # Environment variables will be set in Hugging Face Spaces
20
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
21
+ ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY")
22
+ VOICE_ID = os.getenv("VOICE_ID", "9BWtsMINqrJLrRacOk9x")
23
+
24
+ def analyze_text_emotion(text):
25
+ try:
26
+ emotion_result = text_emotion_classifier(text)
27
+ emotion_data = emotion_result[0]
28
+ return f"Emotion: {emotion_data['label']}\nConfidence: {emotion_data['score']:.2f}"
29
+ except Exception as e:
30
+ return f"Error: {str(e)}"
31
+
32
+ def analyze_voice_emotion(audio):
33
+ try:
34
+ # Convert audio to numpy array
35
+ y = audio[1]
36
+ sr = audio[0]
37
+
38
+ # Extract features
39
+ pitch = float(librosa.feature.spectral_centroid(y=y, sr=sr).mean())
40
+ intensity = float(librosa.feature.rms(y=y).mean())
41
+ tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
42
+
43
+ # Simple emotion classification
44
+ if pitch < 150 and intensity < 0.02:
45
+ emotion = "sadness"
46
+ elif pitch > 200 and intensity > 0.05:
47
+ emotion = "anger"
48
+ elif pitch > 150 and intensity < 0.03:
49
+ emotion = "joy"
50
+ else:
51
+ emotion = "anxiety"
52
+
53
+ return f"Emotion: {emotion}\nPitch: {pitch:.2f}\nIntensity: {intensity:.2f}\nTempo: {tempo:.2f}"
54
+ except Exception as e:
55
+ return f"Error: {str(e)}"
56
+
57
+ def chat_and_tts(message):
58
+ try:
59
+ # Get ChatGPT response
60
+ openai.api_key = OPENAI_API_KEY
61
+ chat_response = openai.ChatCompletion.create(
62
+ model="gpt-3.5-turbo",
63
+ messages=[
64
+ {"role": "system", "content": "You are a helpful assistant."},
65
+ {"role": "user", "content": message},
66
+ ]
67
+ )
68
+ response_text = chat_response['choices'][0]['message']['content'].strip()
69
+
70
+ # Convert to speech using Eleven Labs
71
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
72
+ headers = {
73
+ "xi-api-key": ELEVEN_LABS_API_KEY,
74
+ "Content-Type": "application/json"
75
+ }
76
+ data = {
77
+ "text": response_text,
78
+ "voice_settings": {
79
+ "stability": 0.75,
80
+ "similarity_boost": 0.75
81
+ }
82
+ }
83
+ response = requests.post(url, json=data, headers=headers)
84
+
85
+ if response.status_code != 200:
86
+ return response_text, None
87
+
88
+ # Save audio temporarily
89
+ audio_path = "response.mp3"
90
+ with open(audio_path, "wb") as f:
91
+ f.write(response.content)
92
+
93
+ return response_text, audio_path
94
+ except Exception as e:
95
+ return f"Error: {str(e)}", None
96
+
97
+ # Create Gradio interface
98
+ with gr.Blocks(title="AI Therapist") as demo:
99
+ gr.Markdown("# AI Virtual Therapist")
100
+
101
+ with gr.Tab("Text Emotion Analysis"):
102
+ text_input = gr.Textbox(label="Enter text")
103
+ text_button = gr.Button("Analyze Text Emotion")
104
+ text_output = gr.Textbox(label="Emotion Analysis Result")
105
+ text_button.click(analyze_text_emotion, inputs=text_input, outputs=text_output)
106
+
107
+ with gr.Tab("Voice Emotion Analysis"):
108
+ audio_input = gr.Audio(label="Upload Audio", type="numpy")
109
+ audio_button = gr.Button("Analyze Voice Emotion")
110
+ audio_output = gr.Textbox(label="Voice Analysis Result")
111
+ audio_button.click(analyze_voice_emotion, inputs=audio_input, outputs=audio_output)
112
+
113
+ with gr.Tab("Chat with TTS"):
114
+ chat_input = gr.Textbox(label="Enter your message")
115
+ chat_button = gr.Button("Send Message")
116
+ chat_output = gr.Textbox(label="Assistant Response")
117
+ audio_output = gr.Audio(label="Voice Response")
118
+ chat_button.click(chat_and_tts, inputs=chat_input, outputs=[chat_output, audio_output])
119
+
120
+ # Mount Gradio app to FastAPI
121
+ app = gr.mount_gradio_app(app, demo, path="/")
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ gradio
3
+ uvicorn
4
+ python-multipart
5
+ openai
6
+ librosa
7
+ transformers
8
+ torch
9
+ requests
10
+ python-dotenv
11
+ soundfile