Spaces:
Running
Running
Nikhil SST
commited on
Commit
·
358065e
1
Parent(s):
7085a87
Fix initialization and add README
Browse files- README.md +7 -5
- app.py +15 -13
- requirements.txt +3 -2
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
---
|
2 |
-
title: AI Virtual
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
1 |
---
|
2 |
+
title: AI Virtual Therapist
|
3 |
+
emoji: 🧠
|
4 |
+
colorFrom: blue
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.12.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
# AI Virtual Therapist
|
13 |
+
|
14 |
+
This application provides text emotion analysis, voice emotion analysis, and chat with text-to-speech capabilities.
|
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from fastapi import FastAPI
|
3 |
import librosa
|
4 |
import openai
|
5 |
from transformers import pipeline
|
@@ -14,9 +14,9 @@ app = FastAPI()
|
|
14 |
# Initialize emotion classifier
|
15 |
text_emotion_classifier = pipeline("text-classification",
|
16 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
17 |
-
device=-1)
|
18 |
|
19 |
-
# Environment variables
|
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")
|
@@ -31,16 +31,16 @@ def analyze_text_emotion(text):
|
|
31 |
|
32 |
def analyze_voice_emotion(audio):
|
33 |
try:
|
34 |
-
|
|
|
|
|
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:
|
@@ -52,11 +52,13 @@ def analyze_voice_emotion(audio):
|
|
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 |
-
|
|
|
|
|
60 |
openai.api_key = OPENAI_API_KEY
|
61 |
chat_response = openai.ChatCompletion.create(
|
62 |
model="gpt-3.5-turbo",
|
@@ -67,7 +69,6 @@ def chat_and_tts(message):
|
|
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,
|
@@ -85,7 +86,6 @@ def chat_and_tts(message):
|
|
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)
|
@@ -95,7 +95,9 @@ def chat_and_tts(message):
|
|
95 |
return f"Error: {str(e)}", None
|
96 |
|
97 |
# Create Gradio interface
|
98 |
-
|
|
|
|
|
99 |
gr.Markdown("# AI Virtual Therapist")
|
100 |
|
101 |
with gr.Tab("Text Emotion Analysis"):
|
@@ -117,5 +119,5 @@ with gr.Blocks(title="AI Therapist") as demo:
|
|
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 |
-
|
121 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastapi import FastAPI
|
3 |
import librosa
|
4 |
import openai
|
5 |
from transformers import pipeline
|
|
|
14 |
# Initialize emotion classifier
|
15 |
text_emotion_classifier = pipeline("text-classification",
|
16 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
17 |
+
device=-1)
|
18 |
|
19 |
+
# Environment variables
|
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")
|
|
|
31 |
|
32 |
def analyze_voice_emotion(audio):
|
33 |
try:
|
34 |
+
if audio is None:
|
35 |
+
return "Please upload an audio file"
|
36 |
+
|
37 |
y = audio[1]
|
38 |
sr = audio[0]
|
39 |
|
|
|
40 |
pitch = float(librosa.feature.spectral_centroid(y=y, sr=sr).mean())
|
41 |
intensity = float(librosa.feature.rms(y=y).mean())
|
42 |
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
43 |
|
|
|
44 |
if pitch < 150 and intensity < 0.02:
|
45 |
emotion = "sadness"
|
46 |
elif pitch > 200 and intensity > 0.05:
|
|
|
52 |
|
53 |
return f"Emotion: {emotion}\nPitch: {pitch:.2f}\nIntensity: {intensity:.2f}\nTempo: {tempo:.2f}"
|
54 |
except Exception as e:
|
55 |
+
return f"Error analyzing audio: {str(e)}"
|
56 |
|
57 |
def chat_and_tts(message):
|
58 |
try:
|
59 |
+
if not OPENAI_API_KEY or not ELEVEN_LABS_API_KEY:
|
60 |
+
return "API keys not configured", None
|
61 |
+
|
62 |
openai.api_key = OPENAI_API_KEY
|
63 |
chat_response = openai.ChatCompletion.create(
|
64 |
model="gpt-3.5-turbo",
|
|
|
69 |
)
|
70 |
response_text = chat_response['choices'][0]['message']['content'].strip()
|
71 |
|
|
|
72 |
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
|
73 |
headers = {
|
74 |
"xi-api-key": ELEVEN_LABS_API_KEY,
|
|
|
86 |
if response.status_code != 200:
|
87 |
return response_text, None
|
88 |
|
|
|
89 |
audio_path = "response.mp3"
|
90 |
with open(audio_path, "wb") as f:
|
91 |
f.write(response.content)
|
|
|
95 |
return f"Error: {str(e)}", None
|
96 |
|
97 |
# Create Gradio interface
|
98 |
+
demo = gr.Blocks(title="AI Therapist")
|
99 |
+
|
100 |
+
with demo:
|
101 |
gr.Markdown("# AI Virtual Therapist")
|
102 |
|
103 |
with gr.Tab("Text Emotion Analysis"):
|
|
|
119 |
audio_output = gr.Audio(label="Voice Response")
|
120 |
chat_button.click(chat_and_tts, inputs=chat_input, outputs=[chat_output, audio_output])
|
121 |
|
122 |
+
if __name__ == "__main__":
|
123 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
|
|
1 |
fastapi
|
2 |
-
gradio
|
3 |
uvicorn
|
4 |
python-multipart
|
5 |
openai
|
@@ -8,4 +8,5 @@ transformers
|
|
8 |
torch
|
9 |
requests
|
10 |
python-dotenv
|
11 |
-
soundfile
|
|
|
|
1 |
+
gradio==4.12.0
|
2 |
fastapi
|
|
|
3 |
uvicorn
|
4 |
python-multipart
|
5 |
openai
|
|
|
8 |
torch
|
9 |
requests
|
10 |
python-dotenv
|
11 |
+
soundfile
|
12 |
+
numpy
|