Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,46 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Preprocess the images by resizing them to 512x512
|
| 33 |
-
person_image = person_image.convert("RGB").resize((512, 512))
|
| 34 |
-
garment_image = garment_image.convert("RGB").resize((512, 512))
|
| 35 |
-
|
| 36 |
-
# Use segment_body to generate the body mask for inpainting
|
| 37 |
-
seg_image, mask_image = segment_body(person_image, face=False) # You can control face removal here (face=False)
|
| 38 |
-
|
| 39 |
-
# Resize mask to 512x512 to match the inpainting requirements
|
| 40 |
-
mask_image = mask_image.resize((512, 512))
|
| 41 |
-
|
| 42 |
-
# Perform inpainting using the pipeline
|
| 43 |
-
results = pipeline(
|
| 44 |
-
prompt=prompt,
|
| 45 |
-
negative_prompt="ugly, bad quality, bad anatomy",
|
| 46 |
-
image=person_image,
|
| 47 |
-
mask_image=mask_image, # Use the mask from segmentation
|
| 48 |
-
ip_adapter_image=garment_image, # Garment image as the IP Adapter image
|
| 49 |
-
strength=0.99,
|
| 50 |
-
guidance_scale=8.0,
|
| 51 |
-
num_inference_steps=100
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
return results.images[0] # Return the generated image
|
| 55 |
-
|
| 56 |
-
# Set up the Gradio interface
|
| 57 |
-
demo = gr.Interface(
|
| 58 |
-
fn=inpaint,
|
| 59 |
inputs=[
|
| 60 |
-
gr.Image(type="pil", label="
|
| 61 |
-
gr.Image(type="pil", label="
|
| 62 |
-
gr.Textbox(label="Prompt", placeholder="Enter the prompt for the model") # Text prompt for inpainting
|
| 63 |
],
|
| 64 |
-
outputs=gr.Image(type="pil"),
|
| 65 |
-
title="
|
| 66 |
-
description="
|
| 67 |
-
server_timeout=100, # Increase timeout duration to prevent session errors
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# URL del modelo TryOnGAN (o cualquier otro modelo Hugging Face)
|
| 7 |
+
MODEL_URL = "https://huggingface.co/spaces/akhaliq/TryOnGAN"
|
| 8 |
+
|
| 9 |
+
# Función que procesa la imagen usando la API de Hugging Face
|
| 10 |
+
def try_on_clothes(photo, clothing):
|
| 11 |
+
# Convertir las imágenes a formato adecuado para la solicitud
|
| 12 |
+
files = {
|
| 13 |
+
'photo': ('photo.jpg', photo, 'image/jpeg'),
|
| 14 |
+
'clothing': ('clothing.jpg', clothing, 'image/jpeg')
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Enviar las imágenes al modelo de Hugging Face
|
| 19 |
+
response = requests.post(MODEL_URL, files=files)
|
| 20 |
+
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
# Suponiendo que la respuesta es una imagen generada (puede variar según el modelo)
|
| 23 |
+
output_image = Image.open(io.BytesIO(response.content))
|
| 24 |
+
return output_image
|
| 25 |
+
else:
|
| 26 |
+
return "Error al procesar las imágenes, intente nuevamente."
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
# Crear la interfaz de Gradio
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=try_on_clothes,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
inputs=[
|
| 34 |
+
gr.Image(type="pil", label="Foto del Maniquí"),
|
| 35 |
+
gr.Image(type="pil", label="Prenda a Probar")
|
|
|
|
| 36 |
],
|
| 37 |
+
outputs=gr.Image(type="pil", label="Resultado del Probador Virtual"),
|
| 38 |
+
title="Probador Virtual AI",
|
| 39 |
+
description="Sube una foto de tu maniquí y una prenda para probarla en tiempo real con TryOnGAN."
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Ejecutar la interfaz
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
iface.launch()
|
| 45 |
+
|
| 46 |
+
|