苏泓源 commited on
Commit
ef007f9
Β·
1 Parent(s): 9519c15
Files changed (8) hide show
  1. README.md +13 -13
  2. app.py +29 -7
  3. data.npy +0 -0
  4. data.py +5 -0
  5. model.pth +0 -0
  6. model.py +24 -0
  7. requirements.txt +2 -0
  8. test_model.py +11 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
- ---
2
- title: IMUFLP
3
- emoji: πŸ¦€
4
- colorFrom: yellow
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 4.17.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Test
3
+ emoji: πŸ“ˆ
4
+ colorFrom: blue
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.15.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,7 +1,29 @@
1
- import gradio as gr
2
-
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
-
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from model import Net
5
+
6
+
7
+ model = Net(100, 50, 10)
8
+ model.load_state_dict(torch.load('model.pth'))
9
+ model.eval()
10
+
11
+ def infer(file_obj):
12
+ results = ""
13
+ for file in file_obj:
14
+ data = np.load(file.name)
15
+ output = model(torch.from_numpy(data).float()).detach().numpy()
16
+ results += np.array_str(output) + "\n"
17
+ return results
18
+
19
+ with gr.Blocks("Test") as demo:
20
+ gr.Markdown("## Test")
21
+ output = gr.Textbox(label="Output")
22
+ data = gr.UploadButton(
23
+ label="Upload a .npy",
24
+ file_count="multiple",
25
+ file_types=[".npy"])
26
+ data.upload(fn=infer, inputs=data, outputs=output)
27
+
28
+ if __name__ == "__main__":
29
+ demo.launch()
data.npy ADDED
Binary file (528 Bytes). View file
 
data.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ data = np.arange(100).reshape(1, 100)
4
+ print(np.array_str(data))
5
+ np.save('data.npy', data)
model.pth ADDED
Binary file (24.3 kB). View file
 
model.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+
5
+
6
+ class Net(nn.Module):
7
+ def __init__(self, input_size, hidden_size, num_classes):
8
+ super(Net, self).__init__()
9
+ self.fc1 = nn.Linear(input_size, hidden_size)
10
+ self.relu = nn.ReLU()
11
+ self.fc2 = nn.Linear(hidden_size, num_classes)
12
+ self.softmax = nn.Softmax(dim=1)
13
+
14
+ def forward(self, x):
15
+ out = self.fc1(x)
16
+ out = self.relu(out)
17
+ out = self.fc2(out)
18
+ out = self.softmax(out)
19
+ return out
20
+
21
+
22
+ if __name__ == '__main__':
23
+ net = Net(100, 50, 10)
24
+ torch.save(net.state_dict(), 'model.pth')
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch >= 2.1.0
2
+ numpy >= 1.26.1
test_model.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+
4
+ from model import Net
5
+
6
+ a = np.arange(100).reshape(1, 100)
7
+ model = Net(100, 50, 10)
8
+ model.load_state_dict(torch.load('model.pth'))
9
+ model.eval()
10
+ output = model(torch.from_numpy(a).float())
11
+ print(output)