-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (48 loc) · 1.75 KB
/
Copy pathapp.py
File metadata and controls
63 lines (48 loc) · 1.75 KB
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
import os
import logging
from flask import Flask, request, jsonify, render_template
from utils.chatbot_utils import load_faq_data, prepare_embeddings, get_response
app = Flask(__name__)
# Config
PORT = int(os.getenv("PORT", 5000))
DEBUG = os.getenv("DEBUG", "False").lower() == "true"
LOG_FILE = os.getenv("LOG_FILE", "chatbot.log")
# Logging setup (stdout for Render)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Load FAQ data & FAISS index
faq_data = load_faq_data()
# Optional: save/load index from disk
INDEX_FILE = "faq_index.bin"
if os.path.exists(INDEX_FILE):
from utils.chatbot_utils import faiss
faq_index = faiss.read_index(INDEX_FILE)
faq_embeddings = None # embeddings are stored in index
else:
faq_index, faq_embeddings = prepare_embeddings(faq_data)
# Save index for future use
from utils.chatbot_utils import faiss
faiss.write_index(faq_index, INDEX_FILE)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/chat", methods=["POST"])
def chat():
try:
user_input = request.json.get("message", "").strip()
if not user_input:
return jsonify({"error": "No message provided"}), 400
response, confidence = get_response(user_input, faq_data, faq_index)
logging.info(f"User: {user_input}")
logging.info(f"Bot: {response} (confidence: {confidence:.2f})")
return jsonify({"response": response})
except Exception as e:
logging.error(f"Error in /chat: {e}")
return jsonify({"error": "Internal server error"}), 500
@app.route("/health")
def health():
return jsonify({"status": "ok"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=PORT, debug=DEBUG)