devdatanalytics/react
0
1from flask import Flask, request, jsonify2from fastai.vision.all import *3from huggingface_hub import from_pretrained_fastai4from PIL import Image5import io6 7app = Flask(__name__)8 9def classify_image(image_file):10 # Load the trained model from Huggingface11 learn = from_pretrained_fastai("devdatanalytics/commonbean")12 13 # Open the image file14 img = Image.open(image_file)15 16 # Perform any necessary preprocessing on the image17 # For example, resizing or normalization18 img = img.resize((224, 224)) # Resize the image to match the model's input size19 20 # Convert the image to a BytesIO object21 img_bytes = io.BytesIO()22 img.save(img_bytes, format='PNG')23 img_bytes.seek(0)24 25 # Perform the classification26 pred_class, pred_idx, probs = learn.predict(img_bytes)27 28 # Return the classification results29 return f"Predicted Class: {pred_class}, Probability: {probs[pred_idx]:.2f}"30 31@app.route('/classify', methods=['POST'])32def classify():33 if 'image' not in request.files:34 return "No image file found", 40035 36 image_file = request.files['image']37 38 # Perform image classification39 classification_results = classify_image(image_file)40 41 return jsonify(classification_results)42 43if __name__ == '__main__':44 app.run()