Spaces:
No application file
No application file
MarceloLZR
commited on
Commit
•
1aff432
1
Parent(s):
23e61ac
Upload 3 files
Browse files- Apy.py +50 -0
- model.tflite +3 -0
- requirements.txt +5 -0
Apy.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Cargar el modelo TFLite
|
10 |
+
interpreter = tf.lite.Interpreter(model_path="model.tflite")
|
11 |
+
interpreter.allocate_tensors()
|
12 |
+
|
13 |
+
# Obtener detalles de las entradas y salidas del modelo
|
14 |
+
input_details = interpreter.get_input_details()
|
15 |
+
output_details = interpreter.get_output_details()
|
16 |
+
|
17 |
+
# Función para preprocesar la imagen
|
18 |
+
def preprocess_image(image):
|
19 |
+
image = cv2.resize(image, (224, 224))
|
20 |
+
image = image / 255.0
|
21 |
+
image = np.expand_dims(image, axis=0).astype(np.float32)
|
22 |
+
return image
|
23 |
+
|
24 |
+
# Ruta de predicción
|
25 |
+
@app.post("/predict/")
|
26 |
+
async def predict(file: UploadFile = File(...)):
|
27 |
+
try:
|
28 |
+
# Leer la imagen
|
29 |
+
image = await file.read()
|
30 |
+
image = cv2.imdecode(np.frombuffer(image, np.uint8), cv2.IMREAD_COLOR)
|
31 |
+
|
32 |
+
# Preprocesar la imagen
|
33 |
+
processed_image = preprocess_image(image)
|
34 |
+
|
35 |
+
# Realizar la predicción
|
36 |
+
interpreter.set_tensor(input_details[0]['index'], processed_image)
|
37 |
+
interpreter.invoke()
|
38 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
39 |
+
|
40 |
+
# Determinar la clase y la confianza
|
41 |
+
class_idx = np.argmax(output_data[0])
|
42 |
+
labels = ['Benign', 'Malignant']
|
43 |
+
result = labels[class_idx]
|
44 |
+
confidence = float(output_data[0][class_idx])
|
45 |
+
|
46 |
+
return {"class": result, "confidence": confidence}
|
47 |
+
except Exception as e:
|
48 |
+
raise HTTPException(status_code=500, detail=str(e))
|
49 |
+
|
50 |
+
|
model.tflite
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:77c9587ffe7289faecba206ef81375fe7204f096a9b0f52ff2da054b5d1a0ea3
|
3 |
+
size 11561972
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
tensorflow
|
4 |
+
opencv-python-headless
|
5 |
+
numpy
|