Fluospark128 commited on
Commit
054fff2
·
verified ·
1 Parent(s): 85a0349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -45
app.py CHANGED
@@ -3,10 +3,9 @@ import gradio as gr
3
  import requests
4
  import json
5
  import speech_recognition as sr
 
6
  from tempfile import NamedTemporaryFile
7
  from dotenv import load_dotenv
8
- import torchaudio
9
- from transformers import pipeline
10
  import logging
11
 
12
  # Set up logging
@@ -18,7 +17,7 @@ load_dotenv()
18
 
19
  # Groq API setup
20
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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")
21
- GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768") # Options: llama3-8b, gemma-7b, mixtral-8x7b-32768
22
 
23
  GROQ_API_URL = f"https://api.groq.com/openai/v1/chat/completions"
24
 
@@ -29,14 +28,6 @@ headers = {
29
 
30
  logger.info(f"Groq API configured with model: {GROQ_MODEL}")
31
 
32
- # Load YarnGPT as text-to-speech pipeline
33
- try:
34
- tts_pipeline = pipeline("text-to-speech", model="saheedniyi/YarnGPT") # from_tf = True)
35
- logger.info("Text-to-speech pipeline loaded successfully")
36
- except Exception as e:
37
- logger.error(f"Error loading text-to-speech pipeline: {e}")
38
- raise
39
-
40
  # Emotion options with descriptions
41
  emotion_options = {
42
  "neutral": "Neutral or balanced mood",
@@ -51,7 +42,6 @@ emotion_options = {
51
 
52
  # Audio transcription function
53
  def transcribe_audio(audio_path):
54
- """Transcribe audio file to text using Google's speech recognition"""
55
  recognizer = sr.Recognizer()
56
  try:
57
  with sr.AudioFile(audio_path) as source:
@@ -69,17 +59,13 @@ def transcribe_audio(audio_path):
69
  logger.error(f"Unexpected error during transcription: {e}")
70
  return ""
71
 
72
- # Chat history to maintain conversation context
73
  conversation_history = []
74
 
75
-
76
- # Function to call Groq API
77
  def get_groq_response(prompt, conversation_messages=[]):
78
- """Call Groq API to get a response"""
79
- # Format messages for the Groq API (which uses OpenAI-compatible format)
80
  messages = [{"role": "system", "content": prompt}]
81
 
82
- # Add conversation history
83
  for message in conversation_messages:
84
  if message.startswith("User: "):
85
  messages.append({"role": "user", "content": message[6:]})
@@ -95,8 +81,7 @@ def get_groq_response(prompt, conversation_messages=[]):
95
 
96
  try:
97
  response = requests.post(GROQ_API_URL, headers=headers, json=data)
98
- response.raise_for_status() # Raise exception for HTTP errors
99
-
100
  result = response.json()
101
  return result["choices"][0]["message"]["content"]
102
  except requests.exceptions.RequestException as e:
@@ -105,12 +90,9 @@ def get_groq_response(prompt, conversation_messages=[]):
105
  logger.error(f"Response content: {e.response.text}")
106
  raise Exception(f"Failed to get response from Groq: {str(e)}")
107
 
108
- # Main AI interaction function
109
  def chat_with_ai(audio, text_input, emotion, history):
110
- """Process user input and generate AI response with voice"""
111
  global conversation_history
112
-
113
- # Get user text from either text input or audio transcription
114
  user_text = text_input or ""
115
 
116
  if audio:
@@ -124,13 +106,9 @@ def chat_with_ai(audio, text_input, emotion, history):
124
  if not user_text.strip():
125
  return "No input provided. Please type a message or speak clearly.", None, history
126
 
127
- # Update conversation history
128
  conversation_history.append(f"User: {user_text}")
129
-
130
- # Format conversation history for context (limit to last 10 exchanges)
131
  recent_messages = conversation_history[-20:]
132
-
133
- # Emotion-aware prompt
134
  system_prompt = f"""You are an empathetic AI assistant who provides supportive responses to users based on their emotional state.
135
  The user is currently feeling {emotion} ({emotion_options[emotion]}).
136
  Respond appropriately considering their emotional state.
@@ -138,33 +116,30 @@ Be supportive, empathetic, and helpful.
138
  Keep your responses concise and focused on helping the user."""
139
 
140
  try:
141
- # Call Groq API with the prepared messages
142
  ai_response = get_groq_response(system_prompt, recent_messages)
143
  logger.info(f"Generated AI response: {ai_response[:30]}...")
144
  except Exception as e:
145
  logger.error(f"Error generating AI response: {e}")
146
  return "Sorry, I encountered an error generating a response. Please try again.", None, history
147
 
148
- # Update conversation history
149
  conversation_history.append(f"AI: {ai_response}")
150
-
151
- # Limit history size
152
  if len(conversation_history) > 40:
153
  conversation_history = conversation_history[-40:]
154
 
155
- # Generate speech from text
156
  try:
157
- tts_output = tts_pipeline(ai_response)
158
  audio_path = NamedTemporaryFile(delete=False, suffix=".wav").name
159
- torchaudio.save(audio_path, tts_output["audio"], sample_rate=tts_output["sampling_rate"])
160
- logger.info("Text-to-speech generated successfully")
 
 
161
  except Exception as e:
162
  logger.error(f"Error generating speech: {e}")
163
  return ai_response, None, history + [[user_text, ai_response]]
164
 
165
  return ai_response, audio_path, history + [[user_text, ai_response]]
166
 
167
- # Function to clear conversation
168
  def clear_conversation():
169
  global conversation_history
170
  conversation_history = []
@@ -173,22 +148,19 @@ def clear_conversation():
173
  # Gradio interface
174
  with gr.Blocks(title="Mind AID AI Assistant") as iface:
175
  gr.Markdown("# Mind AID: Emotion-Aware Conversational AI")
176
- gr.Markdown(f"Using Groq's {GROQ_MODEL} model for AI responses and YarnGPT for voice synthesis")
177
  gr.Markdown("Talk or type to the AI assistant. Your emotional state helps tailor the response.")
178
 
179
  with gr.Row():
180
  with gr.Column(scale=3):
181
- # Emotion selection with dropdown and descriptions
182
  emotion = gr.Dropdown(
183
  label="How are you feeling right now?",
184
  choices=list(emotion_options.keys()),
185
  value="neutral",
186
  type="value"
187
  )
188
-
189
  emotion_description = gr.Markdown("**Current mood:** Neutral or balanced mood")
190
 
191
- # Update the emotion description when dropdown changes
192
  def update_emotion_description(emotion_value):
193
  return f"**Current mood:** {emotion_options.get(emotion_value, 'Unknown')}"
194
 
@@ -232,7 +204,6 @@ with gr.Blocks(title="Mind AID AI Assistant") as iface:
232
  outputs=[status_box, output_audio, chat_history]
233
  )
234
 
235
- # Also allow enter key to submit
236
  text_input.submit(
237
  fn=chat_with_ai,
238
  inputs=[audio_input, text_input, emotion, chat_history],
@@ -245,10 +216,10 @@ with gr.Blocks(title="Mind AID AI Assistant") as iface:
245
  outputs=[chat_history, audio_input, text_input, status_box]
246
  )
247
 
248
- # Launch the application
249
  if __name__ == "__main__":
250
  try:
251
  logger.info("Starting Mind AID application with Groq API integration")
252
- iface.launch(share=True) # share=True creates a public link
253
  except Exception as e:
254
  logger.error(f"Error launching Gradio interface: {e}")
 
3
  import requests
4
  import json
5
  import speech_recognition as sr
6
+ import pyttsx3
7
  from tempfile import NamedTemporaryFile
8
  from dotenv import load_dotenv
 
 
9
  import logging
10
 
11
  # Set up logging
 
17
 
18
  # Groq API setup
19
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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")
20
+ GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768")
21
 
22
  GROQ_API_URL = f"https://api.groq.com/openai/v1/chat/completions"
23
 
 
28
 
29
  logger.info(f"Groq API configured with model: {GROQ_MODEL}")
30
 
 
 
 
 
 
 
 
 
31
  # Emotion options with descriptions
32
  emotion_options = {
33
  "neutral": "Neutral or balanced mood",
 
42
 
43
  # Audio transcription function
44
  def transcribe_audio(audio_path):
 
45
  recognizer = sr.Recognizer()
46
  try:
47
  with sr.AudioFile(audio_path) as source:
 
59
  logger.error(f"Unexpected error during transcription: {e}")
60
  return ""
61
 
62
+ # Chat history
63
  conversation_history = []
64
 
65
+ # Call Groq API
 
66
  def get_groq_response(prompt, conversation_messages=[]):
 
 
67
  messages = [{"role": "system", "content": prompt}]
68
 
 
69
  for message in conversation_messages:
70
  if message.startswith("User: "):
71
  messages.append({"role": "user", "content": message[6:]})
 
81
 
82
  try:
83
  response = requests.post(GROQ_API_URL, headers=headers, json=data)
84
+ response.raise_for_status()
 
85
  result = response.json()
86
  return result["choices"][0]["message"]["content"]
87
  except requests.exceptions.RequestException as e:
 
90
  logger.error(f"Response content: {e.response.text}")
91
  raise Exception(f"Failed to get response from Groq: {str(e)}")
92
 
93
+ # Main function
94
  def chat_with_ai(audio, text_input, emotion, history):
 
95
  global conversation_history
 
 
96
  user_text = text_input or ""
97
 
98
  if audio:
 
106
  if not user_text.strip():
107
  return "No input provided. Please type a message or speak clearly.", None, history
108
 
 
109
  conversation_history.append(f"User: {user_text}")
 
 
110
  recent_messages = conversation_history[-20:]
111
+
 
112
  system_prompt = f"""You are an empathetic AI assistant who provides supportive responses to users based on their emotional state.
113
  The user is currently feeling {emotion} ({emotion_options[emotion]}).
114
  Respond appropriately considering their emotional state.
 
116
  Keep your responses concise and focused on helping the user."""
117
 
118
  try:
 
119
  ai_response = get_groq_response(system_prompt, recent_messages)
120
  logger.info(f"Generated AI response: {ai_response[:30]}...")
121
  except Exception as e:
122
  logger.error(f"Error generating AI response: {e}")
123
  return "Sorry, I encountered an error generating a response. Please try again.", None, history
124
 
 
125
  conversation_history.append(f"AI: {ai_response}")
 
 
126
  if len(conversation_history) > 40:
127
  conversation_history = conversation_history[-40:]
128
 
129
+ # Generate speech using pyttsx3
130
  try:
 
131
  audio_path = NamedTemporaryFile(delete=False, suffix=".wav").name
132
+ engine = pyttsx3.init()
133
+ engine.save_to_file(ai_response, audio_path)
134
+ engine.runAndWait()
135
+ logger.info("Text-to-speech generated successfully (offline)")
136
  except Exception as e:
137
  logger.error(f"Error generating speech: {e}")
138
  return ai_response, None, history + [[user_text, ai_response]]
139
 
140
  return ai_response, audio_path, history + [[user_text, ai_response]]
141
 
142
+ # Clear conversation
143
  def clear_conversation():
144
  global conversation_history
145
  conversation_history = []
 
148
  # Gradio interface
149
  with gr.Blocks(title="Mind AID AI Assistant") as iface:
150
  gr.Markdown("# Mind AID: Emotion-Aware Conversational AI")
151
+ gr.Markdown(f"Using Groq's {GROQ_MODEL} model for AI responses and offline TTS with pyttsx3")
152
  gr.Markdown("Talk or type to the AI assistant. Your emotional state helps tailor the response.")
153
 
154
  with gr.Row():
155
  with gr.Column(scale=3):
 
156
  emotion = gr.Dropdown(
157
  label="How are you feeling right now?",
158
  choices=list(emotion_options.keys()),
159
  value="neutral",
160
  type="value"
161
  )
 
162
  emotion_description = gr.Markdown("**Current mood:** Neutral or balanced mood")
163
 
 
164
  def update_emotion_description(emotion_value):
165
  return f"**Current mood:** {emotion_options.get(emotion_value, 'Unknown')}"
166
 
 
204
  outputs=[status_box, output_audio, chat_history]
205
  )
206
 
 
207
  text_input.submit(
208
  fn=chat_with_ai,
209
  inputs=[audio_input, text_input, emotion, chat_history],
 
216
  outputs=[chat_history, audio_input, text_input, status_box]
217
  )
218
 
219
+ # Launch the app
220
  if __name__ == "__main__":
221
  try:
222
  logger.info("Starting Mind AID application with Groq API integration")
223
+ iface.launch(share=True)
224
  except Exception as e:
225
  logger.error(f"Error launching Gradio interface: {e}")