File size: 1,345 Bytes
f056e4f
7ae04cd
f056e4f
 
 
 
7ae04cd
 
f056e4f
7ae04cd
 
f056e4f
7ae04cd
f056e4f
 
7ae04cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f056e4f
 
 
 
7ae04cd
f056e4f
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
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)))