Spaces:
Sleeping
Sleeping
Commit
·
e85255d
1
Parent(s):
b46da1b
change in main.py to add a more detailed log output
Browse files
main.py
CHANGED
|
@@ -101,7 +101,7 @@ You are an expert AI assistant for a premier real estate developer.
|
|
| 101 |
"""
|
| 102 |
|
| 103 |
|
| 104 |
-
# ---
|
| 105 |
def transcribe_audio(audio_path: str, audio_bytes: bytes) -> str:
|
| 106 |
for attempt in range(3):
|
| 107 |
try:
|
|
@@ -129,7 +129,7 @@ def transcribe_audio(audio_path: str, audio_bytes: bytes) -> str:
|
|
| 129 |
return text
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
-
logging.error(f"Transcription error (attempt {attempt+1}): {e}")
|
| 133 |
if attempt == 2:
|
| 134 |
return ""
|
| 135 |
return ""
|
|
@@ -145,13 +145,14 @@ def generate_elevenlabs_sync(text: str, voice: str) -> bytes:
|
|
| 145 |
output_format="mp3_44100_128"
|
| 146 |
)
|
| 147 |
except Exception as e:
|
| 148 |
-
logging.error(f"ElevenLabs error (attempt {attempt+1}): {e}")
|
| 149 |
if attempt == 2:
|
| 150 |
return b''
|
| 151 |
return b''
|
| 152 |
|
| 153 |
-
|
| 154 |
async def formulate_search_plan(user_query: str) -> dict:
|
|
|
|
| 155 |
for attempt in range(3):
|
| 156 |
try:
|
| 157 |
response = await run_in_threadpool(
|
|
@@ -161,13 +162,24 @@ async def formulate_search_plan(user_query: str) -> dict:
|
|
| 161 |
response_format={"type": "json_object"},
|
| 162 |
temperature=0.0
|
| 163 |
)
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
except Exception as e:
|
| 166 |
-
|
|
|
|
| 167 |
if attempt == 2:
|
|
|
|
| 168 |
return {"search_query": user_query, "filter_table": None}
|
|
|
|
|
|
|
| 169 |
return {"search_query": user_query, "filter_table": None}
|
| 170 |
-
|
| 171 |
|
| 172 |
async def get_agent_response(user_text: str) -> str:
|
| 173 |
for attempt in range(3):
|
|
@@ -197,7 +209,7 @@ async def get_agent_response(user_text: str) -> str:
|
|
| 197 |
)
|
| 198 |
return response.choices[0].message.content.strip()
|
| 199 |
except Exception as e:
|
| 200 |
-
logging.error(f"RAG error (attempt {attempt+1}): {e}")
|
| 201 |
if attempt == 2:
|
| 202 |
return "Sorry, I couldn't respond. Please try again."
|
| 203 |
return "Sorry, I couldn't respond."
|
|
@@ -220,7 +232,7 @@ async def test_text_query_endpoint(query: TextQuery):
|
|
| 220 |
return {"response": response}
|
| 221 |
|
| 222 |
|
| 223 |
-
# ---
|
| 224 |
async def process_audio(audio_path):
|
| 225 |
if not audio_path or not os.path.exists(audio_path):
|
| 226 |
return None, "No valid audio file received."
|
|
@@ -262,7 +274,7 @@ async def process_audio(audio_path):
|
|
| 262 |
return out_path, f"**You:** {user_text}\n\n**AI:** {agent_response}"
|
| 263 |
|
| 264 |
except Exception as e:
|
| 265 |
-
logging.error(f"Audio processing error: {e}", exc_info=True)
|
| 266 |
return None, f"Error: {str(e)}"
|
| 267 |
|
| 268 |
|
|
@@ -277,13 +289,9 @@ with gr.Blocks(title="Real Estate AI") as demo:
|
|
| 277 |
|
| 278 |
out_text = gr.Textbox(label="Conversation", lines=8)
|
| 279 |
|
| 280 |
-
# Only trigger on real file (not example text)
|
| 281 |
inp.change(process_audio, inp, [out_audio, out_text])
|
| 282 |
|
| 283 |
-
#
|
| 284 |
-
# Remove text examples to avoid FileNotFoundError
|
| 285 |
-
# Or: Record real .wav files and upload to repo
|
| 286 |
-
# For now: disable examples
|
| 287 |
# gr.Examples(examples=[], inputs=inp)
|
| 288 |
|
| 289 |
|
|
|
|
| 101 |
"""
|
| 102 |
|
| 103 |
|
| 104 |
+
# --- AUDIO & LLM HELPERS ---
|
| 105 |
def transcribe_audio(audio_path: str, audio_bytes: bytes) -> str:
|
| 106 |
for attempt in range(3):
|
| 107 |
try:
|
|
|
|
| 129 |
return text
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
+
logging.error(f"Transcription error (attempt {attempt+1}): {e}", exc_info=True) # Added exc_info
|
| 133 |
if attempt == 2:
|
| 134 |
return ""
|
| 135 |
return ""
|
|
|
|
| 145 |
output_format="mp3_44100_128"
|
| 146 |
)
|
| 147 |
except Exception as e:
|
| 148 |
+
logging.error(f"ElevenLabs error (attempt {attempt+1}): {e}", exc_info=True) # Added exc_info
|
| 149 |
if attempt == 2:
|
| 150 |
return b''
|
| 151 |
return b''
|
| 152 |
|
| 153 |
+
# --- UPDATED formulate_search_plan with logging ---
|
| 154 |
async def formulate_search_plan(user_query: str) -> dict:
|
| 155 |
+
logging.info(f"Formulating search plan for query: {user_query}") # Log incoming query
|
| 156 |
for attempt in range(3):
|
| 157 |
try:
|
| 158 |
response = await run_in_threadpool(
|
|
|
|
| 162 |
response_format={"type": "json_object"},
|
| 163 |
temperature=0.0
|
| 164 |
)
|
| 165 |
+
# Log the raw response BEFORE trying to parse
|
| 166 |
+
raw_response_content = response.choices[0].message.content
|
| 167 |
+
logging.info(f"Raw Planner LLM response content: {raw_response_content}")
|
| 168 |
+
|
| 169 |
+
# Try parsing
|
| 170 |
+
plan = json.loads(raw_response_content)
|
| 171 |
+
logging.info(f"Successfully parsed search plan: {plan}")
|
| 172 |
+
return plan
|
| 173 |
except Exception as e:
|
| 174 |
+
# Log the specific error during parsing or API call, with traceback
|
| 175 |
+
logging.error(f"Planner error (attempt {attempt+1}): {e}", exc_info=True)
|
| 176 |
if attempt == 2:
|
| 177 |
+
logging.warning("Planner failed after 3 attempts. Using fallback.")
|
| 178 |
return {"search_query": user_query, "filter_table": None}
|
| 179 |
+
# Fallback if loop finishes unexpectedly
|
| 180 |
+
logging.error("Planner loop finished unexpectedly. Using fallback.")
|
| 181 |
return {"search_query": user_query, "filter_table": None}
|
| 182 |
+
# --- END UPDATED FUNCTION ---
|
| 183 |
|
| 184 |
async def get_agent_response(user_text: str) -> str:
|
| 185 |
for attempt in range(3):
|
|
|
|
| 209 |
)
|
| 210 |
return response.choices[0].message.content.strip()
|
| 211 |
except Exception as e:
|
| 212 |
+
logging.error(f"RAG error (attempt {attempt+1}): {e}", exc_info=True) # Added exc_info
|
| 213 |
if attempt == 2:
|
| 214 |
return "Sorry, I couldn't respond. Please try again."
|
| 215 |
return "Sorry, I couldn't respond."
|
|
|
|
| 232 |
return {"response": response}
|
| 233 |
|
| 234 |
|
| 235 |
+
# --- GRADIO AUDIO PROCESSING ---
|
| 236 |
async def process_audio(audio_path):
|
| 237 |
if not audio_path or not os.path.exists(audio_path):
|
| 238 |
return None, "No valid audio file received."
|
|
|
|
| 274 |
return out_path, f"**You:** {user_text}\n\n**AI:** {agent_response}"
|
| 275 |
|
| 276 |
except Exception as e:
|
| 277 |
+
logging.error(f"Audio processing error: {e}", exc_info=True) # Added exc_info
|
| 278 |
return None, f"Error: {str(e)}"
|
| 279 |
|
| 280 |
|
|
|
|
| 289 |
|
| 290 |
out_text = gr.Textbox(label="Conversation", lines=8)
|
| 291 |
|
|
|
|
| 292 |
inp.change(process_audio, inp, [out_audio, out_text])
|
| 293 |
|
| 294 |
+
# Removed examples to avoid FileNotFoundError with text inputs
|
|
|
|
|
|
|
|
|
|
| 295 |
# gr.Examples(examples=[], inputs=inp)
|
| 296 |
|
| 297 |
|