Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene):
|
| 2 |
cloth = cloth_selected if cloth_selected else cloth_upload
|
| 3 |
user = user_selected if user_selected else user_upload
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
import time
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
API_KEY = 'N80HWHVG3DV8URRNYZY382UPSHP1N8G1SNPYG0E9'
|
| 11 |
+
API_URL = 'https://api.runpod.ai/v2/31jyh9kh7nwyga'
|
| 12 |
+
|
| 13 |
+
cloth_images = [
|
| 14 |
+
{"url": "https://i.postimg.cc/7ZzLZtbc/hmgoepprod-4-1.jpg", "label": "Jacket 1"},
|
| 15 |
+
{"url": "https://i.postimg.cc/7Yd6DrP0/hmgoepprod-6.jpg", "label": "Jacket 2"},
|
| 16 |
+
{"url": "https://i.postimg.cc/8z40MNFr/hnm.png", "label": "Jacket 3"},
|
| 17 |
+
{"url": "https://i.postimg.cc/mkqbb74B/hmgoepprod-5.jpg", "label": "Jacket 4"},
|
| 18 |
+
{"url": "https://i.postimg.cc/vBwySGzH/hmgoepprod.jpg", "label": "Jacket 5"},
|
| 19 |
+
{"url": "https://i.postimg.cc/6qSYYprM/Whats-App-Image-2024-08-18-at-13-45-37-2.jpg", "label": "Jacket 6"},
|
| 20 |
+
{"url": "https://i.postimg.cc/tJHhTnzQ/Screenshot-2024-08-20-at-10-04-29-PM.png", "label": "Jacket 7"},
|
| 21 |
+
{"url": "https://i.postimg.cc/yxQYGwxQ/Screenshot-2024-08-20-at-10-06-28-PM.png", "label": "Jacket 8"},
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
user_images = [
|
| 25 |
+
{"url": "https://iili.io/dEk7qtp.md.jpg", "label": "User 1 (Male)"},
|
| 26 |
+
{"url": "https://iili.io/d1RpKBa.md.jpg", "label": "User 2 (Female)"},
|
| 27 |
+
{"url": "https://i.postimg.cc/QtzQrnFt/Whats-App-Image-2024-08-18-at-13-42-10.jpg", "label": "User 3 (Female)"},
|
| 28 |
+
{"url": "https://i.postimg.cc/rsKLDzyj/Whats-App-Image-2024-08-19-at-12-13-41.jpg", "label": "User 4 (Female)"},
|
| 29 |
+
{"url": "https://i.postimg.cc/cHkpGZ7n/Whats-App-Image-2024-08-19-at-00-27-16-1.jpg", "label": "User 5 (Female)"},
|
| 30 |
+
{"url": "https://i.postimg.cc/g2bfWghN/Whats-App-Image-2024-08-19-at-12-13-41-1.jpg", "label": "User 6 (Female)"},
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
scene_options = [
|
| 34 |
+
"Gym", "City Street", "Party", "Beach", "Office", "Park", "Cafe",
|
| 35 |
+
"Shopping Mall", "Concert", "Sports Stadium"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def get_base64_from_url(url):
|
| 39 |
+
response = requests.get(url)
|
| 40 |
+
return base64.b64encode(response.content).decode('utf-8')
|
| 41 |
+
|
| 42 |
+
def get_base64_from_image(image):
|
| 43 |
+
buffered = io.BytesIO()
|
| 44 |
+
image.save(buffered, format="PNG")
|
| 45 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 46 |
+
|
| 47 |
+
def generate_tryon(cloth_input, user_input, background):
|
| 48 |
+
if isinstance(cloth_input, str): # URL selected
|
| 49 |
+
cloth_base64 = get_base64_from_url(cloth_input)
|
| 50 |
+
else: # Image uploaded
|
| 51 |
+
cloth_base64 = get_base64_from_image(cloth_input)
|
| 52 |
+
|
| 53 |
+
if isinstance(user_input, str): # URL selected
|
| 54 |
+
user_base64 = get_base64_from_url(user_input)
|
| 55 |
+
else: # Image uploaded
|
| 56 |
+
user_base64 = get_base64_from_image(user_input)
|
| 57 |
+
|
| 58 |
+
input_data = {
|
| 59 |
+
"user_image": user_base64,
|
| 60 |
+
"product_image": cloth_base64,
|
| 61 |
+
"background": background
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
response = requests.post(
|
| 65 |
+
f"{API_URL}/run",
|
| 66 |
+
headers={
|
| 67 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 68 |
+
"Content-Type": "application/json"
|
| 69 |
+
},
|
| 70 |
+
data=json.dumps({"input": input_data})
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if not response.ok:
|
| 74 |
+
error_text = response.text
|
| 75 |
+
raise Exception(f"Failed to upload image: {response.status_code} {response.reason} - {error_text}")
|
| 76 |
+
|
| 77 |
+
job_id = response.json()['id']
|
| 78 |
+
|
| 79 |
+
while True:
|
| 80 |
+
status_response = requests.get(
|
| 81 |
+
f"{API_URL}/status/{job_id}",
|
| 82 |
+
headers={"Authorization": f"Bearer {API_KEY}"}
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if not status_response.ok:
|
| 86 |
+
raise Exception(f"Status check failed: {status_response.status_code} {status_response.reason} - {status_response.text}")
|
| 87 |
+
|
| 88 |
+
status_data = status_response.json()
|
| 89 |
+
if status_data['status'] == 'COMPLETED':
|
| 90 |
+
output_base64 = status_data['output']['output']
|
| 91 |
+
output_image = Image.open(io.BytesIO(base64.b64decode(output_base64)))
|
| 92 |
+
return output_image
|
| 93 |
+
elif status_data['status'] == 'FAILED':
|
| 94 |
+
raise Exception(f"Job processing failed: {status_data}")
|
| 95 |
+
|
| 96 |
+
time.sleep(2)
|
| 97 |
+
|
| 98 |
def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene):
|
| 99 |
cloth = cloth_selected if cloth_selected else cloth_upload
|
| 100 |
user = user_selected if user_selected else user_upload
|