Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -49,7 +49,8 @@ def chat():
|
|
| 49 |
{
|
| 50 |
"query": "user message text",
|
| 51 |
"image_url": "optional image URL",
|
| 52 |
-
"session_id": "optional chat ID for conversation memory"
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
Response format:
|
|
@@ -72,17 +73,22 @@ def chat():
|
|
| 72 |
user_message = data.get('query')
|
| 73 |
chat_id = data.get('session_id')
|
| 74 |
image_url = data.get('image_url')
|
|
|
|
| 75 |
else:
|
| 76 |
# Fallback for form data (backward compatibility)
|
| 77 |
data = request.form
|
| 78 |
user_message = data.get('message') or data.get('query')
|
| 79 |
chat_id = data.get('session_id')
|
| 80 |
image_url = data.get('image_url')
|
|
|
|
| 81 |
|
| 82 |
# Validate input
|
| 83 |
if not user_message and not image_url:
|
| 84 |
return jsonify({"error": "Cannot process an empty message. Provide either 'query' text or 'image_url'."}), 400
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
# Clean up empty/null chat_id
|
| 87 |
if chat_id in [None, '', 'null', 'undefined']:
|
| 88 |
chat_id = None
|
|
@@ -90,6 +96,7 @@ def chat():
|
|
| 90 |
# Process the query with the assistant
|
| 91 |
result = assistant.process_query(
|
| 92 |
user_message=user_message or "",
|
|
|
|
| 93 |
chat_id=chat_id,
|
| 94 |
image_url=image_url
|
| 95 |
)
|
|
@@ -119,7 +126,10 @@ def chat():
|
|
| 119 |
@app.route('/chats', methods=['GET'])
|
| 120 |
def get_all_chats():
|
| 121 |
"""
|
| 122 |
-
Get all chat sessions with basic information.
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
Response format:
|
| 125 |
[
|
|
@@ -136,7 +146,11 @@ def get_all_chats():
|
|
| 136 |
return jsonify({"error": "Conversation manager not available"}), 503
|
| 137 |
|
| 138 |
try:
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
return jsonify(chats)
|
| 141 |
|
| 142 |
except Exception as e:
|
|
@@ -146,7 +160,10 @@ def get_all_chats():
|
|
| 146 |
@app.route('/chat/<chat_id>/info', methods=['GET'])
|
| 147 |
def get_chat_info(chat_id):
|
| 148 |
"""
|
| 149 |
-
Get information about a specific chat.
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
Response format:
|
| 152 |
{
|
|
@@ -164,7 +181,11 @@ def get_chat_info(chat_id):
|
|
| 164 |
if not chat_id:
|
| 165 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
return jsonify(info)
|
| 169 |
|
| 170 |
except Exception as e:
|
|
@@ -174,7 +195,10 @@ def get_chat_info(chat_id):
|
|
| 174 |
@app.route('/chat/<chat_id>/messages', methods=['GET'])
|
| 175 |
def get_chat_messages(chat_id):
|
| 176 |
"""
|
| 177 |
-
Get all messages for a specific chat.
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
Response format:
|
| 180 |
[
|
|
@@ -200,7 +224,11 @@ def get_chat_messages(chat_id):
|
|
| 200 |
if not chat_id:
|
| 201 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 202 |
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
return jsonify(messages)
|
| 205 |
|
| 206 |
except Exception as e:
|
|
@@ -210,7 +238,10 @@ def get_chat_messages(chat_id):
|
|
| 210 |
@app.route('/chat/<chat_id>', methods=['DELETE'])
|
| 211 |
def delete_chat(chat_id):
|
| 212 |
"""
|
| 213 |
-
Delete a specific chat and all its messages.
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
Response format:
|
| 216 |
{
|
|
@@ -226,7 +257,11 @@ def delete_chat(chat_id):
|
|
| 226 |
if not chat_id:
|
| 227 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 228 |
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
if success:
|
| 232 |
return jsonify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|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
# --- Legacy Endpoints for Backward Compatibility ---
|
| 249 |
|
| 250 |
@app.route('/history/sessions', methods=['GET'])
|
|
|
|
| 49 |
{
|
| 50 |
"query": "user message text",
|
| 51 |
"image_url": "optional image URL",
|
| 52 |
+
"session_id": "optional chat ID for conversation memory",
|
| 53 |
+
"user_id": "user identifier for authentication"
|
| 54 |
}
|
| 55 |
|
| 56 |
Response format:
|
|
|
|
| 73 |
user_message = data.get('query')
|
| 74 |
chat_id = data.get('session_id')
|
| 75 |
image_url = data.get('image_url')
|
| 76 |
+
user_id = data.get('user_id')
|
| 77 |
else:
|
| 78 |
# Fallback for form data (backward compatibility)
|
| 79 |
data = request.form
|
| 80 |
user_message = data.get('message') or data.get('query')
|
| 81 |
chat_id = data.get('session_id')
|
| 82 |
image_url = data.get('image_url')
|
| 83 |
+
user_id = data.get('user_id')
|
| 84 |
|
| 85 |
# Validate input
|
| 86 |
if not user_message and not image_url:
|
| 87 |
return jsonify({"error": "Cannot process an empty message. Provide either 'query' text or 'image_url'."}), 400
|
| 88 |
|
| 89 |
+
if not user_id:
|
| 90 |
+
return jsonify({"error": "User ID is required for authentication."}), 400
|
| 91 |
+
|
| 92 |
# Clean up empty/null chat_id
|
| 93 |
if chat_id in [None, '', 'null', 'undefined']:
|
| 94 |
chat_id = None
|
|
|
|
| 96 |
# Process the query with the assistant
|
| 97 |
result = assistant.process_query(
|
| 98 |
user_message=user_message or "",
|
| 99 |
+
user_id=user_id,
|
| 100 |
chat_id=chat_id,
|
| 101 |
image_url=image_url
|
| 102 |
)
|
|
|
|
| 126 |
@app.route('/chats', methods=['GET'])
|
| 127 |
def get_all_chats():
|
| 128 |
"""
|
| 129 |
+
Get all chat sessions with basic information for a specific user.
|
| 130 |
+
|
| 131 |
+
Query parameters:
|
| 132 |
+
- user_id: Required user identifier
|
| 133 |
|
| 134 |
Response format:
|
| 135 |
[
|
|
|
|
| 146 |
return jsonify({"error": "Conversation manager not available"}), 503
|
| 147 |
|
| 148 |
try:
|
| 149 |
+
user_id = request.args.get('user_id')
|
| 150 |
+
if not user_id:
|
| 151 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 152 |
+
|
| 153 |
+
chats = assistant.get_all_chats(user_id)
|
| 154 |
return jsonify(chats)
|
| 155 |
|
| 156 |
except Exception as e:
|
|
|
|
| 160 |
@app.route('/chat/<chat_id>/info', methods=['GET'])
|
| 161 |
def get_chat_info(chat_id):
|
| 162 |
"""
|
| 163 |
+
Get information about a specific chat for a specific user.
|
| 164 |
+
|
| 165 |
+
Query parameters:
|
| 166 |
+
- user_id: Required user identifier
|
| 167 |
|
| 168 |
Response format:
|
| 169 |
{
|
|
|
|
| 181 |
if not chat_id:
|
| 182 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 183 |
|
| 184 |
+
user_id = request.args.get('user_id')
|
| 185 |
+
if not user_id:
|
| 186 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 187 |
+
|
| 188 |
+
info = assistant.get_chat_info(chat_id, user_id)
|
| 189 |
return jsonify(info)
|
| 190 |
|
| 191 |
except Exception as e:
|
|
|
|
| 195 |
@app.route('/chat/<chat_id>/messages', methods=['GET'])
|
| 196 |
def get_chat_messages(chat_id):
|
| 197 |
"""
|
| 198 |
+
Get all messages for a specific chat and user.
|
| 199 |
+
|
| 200 |
+
Query parameters:
|
| 201 |
+
- user_id: Required user identifier
|
| 202 |
|
| 203 |
Response format:
|
| 204 |
[
|
|
|
|
| 224 |
if not chat_id:
|
| 225 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 226 |
|
| 227 |
+
user_id = request.args.get('user_id')
|
| 228 |
+
if not user_id:
|
| 229 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 230 |
+
|
| 231 |
+
messages = assistant.get_messages(chat_id, user_id)
|
| 232 |
return jsonify(messages)
|
| 233 |
|
| 234 |
except Exception as e:
|
|
|
|
| 238 |
@app.route('/chat/<chat_id>', methods=['DELETE'])
|
| 239 |
def delete_chat(chat_id):
|
| 240 |
"""
|
| 241 |
+
Delete a specific chat and all its messages for a specific user.
|
| 242 |
+
|
| 243 |
+
Query parameters:
|
| 244 |
+
- user_id: Required user identifier
|
| 245 |
|
| 246 |
Response format:
|
| 247 |
{
|
|
|
|
| 257 |
if not chat_id:
|
| 258 |
return jsonify({"error": "Chat ID is required"}), 400
|
| 259 |
|
| 260 |
+
user_id = request.args.get('user_id')
|
| 261 |
+
if not user_id:
|
| 262 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 263 |
+
|
| 264 |
+
success = assistant.clear_history(chat_id, user_id)
|
| 265 |
|
| 266 |
if success:
|
| 267 |
return jsonify({
|
|
|
|
| 280 |
logger.error(f"Error deleting chat {chat_id}: {e}")
|
| 281 |
return jsonify({"error": "Could not delete chat"}), 500
|
| 282 |
|
| 283 |
+
# --- New User Management Endpoints ---
|
| 284 |
+
|
| 285 |
+
@app.route('/users/<user_id>/stats', methods=['GET'])
|
| 286 |
+
def get_user_stats(user_id):
|
| 287 |
+
"""
|
| 288 |
+
Get statistics for a specific user.
|
| 289 |
+
|
| 290 |
+
Response format:
|
| 291 |
+
{
|
| 292 |
+
"user_id": "user_abc123",
|
| 293 |
+
"total_sessions": 5,
|
| 294 |
+
"total_messages": 42,
|
| 295 |
+
"recent_sessions_24h": 2,
|
| 296 |
+
"average_messages_per_session": 8.4,
|
| 297 |
+
"first_session": "2024-01-01T10:00:00",
|
| 298 |
+
"last_activity": "2024-01-01T15:30:00"
|
| 299 |
+
}
|
| 300 |
+
"""
|
| 301 |
+
if not conv_manager:
|
| 302 |
+
return jsonify({"error": "Conversation manager not available"}), 503
|
| 303 |
+
|
| 304 |
+
try:
|
| 305 |
+
if not user_id:
|
| 306 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 307 |
+
|
| 308 |
+
stats = conv_manager.get_user_stats(user_id)
|
| 309 |
+
return jsonify(stats)
|
| 310 |
+
|
| 311 |
+
except Exception as e:
|
| 312 |
+
logger.error(f"Error getting user stats for {user_id}: {e}")
|
| 313 |
+
return jsonify({"error": "Could not fetch user statistics"}), 500
|
| 314 |
+
|
| 315 |
+
@app.route('/users/<user_id>/chats', methods=['DELETE'])
|
| 316 |
+
def delete_all_user_chats(user_id):
|
| 317 |
+
"""
|
| 318 |
+
Delete all chats for a specific user.
|
| 319 |
+
|
| 320 |
+
Response format:
|
| 321 |
+
{
|
| 322 |
+
"status": "success",
|
| 323 |
+
"message": "All chats deleted successfully",
|
| 324 |
+
"user_id": "user_abc123"
|
| 325 |
+
}
|
| 326 |
+
"""
|
| 327 |
+
if not conv_manager:
|
| 328 |
+
return jsonify({"error": "Conversation manager not available"}), 503
|
| 329 |
+
|
| 330 |
+
try:
|
| 331 |
+
if not user_id:
|
| 332 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 333 |
+
|
| 334 |
+
success = conv_manager.delete_all_user_data(user_id)
|
| 335 |
+
|
| 336 |
+
if success:
|
| 337 |
+
return jsonify({
|
| 338 |
+
"status": "success",
|
| 339 |
+
"message": f"All chats deleted for user {user_id}",
|
| 340 |
+
"user_id": user_id
|
| 341 |
+
})
|
| 342 |
+
else:
|
| 343 |
+
return jsonify({
|
| 344 |
+
"status": "error",
|
| 345 |
+
"message": f"Failed to delete chats for user {user_id}",
|
| 346 |
+
"user_id": user_id
|
| 347 |
+
}), 500
|
| 348 |
+
|
| 349 |
+
except Exception as e:
|
| 350 |
+
logger.error(f"Error deleting all chats for user {user_id}: {e}")
|
| 351 |
+
return jsonify({"error": "Could not delete user chats"}), 500
|
| 352 |
+
|
| 353 |
+
@app.route('/users/<user_id>/export', methods=['GET'])
|
| 354 |
+
def export_user_data(user_id):
|
| 355 |
+
"""
|
| 356 |
+
Export all data for a specific user.
|
| 357 |
+
|
| 358 |
+
Response format:
|
| 359 |
+
{
|
| 360 |
+
"user_id": "user_abc123",
|
| 361 |
+
"export_date": "2024-01-01T10:00:00",
|
| 362 |
+
"stats": {...},
|
| 363 |
+
"sessions": [...]
|
| 364 |
+
}
|
| 365 |
+
"""
|
| 366 |
+
if not conv_manager:
|
| 367 |
+
return jsonify({"error": "Conversation manager not available"}), 503
|
| 368 |
+
|
| 369 |
+
try:
|
| 370 |
+
if not user_id:
|
| 371 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 372 |
+
|
| 373 |
+
# Get user stats and sessions
|
| 374 |
+
stats = conv_manager.get_user_stats(user_id)
|
| 375 |
+
sessions = conv_manager.get_all_chat_sessions(user_id)
|
| 376 |
+
|
| 377 |
+
# Get all messages for each session
|
| 378 |
+
detailed_sessions = []
|
| 379 |
+
for session in sessions:
|
| 380 |
+
session_data = session.copy()
|
| 381 |
+
messages = conv_manager.get_history(session['session_id'], user_id)
|
| 382 |
+
session_data['messages'] = messages
|
| 383 |
+
detailed_sessions.append(session_data)
|
| 384 |
+
|
| 385 |
+
export_data = {
|
| 386 |
+
"user_id": user_id,
|
| 387 |
+
"export_date": datetime.utcnow().isoformat(),
|
| 388 |
+
"stats": stats,
|
| 389 |
+
"sessions": detailed_sessions,
|
| 390 |
+
"total_exported_sessions": len(detailed_sessions),
|
| 391 |
+
"total_exported_messages": sum(len(s.get('messages', [])) for s in detailed_sessions)
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
return jsonify(export_data)
|
| 395 |
+
|
| 396 |
+
except Exception as e:
|
| 397 |
+
logger.error(f"Error exporting data for user {user_id}: {e}")
|
| 398 |
+
return jsonify({"error": "Could not export user data"}), 500
|
| 399 |
+
|
| 400 |
+
@app.route('/users/<user_id>', methods=['DELETE'])
|
| 401 |
+
def delete_user_account(user_id):
|
| 402 |
+
"""
|
| 403 |
+
Delete a user account and all associated data.
|
| 404 |
+
|
| 405 |
+
Response format:
|
| 406 |
+
{
|
| 407 |
+
"status": "success",
|
| 408 |
+
"message": "User account deleted successfully",
|
| 409 |
+
"user_id": "user_abc123"
|
| 410 |
+
}
|
| 411 |
+
"""
|
| 412 |
+
if not conv_manager:
|
| 413 |
+
return jsonify({"error": "Conversation manager not available"}), 503
|
| 414 |
+
|
| 415 |
+
try:
|
| 416 |
+
if not user_id:
|
| 417 |
+
return jsonify({"error": "User ID is required"}), 400
|
| 418 |
+
|
| 419 |
+
# Delete all user data
|
| 420 |
+
success = conv_manager.delete_all_user_data(user_id)
|
| 421 |
+
|
| 422 |
+
if success:
|
| 423 |
+
return jsonify({
|
| 424 |
+
"status": "success",
|
| 425 |
+
"message": f"User account {user_id} deleted successfully",
|
| 426 |
+
"user_id": user_id
|
| 427 |
+
})
|
| 428 |
+
else:
|
| 429 |
+
return jsonify({
|
| 430 |
+
"status": "error",
|
| 431 |
+
"message": f"Failed to delete user account {user_id}",
|
| 432 |
+
"user_id": user_id
|
| 433 |
+
}), 500
|
| 434 |
+
|
| 435 |
+
except Exception as e:
|
| 436 |
+
logger.error(f"Error deleting user account {user_id}: {e}")
|
| 437 |
+
return jsonify({"error": "Could not delete user account"}), 500
|
| 438 |
+
|
| 439 |
# --- Legacy Endpoints for Backward Compatibility ---
|
| 440 |
|
| 441 |
@app.route('/history/sessions', methods=['GET'])
|