Darius Morawiec commited on
Commit
f0f166f
·
1 Parent(s): def56a1

Add initial space

Browse files
Files changed (7) hide show
  1. .gitignore +373 -0
  2. .python-version +1 -0
  3. README.md +1 -0
  4. app.py +192 -0
  5. pyproject.toml +8 -0
  6. requirements.txt +2 -0
  7. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,373 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .gradio
2
+ *.pth
3
+ *.csv
4
+
5
+ # Created by https://www.toptal.com/developers/gitignore/api/macos,dotenv,python,windows,intellij,visualstudiocode
6
+ # Edit at https://www.toptal.com/developers/gitignore?templates=macos,dotenv,python,windows,intellij,visualstudiocode
7
+
8
+ ### dotenv ###
9
+ .env
10
+
11
+ ### Intellij ###
12
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
13
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
14
+
15
+ # User-specific stuff
16
+ .idea/**/workspace.xml
17
+ .idea/**/tasks.xml
18
+ .idea/**/usage.statistics.xml
19
+ .idea/**/dictionaries
20
+ .idea/**/shelf
21
+
22
+ # AWS User-specific
23
+ .idea/**/aws.xml
24
+
25
+ # Generated files
26
+ .idea/**/contentModel.xml
27
+
28
+ # Sensitive or high-churn files
29
+ .idea/**/dataSources/
30
+ .idea/**/dataSources.ids
31
+ .idea/**/dataSources.local.xml
32
+ .idea/**/sqlDataSources.xml
33
+ .idea/**/dynamic.xml
34
+ .idea/**/uiDesigner.xml
35
+ .idea/**/dbnavigator.xml
36
+
37
+ # Gradle
38
+ .idea/**/gradle.xml
39
+ .idea/**/libraries
40
+
41
+ # Gradle and Maven with auto-import
42
+ # When using Gradle or Maven with auto-import, you should exclude module files,
43
+ # since they will be recreated, and may cause churn. Uncomment if using
44
+ # auto-import.
45
+ # .idea/artifacts
46
+ # .idea/compiler.xml
47
+ # .idea/jarRepositories.xml
48
+ # .idea/modules.xml
49
+ # .idea/*.iml
50
+ # .idea/modules
51
+ # *.iml
52
+ # *.ipr
53
+
54
+ # CMake
55
+ cmake-build-*/
56
+
57
+ # Mongo Explorer plugin
58
+ .idea/**/mongoSettings.xml
59
+
60
+ # File-based project format
61
+ *.iws
62
+
63
+ # IntelliJ
64
+ out/
65
+
66
+ # mpeltonen/sbt-idea plugin
67
+ .idea_modules/
68
+
69
+ # JIRA plugin
70
+ atlassian-ide-plugin.xml
71
+
72
+ # Cursive Clojure plugin
73
+ .idea/replstate.xml
74
+
75
+ # SonarLint plugin
76
+ .idea/sonarlint/
77
+
78
+ # Crashlytics plugin (for Android Studio and IntelliJ)
79
+ com_crashlytics_export_strings.xml
80
+ crashlytics.properties
81
+ crashlytics-build.properties
82
+ fabric.properties
83
+
84
+ # Editor-based Rest Client
85
+ .idea/httpRequests
86
+
87
+ # Android studio 3.1+ serialized cache file
88
+ .idea/caches/build_file_checksums.ser
89
+
90
+ ### Intellij Patch ###
91
+ # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
92
+
93
+ # *.iml
94
+ # modules.xml
95
+ # .idea/misc.xml
96
+ # *.ipr
97
+
98
+ # Sonarlint plugin
99
+ # https://plugins.jetbrains.com/plugin/7973-sonarlint
100
+ .idea/**/sonarlint/
101
+
102
+ # SonarQube Plugin
103
+ # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
104
+ .idea/**/sonarIssues.xml
105
+
106
+ # Markdown Navigator plugin
107
+ # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
108
+ .idea/**/markdown-navigator.xml
109
+ .idea/**/markdown-navigator-enh.xml
110
+ .idea/**/markdown-navigator/
111
+
112
+ # Cache file creation bug
113
+ # See https://youtrack.jetbrains.com/issue/JBR-2257
114
+ .idea/$CACHE_FILE$
115
+
116
+ # CodeStream plugin
117
+ # https://plugins.jetbrains.com/plugin/12206-codestream
118
+ .idea/codestream.xml
119
+
120
+ # Azure Toolkit for IntelliJ plugin
121
+ # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
122
+ .idea/**/azureSettings.xml
123
+
124
+ ### macOS ###
125
+ # General
126
+ .DS_Store
127
+ .AppleDouble
128
+ .LSOverride
129
+
130
+ # Icon must end with two \r
131
+ Icon
132
+
133
+
134
+ # Thumbnails
135
+ ._*
136
+
137
+ # Files that might appear in the root of a volume
138
+ .DocumentRevisions-V100
139
+ .fseventsd
140
+ .Spotlight-V100
141
+ .TemporaryItems
142
+ .Trashes
143
+ .VolumeIcon.icns
144
+ .com.apple.timemachine.donotpresent
145
+
146
+ # Directories potentially created on remote AFP share
147
+ .AppleDB
148
+ .AppleDesktop
149
+ Network Trash Folder
150
+ Temporary Items
151
+ .apdisk
152
+
153
+ ### macOS Patch ###
154
+ # iCloud generated files
155
+ *.icloud
156
+
157
+ ### Python ###
158
+ # Byte-compiled / optimized / DLL files
159
+ __pycache__/
160
+ *.py[cod]
161
+ *$py.class
162
+
163
+ # C extensions
164
+ *.so
165
+
166
+ # Distribution / packaging
167
+ .Python
168
+ build/
169
+ develop-eggs/
170
+ dist/
171
+ downloads/
172
+ eggs/
173
+ .eggs/
174
+ lib/
175
+ lib64/
176
+ parts/
177
+ sdist/
178
+ var/
179
+ wheels/
180
+ share/python-wheels/
181
+ *.egg-info/
182
+ .installed.cfg
183
+ *.egg
184
+ MANIFEST
185
+
186
+ # PyInstaller
187
+ # Usually these files are written by a python script from a template
188
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
189
+ *.manifest
190
+ *.spec
191
+
192
+ # Installer logs
193
+ pip-log.txt
194
+ pip-delete-this-directory.txt
195
+
196
+ # Unit test / coverage reports
197
+ htmlcov/
198
+ .tox/
199
+ .nox/
200
+ .coverage
201
+ .coverage.*
202
+ .cache
203
+ nosetests.xml
204
+ coverage.xml
205
+ *.cover
206
+ *.py,cover
207
+ .hypothesis/
208
+ .pytest_cache/
209
+ cover/
210
+
211
+ # Translations
212
+ *.mo
213
+ *.pot
214
+
215
+ # Django stuff:
216
+ *.log
217
+ local_settings.py
218
+ db.sqlite3
219
+ db.sqlite3-journal
220
+
221
+ # Flask stuff:
222
+ instance/
223
+ .webassets-cache
224
+
225
+ # Scrapy stuff:
226
+ .scrapy
227
+
228
+ # Sphinx documentation
229
+ docs/_build/
230
+
231
+ # PyBuilder
232
+ .pybuilder/
233
+ target/
234
+
235
+ # Jupyter Notebook
236
+ .ipynb_checkpoints
237
+
238
+ # IPython
239
+ profile_default/
240
+ ipython_config.py
241
+
242
+ # pyenv
243
+ # For a library or package, you might want to ignore these files since the code is
244
+ # intended to run in multiple environments; otherwise, check them in:
245
+ # .python-version
246
+
247
+ # pipenv
248
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
249
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
250
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
251
+ # install all needed dependencies.
252
+ #Pipfile.lock
253
+
254
+ # poetry
255
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
256
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
257
+ # commonly ignored for libraries.
258
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
259
+ #poetry.lock
260
+
261
+ # pdm
262
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
263
+ #pdm.lock
264
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
265
+ # in version control.
266
+ # https://pdm.fming.dev/#use-with-ide
267
+ .pdm.toml
268
+
269
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
270
+ __pypackages__/
271
+
272
+ # Celery stuff
273
+ celerybeat-schedule
274
+ celerybeat.pid
275
+
276
+ # SageMath parsed files
277
+ *.sage.py
278
+
279
+ # Environments
280
+ .venv
281
+ env/
282
+ venv/
283
+ ENV/
284
+ env.bak/
285
+ venv.bak/
286
+
287
+ # Spyder project settings
288
+ .spyderproject
289
+ .spyproject
290
+
291
+ # Rope project settings
292
+ .ropeproject
293
+
294
+ # mkdocs documentation
295
+ /site
296
+
297
+ # mypy
298
+ .mypy_cache/
299
+ .dmypy.json
300
+ dmypy.json
301
+
302
+ # Pyre type checker
303
+ .pyre/
304
+
305
+ # pytype static type analyzer
306
+ .pytype/
307
+
308
+ # Cython debug symbols
309
+ cython_debug/
310
+
311
+ # PyCharm
312
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
313
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
314
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
315
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
316
+ #.idea/
317
+
318
+ ### Python Patch ###
319
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
320
+ poetry.toml
321
+
322
+ # ruff
323
+ .ruff_cache/
324
+
325
+ # LSP config files
326
+ pyrightconfig.json
327
+
328
+ ### VisualStudioCode ###
329
+ .vscode/*
330
+ !.vscode/settings.json
331
+ !.vscode/tasks.json
332
+ !.vscode/launch.json
333
+ !.vscode/extensions.json
334
+ !.vscode/*.code-snippets
335
+
336
+ # Local History for Visual Studio Code
337
+ .history/
338
+
339
+ # Built Visual Studio Code Extensions
340
+ *.vsix
341
+
342
+ ### VisualStudioCode Patch ###
343
+ # Ignore all local history of files
344
+ .history
345
+ .ionide
346
+
347
+ ### Windows ###
348
+ # Windows thumbnail cache files
349
+ Thumbs.db
350
+ Thumbs.db:encryptable
351
+ ehthumbs.db
352
+ ehthumbs_vista.db
353
+
354
+ # Dump file
355
+ *.stackdump
356
+
357
+ # Folder config file
358
+ [Dd]esktop.ini
359
+
360
+ # Recycle Bin used on file shares
361
+ $RECYCLE.BIN/
362
+
363
+ # Windows Installer files
364
+ *.cab
365
+ *.msi
366
+ *.msix
367
+ *.msm
368
+ *.msp
369
+
370
+ # Windows shortcuts
371
+ *.lnk
372
+
373
+ # End of https://www.toptal.com/developers/gitignore/api/macos,dotenv,python,windows,intellij,visualstudiocode
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
README.md CHANGED
@@ -5,6 +5,7 @@ colorFrom: gray
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.46.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.46.0
8
+ python_version: 3.10.0
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PIL.Image
3
+ import torch
4
+ from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor
5
+
6
+ # DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
7
+ DEVICE = "cpu"
8
+
9
+
10
+ class Detector:
11
+ def __init__(self, model_id: str):
12
+ self.device = DEVICE
13
+ self.processor = AutoProcessor.from_pretrained(model_id)
14
+ self.model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(
15
+ self.device
16
+ )
17
+
18
+ def detect(
19
+ self,
20
+ image: PIL.Image.Image,
21
+ text_labels: list[str],
22
+ threshold: float = 0.4,
23
+ ):
24
+ inputs = self.processor(
25
+ images=image, text=[text_labels], return_tensors="pt"
26
+ ).to(self.device)
27
+
28
+ with torch.no_grad():
29
+ outputs = self.model(**inputs)
30
+
31
+ results = self.processor.post_process_grounded_object_detection(
32
+ outputs, threshold=threshold, target_sizes=[(image.height, image.width)]
33
+ )
34
+
35
+ detections = []
36
+ result = results[0]
37
+ for box, score, labels in zip(
38
+ result["boxes"], result["scores"], result["labels"]
39
+ ):
40
+ box = [round(x, 2) for x in box.tolist()]
41
+ detections.append(
42
+ dict(
43
+ label=labels,
44
+ confidence=round(score.item(), 3),
45
+ box=box,
46
+ )
47
+ )
48
+ return detections
49
+
50
+
51
+ models = dict(
52
+ tiny=Detector("iSEE-Laboratory/llmdet_tiny"),
53
+ base=Detector("iSEE-Laboratory/llmdet_base"),
54
+ large=Detector("iSEE-Laboratory/llmdet_large"),
55
+ )
56
+
57
+
58
+ def _postprocess(detections):
59
+ annotations = []
60
+ for detection in detections:
61
+ box = detection["box"]
62
+ mask = (int(box[0]), int(box[1]), int(box[2]), int(box[3]))
63
+ label = f"{detection['label']} ({detection['confidence']:.2f})"
64
+ annotations.append((mask, label))
65
+ return annotations
66
+
67
+
68
+ def detect_objects(image, labels, confidence_threshold):
69
+ labels = [label.strip() for label in labels.split(",")]
70
+ return (
71
+ (
72
+ image,
73
+ _postprocess(
74
+ models["tiny"].detect(
75
+ image,
76
+ labels,
77
+ threshold=confidence_threshold,
78
+ )
79
+ ),
80
+ ),
81
+ (
82
+ image,
83
+ _postprocess(
84
+ models["base"].detect(
85
+ image,
86
+ labels,
87
+ threshold=confidence_threshold,
88
+ )
89
+ ),
90
+ ),
91
+ (
92
+ image,
93
+ _postprocess(
94
+ models["large"].detect(
95
+ image,
96
+ labels,
97
+ threshold=confidence_threshold,
98
+ )
99
+ ),
100
+ ),
101
+ )
102
+
103
+
104
+ with gr.Blocks() as demo:
105
+ gr.Markdown("# LLMDet Open Vocabulary Object Detection")
106
+
107
+ confidence_slider = gr.Slider(
108
+ 0,
109
+ 1,
110
+ value=0.4,
111
+ step=0.01,
112
+ interactive=True,
113
+ label="Confidence threshold",
114
+ )
115
+
116
+ labels = [
117
+ "backpack",
118
+ "bag",
119
+ "belt",
120
+ "blouse",
121
+ "boot",
122
+ "bracelet",
123
+ "cap",
124
+ "cardigan",
125
+ "coat",
126
+ "dress",
127
+ "earring",
128
+ "flipflop",
129
+ "glasses",
130
+ "glove",
131
+ "handbag",
132
+ "hat",
133
+ "heels",
134
+ "jacket",
135
+ "jeans",
136
+ "loafer",
137
+ "necklace",
138
+ "pullover",
139
+ "raincoat",
140
+ "ring",
141
+ "sandal",
142
+ "scarf",
143
+ "shirt",
144
+ "shoe",
145
+ "shorts",
146
+ "skirt",
147
+ "slippers",
148
+ "sneaker",
149
+ "socks",
150
+ "suitcase",
151
+ "sunglasses",
152
+ "sweater",
153
+ "tshirt",
154
+ "tie",
155
+ "top",
156
+ "trouser",
157
+ "umbrella",
158
+ "vest",
159
+ "watch",
160
+ ]
161
+
162
+ # Requested labels
163
+ text_input = gr.Textbox(
164
+ label="Object labels (comma separated)!",
165
+ placeholder="shirt, jeans, shoe",
166
+ lines=1,
167
+ value=",".join(labels),
168
+ )
169
+
170
+ with gr.Row():
171
+ image_input = gr.Image(type="pil", image_mode="RGB")
172
+
173
+ with gr.Row():
174
+ output_annotated_image_tiny = gr.AnnotatedImage(label="TINY")
175
+ output_annotated_image_base = gr.AnnotatedImage(label="BASE")
176
+ output_annotated_image_large = gr.AnnotatedImage(label="LARGE")
177
+
178
+ detect_button = gr.Button("Detect")
179
+
180
+ # Connect the button to the detection function
181
+ detect_button.click(
182
+ fn=detect_objects,
183
+ inputs=[image_input, text_input, confidence_slider],
184
+ outputs=[
185
+ output_annotated_image_tiny,
186
+ output_annotated_image_base,
187
+ output_annotated_image_large,
188
+ ],
189
+ )
190
+
191
+ if __name__ == "__main__":
192
+ demo.launch()
pyproject.toml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "llmdet-arena"
3
+ version = "0.1.0"
4
+ requires-python = ">=3.10"
5
+ dependencies = [
6
+ "gradio~=5.46.0",
7
+ "transformers[torch]~=4.56.1",
8
+ ]
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio~=5.46.0
2
+ transformers[torch]~=4.56.1
uv.lock ADDED
The diff for this file is too large to render. See raw diff