Spaces:
Running
Running
Commit
·
417ea6c
1
Parent(s):
b971859
Add 89999999999999999999999999999
Browse files- app.py +47 -12
- groq_websocket_handler.py +17 -3
app.py
CHANGED
|
@@ -226,33 +226,68 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 226 |
@app.websocket("/ws/stream")
|
| 227 |
async def websocket_stream_endpoint(websocket: WebSocket):
|
| 228 |
"""
|
| 229 |
-
Enhanced WebSocket endpoint with
|
| 230 |
-
|
| 231 |
"""
|
| 232 |
from groq_websocket_handler import groq_websocket_handler
|
| 233 |
import json
|
|
|
|
| 234 |
|
| 235 |
# Accept connection and get session ID
|
| 236 |
session_id = await groq_websocket_handler.connect(websocket)
|
| 237 |
|
|
|
|
|
|
|
|
|
|
| 238 |
try:
|
| 239 |
while True:
|
| 240 |
-
# Receive message from client
|
| 241 |
-
message_text = await websocket.receive_text()
|
| 242 |
-
|
| 243 |
try:
|
|
|
|
|
|
|
| 244 |
message = json.loads(message_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
except json.JSONDecodeError:
|
| 246 |
-
await
|
| 247 |
"type": "error",
|
| 248 |
-
"message": "Invalid JSON message"
|
| 249 |
-
"timestamp": datetime.now().isoformat()
|
| 250 |
})
|
| 251 |
continue
|
| 252 |
-
|
| 253 |
-
# Handle different message types
|
| 254 |
-
await groq_websocket_handler.handle_stream_message(websocket, session_id, message)
|
| 255 |
-
|
| 256 |
except Exception as e:
|
| 257 |
logger.error(f"❌ WebSocket stream error: {e}")
|
| 258 |
finally:
|
|
|
|
| 226 |
@app.websocket("/ws/stream")
|
| 227 |
async def websocket_stream_endpoint(websocket: WebSocket):
|
| 228 |
"""
|
| 229 |
+
Enhanced WebSocket endpoint compatible with friend's frontend format
|
| 230 |
+
Handles both JSON audio format and friend's JSON+Binary format
|
| 231 |
"""
|
| 232 |
from groq_websocket_handler import groq_websocket_handler
|
| 233 |
import json
|
| 234 |
+
import base64
|
| 235 |
|
| 236 |
# Accept connection and get session ID
|
| 237 |
session_id = await groq_websocket_handler.connect(websocket)
|
| 238 |
|
| 239 |
+
# Send connection successful message (friend's format)
|
| 240 |
+
await websocket.send_json({"type": "connection_successful"})
|
| 241 |
+
|
| 242 |
try:
|
| 243 |
while True:
|
|
|
|
|
|
|
|
|
|
| 244 |
try:
|
| 245 |
+
# Try to receive JSON message first
|
| 246 |
+
message_text = await websocket.receive_text()
|
| 247 |
message = json.loads(message_text)
|
| 248 |
+
|
| 249 |
+
# Check if this is friend's format with lang field
|
| 250 |
+
if "lang" in message and "type" not in message:
|
| 251 |
+
logger.info(f"📱 Received friend's format: {message}")
|
| 252 |
+
|
| 253 |
+
# This is friend's format - expect binary audio next
|
| 254 |
+
try:
|
| 255 |
+
# Receive the binary audio data
|
| 256 |
+
audio_bytes = await websocket.receive_bytes()
|
| 257 |
+
logger.info(f"🎤 Received {len(audio_bytes)} bytes of audio data")
|
| 258 |
+
|
| 259 |
+
# Convert to base64 for internal processing
|
| 260 |
+
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 261 |
+
|
| 262 |
+
# Create standard message format for internal processing
|
| 263 |
+
standard_message = {
|
| 264 |
+
"type": "audio_data",
|
| 265 |
+
"language": "en" if message.get("lang", "").lower().startswith("eng") else message.get("lang", "en"),
|
| 266 |
+
"audio_data": audio_base64,
|
| 267 |
+
"user_id": message.get("user_id")
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
# Process using standard handler
|
| 271 |
+
await groq_websocket_handler.handle_stream_message(websocket, session_id, standard_message)
|
| 272 |
+
|
| 273 |
+
except Exception as audio_error:
|
| 274 |
+
logger.error(f"❌ Error receiving binary audio: {audio_error}")
|
| 275 |
+
await websocket.send_json({
|
| 276 |
+
"type": "error",
|
| 277 |
+
"message": "Failed to receive audio data"
|
| 278 |
+
})
|
| 279 |
+
|
| 280 |
+
else:
|
| 281 |
+
# This is standard format - process normally
|
| 282 |
+
await groq_websocket_handler.handle_stream_message(websocket, session_id, message)
|
| 283 |
+
|
| 284 |
except json.JSONDecodeError:
|
| 285 |
+
await websocket.send_json({
|
| 286 |
"type": "error",
|
| 287 |
+
"message": "Invalid JSON message"
|
|
|
|
| 288 |
})
|
| 289 |
continue
|
| 290 |
+
|
|
|
|
|
|
|
|
|
|
| 291 |
except Exception as e:
|
| 292 |
logger.error(f"❌ WebSocket stream error: {e}")
|
| 293 |
finally:
|
groq_websocket_handler.py
CHANGED
|
@@ -159,7 +159,7 @@ class GroqWebSocketHandler:
|
|
| 159 |
})
|
| 160 |
return
|
| 161 |
|
| 162 |
-
# Send transcription result
|
| 163 |
await self.send_message(session_id, {
|
| 164 |
"type": "transcription_complete",
|
| 165 |
"transcribed_text": transcribed_text,
|
|
@@ -168,6 +168,12 @@ class GroqWebSocketHandler:
|
|
| 168 |
"timestamp": time.time()
|
| 169 |
})
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
# Process the transcribed query
|
| 172 |
await self._process_transcribed_query(websocket, session_id, transcribed_text, user_language)
|
| 173 |
|
|
@@ -229,16 +235,24 @@ class GroqWebSocketHandler:
|
|
| 229 |
|
| 230 |
processing_time = time.time() - processing_start
|
| 231 |
|
| 232 |
-
# Send response
|
|
|
|
|
|
|
| 233 |
await self.send_message(session_id, {
|
| 234 |
"type": "response_complete",
|
| 235 |
-
"response":
|
| 236 |
"sources": response_data.get("sources", []),
|
| 237 |
"processing_time": processing_time,
|
| 238 |
"query_context": query_context,
|
| 239 |
"timestamp": time.time()
|
| 240 |
})
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
# Update conversation history
|
| 243 |
if session_id in self.user_sessions:
|
| 244 |
self.user_sessions[session_id]["conversation_history"].append({
|
|
|
|
| 159 |
})
|
| 160 |
return
|
| 161 |
|
| 162 |
+
# Send transcription result (both formats for compatibility)
|
| 163 |
await self.send_message(session_id, {
|
| 164 |
"type": "transcription_complete",
|
| 165 |
"transcribed_text": transcribed_text,
|
|
|
|
| 168 |
"timestamp": time.time()
|
| 169 |
})
|
| 170 |
|
| 171 |
+
# Also send friend's format
|
| 172 |
+
await self.send_message(session_id, {
|
| 173 |
+
"type": "transcription",
|
| 174 |
+
"text": transcribed_text
|
| 175 |
+
})
|
| 176 |
+
|
| 177 |
# Process the transcribed query
|
| 178 |
await self._process_transcribed_query(websocket, session_id, transcribed_text, user_language)
|
| 179 |
|
|
|
|
| 235 |
|
| 236 |
processing_time = time.time() - processing_start
|
| 237 |
|
| 238 |
+
# Send response (both formats for compatibility)
|
| 239 |
+
response_text = response_data.get("response", "I couldn't generate a response.")
|
| 240 |
+
|
| 241 |
await self.send_message(session_id, {
|
| 242 |
"type": "response_complete",
|
| 243 |
+
"response": response_text,
|
| 244 |
"sources": response_data.get("sources", []),
|
| 245 |
"processing_time": processing_time,
|
| 246 |
"query_context": query_context,
|
| 247 |
"timestamp": time.time()
|
| 248 |
})
|
| 249 |
|
| 250 |
+
# Also send friend's format
|
| 251 |
+
await self.send_message(session_id, {
|
| 252 |
+
"type": "llm_response",
|
| 253 |
+
"text": response_text
|
| 254 |
+
})
|
| 255 |
+
|
| 256 |
# Update conversation history
|
| 257 |
if session_id in self.user_sessions:
|
| 258 |
self.user_sessions[session_id]["conversation_history"].append({
|