Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['TRANSFORMERS_CACHE'] = '/data'
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
classifier = pipeline("image-classification", model="umutbozdag/plant-identity")
|
| 14 |
+
|
| 15 |
+
@app.route("/predict", methods=["POST"])
|
| 16 |
+
def predict():
|
| 17 |
+
if "file" not in request.files:
|
| 18 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 19 |
+
|
| 20 |
+
file = request.files["file"]
|
| 21 |
+
image_bytes = file.read()
|
| 22 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
results = classifier(image)
|
| 26 |
+
top_result = results[0]
|
| 27 |
+
label = top_result["label"]
|
| 28 |
+
score = round(top_result["score"] * 100, 2)
|
| 29 |
+
return jsonify({"label": label, "confidence": f"{score}%"})
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return jsonify({"error": str(e)}), 500
|
| 32 |
+
|
| 33 |
+
@app.route("/", methods=["GET"])
|
| 34 |
+
def home():
|
| 35 |
+
return "Bitki Tanıma API - Lumi 🌿"
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
app.run(host="0.0.0.0", port=7860)
|