Moditha24 commited on
Commit
c68a924
·
verified ·
1 Parent(s): 2cec8f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
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 = 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()
 
 
 
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()