mclemcrew commited on
Commit
e3d7dfa
·
1 Parent(s): d11be63

updates for messaging

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -119,14 +119,23 @@ def generate_response(audio_path, message, chat_history=None):
119
  system_prompt = "You are an expert audio engineer assisting with music production and mixing. Provide clear, specific advice on audio engineering techniques, mixing adjustments, and production decisions based on the audio samples and the user's questions. Focus on practical, actionable guidance."
120
  conversation.append({"role": "system", "content": system_prompt})
121
 
 
 
 
122
  if chat_history:
123
  history_limit = min(len(chat_history), 3)
124
- for user_msg, bot_msg in chat_history[-history_limit:]:
 
 
 
 
 
125
  if user_msg: # Skip None entries
126
  conversation.append({"role": "user", "content": user_msg})
127
  if bot_msg and bot_msg != "⏳ Generating response, please wait...":
128
  conversation.append({"role": "assistant", "content": bot_msg})
129
 
 
130
  if audio_path:
131
  # Process the audio first to ensure it's valid
132
  audio_data = process_audio_file(audio_path)
@@ -154,11 +163,32 @@ def generate_response(audio_path, message, chat_history=None):
154
  else:
155
  conversation.append({"role": "user", "content": message})
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  logger.info("Applying chat template")
158
  text = processor.apply_chat_template(
159
  conversation, add_generation_prompt=True, tokenize=False
160
  )
161
 
 
 
 
162
  logger.info("Processing inputs")
163
  inputs = processor(
164
  text=text,
@@ -284,25 +314,30 @@ def create_interface():
284
  # Add user message to history
285
  chat_history.append((message, None))
286
 
287
- # Add a loading message directly in the chat
 
 
 
288
  loading_msg = "⏳ Generating response, please wait..."
289
- chat_history.append((None, loading_msg))
290
- yield chat_history, "", gr.update(value="*Processing audio analysis...*")
291
 
292
  try:
293
- # Generate response
294
- response = generate_response(audio_path, message, chat_history[:-1])
 
 
 
295
 
296
- # Remove the loading message and add the real response
297
- chat_history.pop() # Remove loading message
298
  chat_history[-1] = (message, response)
299
  yield chat_history, "", gr.update(value="*✅ Audio loaded successfully! You can start chatting now.*")
300
  except Exception as e:
301
- # Remove the loading message and add the error
302
- chat_history.pop() # Remove loading message
303
  chat_history[-1] = (message, f"Error: {str(e)}")
304
  yield chat_history, "", gr.update(value="*✅ Audio loaded successfully! You can start chatting now.*")
305
-
306
  # Connect submit button
307
  submit_btn.click(
308
  chat_response,
 
119
  system_prompt = "You are an expert audio engineer assisting with music production and mixing. Provide clear, specific advice on audio engineering techniques, mixing adjustments, and production decisions based on the audio samples and the user's questions. Focus on practical, actionable guidance."
120
  conversation.append({"role": "system", "content": system_prompt})
121
 
122
+ # Log the incoming chat history for debugging
123
+ logger.info(f"Chat history received: {chat_history}")
124
+
125
  if chat_history:
126
  history_limit = min(len(chat_history), 3)
127
+ # Skip the last entry if it's the loading message
128
+ actual_history = chat_history
129
+ if chat_history and chat_history[-1][1] == "⏳ Generating response, please wait...":
130
+ actual_history = chat_history[:-1]
131
+
132
+ for i, (user_msg, bot_msg) in enumerate(actual_history[-history_limit:]):
133
  if user_msg: # Skip None entries
134
  conversation.append({"role": "user", "content": user_msg})
135
  if bot_msg and bot_msg != "⏳ Generating response, please wait...":
136
  conversation.append({"role": "assistant", "content": bot_msg})
137
 
138
+ # Add the current message
139
  if audio_path:
140
  # Process the audio first to ensure it's valid
141
  audio_data = process_audio_file(audio_path)
 
163
  else:
164
  conversation.append({"role": "user", "content": message})
165
 
166
+ # Log the full conversation being sent to the model
167
+ safe_conversation = []
168
+ for msg in conversation:
169
+ if isinstance(msg["content"], list):
170
+ # For messages with audio, just indicate audio presence
171
+ content_copy = []
172
+ for item in msg["content"]:
173
+ if item["type"] == "audio":
174
+ content_copy.append({"type": "audio", "audio_present": True})
175
+ else:
176
+ content_copy.append(item)
177
+ safe_msg = {"role": msg["role"], "content": content_copy}
178
+ else:
179
+ safe_msg = msg.copy()
180
+ safe_conversation.append(safe_msg)
181
+
182
+ logger.info(f"Full conversation being sent to model: {safe_conversation}")
183
+
184
  logger.info("Applying chat template")
185
  text = processor.apply_chat_template(
186
  conversation, add_generation_prompt=True, tokenize=False
187
  )
188
 
189
+ # Log the templated conversation for debugging
190
+ logger.info(f"Templated conversation (truncated): {text[:300]}...")
191
+
192
  logger.info("Processing inputs")
193
  inputs = processor(
194
  text=text,
 
314
  # Add user message to history
315
  chat_history.append((message, None))
316
 
317
+ # Create a copy of chat history for display purposes
318
+ display_history = chat_history.copy()
319
+
320
+ # Add a loading message directly in the chat (only for display)
321
  loading_msg = "⏳ Generating response, please wait..."
322
+ display_history.append((None, loading_msg))
323
+ yield display_history, "", gr.update(value="*Processing audio analysis...*")
324
 
325
  try:
326
+ # Log the chat history for debugging
327
+ logger.info(f"Sending chat history to generate_response: {chat_history}")
328
+
329
+ # Generate response using the actual history (without loading message)
330
+ response = generate_response(audio_path, message, chat_history)
331
 
332
+ # Update the real chat history with the response
 
333
  chat_history[-1] = (message, response)
334
  yield chat_history, "", gr.update(value="*✅ Audio loaded successfully! You can start chatting now.*")
335
  except Exception as e:
336
+ # Update with error
337
+ logger.error(f"Error in chat_response: {str(e)}")
338
  chat_history[-1] = (message, f"Error: {str(e)}")
339
  yield chat_history, "", gr.update(value="*✅ Audio loaded successfully! You can start chatting now.*")
340
+
341
  # Connect submit button
342
  submit_btn.click(
343
  chat_response,