|
from flask import Flask, jsonify, request |
|
from transformers import pipeline, AutoTokenizer |
|
import joblib |
|
import json |
|
|
|
|
|
model = joblib.load("iris_svm.joblib") |
|
|
|
|
|
with open("config.json", "r") as f: |
|
config = json.load(f) |
|
|
|
|
|
features = config["features"] |
|
target = config["targets"][0] |
|
target_mapping = config["target_mapping"] |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route("/predict", methods=["POST"]) |
|
def predict(): |
|
|
|
input_data = request.json |
|
|
|
|
|
input_text = f"SepalLengthCm: {input_data['SepalLengthCm']}, SepalWidthCm: {input_data['SepalWidthCm']}, PetalLengthCm: {input_data['PetalLengthCm']}, PetalWidthCm: {input_data['PetalWidthCm']}" |
|
|
|
|
|
tokenized_input = tokenizer(input_text, return_tensors="pt") |
|
|
|
|
|
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0) |
|
predicted_class_id = classifier(tokenized_input)[0]['label'] |
|
|
|
|
|
predicted_class_name = list(target_mapping.keys())[list(target_mapping.values()).index(predicted_class_id)] |
|
|
|
|
|
return jsonify({"predicted_class": predicted_class_name}) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |
|
|