File size: 6,409 Bytes
3f871dd
303fc54
3f871dd
 
 
303fc54
8e51149
 
 
 
 
 
3f871dd
8e51149
 
 
3f871dd
8e51149
 
 
 
 
3f871dd
 
 
8e51149
3f871dd
8e51149
3f871dd
 
 
 
 
 
 
d83d3e2
3f871dd
 
8e51149
3f871dd
 
 
8e51149
3f871dd
8e51149
 
3f871dd
 
 
 
8e51149
 
3f871dd
 
 
 
8e51149
 
3f871dd
 
8e51149
 
3f871dd
 
 
 
 
d83d3e2
3f871dd
 
8e51149
3f871dd
 
 
 
 
8e51149
 
3f871dd
 
 
 
303fc54
 
 
3f871dd
8e51149
3f871dd
 
 
 
 
 
 
 
 
 
 
8e51149
 
 
 
3f871dd
 
 
 
8e51149
3f871dd
8e51149
3f871dd
8e51149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f871dd
 
 
 
 
 
 
8e51149
3f871dd
 
 
 
 
 
 
 
 
 
 
 
 
8e51149
3f871dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e51149
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gradio as gr
import torch
import speech_recognition as sr
from groq import Groq
import os
import tempfile
from gtts import gTTS
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Set device (CPU only for Hugging Face Spaces free tier)
device = torch.device("cpu")
logger.info(f"Using device: {device}")

# Groq API client with API key from Hugging Face Secrets
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    logger.error("GROQ_API_KEY environment variable not set")
    raise ValueError("GROQ_API_KEY environment variable not set")

try:
    client = Groq(api_key=GROQ_API_KEY)
    logger.info("Grok client initialized successfully")
except Exception as e:
    logger.error(f"Error initializing Groq client: {str(e)}")
    raise

# Functions
def predict_text_emotion(text):
    prompt = f"The user has entered text '{text}' classify user's emotion as happy or sad or anxious or angry. Respond in only one word."
    try:
        completion = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[{"role": "user", "content": prompt}],
            temperature=1,
            max_tokens=64,
            top_p=1,
            stream=False,
        )
        return completion.choices[0].message.content.strip().lower()
    except Exception as e:
        logger.error(f"Error with Groq API (text emotion): {str(e)}")
        return "neutral"

def transcribe_audio(audio_path):
    r = sr.Recognizer()
    try:
        with sr.AudioFile(audio_path) as source:
            audio_text = r.listen(source)
        text = r.recognize_google(audio_text)
        return text
    except sr.UnknownValueError:
        return "I didn’t catch that—could you try again?"
    except sr.RequestError as e:
        logger.error(f"Speech recognition error: {str(e)}")
        return "Speech recognition unavailable—try typing instead."
    except Exception as e:
        logger.error(f"Unexpected error in audio transcription: {str(e)}")
        return "Error processing audio."

def generate_response(user_input, emotion):
    prompt = f"The user is feeling {emotion}. They said: '{user_input}'. Respond in a friendly caring manner with the user so the user feels being loved."
    try:
        completion = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[{"role": "user", "content": prompt}],
            temperature=1,
            max_tokens=64,
            top_p=1,
            stream=False,
        )
        return completion.choices[0].message.content
    except Exception as e:
        logger.error(f"Error with Groq API (response generation): {str(e)}")
        return "I'm here for you, but something went wrong. How can I help?"

def text_to_speech(text):
    try:
        tts = gTTS(text=text, lang='en', slow=False)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
            tts.save(temp_audio.name)
            return temp_audio.name
    except Exception as e:
        logger.error(f"Error generating speech: {str(e)}")
        return None

# Chat function for Gradio with voice output
def chat_function(input_type, text_input, audio_input, chat_history):
    if input_type == "text" and text_input:
        user_input = text_input
    elif input_type == "voice" and audio_input:
        user_input = transcribe_audio(audio_input)
    else:
        return chat_history, "Please provide text or voice input.", gr.update(value=text_input), None

    emotion = predict_text_emotion(user_input)
    response = generate_response(user_input, emotion)
    
    chat_history = chat_history or []
    chat_history.append({"role": "user", "content": user_input})
    chat_history.append({"role": "assistant", "content": response})

    audio_output = text_to_speech(response)
    return chat_history, f"Detected Emotion: {emotion}", "", audio_output

# Custom CSS for styling
css = """
.chatbot .message-user {
    background-color: #e3f2fd;
    border-radius: 10px;
    padding: 10px;
    margin: 5px 0;
}
.chatbot .message-assistant {
    background-color: #c8e6c9;
    border-radius: 10px;
    padding: 10px;
    margin: 5px 0;
}
.input-container {
    padding: 10px;
    background-color: #f9f9f9;
    border-radius: 10px;
    margin-top: 10px;
}
"""

# Build the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
    gr.Markdown(
        """
        # Multimodal Mental Health AI Agent
        Chat with our empathetic AI designed to support you by understanding your emotions through text and voice.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            emotion_display = gr.Textbox(label="Emotion", interactive=False, placeholder="Detected emotion will appear here")
        
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(label="Conversation History", height=500, type="messages", elem_classes="chatbot")

    with gr.Row(elem_classes="input-container"):
        input_type = gr.Radio(["text", "voice"], label="Input Method", value="text")
        text_input = gr.Textbox(label="Type Your Message", placeholder="How are you feeling today?", visible=True)
        audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Record Your Message", visible=False)
        submit_btn = gr.Button("Send", variant="primary")
        clear_btn = gr.Button("Clear Chat", variant="secondary")
        audio_output = gr.Audio(label="Assistant Response", type="filepath", interactive=False, autoplay=True)

    # Dynamic visibility based on input type
    def update_visibility(input_type):
        return gr.update(visible=input_type == "text"), gr.update(visible=input_type == "voice")

    input_type.change(fn=update_visibility, inputs=input_type, outputs=[text_input, audio_input])

    # Submit action with voice output
    submit_btn.click(
        fn=chat_function,
        inputs=[input_type, text_input, audio_input, chatbot],
        outputs=[chatbot, emotion_display, text_input, audio_output]
    )

    # Clear chat and audio
    clear_btn.click(
        lambda: ([], "", "", None),
        inputs=None,
        outputs=[chatbot, emotion_display, text_input, audio_output]
    )

# Launch the app (commented out for Hugging Face Spaces)
# if __name__ == "__main__":
#     app.launch(server_name="0.0.0.0", server_port=7860)