Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from config import MLP
|
5 |
+
|
6 |
+
# Load model
|
7 |
+
model = MLP()
|
8 |
+
model.load_state_dict(torch.load("pytorch_model.pth", map_location=torch.device("cpu")))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Example class names (you can change this)
|
12 |
+
class_names = [f"Class {i}" for i in range(8)]
|
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 |
+
output = model(tensor)
|
22 |
+
probs = torch.nn.functional.softmax(output[0], dim=0)
|
23 |
+
return {class_names[i]: float(probs[i]) for i in range(8)}
|
24 |
+
|
25 |
+
# Gradio interface
|
26 |
+
demo = gr.Interface(
|
27 |
+
fn=predict,
|
28 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter 1000 comma-separated numbers..."),
|
29 |
+
outputs=gr.Label(num_top_classes=3),
|
30 |
+
title="MLP Vector Classifier"
|
31 |
+
)
|
32 |
+
|
33 |
+
demo.launch()
|