File size: 1,029 Bytes
3ef8448 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from flask import Flask, render_template, request, jsonify
from transformers import BertForSequenceClassification, BertTokenizer
import torch
app = Flask(__name__)
# Load the model's state_dict
model_state_dict = torch.load("bert_classifier_three_labeled.pth")
# Initialize the model with the same architecture
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
# Load the model's state_dict
model.load_state_dict(model_state_dict)
# Load the tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
def predict(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
return probs[0].tolist()
@app.route('/', methods=['GET', 'POST'])
def index():
result = None
if request.method == 'POST':
prompt = request.form['prompt']
result = predict(prompt)
return render_template('index.html', result=result)
if __name__ == '__main__':
app.run(debug=True)
|