Spaces:
Sleeping
Sleeping
File size: 3,692 Bytes
6e39c64 2f91258 6e39c64 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import os
import uuid
import tempfile
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from Src.pipeline.rag_pipeline import RAGPipeline
from dotenv import load_dotenv
load_dotenv()
app = Flask(
__name__,
static_folder='frontend/dist/client',
static_url_path=""
)
CORS(app, resources={r"/api/*": {"origins": "*"}})
sessions: dict = {}
@app.route("/")
def home():
return {"message": "DocuMind AI Backend Running"}
@app.route("/api/upload", methods=["POST"])
def upload_pdf():
# 1. Validate file is in the request
if "file" not in request.files:
return jsonify({"error": "No file provided. Field name must be 'file'."}), 400
file = request.files["file"]
if not file.filename.lower().endswith(".pdf"):
return jsonify({"error": "Only PDF files are supported."}), 400
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
try:
file.save(tmp.name)
tmp.close()
pipeline = RAGPipeline(tmp.name)
result = pipeline.build_index()
print(f"[Upload] {result} | file: {file.filename}")
except Exception as e:
print(f"[Upload ERROR] {e}")
return jsonify({"error": f"Failed to process PDF: {str(e)}"}), 500
finally:
if os.path.exists(tmp.name):
os.unlink(tmp.name)
session_id = str(uuid.uuid4())
sessions[session_id] = {
"pipeline": pipeline,
"filename": file.filename
}
print(f"[Upload] Session created β {session_id}")
return jsonify({
"message": f"'{file.filename}' processed successfully.",
"session_id": session_id
}), 200
@app.route("/api/chat", methods=["POST"])
@app.route("/api/chat", methods=["POST"])
def chat():
data = request.get_json()
if not data:
return jsonify({"error": "Request body must be JSON."}), 400
question = data.get("question", "").strip()
session_id = data.get("session_id", "").strip()
if not question:
return jsonify({"error": "Question is required."}), 400
if not session_id:
return jsonify({"error": "Session ID is required."}), 400
session = sessions.get(session_id)
if not session:
return jsonify({
"error": "Session not found. Please upload PDF again."
}), 404
try:
pipeline = session["pipeline"]
answer = pipeline.ask(question)
print(f"[Chat] Q: {question}")
print(f"[Chat] A: {answer}")
return jsonify({
"answer": answer
}), 200
except Exception as e:
import traceback
print("\n========== CHAT ERROR ==========")
traceback.print_exc()
print("================================\n")
return jsonify({
"error": str(e)
}), 500
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve_react(path):
full_path = os.path.join(app.static_folder, path)
if path and os.path.exists(full_path):
return send_from_directory(app.static_folder, path)
return send_from_directory(app.static_folder, "index.html")
if __name__ == "__main__":
print("\n DocuMind AI β Server Starting")
print("=" * 45)
print(" Login β http://localhost:5000/login.html")
print(" Register β http://localhost:5000/register.html")
print(" App β http://localhost:5000")
print(" Upload β POST /api/upload")
print(" Chat β POST /api/chat")
print("=" * 45)
os.makedirs("artifacts/faiss_index", exist_ok=True)
port = int(os.environ.get("PORT", 7860))
app.run(host="0.0.0.0", port=port) |