mgbam commited on
Commit
9a4c62a
·
verified ·
1 Parent(s): 0790420

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -98,12 +98,11 @@ async def websocket_endpoint(ws: WebSocket):
98
  del sessions[stream_sid]
99
  print(f"Session cleaned up for stream {stream_sid}")
100
 
101
- # THIS IS THE CORRECTED FUNCTION FROM THE PREVIOUS STEP, NOW IN THE FULL FILE
102
  async def process_user_audio(ws: WebSocket, stream_sid: str, audio_chunk: np.ndarray):
103
  """The main logic loop: STT -> LLM -> (Tool/TTS)"""
104
  print(f"[{stream_sid}] Processing audio chunk of size {len(audio_chunk)}...")
105
 
106
- # 1. Speech-to-Text
107
  user_text = await transcribe_audio_chunk(audio_chunk)
108
  if not user_text:
109
  print(f"[{stream_sid}] No text transcribed.")
@@ -122,7 +121,6 @@ async def process_user_audio(ws: WebSocket, stream_sid: str, audio_chunk: np.nda
122
  if chunk is None: break
123
  yield chunk
124
 
125
- # 2. Start LLM and TTS tasks concurrently
126
  llm_task = asyncio.create_task(get_llm_response(sessions[stream_sid]["messages"], llm_chunk_handler))
127
  tts_task = asyncio.create_task(stream_and_send_audio(ws, stream_sid, tts_text_iterator()))
128
 
@@ -133,13 +131,10 @@ async def process_user_audio(ws: WebSocket, stream_sid: str, audio_chunk: np.nda
133
  if assistant_message and assistant_message.get("content"):
134
  sessions[stream_sid]["messages"].append(assistant_message)
135
 
136
- # 3. Handle Tool Calls if any
137
  if tool_calls:
138
- # Before executing, add the assistant's tool request to history
139
  sessions[stream_sid]["messages"].append(assistant_message)
140
 
141
  for tool_call_data in tool_calls:
142
- # Recreate a simple object that mimics the structure tool_handler expects
143
  tool_call = type('ToolCall', (), {
144
  'id': tool_call_data.get('id'),
145
  'function': type('Function', (), tool_call_data.get('function'))
@@ -149,7 +144,6 @@ async def process_user_audio(ws: WebSocket, stream_sid: str, audio_chunk: np.nda
149
  tool_result_message = execute_tool_call(tool_call)
150
  sessions[stream_sid]["messages"].append(tool_result_message)
151
 
152
- # 4. Get a final response from the LLM after executing the tool
153
  final_tts_queue = asyncio.Queue()
154
  async def final_llm_chunk_handler(chunk): await final_tts_queue.put(chunk)
155
  async def final_tts_iterator():
@@ -186,5 +180,7 @@ async def stream_and_send_audio(ws: WebSocket, stream_sid: str, text_iterator):
186
 
187
  if __name__ == "__main__":
188
  import uvicorn
189
- print("Starting RentBot server...")
190
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
 
98
  del sessions[stream_sid]
99
  print(f"Session cleaned up for stream {stream_sid}")
100
 
101
+
102
  async def process_user_audio(ws: WebSocket, stream_sid: str, audio_chunk: np.ndarray):
103
  """The main logic loop: STT -> LLM -> (Tool/TTS)"""
104
  print(f"[{stream_sid}] Processing audio chunk of size {len(audio_chunk)}...")
105
 
 
106
  user_text = await transcribe_audio_chunk(audio_chunk)
107
  if not user_text:
108
  print(f"[{stream_sid}] No text transcribed.")
 
121
  if chunk is None: break
122
  yield chunk
123
 
 
124
  llm_task = asyncio.create_task(get_llm_response(sessions[stream_sid]["messages"], llm_chunk_handler))
125
  tts_task = asyncio.create_task(stream_and_send_audio(ws, stream_sid, tts_text_iterator()))
126
 
 
131
  if assistant_message and assistant_message.get("content"):
132
  sessions[stream_sid]["messages"].append(assistant_message)
133
 
 
134
  if tool_calls:
 
135
  sessions[stream_sid]["messages"].append(assistant_message)
136
 
137
  for tool_call_data in tool_calls:
 
138
  tool_call = type('ToolCall', (), {
139
  'id': tool_call_data.get('id'),
140
  'function': type('Function', (), tool_call_data.get('function'))
 
144
  tool_result_message = execute_tool_call(tool_call)
145
  sessions[stream_sid]["messages"].append(tool_result_message)
146
 
 
147
  final_tts_queue = asyncio.Queue()
148
  async def final_llm_chunk_handler(chunk): await final_tts_queue.put(chunk)
149
  async def final_tts_iterator():
 
180
 
181
  if __name__ == "__main__":
182
  import uvicorn
183
+ # Hugging Face Spaces expects the app to run on port 7860
184
+ port = int(os.environ.get("PORT", 7860))
185
+ print(f"Starting RentBot server on port {port}...")
186
+ uvicorn.run(app, host="0.0.0.0", port=port)