Ahil1991 commited on
Commit
0c9a461
·
verified ·
1 Parent(s): eebf07a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -1,38 +1,32 @@
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://ahil-mohammad.github.io/Bee-Server/" # 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)))
 
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)