bee-back / app.py
Ahil1991's picture
Update app.py
7ae04cd verified
raw
history blame
1.35 kB
from flask import Flask, request, jsonify
import requests
import os
app = Flask(__name__)
# URL of the external chatbot API
EXTERNAL_CHATBOT_URL = "https://your-external-chatbot-url.com/api/chat" # Replace with the correct URL
@app.route("/send_message", methods=["POST"])
def send_message():
try:
# Get the user message from the frontend
user_message = request.json.get("message", "")
# Send the message to the external chatbot API
external_response = requests.post(
EXTERNAL_CHATBOT_URL,
json={"message": user_message},
headers={"Content-Type": "application/json"}
)
# Check if the external API responded successfully
if external_response.status_code == 200:
# Get the chatbot's response
chatbot_response = external_response.json().get("response", "No response.")
else:
chatbot_response = f"Error from external chatbot: {external_response.status_code}"
# Send the chatbot response back to the frontend
return jsonify({"response": chatbot_response})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
# Default host and port for Hugging Face Spaces
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))