Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import numpy as np
|
| 7 |
-
import io
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
CORS(app)
|
| 11 |
-
|
| 12 |
-
# Load pre-trained model (MobileNetV2 - lightweight for free tier)
|
| 13 |
-
model = MobileNetV2(weights='imagenet')
|
| 14 |
-
|
| 15 |
-
@app.route('/health', methods=['GET'])
|
| 16 |
-
def health():
|
| 17 |
-
return jsonify({'status': 'healthy', 'model': 'MobileNetV2'})
|
| 18 |
-
|
| 19 |
-
@app.route('/predict', methods=['POST'])
|
| 20 |
-
def predict():
|
| 21 |
-
try:
|
| 22 |
-
if 'image' not in request.files:
|
| 23 |
-
return jsonify({'error': 'No image provided'}), 400
|
| 24 |
-
|
| 25 |
-
file = request.files['image']
|
| 26 |
-
img = Image.open(io.BytesIO(file.read()))
|
| 27 |
-
|
| 28 |
-
# Preprocess image
|
| 29 |
-
img = img.resize((224, 224))
|
| 30 |
-
img_array = np.array(img)
|
| 31 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 32 |
-
img_array = preprocess_input(img_array)
|
| 33 |
-
|
| 34 |
-
# Make prediction
|
| 35 |
-
predictions = model.predict(img_array)
|
| 36 |
-
decoded = decode_predictions(predictions, top=5)[0]
|
| 37 |
-
|
| 38 |
-
results = [
|
| 39 |
-
{'label': label, 'confidence': float(confidence)}
|
| 40 |
-
for (_, label, confidence) in decoded
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
return jsonify({
|
| 44 |
-
'success': True,
|
| 45 |
-
'predictions': results
|
| 46 |
-
})
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
return jsonify({'error': str(e)}), 500
|
| 50 |
-
|
| 51 |
-
if __name__ == '__main__':
|
| 52 |
-
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|