Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
from tensorflow.keras.applications.inception_v3 import preprocess_input
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Lista de clases
|
| 11 |
+
class_names = ['acanthoica', 'akashiwo', 'alexandrium', 'amoeba', 'amphidinium', 'amylax', 'apedinella',
|
| 12 |
+
'asterionellopsis', 'bacillaria', 'bacteriastrum', 'biddulphia', 'calciopappus', 'cerataulina',
|
| 13 |
+
'ceratium', 'chaetoceros', 'chrysochromulina', 'cochlodinium', 'corethron', 'corymbellus',
|
| 14 |
+
'coscinodiscus', 'cryptophyta', 'cylindrotheca', 'dactyliosolen', 'delphineis', 'dictyocha',
|
| 15 |
+
'dinobryon', 'dinophysis', 'ditylum', 'emiliania', 'ephemera', 'eucampia', 'euglena',
|
| 16 |
+
'gonyaulax', 'guinardia', 'gyrodinium', 'hemiaulus', 'heterocapsa', 'karenia', 'katodinium',
|
| 17 |
+
'kryptoperidinium', 'laboea', 'lauderia', 'leptocylindrus', 'licmophora', 'nanoneis',
|
| 18 |
+
'odontella', 'ophiaster', 'ostreopsis', 'oxytoxum', 'paralia', 'parvicorbicula', 'phaeocystis',
|
| 19 |
+
'pleuronema', 'pleurosigma', 'polykrikos', 'prorocentrum', 'proterythropsis', 'protoperidinium',
|
| 20 |
+
'pseudo-nitzschia', 'pseudochattonella', 'pyramimonas', 'rhabdolithes', 'rhizosolenia',
|
| 21 |
+
'scrippsiella', 'skeletonema', 'stephanopyxis', 'syracosphaera', 'thalassionema', 'thalassiosira',
|
| 22 |
+
'trichodesmium', 'vicicitus', 'warnowia']
|
| 23 |
+
|
| 24 |
+
# Descargar y cargar el modelo desde Hugging Face Hub
|
| 25 |
+
model_path = hf_hub_download(repo_id="Daniel00611/InceptionV3_72", filename="InceptionV3_72.keras")
|
| 26 |
+
model = load_model(model_path)
|
| 27 |
+
|
| 28 |
+
def preprocess_image(image_file, target_size=(299, 299)):
|
| 29 |
+
img = image.load_img(image_file, target_size=target_size)
|
| 30 |
+
img_array = image.img_to_array(img)
|
| 31 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 32 |
+
img_array = preprocess_input(img_array)
|
| 33 |
+
return img_array
|
| 34 |
+
|
| 35 |
+
@app.post("/predict/")
|
| 36 |
+
async def predict(file: UploadFile = File(...)):
|
| 37 |
+
# Procesar la imagen
|
| 38 |
+
img_array = preprocess_image(file.file)
|
| 39 |
+
|
| 40 |
+
# Realizar la predicci贸n
|
| 41 |
+
predictions = model.predict(img_array)[0]
|
| 42 |
+
|
| 43 |
+
# Obtener el top 10 de predicciones
|
| 44 |
+
top_10_indices = predictions.argsort()[-10:][::-1]
|
| 45 |
+
top_10_classes = [class_names[i] for i in top_10_indices]
|
| 46 |
+
top_10_probabilities = predictions[top_10_indices]
|
| 47 |
+
|
| 48 |
+
# Formar la respuesta en formato JSON
|
| 49 |
+
result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
|
| 50 |
+
return {"predictions": result}
|
| 51 |
+
|
| 52 |
+
@app.get("/")
|
| 53 |
+
def greet_json():
|
| 54 |
+
return {"Hello": "World!"}
|