Spaces:
Sleeping
Sleeping
Add application file
Browse filesFiles changed (3)
- app.pyAdded+46
- class_names.txtAdded+100
- pytorch_model.binAdded+3
12345678910111213141516171819202122232425262728293031323334353637383940414243444546from pathlib import Pathimport torchimport gradio as grfrom torch import nn
LABELS = Path("class_names.txt").read_text().splitlines()
model = nn.Sequential( nn.Conv2d(1, 32, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Flatten(), nn.Linear(1152, 256), nn.ReLU(), nn.Linear(256, len(LABELS)),)state_dict = torch.load("pytorch_model.bin", map_location="cpu")model.load_state_dict(state_dict, strict=False)model.eval()
def predict(im): x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0 with torch.no_grad(): out = model(x) probabilities = torch.nn.functional.softmax(out[0], dim=0) values, indices = torch.topk(probabilities, 5) return {LABELS[i]: v.item() for i, v in zip(indices, values)}
interface = gr.Interface( predict, inputs="sketchpad", outputs="label", theme="huggingface", title="Sketch Recognition", description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!", article="<p style='text-align: center'>Sketch Recognition | Demo Model</p>", live=True,)interface.launch(share=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100airplanealarm_clockanvilappleaxebaseballbaseball_batbasketballbeardbedbenchbicyclebirdbookbreadbridgebroombutterflycameracandlecarcatceiling_fancell_phonechaircircleclockcloudcoffee_cupcookiecupdiving_boarddonutdoordrumsdumbbellenvelopeeyeeyeglassesfacefanflowerfrying_pangrapeshammerhatheadphoneshelmethot_dogice_creamkeyknifeladderlaptoplight_bulblightninglinelollipopmicrophonemoonmountainmoustachemushroompantspaper_clippencilpillowpizzapower_outletradiorainbowriflesawscissorsscrewdrivershortsshovelsmiley_facesnakesockspiderspoonsquarestarstop_signsuitcasesunswordsyringet-shirttabletennis_racquettenttoothtraffic_lighttreetriangleumbrellawheelwristwatch123version https://git-lfs.github.com/spec/v1oid sha256:effb6ea6f1593c09e8247944028ed9c309b5ff1cef82ba38b822bee2ca4d0f3csize 1656903app.pyAdded+46
class_names.txtAdded+100
pytorch_model.binAdded+3