ishworrsubedii commited on
Commit
1ec97e3
·
1 Parent(s): 6ff09bb

add: replicate image generation

Browse files
.gitignore CHANGED
@@ -1,7 +1,143 @@
 
 
1
  # Byte-compiled / optimized / DLL files
2
  __pycache__/
3
  *.py[cod]
4
  *$py.class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  .idea/
6
 
 
7
  *.log
 
1
+ # .gitignore
2
+
3
  # Byte-compiled / optimized / DLL files
4
  __pycache__/
5
  *.py[cod]
6
  *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+ cover/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ .python-version
88
+
89
+ # pipenv
90
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
91
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
92
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
93
+ # install all needed dependencies.
94
+ # Pipfile.lock
95
+
96
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
97
+ __pypackages__/
98
+
99
+ # Celery stuff
100
+ celerybeat-schedule
101
+ celerybeat.pid
102
+
103
+ # SageMath parsed files
104
+ *.sage.py
105
+
106
+ # Environments
107
+ .env
108
+ .venv
109
+ env/
110
+ venv/
111
+ ENV/
112
+ env.bak/
113
+ venv.bak/
114
+
115
+ # Spyder project settings
116
+ .spyderproject
117
+ .spyderworkspace
118
+
119
+ # Rope project settings
120
+ .ropeproject
121
+
122
+ # mkdocs documentation
123
+ /site
124
+
125
+ # mypy
126
+ .mypy_cache/
127
+ .dmypy.json
128
+ dmypy.json
129
+
130
+ # Pyre type checker
131
+ .pyre/
132
+
133
+ # pytype static type analyzer
134
+ .pytype/
135
+
136
+ # Cython debug symbols
137
+ cython_debug/
138
+
139
+ # PyCharm
140
  .idea/
141
 
142
+ # Local logs
143
  *.log
app.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  from fastapi.encoders import jsonable_encoder
2
  from src.utils import supabaseGetPublicURL, deductAndTrackCredit, returnBytesData
3
  from fastapi import FastAPI, File, UploadFile, Header, HTTPException, Form, Depends
@@ -12,13 +16,10 @@ import pandas as pd
12
  import base64
13
  import os
14
  from pydantic import BaseModel
15
- from gradio_client import Client, file
16
- from huggingface_hub import login
17
 
18
- HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
19
- login(HUGGINGFACE_TOKEN)
20
 
21
- client = Client("techconsptr/CTO_TCP_V1")
22
  pipeline = Pipeline()
23
  app = FastAPI(title="Magical Mirror Web Plugin")
24
 
@@ -29,8 +30,8 @@ app.add_middleware(
29
  allow_methods=["*"],
30
  allow_headers=["*"],
31
  )
32
- url: str = os.environ["SUPABASE_URL"]
33
- key: str = os.environ["SUPABASE_KEY"]
34
  supabase: Client = create_client(url, key)
35
 
36
 
@@ -40,14 +41,68 @@ class ClothingTryOnEntity(BaseModel):
40
 
41
 
42
  @app.post("/clothingTryOn")
43
- async def clothing_try_on(request: ClothingTryOnEntity):
44
- result = client.predict(
45
- input_image_base64=request.image,
46
- mask_image_base64=request.mask,
47
- api_name="/clothing_try_on_base64"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  )
 
 
 
 
 
 
 
 
 
 
49
 
50
- return JSONResponse(content=result, status_code=200)
 
 
 
 
 
51
 
52
 
53
  @app.post("/productData/{storeId}")
@@ -248,3 +303,9 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
248
 
249
  else:
250
  return JSONResponse(content=response)
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import requests
4
+ from PIL.ImageOps import grayscale
5
  from fastapi.encoders import jsonable_encoder
6
  from src.utils import supabaseGetPublicURL, deductAndTrackCredit, returnBytesData
7
  from fastapi import FastAPI, File, UploadFile, Header, HTTPException, Form, Depends
 
16
  import base64
17
  import os
18
  from pydantic import BaseModel
19
+ import replicate
 
20
 
21
+ from txt import jewellery_mask
 
22
 
 
23
  pipeline = Pipeline()
24
  app = FastAPI(title="Magical Mirror Web Plugin")
25
 
 
30
  allow_methods=["*"],
31
  allow_headers=["*"],
32
  )
33
+ url: str = os.getenv("SUPABASE_URL")
34
+ key: str = os.getenv("SUPABASE_KEY")
35
  supabase: Client = create_client(url, key)
36
 
37
 
 
41
 
42
 
43
  @app.post("/clothingTryOn")
