|
import torch |
|
import gradio as gr |
|
import numpy as np |
|
from config import MLP |
|
|
|
|
|
model = MLP() |
|
model.load_state_dict(torch.load("pytorch_model.pth", map_location=torch.device("cpu"))) |
|
model.eval() |
|
|
|
|
|
class_names = [f"Class {i}" for i in range(8)] |
|
|
|
|
|
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)} |
|
|
|
|
|
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() |
|
|