siranida commited on
Commit
c2bb3fa
·
verified ·
1 Parent(s): f9bc19a

Update app.py

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