Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,9 +29,28 @@ def home():
|
|
29 |
# API route for chatbot responses
|
30 |
@app.route("/chat", methods=["POST"])
|
31 |
def chat():
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
port = int(os.environ.get("PORT", 7860)) # Default to 7860, but use any assigned port
|
|
|
29 |
# API route for chatbot responses
|
30 |
@app.route("/chat", methods=["POST"])
|
31 |
def chat():
|
32 |
+
try:
|
33 |
+
if not request.is_json:
|
34 |
+
print("Error: Request is not JSON")
|
35 |
+
return jsonify({"error": "Request must be JSON"}), 415
|
36 |
+
|
37 |
+
data = request.get_json()
|
38 |
+
user_message = data.get("message", "")
|
39 |
+
|
40 |
+
if not user_message:
|
41 |
+
print("Error: No message received")
|
42 |
+
return jsonify({"error": "No message provided"}), 400
|
43 |
+
|
44 |
+
print(f"Received message: {user_message}")
|
45 |
+
|
46 |
+
bot_reply = generate_response(user_message)
|
47 |
+
print(f"AI response: {bot_reply}")
|
48 |
+
|
49 |
+
return jsonify({"reply": bot_reply})
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error processing request: {e}")
|
53 |
+
return jsonify({"error": str(e)}), 500
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
port = int(os.environ.get("PORT", 7860)) # Default to 7860, but use any assigned port
|