Spaces:
Build error
Build error
Zai commited on
Commit ·
e34d96b
1
Parent(s): 765c0c2
deploy
Browse filesFiles changed (5)
- __pycache__/model.cpython-310.pycAdded
- app.pyAdded+29
- mnist_model.pthAdded+3
- model.pyAdded+36
- requirements.txtAdded
1234567891011121314151617181920212223242526272829import gradio as grfrom model import Net, predictimport torchimport torchvision.transforms as transformsfrom PIL import Image
model = Net()model.load_state_dict(torch.load("mnist_model.pth", map_location=torch.device("cpu")))model.eval()
transform = transforms.Compose([ transforms.Grayscale(), # Convert to grayscale if needed transforms.Resize((28, 28)), # Fix: pass size as a tuple transforms.ToTensor() # Convert to a PyTorch tensor])
def predict_image(image): input_tensors = transform(Image.fromarray(image)).unsqueeze(0)
result = predict(model,input_tensors)
return result
app = gr.Interface(predict_image, gr.Image(), "text")
app.launch()123version https://git-lfs.github.com/spec/v1oid sha256:a49b6e2a632bf6efd53d49aab0ec533481c431d7404429b6a6147221c942d0f7size 1689904123456789101112131415161718192021222324252627282930313233343536import torchfrom torch import nn
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1) self.relu = nn.ReLU() self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1) self.flatten = nn.Flatten() self.fc1 = nn.Linear(64 * 7 * 7, 128) self.fc2 = nn.Linear(128, 10)
def forward(self, x): x = self.conv1(x) x = self.relu(x) x = self.maxpool(x) x = self.conv2(x) x = self.relu(x) x = self.maxpool(x) x = self.flatten(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) return x
def predict(model, image): model.eval() with torch.no_grad(): output = model(image)
result = torch.argmax(output,dim=1).item() return result__pycache__/model.cpython-310.pycAdded
app.pyAdded+29
mnist_model.pthAdded+3
model.pyAdded+36
requirements.txtAdded