karanravindra commited on
Commit
b664581
1 Parent(s): d233050

update spaces

Browse files
Files changed (2) hide show
  1. app.py +58 -0
  2. reqirements.txt +99 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import gradio as gr
4
+ import torch
5
+ import torch.nn.functional as F
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ from digitnet import Model
9
+
10
+ torch.set_grad_enabled(False)
11
+
12
+ hf_hub_download("karanravindra/digitnet", filename="mnist", local_dir=".")
13
+
14
+ model = Model(1, 16, num_classes=36)
15
+ model.load_state_dict(
16
+ torch.load("models/emnist-model.pth", weights_only=True, map_location="cpu")
17
+ )
18
+ model.eval()
19
+
20
+ all_classes = list("0123456789abcdefghijklmnopqrstuvwxyz")
21
+ all_classes = map(str.upper, all_classes)
22
+
23
+
24
+ def predict(inputs: dict) -> dict[str, float]:
25
+ print(f"[{time.strftime('%H:%M:%S')}] Predicting...")
26
+
27
+ img = torch.from_numpy(inputs["composite"]).unsqueeze(0).unsqueeze(0).float()
28
+ img = img / img.max()
29
+
30
+ img = F.interpolate(img, size=(32, 32), mode="bilinear", align_corners=False)
31
+
32
+ logits = model(img)
33
+
34
+ probs = torch.softmax(logits, dim=1).squeeze()
35
+
36
+ return {c: p for c, p in zip(all_classes, probs.tolist())}
37
+
38
+
39
+ input = gr.Sketchpad(
40
+ image_mode="L",
41
+ canvas_size=(600, 600),
42
+ layers=False,
43
+ brush=gr.Brush(colors=["rgb(239, 68, 68)"], color_mode="fixed", default_size=20),
44
+ )
45
+
46
+ output = gr.Label(num_top_classes=10)
47
+
48
+ demo = gr.Interface(
49
+ fn=predict,
50
+ live=True,
51
+ inputs=input,
52
+ outputs="label",
53
+ title="DigitNet",
54
+ description="A simple handwritten number and letter classifier.\n\nDraw a digit or letter in the box below and see the model's predictions.",
55
+ )
56
+
57
+ if __name__ == "__main__":
58
+ demo.launch()
reqirements.txt ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ digitnet @ git+https://github.com/karanravindra/digitnet@main
2
+
3
+ # This file was autogenerated via `uv export`.
4
+ -e .
5
+ aiofiles==23.2.1
6
+ aiohappyeyeballs==2.4.3
7
+ aiohttp==3.10.10
8
+ aiosignal==1.3.1
9
+ annotated-types==0.7.0
10
+ anyio==4.6.2.post1
11
+ async-timeout==4.0.3 ; python_full_version < '3.11'
12
+ attrs==24.2.0
13
+ certifi==2024.8.30
14
+ charset-normalizer==3.4.0
15
+ click==8.1.7
16
+ colorama==0.4.6 ; platform_system == 'Windows'
17
+ docker-pycreds==0.4.0
18
+ exceptiongroup==1.2.2 ; python_full_version < '3.11'
19
+ fastapi==0.115.2
20
+ ffmpy==0.4.0
21
+ filelock==3.16.1
22
+ frozenlist==1.4.1
23
+ fsspec==2024.10.0
24
+ gitdb==4.0.11
25
+ gitpython==3.1.43
26
+ gradio==5.1.0
27
+ gradio-client==1.4.0
28
+ h11==0.14.0
29
+ httpcore==1.0.6
30
+ httpx==0.27.2
31
+ huggingface-hub==0.26.0
32
+ idna==3.10
33
+ jinja2==3.1.4
34
+ lightning==2.4.0
35
+ lightning-utilities==0.11.8
36
+ markdown-it-py==3.0.0 ; sys_platform != 'emscripten'
37
+ markupsafe==2.1.5
38
+ mdurl==0.1.2 ; sys_platform != 'emscripten'
39
+ mpmath==1.3.0
40
+ multidict==6.1.0
41
+ networkx==3.4.1
42
+ numpy==1.26.4
43
+ nvidia-cublas-cu12==12.4.5.8 ; platform_machine == 'x86_64' and platform_system == 'Linux'
44
+ nvidia-cuda-cupti-cu12==12.4.127 ; platform_machine == 'x86_64' and platform_system == 'Linux'
45
+ nvidia-cuda-nvrtc-cu12==12.4.127 ; platform_machine == 'x86_64' and platform_system == 'Linux'
46
+ nvidia-cuda-runtime-cu12==12.4.127 ; platform_machine == 'x86_64' and platform_system == 'Linux'
47
+ nvidia-cudnn-cu12==9.1.0.70 ; platform_machine == 'x86_64' and platform_system == 'Linux'
48
+ nvidia-cufft-cu12==11.2.1.3 ; platform_machine == 'x86_64' and platform_system == 'Linux'
49
+ nvidia-curand-cu12==10.3.5.147 ; platform_machine == 'x86_64' and platform_system == 'Linux'
50
+ nvidia-cusolver-cu12==11.6.1.9 ; platform_machine == 'x86_64' and platform_system == 'Linux'
51
+ nvidia-cusparse-cu12==12.3.1.170 ; platform_machine == 'x86_64' and platform_system == 'Linux'
52
+ nvidia-nccl-cu12==2.21.5 ; platform_machine == 'x86_64' and platform_system == 'Linux'
53
+ nvidia-nvjitlink-cu12==12.4.127 ; platform_machine == 'x86_64' and platform_system == 'Linux'
54
+ nvidia-nvtx-cu12==12.4.127 ; platform_machine == 'x86_64' and platform_system == 'Linux'
55
+ orjson==3.10.9
56
+ packaging==24.1
57
+ pandas==2.2.3
58
+ pillow==10.4.0
59
+ platformdirs==4.3.6
60
+ propcache==0.2.0
61
+ protobuf==5.28.2
62
+ psutil==6.1.0
63
+ pydantic==2.9.2
64
+ pydantic-core==2.23.4
65
+ pydub==0.25.1
66
+ pygments==2.18.0 ; sys_platform != 'emscripten'
67
+ python-dateutil==2.9.0.post0
68
+ python-multipart==0.0.12
69
+ pytorch-lightning==2.4.0
70
+ pytz==2024.2
71
+ pyyaml==6.0.2
72
+ requests==2.32.3
73
+ rich==13.9.2 ; sys_platform != 'emscripten'
74
+ ruff==0.7.0 ; sys_platform != 'emscripten'
75
+ semantic-version==2.10.0
76
+ sentry-sdk==2.17.0
77
+ setproctitle==1.3.3
78
+ setuptools==75.2.0
79
+ shellingham==1.5.4 ; sys_platform != 'emscripten'
80
+ six==1.16.0
81
+ smmap==5.0.1
82
+ sniffio==1.3.1
83
+ starlette==0.40.0
84
+ sympy==1.13.1
85
+ tomlkit==0.12.0
86
+ torch==2.5.0
87
+ torchinfo==1.8.0
88
+ torchmetrics==1.5.0
89
+ torchvision==0.20.0
90
+ tqdm==4.66.5
91
+ triton==3.1.0 ; python_full_version < '3.13' and platform_machine == 'x86_64' and platform_system == 'Linux'
92
+ typer==0.12.5 ; sys_platform != 'emscripten'
93
+ typing-extensions==4.12.2
94
+ tzdata==2024.2
95
+ urllib3==2.2.3
96
+ uvicorn==0.32.0 ; sys_platform != 'emscripten'
97
+ wandb==0.18.5
98
+ websockets==12.0
99
+ yarl==1.15.5