44
+ async def clothing_try_on(image: UploadFile = File(...),
45
+ mask: UploadFile = File(...)):
46
+ image_bytes = await image.read()
47
+ mask_bytes = await mask.read()
48
+ image, mask = Image.open(BytesIO(image_bytes)).convert("RGB"), Image.open(
49
+ BytesIO(mask_bytes)).convert("RGB")
50
+
51
+ actual_image = image.copy()
52
+
53
+ jewellery_mask = Image.fromarray(
54
+ np.bitwise_and(np.array(mask), np.array(image))
55
+ )
56
+
57
+ arr_orig = np.array(grayscale(mask))
58
+
59
+ image = cv2.inpaint(np.array(image), arr_orig, 15, cv2.INPAINT_TELEA)
60
+ image = Image.fromarray(image).resize((512, 512))
61
+
62
+ arr = arr_orig.copy()
63
+ mask_y = np.where(arr == arr[arr != 0][0])[0][0]
64
+ arr[mask_y:, :] = 255
65
+
66
+ mask = Image.fromarray(arr).resize((512, 512))
67
+
68
+ mask_img_base_64, act_img_base_64 = BytesIO(), BytesIO()
69
+ mask.save(mask_img_base_64, format="WEBP")
70
+ image.save(act_img_base_64, format="WEBP")
71
+ mask_bytes_ = base64.b64encode(mask_img_base_64.getvalue()).decode("utf-8")
72
+ image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
73
+
74
+ mask_data_uri = f"data:image/webp;base64,{mask_bytes_}"
75
+ image_data_uri = f"data:image/webp;base64,{image_bytes_}"
76
+
77
+ input = {
78
+ "mask": mask_data_uri,
79
+ "image": image_data_uri,
80
+ "prompt": "South Indian Saree, properly worn, natural setting, elegant, natural look, neckline without jewellery, simple",
81
+ "negative_prompt": "necklaces, jewellery, jewelry, necklace, neckpiece, garland, chain, neck wear, jewelled neck, jeweled neck, necklace on neck, jewellery on neck, accessories, watermark, text, changed background, wider body, narrower body, bad proportions, extra limbs, mutated hands, changed sizes, altered proportions, unnatural body proportions, blury, ugly",
82
+ "num_inference_steps": 25
83
+ }
84
+
85
+ output = replicate.run(
86
+ "stability-ai/stable-diffusion-inpainting:95b7223104132402a9ae91cc677285bc5eb997834bd2349fa486f53910fd68b3",
87
+ input=input
88
  )
89
+ image_url = output[0]
90
+
91
+ response = requests.get(image_url)
92
+ output_image = Image.open(BytesIO(response.content)).resize(actual_image.size)
93
+
94
+ result = Image.fromarray(np.bitwise_or(np.array(output_image), np.array(jewellery_mask)))
95
+
96
+ in_mem_file = BytesIO()
97
+ result.save(in_mem_file, format="WEBP", quality=85)
98
+ base_64_output = base64.b64encode(in_mem_file.getvalue()).decode('utf-8')
99
 
100
+ response = {
101
+ "output": f"data:image/WEBP;base64,{base_64_output}",
102
+ 'code': 200
103
+ }
104
+
105
+ return JSONResponse(content=response, status_code=200)
106
 
107
 
108
  @app.post("/productData/{storeId}")
 
303
 
304
  else:
305
  return JSONResponse(content=response)
306
+
307
+
308
+ if __name__ == '__main__':
309
+ import uvicorn
310
+
311
+ uvicorn.run(app, host="localhost", port=8000)
requirements.txt CHANGED
@@ -77,6 +77,5 @@ uvicorn==0.30.1
77
  uvloop==0.19.0
78
  watchfiles==0.22.0
79
  websockets==12.0
80
- huggingface_hub
81
- gradio_client
82
  -e .
 
77
  uvloop==0.19.0
78
  watchfiles==0.22.0
79
  websockets==12.0
80
+ replicate
 
81
  -e .
src/components/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (152 Bytes)
 
src/components/__pycache__/necklaceTryOn.cpython-310.pyc DELETED
Binary file (6.35 kB)
 
src/pipelines/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (151 Bytes)
 
src/pipelines/__pycache__/completePipeline.cpython-310.pyc DELETED
Binary file (925 Bytes)
 
src/utils/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (1.7 kB)
 
src/utils/__pycache__/backgroundEnhancerArchitecture.cpython-310.pyc DELETED
Binary file (9.92 kB)
 
src/utils/__pycache__/exceptions.cpython-310.pyc DELETED
Binary file (1.03 kB)
 
src/utils/__pycache__/logger.cpython-310.pyc DELETED
Binary file (683 Bytes)