SergioI1991 commited on
Commit
256145e
verified
1 Parent(s): fdea08f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -3,20 +3,20 @@ import tensorflow as tf
3
  import numpy as np
4
 
5
  IMG_SIZE = (224, 224)
 
6
  CLASS_NAMES = ['no_valido', 'valido']
7
 
 
 
 
8
  def preprocess_image(img):
 
9
  img = tf.image.resize(img, IMG_SIZE)
10
  img_array = tf.expand_dims(img, 0)
11
  img_array = img_array / 255.0
12
  return img_array
13
 
14
- def predict(rx_image, keras_model_file):
15
- model_path = "temp_model.keras"
16
- with open(model_path, "wb") as f:
17
- f.write(keras_model_file.read())
18
-
19
- model = tf.keras.models.load_model(model_path)
20
  img_array = preprocess_image(rx_image)
21
  preds = model.predict(img_array)
22
  score = tf.nn.softmax(preds[0])
@@ -29,16 +29,23 @@ def predict(rx_image, keras_model_file):
29
  other_class = CLASS_NAMES[other_index]
30
  other_confidence = score[other_index] * 100
31
 
32
- return f"Resultado: {predicted_class.upper()} ({confidence:.2f}%)\n" + \
33
- f"Probabilidad {other_class}: {other_confidence:.2f}%"
 
 
 
 
 
 
 
 
34
 
 
35
  iface = gr.Interface(
36
  fn=predict,
37
- inputs=[
38
- gr.Image(type="numpy", label="Sube tu RX"),
39
- gr.File(file_types=[".keras"], label="Sube tu modelo Keras")
40
- ],
41
- outputs=gr.Textbox(label="Resultado")
42
  )
43
 
44
  iface.launch()
 
3
  import numpy as np
4
 
5
  IMG_SIZE = (224, 224)
6
+ MODEL_PATH = "dental_classifier_model.keras"
7
  CLASS_NAMES = ['no_valido', 'valido']
8
 
9
+ # Cargar modelo una vez al inicio
10
+ model = tf.keras.models.load_model(MODEL_PATH)
11
+
12
  def preprocess_image(img):
13
+ # Redimensionar y normalizar la imagen
14
  img = tf.image.resize(img, IMG_SIZE)
15
  img_array = tf.expand_dims(img, 0)
16
  img_array = img_array / 255.0
17
  return img_array
18
 
19
+ def predict(rx_image):
 
 
 
 
 
20
  img_array = preprocess_image(rx_image)
21
  preds = model.predict(img_array)
22
  score = tf.nn.softmax(preds[0])
 
29
  other_class = CLASS_NAMES[other_index]
30
  other_confidence = score[other_index] * 100
31
 
32
+ # Crear texto de salida detallado
33
+ resultado_texto = (
34
+ f"--- Resultado de la Clasificaci贸n ---\n"
35
+ f"La imagen es: **{predicted_class.upper()}**\n"
36
+ f"Confianza: {confidence:.2f}%\n"
37
+ f"Probabilidad de ser {other_class}: {other_confidence:.2f}%\n"
38
+ f"------------------------------------"
39
+ )
40
+
41
+ return rx_image, resultado_texto
42
 
43
+ # Interfaz Gradio
44
  iface = gr.Interface(
45
  fn=predict,
46
+ inputs=gr.Image(type="numpy", label="Sube tu RX"),
47
+ outputs=[gr.Image(type="numpy", label="RX procesada"), gr.Textbox(label="Resultado")],
48
+ live=False
 
 
49
  )
50
 
51
  iface.launch()