Spaces:
Runtime error
Runtime error
Commit ·
8aef683
1
Parent(s): a4ba4db
Despliegue junto al modelo
Browse files- app.py +46 -0
- modelo_xor.pt +3 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class XORModel(nn.Module):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super(XORModel, self).__init__()
|
| 9 |
+
self.fc1 = nn.Linear(2, 2)
|
| 10 |
+
self.fc2 = nn.Linear(2, 1)
|
| 11 |
+
|
| 12 |
+
def forward(self, x):
|
| 13 |
+
x = torch.sigmoid(self.fc1(x))
|
| 14 |
+
x = torch.sigmoid(self.fc2(x))
|
| 15 |
+
return x
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
model = XORModel()
|
| 19 |
+
model.load_state_dict(torch.load("modelo_xor.pt"))
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def predict(x1, x2):
|
| 24 |
+
resultado = 0
|
| 25 |
+
inputs = torch.tensor([[x1, x2]], dtype=torch.float32)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
prediction = model(inputs)
|
| 28 |
+
prediction = (prediction > 0.5).item()
|
| 29 |
+
if prediction == 0:
|
| 30 |
+
resultado = 0
|
| 31 |
+
else:
|
| 32 |
+
resultado = 1
|
| 33 |
+
return resultado
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Define la interfaz Gradio
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=predict,
|
| 39 |
+
inputs=[gr.components.Number(), gr.components.Number()],
|
| 40 |
+
outputs=gr.components.Label(),
|
| 41 |
+
title="Modelo XOR con PyTorch",
|
| 42 |
+
description="Ingresa dos números (0 o 1) para predecir el resultado XOR.",
|
| 43 |
+
live=True, # Muestra el resultado en tiempo real
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
iface.launch()
|
modelo_xor.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d48c0a032db897c8015375217d3a43306ccc50684be105533a9b716fcbe5be7a
|
| 3 |
+
size 2088
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|