siranida commited on
Commit
0c71b1d
·
verified ·
1 Parent(s): 29885d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -23
app.py CHANGED
@@ -1,35 +1,22 @@
1
  from flask import Flask, request, jsonify
2
  from transformers import pipeline
3
- from PIL import Image
4
- import io
 
 
 
5
 
6
  app = Flask(__name__)
7
 
8
- # Hugging Face modelini yüklüyoruz (tek seferlik)
9
  classifier = pipeline("image-classification", model="ahmed792002/vit-plant-classification")
10
 
11
- @app.route("/", methods=["GET"])
12
- def home():
13
- return "Lumi çalışıyor 🌿"
14
-
15
- @app.route("/predict", methods=["POST"])
16
  def predict():
17
  if 'image' not in request.files:
18
- return jsonify({"error": "Görsel dosyası 'image' olarak gönderilmeli."}), 400
19
 
20
- image_file = request.files['image']
21
- try:
22
- image = Image.open(image_file.stream)
23
- results = classifier(image)
24
- # En yüksek olasılıklı sonucu al
25
- best = results[0]
26
- label = best["label"]
27
- score = round(best["score"] * 100, 2)
28
 
29
- return jsonify({
30
- "bitkiAdi": label,
31
- "tahminOrani": f"%{score}"
32
- })
33
- except Exception as e:
34
- return jsonify({"error": str(e)}), 500
35
 
 
1
  from flask import Flask, request, jsonify
2
  from transformers import pipeline
3
+ import os
4
+
5
+ # Önemli: huggingface hub cache dizinini değiştir
6
+ os.environ['HF_HOME'] = '/tmp/huggingface'
7
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers'
8
 
9
  app = Flask(__name__)
10
 
 
11
  classifier = pipeline("image-classification", model="ahmed792002/vit-plant-classification")
12
 
13
+ @app.route('/predict', methods=['POST'])
 
 
 
 
14
  def predict():
15
  if 'image' not in request.files:
16
+ return jsonify({'error': 'No image uploaded'}), 400
17
 
18
+ image = request.files['image']
19
+ predictions = classifier(image)
20
+ return jsonify(predictions)
 
 
 
 
 
21
 
 
 
 
 
 
 
22