Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,32 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
import
|
3 |
-
import
|
4 |
|
5 |
app = Flask(__name__)
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
|
10 |
-
|
|
|
|
|
|
|
11 |
def send_message():
|
12 |
try:
|
13 |
-
# Get the
|
14 |
-
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
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({
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
-
|
38 |
-
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from transformers import pipeline
|
3 |
+
from flask_cors import CORS # Import CORS
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
+
CORS(app) # Enable CORS for all routes
|
7 |
|
8 |
+
# Alternatively, you can enable CORS for specific origins only (e.g., your GitHub Pages URL)
|
9 |
+
# CORS(app, origins=["https://ahil-mohammad.github.io"])
|
10 |
|
11 |
+
# Load your Hugging Face model (e.g., a conversational model)
|
12 |
+
model = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
13 |
+
|
14 |
+
@app.route('/send_message', methods=['POST'])
|
15 |
def send_message():
|
16 |
try:
|
17 |
+
# Get the incoming message from the request
|
18 |
+
data = request.get_json()
|
19 |
+
user_message = data['message']
|
20 |
|
21 |
+
# Generate the model's response
|
22 |
+
response = model(user_message)
|
23 |
+
bot_reply = response[0]['generated_text'] # Assuming the model returns a list of conversations
|
24 |
+
|
25 |
+
# Return the response as a JSON
|
26 |
+
return jsonify({'response': bot_reply})
|
27 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
+
return jsonify({'error': str(e)}), 500
|
30 |
|
31 |
if __name__ == "__main__":
|
32 |
+
app.run(host="0.0.0.0", port=5000)
|
|