Download app.py from Cheomjee/0801eff: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/Cheomjee/0801eff/resolve/main/app.py
- Command line
-
hf download hf://spaces/Cheomjee/0801eff/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Cheomjee/0801eff/resolve/main/app.py
1.47 kB
| import torch | |
| import torchvision.transforms as transforms | |
| from torchvision import models | |
| import gradio as gr | |
| # ์ ์ฒด ๋ชจ๋ธ ๋ก๋ (์ด๋ฏธ ์ ์๋ MyModel ํด๋์ค๋ ํ์ํ์ง ์์) | |
| model = torch.load('resnet18_finetuned_full.pth') # ์ ์ฒด ๋ชจ๋ธ์ ๋ถ๋ฌ์ด | |
| model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ ํ | |
| # ๋ฐ์ดํฐ ๋ณํ ์ ์ | |
| transform = transforms.Compose([ | |
| transforms.Resize((224, 224)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) | |
| ]) | |
| # ํด๋์ค ์ด๋ฆ ๋ฆฌ์คํธ ์ ์ | |
| class_names = ['disposablecup', 'envmark', 'label', 'mug', 'nonlabel', 'reusablecup'] | |
| class_ok = [ 'envmark','mug', 'nonlabel', 'reusablecup'] | |
| class_notok = ['disposablecup', 'label'] | |
| def predict(image): | |
| image = transform(image).unsqueeze(0) | |
| with torch.no_grad(): | |
| outputs = model(image) | |
| print("Raw outputs:", outputs) # ์๋ณธ ์ถ๋ ฅ๊ฐ ํ์ธ | |
| _, predicted = torch.max(outputs, 1) | |
| label = class_names[predicted.item()] | |
| print("Predicted label:", label) | |
| if label in class_ok : result = "์ธ์ฆ์ด ์๋ฃ๋์์ต๋๋ค. (+1p)" | |
| else : result = "์ธ์ฆ์ ์คํจํ์ต๋๋ค. (-)" | |
| # ์์ธก ๊ฒฐ๊ณผ์ ์๋ณธ ์ถ๋ ฅ๊ฐ์ ๋ชจ๋ ๋ฐํ | |
| return result # ์๋ณธ ์ถ๋ ฅ๊ฐ์ NumPy ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐํ | |
| # Gradio ์ธํฐํ์ด์ค ์ค์ (๋ ๊ฐ์ ์ถ๋ ฅ) | |
| iface = gr.Interface(fn=predict, inputs=gr.Image(type='pil'), outputs=['label'], live=True) | |
| iface.launch() | |