Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importar librer铆as
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
from sklearn.datasets import load_iris
|
6 |
+
import tensorflow as tf
|
7 |
+
from keras.models import Sequential
|
8 |
+
from keras.layers import Dense
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
# Cargar el conjunto de datos Iris
|
12 |
+
iris = load_iris()
|
13 |
+
X = iris.data
|
14 |
+
y = iris.target
|
15 |
+
|
16 |
+
# Dividir el conjunto de datos en entrenamiento y prueba
|
17 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
18 |
+
|
19 |
+
# Normalizar los datos
|
20 |
+
scaler = StandardScaler()
|
21 |
+
X_train = scaler.fit_transform(X_train)
|
22 |
+
X_test = scaler.transform(X_test)
|
23 |
+
|
24 |
+
# Construir el modelo
|
25 |
+
model = Sequential([
|
26 |
+
Dense(64, activation='relu', input_dim=4), # Capa oculta con 64 neuronas y funci贸n de activaci贸n ReLU
|
27 |
+
Dense(3, activation='softmax') # Capa de salida con 3 neuronas para las clases de iris
|
28 |
+
])
|
29 |
+
|
30 |
+
# Compilar el modelo
|
31 |
+
model.compile(optimizer='adam',
|
32 |
+
loss='sparse_categorical_crossentropy',
|
33 |
+
metrics=['accuracy'])
|
34 |
+
|
35 |
+
# Entrenar el modelo
|
36 |
+
model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test))
|
37 |
+
|
38 |
+
# Definir una funci贸n para la predicci贸n
|
39 |
+
def predict_iris_species(sepal_length, sepal_width, petal_length, petal_width):
|
40 |
+
input_data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
|
41 |
+
scaled_input_data = scaler.transform(input_data)
|
42 |
+
prediction = model.predict(scaled_input_data)
|
43 |
+
return iris.target_names[np.argmax(prediction)]
|
44 |
+
|
45 |
+
# Definir la interfaz Gradio
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=predict_iris_species,
|
48 |
+
inputs=[
|
49 |
+
gr.Slider(minimum=0, maximum=10, label="Sepal Length"),
|
50 |
+
gr.Slider(minimum=0, maximum=10, label="Sepal Width"),
|
51 |
+
gr.Slider(minimum=0, maximum=10, label="Petal Length"),
|
52 |
+
gr.Slider(minimum=0, maximum=10, label="Petal Width"),
|
53 |
+
],
|
54 |
+
outputs=gr.Label(num_top_classes=3),
|
55 |
+
live=True,
|
56 |
+
)
|
57 |
+
|
58 |
+
# Ejecutar la interfaz
|
59 |
+
iface.launch()
|