yashriva commited on
Commit
41fdb00
1 Parent(s): 91494a2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request
2
+ from transformers import pipeline, AutoTokenizer
3
+ import joblib
4
+ import json
5
+
6
+ # Load the model
7
+ model = joblib.load("iris_svm.joblib")
8
+
9
+ # Load the configuration file
10
+ with open("config.json", "r") as f:
11
+ config = json.load(f)
12
+
13
+ # Get the input features and target variable
14
+ features = config["features"]
15
+ target = config["targets"][0]
16
+ target_mapping = config["target_mapping"]
17
+
18
+ # Initialize the tokenizer
19
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
20
+
21
+ # Initialize the Flask app
22
+ app = Flask(__name__)
23
+
24
+ # Define the prediction route
25
+ @app.route("/predict", methods=["POST"])
26
+ def predict():
27
+ # Get the input data
28
+ input_data = request.json
29
+
30
+ # Construct the input text for the pipeline
31
+ input_text = f"SepalLengthCm: {input_data['SepalLengthCm']}, SepalWidthCm: {input_data['SepalWidthCm']}, PetalLengthCm: {input_data['PetalLengthCm']}, PetalWidthCm: {input_data['PetalWidthCm']}"
32
+
33
+ # Tokenize the input text
34
+ tokenized_input = tokenizer(input_text, return_tensors="pt")
35
+
36
+ # Make a prediction using the pipeline
37
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0)
38
+ predicted_class_id = classifier(tokenized_input)[0]['label']
39
+
40
+ # Convert the predicted class ID to a class name
41
+ predicted_class_name = list(target_mapping.keys())[list(target_mapping.values()).index(predicted_class_id)]
42
+
43
+ # Return the predicted class name as a JSON response
44
+ return jsonify({"predicted_class": predicted_class_name})
45
+
46
+ # Run the Flask app
47
+ if __name__ == "__main__":
48
+ app.run(debug=True)