Update app.py
Browse files
app.py
CHANGED
|
@@ -59,7 +59,7 @@ async def generate_solution_python(chat_history):
|
|
| 59 |
latest_user_input = "an image"
|
| 60 |
break
|
| 61 |
# If a document was processed and its text added, use that
|
| 62 |
-
if part.get("text") and part["text"].startswith("Document content:"):
|
| 63 |
latest_user_input = "a document"
|
| 64 |
break
|
| 65 |
if latest_user_input:
|
|
@@ -122,6 +122,7 @@ def index():
|
|
| 122 |
@app.route('/generate', methods=['POST'])
|
| 123 |
async def generate():
|
| 124 |
"""Handles the AI generation request, managing conversation history and multi-modal input."""
|
|
|
|
| 125 |
try:
|
| 126 |
data = request.get_json()
|
| 127 |
if not data:
|
|
@@ -131,13 +132,16 @@ async def generate():
|
|
| 131 |
image_data = data.get('image_data') # Base64 image data
|
| 132 |
document_text = data.get('document_text') # Text extracted from .txt on frontend
|
| 133 |
pdf_data = data.get('pdf_data') # Base64 PDF data
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
if not session_id:
|
| 138 |
session_id = str(uuid.uuid4())
|
| 139 |
print(f"Warning: session_id not provided, generated new one: {session_id}")
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
current_chat_history = conversation_histories.get(session_id, [])
|
| 142 |
|
| 143 |
# Construct the parts for the user message
|
|
@@ -178,7 +182,9 @@ async def generate():
|
|
| 178 |
except Exception as pdf_error:
|
| 179 |
print(f"Error processing PDF: {pdf_error}")
|
| 180 |
user_message_parts.append({"text": f"PDF Document: (Error processing PDF: {pdf_error})"})
|
| 181 |
-
return
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# If only a file was provided without a query, add a default instruction
|
| 184 |
if not user_query and (image_data or document_text or pdf_data):
|
|
@@ -203,7 +209,11 @@ async def generate():
|
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
print(f"Error in /generate endpoint: {e}")
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
if __name__ == '__main__':
|
| 209 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 59 |
latest_user_input = "an image"
|
| 60 |
break
|
| 61 |
# If a document was processed and its text added, use that
|
| 62 |
+
if part.get("text") and part["text"].startswith("PDF Document Content:") or part["text"].startswith("Document content:"):
|
| 63 |
latest_user_input = "a document"
|
| 64 |
break
|
| 65 |
if latest_user_input:
|
|
|
|
| 122 |
@app.route('/generate', methods=['POST'])
|
| 123 |
async def generate():
|
| 124 |
"""Handles the AI generation request, managing conversation history and multi-modal input."""
|
| 125 |
+
session_id = None # Initialize session_id to None
|
| 126 |
try:
|
| 127 |
data = request.get_json()
|
| 128 |
if not data:
|
|
|
|
| 132 |
image_data = data.get('image_data') # Base64 image data
|
| 133 |
document_text = data.get('document_text') # Text extracted from .txt on frontend
|
| 134 |
pdf_data = data.get('pdf_data') # Base64 PDF data
|
| 135 |
+
|
| 136 |
+
# Ensure session_id is assigned before use
|
| 137 |
+
session_id = data.get('session_id')
|
| 138 |
if not session_id:
|
| 139 |
session_id = str(uuid.uuid4())
|
| 140 |
print(f"Warning: session_id not provided, generated new one: {session_id}")
|
| 141 |
|
| 142 |
+
if not (user_query or image_data or document_text or pdf_data):
|
| 143 |
+
return jsonify({"error": "Query, image, or document is required in the request body"}), 400
|
| 144 |
+
|
| 145 |
current_chat_history = conversation_histories.get(session_id, [])
|
| 146 |
|
| 147 |
# Construct the parts for the user message
|
|
|
|
| 182 |
except Exception as pdf_error:
|
| 183 |
print(f"Error processing PDF: {pdf_error}")
|
| 184 |
user_message_parts.append({"text": f"PDF Document: (Error processing PDF: {pdf_error})"})
|
| 185 |
+
# Do not return error to frontend immediately for PDF processing issues
|
| 186 |
+
# Let the LLM try to respond even if PDF extraction failed
|
| 187 |
+
# return jsonify({"error": f"Failed to process PDF: {pdf_error}"}), 400
|
| 188 |
|
| 189 |
# If only a file was provided without a query, add a default instruction
|
| 190 |
if not user_query and (image_data or document_text or pdf_data):
|
|
|
|
| 209 |
|
| 210 |
except Exception as e:
|
| 211 |
print(f"Error in /generate endpoint: {e}")
|
| 212 |
+
# Ensure session_id is handled even in the outer exception for logging/debugging
|
| 213 |
+
if session_id:
|
| 214 |
+
return jsonify({"error": f"Internal server error for session {session_id}: {e}"}), 500
|
| 215 |
+
else:
|
| 216 |
+
return jsonify({"error": f"Internal server error: {e}"}), 500
|
| 217 |
|
| 218 |
if __name__ == '__main__':
|
| 219 |
app.run(host='0.0.0.0', port=7860)
|