Spaces:
Sleeping
Sleeping
File size: 1,616 Bytes
9be21ef 23b0ad9 9be21ef | 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 | # app/app.py
from pathlib import Path
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from services.chat_service import ChatService
BASE_DIR = Path(__file__).resolve().parent.parent
FRONTEND_DIST = BASE_DIR / "FRONTEND" / "dist"
app = Flask(
__name__,
static_folder=str(FRONTEND_DIST),
static_url_path="/",
)
CORS(app)
chatbot = ChatService()
@app.route("/")
def index():
index_path = FRONTEND_DIST / "index.html"
if index_path.exists():
return send_from_directory(app.static_folder, "index.html")
return (
"React build not found. Run `npm install` and `npm run build` in "
"EmpowerHer_Chatbot/FRONTEND, then start the backend.",
404,
)
@app.route("/<path:path>")
def static_proxy(path: str):
file_path = FRONTEND_DIST / path
if file_path.exists():
return send_from_directory(app.static_folder, path)
return send_from_directory(app.static_folder, "index.html")
@app.route("/chat", methods=["POST"])
def chat():
data = request.json
message = data.get("message", "")
history = data.get("history", [])
result = chatbot.generate_reply(message, history=history)
return jsonify({
"reply": result.reply,
"emotions": result.emotions,
"raw_emotions": result.raw_emotions,
"topic": result.topic,
"intent": result.intent,
"kb_sources": result.kb_sources,
"escalation_level": result.escalation_level,
"escalation_reasons": result.escalation_reasons,
})
if __name__ == "__main__":
app.run(debug=True)
|