Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
from transformers import
|
| 3 |
-
from flask_cors import CORS
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
-
CORS(app) # Enable CORS for all routes
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 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 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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})
|