Ahil1991 commited on
Commit
cf6718c
·
verified ·
1 Parent(s): f545b20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,15 +1,13 @@
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():
@@ -18,9 +16,14 @@ def send_message():
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})
 
1
  from flask import Flask, request, jsonify
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from flask_cors import CORS
4
 
5
  app = Flask(__name__)
6
+ CORS(app) # Enable CORS for all routes (you can restrict this if needed)
7
 
8
+ # Load BloomZ model and tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-1b1")
10
+ model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-1b1")
 
 
11
 
12
  @app.route('/send_message', methods=['POST'])
13
  def send_message():
 
16
  data = request.get_json()
17
  user_message = data['message']
18
 
19
+ # Tokenize the input message
20
+ inputs = tokenizer(user_message, return_tensors="pt")
21
+
22
+ # Generate response from the model
23
+ outputs = model.generate(inputs['input_ids'], max_length=50, num_return_sequences=1)
24
+
25
+ # Decode the response
26
+ bot_reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
 
28
  # Return the response as a JSON
29
  return jsonify({'response': bot_reply})