Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,38 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
|
3 |
import os
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
11 |
|
12 |
-
@app.route("/
|
13 |
-
def
|
14 |
try:
|
15 |
-
# Get user message from the
|
16 |
user_message = request.json.get("message", "")
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
return jsonify({"error": str(e)}), 500
|
29 |
|
30 |
if __name__ == "__main__":
|
31 |
-
#
|
32 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
import os
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# URL of the external chatbot API
|
8 |
+
EXTERNAL_CHATBOT_URL = "https://your-external-chatbot-url.com/api/chat" # Replace with the correct URL
|
|
|
|
|
9 |
|
10 |
+
@app.route("/send_message", methods=["POST"])
|
11 |
+
def send_message():
|
12 |
try:
|
13 |
+
# Get the user message from the frontend
|
14 |
user_message = request.json.get("message", "")
|
15 |
|
16 |
+
# Send the message to the external chatbot API
|
17 |
+
external_response = requests.post(
|
18 |
+
EXTERNAL_CHATBOT_URL,
|
19 |
+
json={"message": user_message},
|
20 |
+
headers={"Content-Type": "application/json"}
|
21 |
+
)
|
22 |
+
|
23 |
+
# Check if the external API responded successfully
|
24 |
+
if external_response.status_code == 200:
|
25 |
+
# Get the chatbot's response
|
26 |
+
chatbot_response = external_response.json().get("response", "No response.")
|
27 |
+
else:
|
28 |
+
chatbot_response = f"Error from external chatbot: {external_response.status_code}"
|
29 |
+
|
30 |
+
# Send the chatbot response back to the frontend
|
31 |
+
return jsonify({"response": chatbot_response})
|
32 |
+
|
33 |
except Exception as e:
|
34 |
return jsonify({"error": str(e)}), 500
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
+
# Default host and port for Hugging Face Spaces
|
38 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|