Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,30 @@
|
|
| 1 |
import torch
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
from config import MLP
|
| 5 |
|
| 6 |
-
# Load model
|
| 7 |
-
model =
|
| 8 |
model.load_state_dict(torch.load("pytorch_model.pth", map_location=torch.device("cpu")))
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Prediction function
|
| 15 |
-
def predict(input_vector):
|
| 16 |
-
input_array = np.array(input_vector).astype(np.float32)
|
| 17 |
-
if len(input_array) != 1000:
|
| 18 |
-
return "Error: Input must be 1000 numbers"
|
| 19 |
-
tensor = torch.tensor(input_array).unsqueeze(0)
|
| 20 |
with torch.no_grad():
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Gradio interface
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn=predict,
|
| 28 |
-
inputs=gr.Textbox(
|
| 29 |
-
outputs=gr.
|
| 30 |
-
title="MLP
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from config import Net # Make sure this matches your class name
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load the model
|
| 7 |
+
model = Net()
|
| 8 |
model.load_state_dict(torch.load("pytorch_model.pth", map_location=torch.device("cpu")))
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
+
# Define a prediction function
|
| 12 |
+
def predict(inputs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with torch.no_grad():
|
| 14 |
+
inputs = torch.tensor(inputs).float().unsqueeze(0) # Add batch dimension
|
| 15 |
+
output = model(inputs)
|
| 16 |
+
if isinstance(output, torch.Tensor):
|
| 17 |
+
return output.squeeze().tolist()
|
| 18 |
+
return output # fallback
|
| 19 |
|
| 20 |
+
# Create the Gradio interface
|
| 21 |
demo = gr.Interface(
|
| 22 |
fn=predict,
|
| 23 |
+
inputs=gr.Textbox(label="Enter comma-separated input values (e.g., 1.2, 3.4, 5.6)"),
|
| 24 |
+
outputs=gr.Textbox(label="Model Output"),
|
| 25 |
+
title="PyTorch MLP Classifier"
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Launch
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
demo.launch()
|