|
from flask import Flask, request, jsonify |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from flask_cors import CORS |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-1b1") |
|
model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-1b1") |
|
|
|
@app.route('/send_message', methods=['POST']) |
|
def send_message(): |
|
try: |
|
|
|
data = request.get_json() |
|
user_message = data['message'] |
|
|
|
|
|
inputs = tokenizer(user_message, return_tensors="pt") |
|
|
|
|
|
outputs = model.generate(inputs['input_ids'], max_length=50, num_return_sequences=1) |
|
|
|
|
|
bot_reply = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
return jsonify({'response': bot_reply}) |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=5000) |
|
|