Vasyl Poremchuk commited on
Commit
60e3a8c
·
1 Parent(s): f1bac74

Add the application file & required packages

Browse files
Files changed (3) hide show
  1. .gitignore +259 -0
  2. app.py +81 -0
  3. requirements.txt +9 -0
.gitignore ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### PyCharm+all ###
2
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
3
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
4
+
5
+ # User-specific stuff
6
+ .idea/**/workspace.xml
7
+ .idea/**/tasks.xml
8
+ .idea/**/usage.statistics.xml
9
+ .idea/**/dictionaries
10
+ .idea/**/shelf
11
+
12
+ # AWS User-specific
13
+ .idea/**/aws.xml
14
+
15
+ # Generated files
16
+ .idea/**/contentModel.xml
17
+
18
+ # Sensitive or high-churn files
19
+ .idea/**/dataSources/
20
+ .idea/**/dataSources.ids
21
+ .idea/**/dataSources.local.xml
22
+ .idea/**/sqlDataSources.xml
23
+ .idea/**/dynamic.xml
24
+ .idea/**/uiDesigner.xml
25
+ .idea/**/dbnavigator.xml
26
+
27
+ # Gradle
28
+ .idea/**/gradle.xml
29
+ .idea/**/libraries
30
+
31
+ # Gradle and Maven with auto-import
32
+ # When using Gradle or Maven with auto-import, you should exclude module files,
33
+ # since they will be recreated, and may cause churn. Uncomment if using
34
+ # auto-import.
35
+ # .idea/artifacts
36
+ # .idea/compiler.xml
37
+ # .idea/jarRepositories.xml
38
+ # .idea/modules.xml
39
+ # .idea/*.iml
40
+ # .idea/modules
41
+ # *.iml
42
+ # *.ipr
43
+
44
+ # CMake
45
+ cmake-build-*/
46
+
47
+ # Mongo Explorer plugin
48
+ .idea/**/mongoSettings.xml
49
+
50
+ # File-based project format
51
+ *.iws
52
+
53
+ # IntelliJ
54
+ out/
55
+
56
+ # mpeltonen/sbt-idea plugin
57
+ .idea_modules/
58
+
59
+ # JIRA plugin
60
+ atlassian-ide-plugin.xml
61
+
62
+ # Cursive Clojure plugin
63
+ .idea/replstate.xml
64
+
65
+ # SonarLint plugin
66
+ .idea/sonarlint/
67
+
68
+ # Crashlytics plugin (for Android Studio and IntelliJ)
69
+ com_crashlytics_export_strings.xml
70
+ crashlytics.properties
71
+ crashlytics-build.properties
72
+ fabric.properties
73
+
74
+ # Editor-based Rest Client
75
+ .idea/httpRequests
76
+
77
+ # Android studio 3.1+ serialized cache file
78
+ .idea/caches/build_file_checksums.ser
79
+
80
+ ### PyCharm+all Patch ###
81
+ # Ignore everything but code style settings and run configurations
82
+ # that are supposed to be shared within teams.
83
+
84
+ .idea/*
85
+
86
+ !.idea/codeStyles
87
+ !.idea/runConfigurations
88
+
89
+ ### Python ###
90
+ # Byte-compiled / optimized / DLL files
91
+ __pycache__/
92
+ *.py[cod]
93
+ *$py.class
94
+
95
+ # C extensions
96
+ *.so
97
+
98
+ # Distribution / packaging
99
+ .Python
100
+ build/
101
+ develop-eggs/
102
+ dist/
103
+ downloads/
104
+ eggs/
105
+ .eggs/
106
+ lib/
107
+ lib64/
108
+ parts/
109
+ sdist/
110
+ var/
111
+ wheels/
112
+ share/python-wheels/
113
+ *.egg-info/
114
+ .installed.cfg
115
+ *.egg
116
+ MANIFEST
117
+
118
+ # PyInstaller
119
+ # Usually these files are written by a python script from a template
120
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
121
+ *.manifest
122
+ *.spec
123
+
124
+ # Installer logs
125
+ pip-log.txt
126
+ pip-delete-this-directory.txt
127
+
128
+ # Unit test / coverage reports
129
+ htmlcov/
130
+ .tox/
131
+ .nox/
132
+ .coverage
133
+ .coverage.*
134
+ .cache
135
+ nosetests.xml
136
+ coverage.xml
137
+ *.cover
138
+ *.py,cover
139
+ .hypothesis/
140
+ .pytest_cache/
141
+ cover/
142
+
143
+ # Translations
144
+ *.mo
145
+ *.pot
146
+
147
+ # Django stuff:
148
+ *.log
149
+ local_settings.py
150
+ db.sqlite3
151
+ db.sqlite3-journal
152
+
153
+ # Flask stuff:
154
+ instance/
155
+ .webassets-cache
156
+
157
+ # Scrapy stuff:
158
+ .scrapy
159
+
160
+ # Sphinx documentation
161
+ docs/_build/
162
+
163
+ # PyBuilder
164
+ .pybuilder/
165
+ target/
166
+
167
+ # Jupyter Notebook
168
+ .ipynb_checkpoints
169
+
170
+ # IPython
171
+ profile_default/
172
+ ipython_config.py
173
+
174
+ # pyenv
175
+ # For a library or package, you might want to ignore these files since the code is
176
+ # intended to run in multiple environments; otherwise, check them in:
177
+ # .python-version
178
+
179
+ # pipenv
180
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
181
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
182
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
183
+ # install all needed dependencies.
184
+ #Pipfile.lock
185
+
186
+ # poetry
187
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
188
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
189
+ # commonly ignored for libraries.
190
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
191
+ #poetry.lock
192
+
193
+ # pdm
194
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
195
+ #pdm.lock
196
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
197
+ # in version control.
198
+ # https://pdm.fming.dev/#use-with-ide
199
+ .pdm.toml
200
+
201
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
202
+ __pypackages__/
203
+
204
+ # Celery stuff
205
+ celerybeat-schedule
206
+ celerybeat.pid
207
+
208
+ # SageMath parsed files
209
+ *.sage.py
210
+
211
+ # Environments
212
+ .env
213
+ .venv
214
+ env/
215
+ venv/
216
+ ENV/
217
+ env.bak/
218
+ venv.bak/
219
+
220
+ # Spyder project settings
221
+ .spyderproject
222
+ .spyproject
223
+
224
+ # Rope project settings
225
+ .ropeproject
226
+
227
+ # mkdocs documentation
228
+ /site
229
+
230
+ # mypy
231
+ .mypy_cache/
232
+ .dmypy.json
233
+ dmypy.json
234
+
235
+ # Pyre type checker
236
+ .pyre/
237
+
238
+ # pytype static type analyzer
239
+ .pytype/
240
+
241
+ # Cython debug symbols
242
+ cython_debug/
243
+
244
+ # PyCharm
245
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
246
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
247
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
248
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
249
+ #.idea/
250
+
251
+ ### Python Patch ###
252
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
253
+ poetry.toml
254
+
255
+ # ruff
256
+ .ruff_cache/
257
+
258
+ # LSP config files
259
+ pyrightconfig.json
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModel, AutoProcessor
3
+ import torch
4
+ import requests
5
+ from PIL import Image
6
+ from io import BytesIO
7
+
8
+ fashion_items = [
9
+ "top",
10
+ "trousers",
11
+ "jumper",
12
+ "t-shirt",
13
+ "jeans",
14
+ "sneakers",
15
+ "sweater",
16
+ "jacket",
17
+ "dress",
18
+ "blazer",
19
+ "boots",
20
+ "bag",
21
+ "sunglasses",
22
+ ]
23
+
24
+ # Load model and processor
25
+ model_name = "Marqo/marqo-fashionSigLIP"
26
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
27
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
28
+
29
+ # Preprocess and normalize text data
30
+ with torch.no_grad():
31
+ # Ensure truncation and padding are activated
32
+ processed_texts = processor(
33
+ text=fashion_items,
34
+ return_tensors="pt",
35
+ truncation=True, # Ensure text is truncated to fit model input size
36
+ padding=True, # Pad shorter sequences so that all are the same length
37
+ )["input_ids"]
38
+
39
+ text_features = model.get_text_features(processed_texts)
40
+ text_features = text_features / text_features.norm(dim=-1, keepdim=True)
41
+
42
+
43
+ # Prediction function
44
+ def predict_from_url(url):
45
+ # Check if the URL is empty
46
+ if not url:
47
+ return {"Error": "Please input a URL"}
48
+
49
+ try:
50
+ image = Image.open(BytesIO(requests.get(url).content))
51
+ except Exception as e:
52
+ return {"Error": f"Failed to load image: {str(e)}"}
53
+
54
+ processed_image = processor(images=image, return_tensors="pt")[
55
+ "pixel_values"
56
+ ]
57
+
58
+ with torch.no_grad():
59
+ image_features = model.get_image_features(processed_image)
60
+ image_features = image_features / image_features.norm(
61
+ dim=-1, keepdim=True
62
+ )
63
+ text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
64
+
65
+ return {
66
+ fashion_items[i]: float(text_probs[0, i])
67
+ for i in range(len(fashion_items))
68
+ }
69
+
70
+
71
+ # Gradio interface
72
+ demo = gr.Interface(
73
+ fn=predict_from_url,
74
+ inputs=gr.Textbox(label="Enter Image URL"),
75
+ outputs=gr.Label(label="Classification Results"),
76
+ title="Fashion Item Classifier",
77
+ flagging_mode="never",
78
+ )
79
+
80
+ # Launch the interface
81
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ requests
4
+ Pillow
5
+ open_clip_torch
6
+ ftfy
7
+
8
+ # This is only needed for local deployment
9
+ gradio