Spaces:
Runtime error

manasmaskar
Add detailed error logging to app.py
ef8f606
Raw History Blame Contribute Delete
2.19 kB
from flask import Flask, render_template, request, jsonify
import os
import traceback
app = Flask(__name__)
# Initialize RAG with error handling
rag = None
def init_rag():
global rag
try:
print("πŸš€ Initializing RAG Pipeline...")
print(f"πŸ“ Checking files...")
print(f" data/complete_arlo_docs.json exists: {os.path.exists('data/complete_arlo_docs.json')}")
print(f" OPENAI_API_KEY set: {bool(os.getenv('OPENAI_API_KEY'))}")
from hybrid_search import ArloRAGPipeline
rag = ArloRAGPipeline(enable_web_search=True)
rag.build()
print("βœ… RAG Ready!")
return True
except Exception as e:
print(f"❌ RAG Initialization FAILED:")
print(f"Error: {e}")
print(traceback.format_exc())
return False
# Try to initialize
rag_ready = init_rag()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/health')
def health():
return jsonify({
"status": "healthy" if rag_ready else "degraded",
"rag_ready": rag_ready,
"rag_initialized": rag is not None
})
@app.route('/query', methods=['POST'])
def query():
if not rag_ready or rag is None:
return jsonify({"error": "RAG system not initialized"}), 503
question = request.json.get('question', '')
if not question:
return jsonify({"error": "No question provided"}), 400
# Capture RAG output
import io
import sys
old_stdout = sys.stdout
sys.stdout = buffer = io.StringIO()
try:
rag.query(question)
output = buffer.getvalue()
sys.stdout = old_stdout
# Extract answer
if "πŸ’‘ Answer:" in output:
answer = output.split("πŸ’‘ Answer:")[1].strip()
else:
answer = output
return jsonify({
"question": question,
"answer": answer
})
except Exception as e:
sys.stdout = old_stdout
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
print(f"🌐 Starting Flask on port 7860...")
app.run(host='0.0.0.0', port=7860, debug=False)