Spaces:
Runtime error
Runtime error
Download app.py from manasmaskar/Customer-AI-AGENT: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/manasmaskar/Customer-AI-AGENT/resolve/main/app.py
- Command line
-
hf download hf://spaces/manasmaskar/Customer-AI-AGENT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/manasmaskar/Customer-AI-AGENT/resolve/main/app.py
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() | |
| def home(): | |
| return render_template('index.html') | |
| def health(): | |
| return jsonify({ | |
| "status": "healthy" if rag_ready else "degraded", | |
| "rag_ready": rag_ready, | |
| "rag_initialized": rag is not None | |
| }) | |
| 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) | |