kjn1009 commited on
Commit
e13b4f2
ยท
verified ยท
1 Parent(s): ebd0204

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from torchvision import models
4
+ import gradio as gr
5
+
6
+ # ์ „์ฒด ๋ชจ๋ธ ๋กœ๋“œ (์ด๋ฏธ ์ •์˜๋œ MyModel ํด๋ž˜์Šค๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์Œ)
7
+ model = torch.load('resnet18_finetuned_full.pth') # ์ „์ฒด ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ด
8
+ model.eval() # ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์ „ํ™˜
9
+
10
+ # ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ ์ •์˜
11
+ transform = transforms.Compose([
12
+ transforms.Resize((224, 224)),
13
+ transforms.ToTensor(),
14
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
15
+ ])
16
+
17
+ # ํด๋ž˜์Šค ์ด๋ฆ„ ๋ฆฌ์ŠคํŠธ ์ •์˜
18
+ class_names = ['disposablecup', 'envmark', 'label', 'mug', 'nonlabel', 'reusablecup']
19
+
20
+ def predict(image):
21
+ image = transform(image).unsqueeze(0)
22
+ with torch.no_grad():
23
+ outputs = model(image)
24
+ print("Raw outputs:", outputs) # ์›๋ณธ ์ถœ๋ ฅ๊ฐ’ ํ™•์ธ
25
+ _, predicted = torch.max(outputs, 1)
26
+ label = class_names[predicted.item()]
27
+ print("Predicted label:", label)
28
+
29
+ # ์˜ˆ์ธก ๊ฒฐ๊ณผ์™€ ์›๋ณธ ์ถœ๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜
30
+ return label, outputs.numpy() # ์›๋ณธ ์ถœ๋ ฅ๊ฐ’์„ NumPy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
31
+
32
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • (๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ)
33
+ iface = gr.Interface(fn=predict, inputs=gr.Image(type='pil'), outputs=['label', 'numpy'], live=True)
34
+ iface.launch()