Ahil1991 commited on
Commit
7ae04cd
·
verified ·
1 Parent(s): f5ea211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -1,32 +1,38 @@
1
  from flask import Flask, request, jsonify
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import os
4
 
5
  app = Flask(__name__)
6
 
7
- # Load model and tokenizer
8
- MODEL_NAME = "bigscience/bloomz-1b1" # Replace with your model name
9
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
11
 
12
- @app.route("/api/chat", methods=["POST"])
13
- def chat():
14
  try:
15
- # Get user message from the POST request
16
  user_message = request.json.get("message", "")
17
 
18
- # Tokenize user input
19
- inputs = tokenizer(user_message, return_tensors="pt", truncation=True, max_length=512)
20
-
21
- # Generate response
22
- outputs = model.generate(inputs["input_ids"], max_length=150, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
23
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
-
25
- # Return the response as JSON
26
- return jsonify({"response": response})
 
 
 
 
 
 
 
 
27
  except Exception as e:
28
  return jsonify({"error": str(e)}), 500
29
 
30
  if __name__ == "__main__":
31
- # Use the default port for Hugging Face Spaces
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)))