SergioI1991 commited on
Commit
3074712
·
verified ·
1 Parent(s): 75abdf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py CHANGED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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])
23
+
24
+ predicted_index = np.argmax(score)
25
+ confidence = np.max(score) * 100
26
+ predicted_class = CLASS_NAMES[predicted_index]
27
+
28
+ other_index = 1 - predicted_index
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()