Spaces:
Runtime error

File size: 2,186 Bytes
9242a52
ef8f606
 
9242a52
 
 
ef8f606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9242a52
 
 
 
 
 
 
ef8f606
 
 
 
 
9242a52
 
 
ef8f606
 
 
9242a52
 
 
ef8f606
9242a52
 
 
 
 
ef8f606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9242a52
 
ef8f606
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)