Spaces:
Sleeping
Sleeping
riteshraut commited on
Commit ·
8ebf441
1
Parent(s): 9683d0d
fix
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import nltk
|
| 3 |
from functools import wraps
|
|
|
|
| 4 |
|
| 5 |
# ============================ NLTK MONKEY-PATCH (MUST BE FIRST) ============================
|
| 6 |
# This patch ensures NLTK downloads to a writable directory on platforms like Hugging Face Spaces.
|
|
@@ -181,15 +182,16 @@ def upload_files():
|
|
| 181 |
traceback.print_exc()
|
| 182 |
return jsonify({'status': 'error', 'message': f'An unexpected error occurred: {str(e)}'}), 500
|
| 183 |
|
|
|
|
| 184 |
@app.route('/chat', methods=['POST'])
|
| 185 |
def chat():
|
| 186 |
-
"""Handles chat messages and streams the response."""
|
| 187 |
data = request.get_json()
|
| 188 |
question = data.get('question')
|
| 189 |
session_id = session.get('session_id')
|
| 190 |
|
| 191 |
if not all([question, session_id]) or session_id not in rag_chains:
|
| 192 |
-
return jsonify({'status': 'error', 'message': 'Session not found
|
| 193 |
|
| 194 |
try:
|
| 195 |
rag_chain = rag_chains[session_id]
|
|
@@ -202,9 +204,12 @@ def chat():
|
|
| 202 |
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
| 208 |
# ============================ Text-to-Speech Functions ============================
|
| 209 |
def clean_markdown_for_tts(text: str) -> str:
|
| 210 |
"""Removes markdown for cleaner text-to-speech output."""
|
|
|
|
| 1 |
import os
|
| 2 |
import nltk
|
| 3 |
from functools import wraps
|
| 4 |
+
import traceback
|
| 5 |
|
| 6 |
# ============================ NLTK MONKEY-PATCH (MUST BE FIRST) ============================
|
| 7 |
# This patch ensures NLTK downloads to a writable directory on platforms like Hugging Face Spaces.
|
|
|
|
| 182 |
traceback.print_exc()
|
| 183 |
return jsonify({'status': 'error', 'message': f'An unexpected error occurred: {str(e)}'}), 500
|
| 184 |
|
| 185 |
+
|
| 186 |
@app.route('/chat', methods=['POST'])
|
| 187 |
def chat():
|
| 188 |
+
"""Handles chat messages and streams the response with better error logging."""
|
| 189 |
data = request.get_json()
|
| 190 |
question = data.get('question')
|
| 191 |
session_id = session.get('session_id')
|
| 192 |
|
| 193 |
if not all([question, session_id]) or session_id not in rag_chains:
|
| 194 |
+
return jsonify({'status': 'error', 'message': 'Session not found. Please upload documents again.'}), 400
|
| 195 |
|
| 196 |
try:
|
| 197 |
rag_chain = rag_chains[session_id]
|
|
|
|
| 204 |
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
+
# --- THIS IS THE CRUCIAL PART ---
|
| 208 |
+
print("!!! AN ERROR OCCURRED DURING CHAT STREAMING !!!")
|
| 209 |
+
traceback.print_exc() # This will print the full error to your logs
|
| 210 |
+
# Return a more specific error message to the frontend
|
| 211 |
+
error_message = f"Server error: The RAG chain failed. Check server logs for details."
|
| 212 |
+
|
| 213 |
# ============================ Text-to-Speech Functions ============================
|
| 214 |
def clean_markdown_for_tts(text: str) -> str:
|
| 215 |
"""Removes markdown for cleaner text-to-speech output."""
|