-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (45 loc) · 1.59 KB
/
app.py
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
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from test import generate_response
import os
from dotenv import load_dotenv
import logging
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__, static_folder='static')
CORS(app)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Serve static files
@app.route('/')
def index():
return send_from_directory('static', 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
# Main chat endpoint
@app.route("/api/chat", methods=["POST"])
def chat():
try:
data = request.get_json()
user_message = data.get("message")
if not user_message:
return jsonify({"error": "No message provided"}), 400
# Generate response using RAG system
llm_response = generate_response(user_message)
return jsonify({"response": llm_response})
except Exception as e:
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify({
"error": "Server error",
"message": "An unexpected error occurred. Please try again later."
}), 500
if __name__ == "__main__":
# Create static folder if it doesn't exist
if not os.path.exists('static'):
os.makedirs('static')
port = int(os.getenv("PORT", 5000))
debug = os.getenv("FLASK_ENV") == "development"
app.run(host="0.0.0.0", port=port, debug=debug)