Spaces:
Sleeping
Sleeping
Mikhaylov Alexey
commited on
Commit
·
fdb9dcc
1
Parent(s):
cb04a08
first commit
Browse files- .gitignore +2 -0
- app.py +73 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv
|
| 2 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import zipfile
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import openai
|
| 7 |
+
import os
|
| 8 |
+
from scipy import ndimage
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 16 |
+
removebg_api_key = os.getenv('REMOVEBG_API_KEY')
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def get_circle_footprint(size):
|
| 20 |
+
xx, yy = np.mgrid[0:size * 2, 0:size * 2]
|
| 21 |
+
fp = ((xx - size) ** 2 + (yy - size) ** 2) < (size) ** 2
|
| 22 |
+
return fp
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test(prompt, img_path, mask_margin):
|
| 26 |
+
response = requests.post(
|
| 27 |
+
'https://api.remove.bg/v1.0/removebg',
|
| 28 |
+
files={'image_file': open(img_path, 'rb')},
|
| 29 |
+
data={
|
| 30 |
+
'size': 'auto',
|
| 31 |
+
'format': 'zip'
|
| 32 |
+
},
|
| 33 |
+
headers={'X-Api-Key': removebg_api_key},
|
| 34 |
+
)
|
| 35 |
+
if response.status_code == requests.codes.ok:
|
| 36 |
+
zipFile = zipfile.ZipFile(io.BytesIO(response.content))
|
| 37 |
+
maskIm = Image.open(io.BytesIO(zipFile.read('alpha.png')))
|
| 38 |
+
|
| 39 |
+
alpha= maskIm.getchannel(0)
|
| 40 |
+
if mask_margin > 0:
|
| 41 |
+
inflated_alpha = ndimage.maximum_filter(input=np.array(alpha), footprint=get_circle_footprint(mask_margin))
|
| 42 |
+
alpha = Image.fromarray(np.uint8(inflated_alpha))
|
| 43 |
+
maskIm.paste((255),[0,0,maskIm.size[0],maskIm.size[1]])
|
| 44 |
+
maskIm.putalpha(alpha)
|
| 45 |
+
|
| 46 |
+
maskFile = io.BytesIO()
|
| 47 |
+
maskIm.save(maskFile, format='PNG')
|
| 48 |
+
maskFile.seek(0)
|
| 49 |
+
|
| 50 |
+
response = openai.Image.create_edit(
|
| 51 |
+
image=open(img_path, "rb"),
|
| 52 |
+
mask=maskFile,
|
| 53 |
+
prompt=prompt,
|
| 54 |
+
n=1,
|
| 55 |
+
size="512x512"
|
| 56 |
+
)
|
| 57 |
+
return response['data'][0]['url']
|
| 58 |
+
|
| 59 |
+
# with open('no-bg.zip', 'wb') as out:
|
| 60 |
+
# out.write(response.content)
|
| 61 |
+
else:
|
| 62 |
+
print("Error:", response.status_code, response.text)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 67 |
+
demo = gr.Interface(test, inputs = [
|
| 68 |
+
'text',
|
| 69 |
+
gr.Image(type='filepath', shape=(500,500), label='image'),
|
| 70 |
+
gr.Slider(minimum=0, maximum=10, value=5, step=1, label="mask margin")
|
| 71 |
+
], outputs=["image"])
|
| 72 |
+
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
Binary file (104 Bytes). View file
|
|
|