Umama-at-Bluchip commited on
Commit
6d57dc5
1 Parent(s): f7c91ce

Upload 4 files

Browse files
Files changed (4) hide show
  1. .env +1 -0
  2. app.py +51 -0
  3. requirements.txt +11 -0
  4. utils/functions.py +39 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ API_KEY='AIzaSyBeNMjIT8iPUcnO2zzu7hWkfJFRxFwQTUg'
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ import sounddevice as sd
4
+ from scipy.io.wavfile import write
5
+ import numpy as np
6
+ import soundfile as sf
7
+ from utils.functions import voice_to_text, get_gemini_response, text_to_audio
8
+ import pygame
9
+
10
+ def main():
11
+ st.title("Mental Health Chatbot")
12
+
13
+ if "messages" not in st.session_state:
14
+ st.session_state.messages = []
15
+
16
+ for message in st.session_state.messages:
17
+ with st.container():
18
+ if message["role"] == "user":
19
+ st.markdown(f":speech_balloon: **You:** {message['content']}")
20
+ else:
21
+ st.markdown(f":robot: **Chatbot:** {message['content']}")
22
+
23
+ if st.button("Speak"):
24
+ with st.spinner('Recording...'):
25
+ try:
26
+ fs = 44100
27
+ seconds = 5
28
+ myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
29
+ sd.wait()
30
+ audio_file = "user_audio.wav"
31
+ write(audio_file, fs, myrecording)
32
+
33
+ text_input = voice_to_text(audio_file)
34
+ if text_input != "Error during transcription":
35
+ st.session_state.messages.append({"role": "user", "content": text_input})
36
+ response = get_gemini_response(text_input)
37
+ if response:
38
+ st.session_state.messages.append({"role": "assistant", "content": response})
39
+ audio_array = text_to_audio(response)
40
+ bot_audio_file = "bot_audio.wav"
41
+ sf.write(bot_audio_file, audio_array, 44100)
42
+ pygame.mixer.init()
43
+ pygame.mixer.music.load(bot_audio_file)
44
+ pygame.mixer.music.play()
45
+ while pygame.mixer.music.get_busy():
46
+ time.sleep(1)
47
+ except Exception as e:
48
+ st.error(f"Error: {e}")
49
+
50
+ if __name__ == "__main__":
51
+ main()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ sounddevice
4
+ soundfile
5
+ pygame
6
+ torch
7
+ numpy
8
+ scipy
9
+ requests
10
+ google.generativeai
11
+ time
utils/functions.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ import requests
4
+ import json
5
+ import soundfile as sf
6
+ import os
7
+ import google.generativeai as genai
8
+
9
+
10
+ def voice_to_text(audio_data, model_name="openai/whisper-small", device="cuda" if torch.cuda.is_available() else "cpu"):
11
+
12
+ try:
13
+ model = pipeline("automatic-speech-recognition", model=model_name, device=device, trust_remote_code=True)
14
+ text = model(audio_data)["text"]
15
+ return text
16
+
17
+ except Exception as e:
18
+ return f"Error during transcription"
19
+
20
+
21
+ def get_gemini_response(text):
22
+
23
+ genai.configure(api_key=os.environ['API_KEY'])
24
+ model = genai.GenerativeModel('gemini-1.5-flash')
25
+
26
+ prompt = f"You are a compassionate and supportive mental health assistant. Provide helpful advice, encouragement, and information to the user. Respond in a warm and understanding tone. User: {text}"
27
+
28
+ response = model.generate_content(prompt)
29
+ return response.text
30
+
31
+
32
+
33
+ def text_to_audio(text):
34
+
35
+ device = "cuda" if torch.cuda.is_available() else "cpu"
36
+ model = pipeline("text-to-speech", model="parler-tts/parler_tts_mini_v0.1", device=device, trust_remote_code=True)
37
+
38
+ audio_array = model(text)["audio"]
39
+ return audio_array