Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,126 @@
|
|
| 1 |
-
import json
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import onnxruntime
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from torchvision import transforms
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
# Cargar el modelo ONNX
|
| 11 |
+
ort_session = onnxruntime.InferenceSession("model_new_new_final.onnx")
|
| 12 |
+
|
| 13 |
+
# Abrir archivo JSON
|
| 14 |
+
with open('dat.json') as f:
|
| 15 |
+
data = json.load(f)
|
| 16 |
+
|
| 17 |
+
keys = list(data)
|
| 18 |
+
|
| 19 |
+
# DataFrame de ejemplo
|
| 20 |
+
simple = pd.DataFrame(
|
| 21 |
+
{
|
| 22 |
+
"item": keys,
|
| 23 |
+
"probability": [0] * len(keys)
|
| 24 |
+
}
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def Predict(image):
|
| 28 |
+
# Preprocesar la imagen
|
| 29 |
+
img = cv2.resize(image, (100, 100))
|
| 30 |
+
|
| 31 |
+
# Convertir el arreglo NumPy de vuelta a una imagen PIL
|
| 32 |
+
image = Image.fromarray(image)
|
| 33 |
+
|
| 34 |
+
# Preprocesar la imagen
|
| 35 |
+
img = image.resize((100, 100))
|
| 36 |
+
|
| 37 |
+
# Definir transformaciones
|
| 38 |
+
test_tfms = transforms.Compose([
|
| 39 |
+
transforms.Resize((100, 100)),
|
| 40 |
+
transforms.ToTensor(),
|
| 41 |
+
transforms.Normalize(mean=[0.7611, 0.5869, 0.5923], std=[0.1266, 0.1487, 0.1619])
|
| 42 |
+
])
|
| 43 |
+
|
| 44 |
+
# Aplicar transformaciones
|
| 45 |
+
input_image = test_tfms(img).unsqueeze(0).numpy() # Agregar dimension de lote y convertir a arreglo numpy
|
| 46 |
+
|
| 47 |
+
# Preparar tensor de entrada
|
| 48 |
+
input_name = ort_session.get_inputs()[0].name
|
| 49 |
+
input_dict = {input_name: input_image}
|
| 50 |
+
|
| 51 |
+
# Ejecutar inferencia
|
| 52 |
+
output = ort_session.run(None, input_dict)
|
| 53 |
+
|
| 54 |
+
# Obtener el indice de la clase predicha
|
| 55 |
+
prediction_idx = np.argmax(output)
|
| 56 |
+
|
| 57 |
+
# Recuperar informacion del JSON basada en la clase predicha
|
| 58 |
+
disease_name = keys[prediction_idx]
|
| 59 |
+
description = data[disease_name]['description']
|
| 60 |
+
symptoms = data[disease_name]['symptoms']
|
| 61 |
+
causes = data[disease_name]['causes']
|
| 62 |
+
treatment = data[disease_name]['treatment-1']
|
| 63 |
+
|
| 64 |
+
# Obtener probabilidades para cada clase y convertirlas a enteros
|
| 65 |
+
probabilities = output[0]
|
| 66 |
+
ints_probabilities = probabilities_to_ints(probabilities)
|
| 67 |
+
|
| 68 |
+
# Actualizar la probabilidad en el DataFrame
|
| 69 |
+
simple["probability"] = ints_probabilities[0]
|
| 70 |
+
|
| 71 |
+
# Crear grafico de barras para las probabilidades
|
| 72 |
+
bar_plot = gr.BarPlot(
|
| 73 |
+
value=simple,
|
| 74 |
+
x="item",
|
| 75 |
+
y="probability",
|
| 76 |
+
y_title="Probabilidad",
|
| 77 |
+
x_title="Nombre de la Enfermedad",
|
| 78 |
+
title="Distribucion de Probabilidad",
|
| 79 |
+
tooltip=["item", "probability"],
|
| 80 |
+
vertical = False
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
return disease_name, description, symptoms, causes, treatment, bar_plot
|
| 84 |
+
|
| 85 |
+
# Funcion para convertir probabilidades a enteros
|
| 86 |
+
def probabilities_to_ints(probabilities, total_sum=100):
|
| 87 |
+
# Filtrar los valores negativos
|
| 88 |
+
positive_values = np.maximum(probabilities, 0)
|
| 89 |
+
|
| 90 |
+
# Encontrar el peso positivo total
|
| 91 |
+
total_positive_weight = np.sum(positive_values)
|
| 92 |
+
|
| 93 |
+
# Calcular probabilidades escaladas para valores positivos
|
| 94 |
+
scaled_probabilities = np.zeros_like(probabilities)
|
| 95 |
+
if total_positive_weight > 0:
|
| 96 |
+
scaled_probabilities = positive_values / total_positive_weight * total_sum
|
| 97 |
+
|
| 98 |
+
# Redondear las probabilidades escaladas a enteros
|
| 99 |
+
rounded_probabilities = np.round(scaled_probabilities).astype(int)
|
| 100 |
+
|
| 101 |
+
# Ajustar por errores de redondeo
|
| 102 |
+
rounding_diff = total_sum - np.sum(rounded_probabilities)
|
| 103 |
+
if rounding_diff != 0 and np.sum(positive_values) > 0:
|
| 104 |
+
# Agregar la diferencia de redondeo a la clase con mayor peso positivo
|
| 105 |
+
max_positive_index = np.argmax(positive_values)
|
| 106 |
+
flattened_probabilities = rounded_probabilities.flatten()
|
| 107 |
+
flattened_probabilities[max_positive_index] += rounding_diff
|
| 108 |
+
rounded_probabilities = np.reshape(flattened_probabilities, rounded_probabilities.shape)
|
| 109 |
+
|
| 110 |
+
return rounded_probabilities
|
| 111 |
+
|
| 112 |
+
# Definir la interfaz Gradio
|
| 113 |
+
demo = gr.Interface(fn=Predict,
|
| 114 |
+
inputs="image",
|
| 115 |
+
outputs=[
|
| 116 |
+
gr.Textbox(label='Nombre de la Enfermedad', type="text"),
|
| 117 |
+
gr.Textbox(label='Descripcion', type="text"),
|
| 118 |
+
gr.Textbox(label='Sintomas', type="text"),
|
| 119 |
+
gr.Textbox(label='Causas', type="text"),
|
| 120 |
+
gr.Textbox(label='Tratamiento', type="text"),
|
| 121 |
+
"bar_plot"
|
| 122 |
+
],
|
| 123 |
+
title="Clasificacion de Enfermedades de la Piel",
|
| 124 |
+
description = 'Este espacio se ha desarrollado como parte de una tesis para la Universidad Central de Venezuela con el proposito de realizar diagnosticos precisos sobre una variedad de lesiones cutaneas. Su objetivo es ayudar en la identificacion temprana y precisa de condiciones dermatologicas, incluyendo:\n\n1)Queratosis Actinica \n\n2)Carcinoma Basocelular \n\n3)Dermatofibroma \n\n4)Melanoma \n\n5)Nevus \n\n6)Queratosis Pigmentada Benigna \n\n7)Queratosis Seborreica \n\n8)Carcinoma de Celulas Escamosas \n\n9)Lesion Vascular \n\n')
|
| 125 |
+
|
| 126 |
+
demo.launch(debug=True)
|