File size: 959 Bytes
5b3d6e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import torch
import gradio as gr
import numpy as np
from config import MLP
# Load model
model = MLP()
model.load_state_dict(torch.load("pytorch_model.pth", map_location=torch.device("cpu")))
model.eval()
# Example class names (you can change this)
class_names = [f"Class {i}" for i in range(8)]
# Prediction function
def predict(input_vector):
input_array = np.array(input_vector).astype(np.float32)
if len(input_array) != 1000:
return "Error: Input must be 1000 numbers"
tensor = torch.tensor(input_array).unsqueeze(0)
with torch.no_grad():
output = model(tensor)
probs = torch.nn.functional.softmax(output[0], dim=0)
return {class_names[i]: float(probs[i]) for i in range(8)}
# Gradio interface
demo = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=5, placeholder="Enter 1000 comma-separated numbers..."),
outputs=gr.Label(num_top_classes=3),
title="MLP Vector Classifier"
)
demo.launch()
|