Fluospark128 commited on
Commit
125beff
·
verified ·
1 Parent(s): 8f3a5ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -4,15 +4,14 @@ import google.generativeai as genai
4
  import speech_recognition as sr
5
  from tempfile import NamedTemporaryFile
6
  from dotenv import load_dotenv
7
- import torch
8
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
9
 
10
- # Load environment variables
11
  load_dotenv()
12
- #API_KEY = "AIzaSyB3N9BHeIWs_8sdFK76PU-v9N6prcIq2Hw" #os.getenv("API_KEY") # or hardcode as "your_gemini_api_key"
13
 
14
  # Configure Gemini
15
- genai.configure(api_key="AIzaSyB3N9BHeIWs_8sdFK76PU-v9N6prcIq2Hw") #=API_KEY)
16
  gemini = genai.GenerativeModel("gemini-1.5-pro")
17
 
18
  # Load YarnGPT as text-to-speech pipeline
@@ -28,15 +27,13 @@ def transcribe_audio(audio_path):
28
  audio_data = recognizer.record(source)
29
  try:
30
  return recognizer.recognize_google(audio_data)
31
- except sr.UnknownValueError:
32
- return ""
33
- except sr.RequestError:
34
  return ""
35
 
36
  # Main AI interaction
37
  def chat_with_ai(audio, text_input, emotion):
38
  user_text = text_input or ""
39
-
40
  if audio:
41
  transcribed = transcribe_audio(audio)
42
  if transcribed:
@@ -47,14 +44,16 @@ def chat_with_ai(audio, text_input, emotion):
47
  if not user_text.strip():
48
  return "No input provided.", None
49
 
50
- # Prepare emotion-aware prompt
51
- prompt = f"The user is feeling {emotion}. Respond appropriately to help them feel better.\nUser said: {user_text}"
52
  ai_response = gemini.generate_content(prompt).text
53
 
54
- # Generate audio using YarnGPT
55
- tts_output = tts_pipeline(ai_response)
56
- audio_path = NamedTemporaryFile(delete=False, suffix=".wav").name
57
- torchaudio.save(audio_path, tts_output["audio"], sample_rate=tts_output["sampling_rate"])
 
 
58
 
59
  return ai_response, audio_path
60
 
@@ -64,7 +63,7 @@ with gr.Blocks(title="Mind AID AI Assistant") as iface:
64
 
65
  with gr.Row():
66
  emotion = gr.Dropdown(label="Select Your Emotional State", choices=emotion_options, value="neutral")
67
-
68
  with gr.Row():
69
  text_input = gr.Textbox(label="Or type your message here (optional)", lines=2)
70
  audio_input = gr.Audio(type="filepath", label="Or speak to the AI")
 
4
  import speech_recognition as sr
5
  from tempfile import NamedTemporaryFile
6
  from dotenv import load_dotenv
7
+ import torchaudio # <-- Needed to save audio
8
+ from transformers import pipeline
9
 
10
+ # Load environment variables (if using .env file)
11
  load_dotenv()
 
12
 
13
  # Configure Gemini
14
+ genai.configure(api_key="AIzaSyB3N9BHeIWs_8sdFK76PU-v9N6prcIq2Hw")
15
  gemini = genai.GenerativeModel("gemini-1.5-pro")
16
 
17
  # Load YarnGPT as text-to-speech pipeline
 
27
  audio_data = recognizer.record(source)
28
  try:
29
  return recognizer.recognize_google(audio_data)
30
+ except (sr.UnknownValueError, sr.RequestError):
 
 
31
  return ""
32
 
33
  # Main AI interaction
34
  def chat_with_ai(audio, text_input, emotion):
35
  user_text = text_input or ""
36
+
37
  if audio:
38
  transcribed = transcribe_audio(audio)
39
  if transcribed:
 
44
  if not user_text.strip():
45
  return "No input provided.", None
46
 
47
+ # Emotion-aware prompt
48
+ prompt = f"The user is feeling {emotion}. Respond supportively and help them feel better.\nUser said: {user_text}"
49
  ai_response = gemini.generate_content(prompt).text
50
 
51
+ try:
52
+ tts_output = tts_pipeline(ai_response)
53
+ audio_path = NamedTemporaryFile(delete=False, suffix=".wav").name
54
+ torchaudio.save(audio_path, tts_output["audio"], sample_rate=tts_output["sampling_rate"])
55
+ except Exception as e:
56
+ return ai_response, None
57
 
58
  return ai_response, audio_path
59
 
 
63
 
64
  with gr.Row():
65
  emotion = gr.Dropdown(label="Select Your Emotional State", choices=emotion_options, value="neutral")
66
+
67
  with gr.Row():
68
  text_input = gr.Textbox(label="Or type your message here (optional)", lines=2)
69
  audio_input = gr.Audio(type="filepath", label="Or speak to the AI")