whtet commited on
Commit
504dba0
·
verified ·
1 Parent(s): b541fc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -3
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
- user_message = request.json.get("message", "")
33
- bot_reply = generate_response(user_message)
34
- return jsonify({"reply": bot_reply})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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