Download app/main.py from BalabolS/test: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/BalabolS/test/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/BalabolS/test/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/BalabolS/test/resolve/main/app/main.py
1.25 kB
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.responses import JSONResponse | |
| from PIL import Image | |
| import numpy as np | |
| import tensorflow as tf | |
| import io | |
| app = FastAPI() | |
| # Загрузка модели | |
| model = tf.keras.models.load_model('fine_tuned_model.h5') | |
| class_names = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] | |
| # Трансформация изображения | |
| def transform_image(image_bytes): | |
| image = Image.open(io.BytesIO(image_bytes)).convert("RGB") | |
| image = image.resize((32, 32)) | |
| image = np.array(image) / 255.0 | |
| image = np.expand_dims(image, axis=0) | |
| return image | |
| def healthcheck(): | |
| return JSONResponse(status_code=200, content={}) | |
| async def detect_objects(file: UploadFile = File(...)): | |
| image_bytes = await file.read() | |
| image = transform_image(image_bytes) | |
| predictions = model.predict(image) | |
| predicted_class = class_names[np.argmax(predictions[0])] | |
| response = { | |
| "class": predicted_class, | |
| "confidence": float(np.max(predictions[0])) | |
| } | |
| return JSONResponse(content=response) | |
| async def main(): | |
| return {"message": "FastAPI Object Detection"} | |