Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -234,21 +234,26 @@ def process_message(
|
|
| 234 |
global client
|
| 235 |
|
| 236 |
if client is None:
|
| 237 |
-
return history + [(message, "β Please configure your API key first in the Settings tab.")], "", ""
|
| 238 |
|
| 239 |
if not message.strip() and not files:
|
| 240 |
-
return history + [(
|
| 241 |
|
| 242 |
status_messages = []
|
| 243 |
|
| 244 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# Build messages array
|
| 246 |
messages = []
|
| 247 |
|
| 248 |
# Add conversation history
|
| 249 |
for user_msg, assistant_msg in history:
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 253 |
|
| 254 |
# Build current message content
|
|
@@ -261,18 +266,23 @@ def process_message(
|
|
| 261 |
|
| 262 |
for file in files:
|
| 263 |
if file is not None:
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
if file_count > 0:
|
| 278 |
file_summary = f"π {file_count} file(s) uploaded"
|
|
@@ -281,9 +291,13 @@ def process_message(
|
|
| 281 |
content.insert(0, {"type": "text", "text": file_summary})
|
| 282 |
|
| 283 |
# Add text message
|
| 284 |
-
if message.strip():
|
| 285 |
content.append({"type": "text", "text": message})
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
messages.append({"role": "user", "content": content})
|
| 288 |
|
| 289 |
# Prepare API call parameters
|
|
@@ -298,20 +312,45 @@ def process_message(
|
|
| 298 |
if enable_reasoning:
|
| 299 |
api_params["extra_body"] = {"reasoning": {"enabled": True}}
|
| 300 |
|
| 301 |
-
# Make API call
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
# Extract reasoning if available
|
| 307 |
reasoning_text = ""
|
| 308 |
-
if enable_reasoning
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
# Update history
|
| 314 |
-
|
|
|
|
| 315 |
|
| 316 |
# Combine status messages
|
| 317 |
combined_status = "\n".join(status_messages) if status_messages else "β
Message processed successfully"
|
|
@@ -319,8 +358,13 @@ def process_message(
|
|
| 319 |
return new_history, reasoning_text, combined_status
|
| 320 |
|
| 321 |
except Exception as e:
|
| 322 |
-
error_message = f"β Error: {str(e)}"
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
def clear_conversation():
|
| 326 |
"""Clear conversation history"""
|
|
|
|
| 234 |
global client
|
| 235 |
|
| 236 |
if client is None:
|
| 237 |
+
return history + [(message if message else "No message", "β Please configure your API key first in the Settings tab.")], "", "β API key not configured"
|
| 238 |
|
| 239 |
if not message.strip() and not files:
|
| 240 |
+
return history + [("", "β οΈ Please enter a message or upload files.")], "", "β οΈ No input provided"
|
| 241 |
|
| 242 |
status_messages = []
|
| 243 |
|
| 244 |
try:
|
| 245 |
+
# Ensure history is a list
|
| 246 |
+
if history is None:
|
| 247 |
+
history = []
|
| 248 |
+
|
| 249 |
# Build messages array
|
| 250 |
messages = []
|
| 251 |
|
| 252 |
# Add conversation history
|
| 253 |
for user_msg, assistant_msg in history:
|
| 254 |
+
if user_msg: # Only add if user message exists
|
| 255 |
+
messages.append({"role": "user", "content": user_msg if user_msg else "..."})
|
| 256 |
+
if assistant_msg: # Only add if assistant message exists
|
| 257 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 258 |
|
| 259 |
# Build current message content
|
|
|
|
| 266 |
|
| 267 |
for file in files:
|
| 268 |
if file is not None:
|
| 269 |
+
try:
|
| 270 |
+
file_blocks, status = process_file(file)
|
| 271 |
+
if file_blocks: # Only add if we got valid blocks
|
| 272 |
+
content.extend(file_blocks)
|
| 273 |
+
status_messages.append(status)
|
| 274 |
+
file_count += 1
|
| 275 |
+
|
| 276 |
+
# Count pages for PDFs
|
| 277 |
+
if status and status.startswith("β
") and "page(s)" in status:
|
| 278 |
+
try:
|
| 279 |
+
pages = int(status.split("converted to ")[1].split(" page(s)")[0])
|
| 280 |
+
total_pages += pages
|
| 281 |
+
except:
|
| 282 |
+
pass
|
| 283 |
+
except Exception as file_error:
|
| 284 |
+
error_msg = f"β Error processing file: {str(file_error)}"
|
| 285 |
+
status_messages.append(error_msg)
|
| 286 |
|
| 287 |
if file_count > 0:
|
| 288 |
file_summary = f"π {file_count} file(s) uploaded"
|
|
|
|
| 291 |
content.insert(0, {"type": "text", "text": file_summary})
|
| 292 |
|
| 293 |
# Add text message
|
| 294 |
+
if message and message.strip():
|
| 295 |
content.append({"type": "text", "text": message})
|
| 296 |
|
| 297 |
+
# If no content at all, return error
|
| 298 |
+
if not content:
|
| 299 |
+
return history + [(message if message else "", "β No valid content to process")], "", "β No valid content"
|
| 300 |
+
|
| 301 |
messages.append({"role": "user", "content": content})
|
| 302 |
|
| 303 |
# Prepare API call parameters
|
|
|
|
| 312 |
if enable_reasoning:
|
| 313 |
api_params["extra_body"] = {"reasoning": {"enabled": True}}
|
| 314 |
|
| 315 |
+
# Make API call with additional error handling
|
| 316 |
+
try:
|
| 317 |
+
response = client.chat.completions.create(**api_params)
|
| 318 |
+
except Exception as api_error:
|
| 319 |
+
error_msg = f"β API Error: {str(api_error)}"
|
| 320 |
+
return history + [(message if message else "", error_msg)], "", error_msg
|
| 321 |
+
|
| 322 |
+
# Check if response is valid
|
| 323 |
+
if not response:
|
| 324 |
+
error_message = "β Error: Received None response from API"
|
| 325 |
+
return history + [(message if message else "", error_message)], "", error_message
|
| 326 |
|
| 327 |
+
if not hasattr(response, 'choices') or not response.choices or len(response.choices) == 0:
|
| 328 |
+
error_message = "β Error: Received empty response from API"
|
| 329 |
+
return history + [(message if message else "", error_message)], "", error_message
|
| 330 |
+
|
| 331 |
+
# Get the assistant message safely
|
| 332 |
+
try:
|
| 333 |
+
assistant_message = response.choices[0].message.content
|
| 334 |
+
if not assistant_message:
|
| 335 |
+
assistant_message = "β οΈ Model returned an empty response"
|
| 336 |
+
except (AttributeError, IndexError) as e:
|
| 337 |
+
assistant_message = f"β Error extracting response: {str(e)}"
|
| 338 |
|
| 339 |
# Extract reasoning if available
|
| 340 |
reasoning_text = ""
|
| 341 |
+
if enable_reasoning:
|
| 342 |
+
try:
|
| 343 |
+
if hasattr(response.choices[0].message, 'reasoning_details'):
|
| 344 |
+
reasoning_details = response.choices[0].message.reasoning_details
|
| 345 |
+
if reasoning_details:
|
| 346 |
+
reasoning_text = f"**π§ Reasoning Process:**\n{json.dumps(reasoning_details, indent=2)}"
|
| 347 |
+
except Exception as reasoning_error:
|
| 348 |
+
# Reasoning extraction failed, but that's okay
|
| 349 |
+
pass
|
| 350 |
|
| 351 |
# Update history
|
| 352 |
+
display_message = message if message and message.strip() else "[Files uploaded]"
|
| 353 |
+
new_history = history + [(display_message, assistant_message)]
|
| 354 |
|
| 355 |
# Combine status messages
|
| 356 |
combined_status = "\n".join(status_messages) if status_messages else "β
Message processed successfully"
|
|
|
|
| 358 |
return new_history, reasoning_text, combined_status
|
| 359 |
|
| 360 |
except Exception as e:
|
| 361 |
+
error_message = f"β Error: {str(e)}\n\nType: {type(e).__name__}"
|
| 362 |
+
import traceback
|
| 363 |
+
error_detail = traceback.format_exc()
|
| 364 |
+
print(f"Full error: {error_detail}") # For debugging
|
| 365 |
+
|
| 366 |
+
display_message = message if message and message.strip() else "[Error occurred]"
|
| 367 |
+
return history + [(display_message, error_message)], "", error_message
|
| 368 |
|
| 369 |
def clear_conversation():
|
| 370 |
"""Clear conversation history"""
|