Download app.py from AntonyUWU/tesisapi: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/AntonyUWU/tesisapi/resolve/main/app.py
- Command line
-
hf download hf://spaces/AntonyUWU/tesisapi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AntonyUWU/tesisapi/resolve/main/app.py
1.42 kB
| from fastapi import FastAPI, File, UploadFile | |
| import numpy as np | |
| import tensorflow as tf | |
| import keras | |
| from PIL import Image | |
| from io import BytesIO | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import time | |
| app = FastAPI() | |
| app.title = "API de prueba" | |
| app.version = "0.0.1" | |
| origins = ["*"] | |
| clases = ["Industria", "Golpeada", "Partida"] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Cargar el modelo una vez | |
| model = keras.saving.load_model("models/modelo_fin_effi.keras") | |
| def api_home(): | |
| return {'detail': 'Tesis Uvillas'} | |
| async def analyze(file: UploadFile = File(...)): | |
| image = await file.read() | |
| return predecir(image) | |
| def predecir(imagen): | |
| start_time = time.time() | |
| image = Image.open(BytesIO(imagen)) | |
| image = image.resize((96, 96)) | |
| input_arr = keras.utils.img_to_array(image) | |
| input_arr = np.array([input_arr]) | |
| predictions = model.predict(input_arr) | |
| end_time = time.time() | |
| prediction_time = end_time - start_time | |
| predicted_class = clases[np.argmax(predictions[0])] | |
| prediction_percentages = {clase: float(pred) * 100 for clase, pred in zip(clases, predictions[0])} | |
| return { | |
| 'clase_predicha': predicted_class, | |
| 'porcentajes_de_precisión': prediction_percentages, | |
| 'time':prediction_time | |
| } |