Spaces:
Running
Running
make it work
Browse files- .gitattributes +1 -0
- .gitignore +160 -0
- README.md +3 -2
- app.py +98 -0
- images.json +0 -0
- requirements.txt +10 -0
- vector.index +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.index filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
---
|
2 |
title: Xbgp
|
3 |
-
emoji:
|
4 |
colorFrom: gray
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.4.1
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
1 |
---
|
2 |
title: Xbgp
|
3 |
+
emoji: 🎮
|
4 |
colorFrom: gray
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.4.1
|
8 |
+
python_version: 3.11
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
---
|
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from transformers import AutoImageProcessor, AutoModel
|
4 |
+
import torch
|
5 |
+
from pymongo import MongoClient
|
6 |
+
from PIL import Image
|
7 |
+
import json
|
8 |
+
import numpy as np
|
9 |
+
import faiss
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
|
15 |
+
# Init similarity search AI model and processor
|
16 |
+
torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
dino_v2_model = AutoModel.from_pretrained("facebook/dinov2-base").to(torch_device)
|
18 |
+
dino_v2_image_processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
|
19 |
+
|
20 |
+
# MongoDB
|
21 |
+
MONGO_URI = os.environ.get("MONGO_URI")
|
22 |
+
mongo = MongoClient(MONGO_URI)
|
23 |
+
db = mongo["xbgp"]
|
24 |
+
|
25 |
+
|
26 |
+
def process_image(image):
|
27 |
+
"""
|
28 |
+
Process the image and extract features using the DINOv2 model.
|
29 |
+
"""
|
30 |
+
# Add your image processing code here.
|
31 |
+
# This will include preprocessing the image, passing it through the model,
|
32 |
+
# and then formatting the output (extracted features).
|
33 |
+
|
34 |
+
# Load the index
|
35 |
+
with open("images.json", "r") as f:
|
36 |
+
images = json.load(f)
|
37 |
+
|
38 |
+
# Convert to RGB if it isn't already
|
39 |
+
if image.mode != "RGB":
|
40 |
+
image = image.convert("RGB")
|
41 |
+
|
42 |
+
# Resize to 64px while maintaining aspect ratio
|
43 |
+
width, height = image.size
|
44 |
+
if width < height:
|
45 |
+
w_percent = 64 / float(width)
|
46 |
+
new_width = 64
|
47 |
+
new_height = int(float(height) * float(w_percent))
|
48 |
+
else:
|
49 |
+
h_percent = 64 / float(height)
|
50 |
+
new_height = 64
|
51 |
+
new_width = int(float(width) * float(h_percent))
|
52 |
+
image = image.resize((new_width, new_height), Image.LANCZOS)
|
53 |
+
|
54 |
+
# Extract the features from the uploaded image
|
55 |
+
with torch.no_grad():
|
56 |
+
inputs = dino_v2_image_processor(images=image, return_tensors="pt").to(
|
57 |
+
torch_device
|
58 |
+
)
|
59 |
+
outputs = dino_v2_model(**inputs)
|
60 |
+
|
61 |
+
# Normalize the features before search, whatever that means
|
62 |
+
embeddings = outputs.last_hidden_state
|
63 |
+
embeddings = embeddings.mean(dim=1)
|
64 |
+
vector = embeddings.detach().cpu().numpy()
|
65 |
+
vector = np.float32(vector)
|
66 |
+
faiss.normalize_L2(vector)
|
67 |
+
|
68 |
+
# Read the index file and perform search of top 50 images
|
69 |
+
index = faiss.read_index("vector.index")
|
70 |
+
distances, indices = index.search(vector, 50)
|
71 |
+
matches = []
|
72 |
+
for idx, matching_gamerpic in enumerate(indices[0]):
|
73 |
+
gamerpic = images[matching_gamerpic]
|
74 |
+
print(gamerpic)
|
75 |
+
# Return the corresponding title with only the matched gamerpic
|
76 |
+
title = db.titles.find_one(
|
77 |
+
{"gamerpics.cdn": gamerpic},
|
78 |
+
{"name": 1, "type": 1, "url": 1, "gamerpics.$": 1},
|
79 |
+
)
|
80 |
+
|
81 |
+
print(title)
|
82 |
+
|
83 |
+
title["rank"] = idx
|
84 |
+
title["score"] = str(round((1 / (distances[0][idx] + 1) * 100), 2)) + "%"
|
85 |
+
matches.append(title)
|
86 |
+
|
87 |
+
return matches
|
88 |
+
|
89 |
+
|
90 |
+
# Create a Gradio interface
|
91 |
+
iface = gr.Interface(
|
92 |
+
fn=process_image,
|
93 |
+
inputs=gr.Image(type="pil"), # Adjust the shape as needed
|
94 |
+
outputs="json", # Or any other output format that suits your needs
|
95 |
+
)
|
96 |
+
|
97 |
+
# Launch the Gradio app
|
98 |
+
iface.launch(share="true")
|
images.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
2 |
+
|
3 |
+
torch==2.1.1+cpu
|
4 |
+
numpy==1.26.0
|
5 |
+
pillow==10.0.1
|
6 |
+
transformers==4.34.0
|
7 |
+
pymongo[srv]==3.11
|
8 |
+
faiss-cpu==1.7.4
|
9 |
+
gradio
|
10 |
+
python-dotenv
|
vector.index
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f0902a19181cd3f4abd47ac168e1547522f64d615d566b42846618c5021eeefe
|
3 |
+
size 99370029
|