welcometoFightclub commited on
Commit
303fc54
·
verified ·
1 Parent(s): cd7a05f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
  import speech_recognition as sr
3
  from groq import Groq
4
  import os
@@ -6,9 +8,15 @@ import time
6
  import base64
7
  from io import BytesIO
8
  from gtts import gTTS
 
9
 
10
  # Set device
 
 
11
 
 
 
 
12
 
13
  # Grok API client with API key (stored as environment variable for security)
14
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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") # Replace with your key or use env variable
@@ -113,10 +121,10 @@ def generate_response(user_input, emotion):
113
  def text_to_speech(text):
114
  try:
115
  tts = gTTS(text=text, lang='en', slow=False)
116
- audio_buffer = BytesIO()
117
- tts.write_to_fp(audio_buffer)
118
- audio_buffer.seek(0)
119
- return audio_buffer
120
  except Exception as e:
121
  print(f"Error generating speech: {str(e)}")
122
  return None
@@ -215,6 +223,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
215
  outputs=[chatbot, emotion_display, text_input, audio_output]
216
  )
217
 
218
- # Launch the app (for local testing; deployment will handle this differently)
219
  if __name__ == "__main__":
220
  app.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ import torch
3
+ import cv2
4
  import speech_recognition as sr
5
  from groq import Groq
6
  import os
 
8
  import base64
9
  from io import BytesIO
10
  from gtts import gTTS
11
+ import tempfile
12
 
13
  # Set device
14
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
15
+ print(f"Using device: {device}")
16
 
17
+ # Clear GPU memory if using GPU
18
+ if torch.cuda.is_available():
19
+ torch.cuda.empty_cache()
20
 
21
  # Grok API client with API key (stored as environment variable for security)
22
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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") # Replace with your key or use env variable
 
121
  def text_to_speech(text):
122
  try:
123
  tts = gTTS(text=text, lang='en', slow=False)
124
+ # Create a temporary file to store the audio
125
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
126
+ tts.save(temp_audio.name)
127
+ return temp_audio.name
128
  except Exception as e:
129
  print(f"Error generating speech: {str(e)}")
130
  return None
 
223
  outputs=[chatbot, emotion_display, text_input, audio_output]
224
  )
225
 
226
+ # Launch the app (for local testing)
227
  if __name__ == "__main__":
228
  app.launch(server_name="0.0.0.0", server_port=7860)