Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,11 @@ import traceback
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# 1. Configuración de la API externa
|
| 8 |
API_URL = "https://api-inference.huggingface.co/models/joyfox/Qwen-Image-Edit-Cosplay"
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
| 12 |
-
"""
|
| 13 |
-
Función puente que recibe las peticiones de Android a través de Gradio,
|
| 14 |
-
las reempaqueta con el formato de Qwen y llama a la API de Inferencia Serverless.
|
| 15 |
-
"""
|
| 16 |
if not HF_TOKEN:
|
| 17 |
print("[-] ERROR: El HF_TOKEN no está configurado en los Secrets del Space.")
|
| 18 |
raise gr.Error("Falta el token HF_TOKEN en el servidor.")
|
|
@@ -20,14 +16,13 @@ def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
|
| 20 |
try:
|
| 21 |
print("[+] Petición recibida desde Android a través del puente de Gradio...")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
# Aseguramos que tengan el prefijo Data URI limpio para el modelo Qwen.
|
| 25 |
data_uri_real = imagen_real if imagen_real.startswith("data:") else f"data:image/jpeg;base64,{imagen_real}"
|
| 26 |
data_uri_ref = imagen_ref if imagen_ref.startswith("data:") else f"data:image/jpeg;base64,{imagen_ref}"
|
| 27 |
|
| 28 |
-
prompt_texto = "把图2中动漫人物中的发型,服饰和装扮道具迁移到图1
|
| 29 |
|
| 30 |
-
# Payload
|
| 31 |
json_payload = {
|
| 32 |
"inputs": {
|
| 33 |
"text": prompt_texto,
|
|
@@ -50,11 +45,11 @@ def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
|
| 50 |
if respuesta.status_code == 200:
|
| 51 |
content_type = respuesta.headers.get("content-type", "").lower()
|
| 52 |
|
| 53 |
-
# Si el modelo responde con JSON estructurado
|
| 54 |
if "application/json" in content_type:
|
|
|
|
| 55 |
return respuesta.json()
|
| 56 |
-
# Si responde directamente con los bytes crudos de la imagen procesada
|
| 57 |
else:
|
|
|
|
| 58 |
imagen_procesada_base64 = base64.b64encode(respuesta.content).decode("utf-8")
|
| 59 |
return {"data": f"data:image/jpeg;base64,{imagen_procesada_base64}"}
|
| 60 |
else:
|
|
@@ -67,25 +62,15 @@ def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
|
| 67 |
print("=====================================================\n")
|
| 68 |
raise gr.Error(f"Crash interno en el servidor: {str(e)}")
|
| 69 |
|
| 70 |
-
# 2.
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
gr.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
output_json = gr.JSON()
|
| 80 |
-
|
| 81 |
-
# Definimos la función asociada con un nombre de endpoint explícito (api_name)
|
| 82 |
-
btn = gr.Button("Procesar", visible=False)
|
| 83 |
-
btn.click(
|
| 84 |
-
fn=fusionar_cosplay_bridge,
|
| 85 |
-
inputs=[input_real, input_ref],
|
| 86 |
-
outputs=output_json,
|
| 87 |
-
api_name="predict"
|
| 88 |
-
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
demo.launch()
|
|
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# 1. Configuración de la API externa de Hugging Face
|
| 8 |
API_URL = "https://api-inference.huggingface.co/models/joyfox/Qwen-Image-Edit-Cosplay"
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if not HF_TOKEN:
|
| 13 |
print("[-] ERROR: El HF_TOKEN no está configurado en los Secrets del Space.")
|
| 14 |
raise gr.Error("Falta el token HF_TOKEN en el servidor.")
|
|
|
|
| 16 |
try:
|
| 17 |
print("[+] Petición recibida desde Android a través del puente de Gradio...")
|
| 18 |
|
| 19 |
+
# Aseguramos que los strings Base64 tengan el prefijo Data URI limpio
|
|
|
|
| 20 |
data_uri_real = imagen_real if imagen_real.startswith("data:") else f"data:image/jpeg;base64,{imagen_real}"
|
| 21 |
data_uri_ref = imagen_ref if imagen_ref.startswith("data:") else f"data:image/jpeg;base64,{imagen_ref}"
|
| 22 |
|
| 23 |
+
prompt_texto = "把图2中动漫人物中的发型,服饰和装扮道具迁移到图1 de la 真实人物,图1真实人物保持高度人脸一致性"
|
| 24 |
|
| 25 |
+
# Payload estructurado para el modelo de visión Qwen
|
| 26 |
json_payload = {
|
| 27 |
"inputs": {
|
| 28 |
"text": prompt_texto,
|
|
|
|
| 45 |
if respuesta.status_code == 200:
|
| 46 |
content_type = respuesta.headers.get("content-type", "").lower()
|
| 47 |
|
|
|
|
| 48 |
if "application/json" in content_type:
|
| 49 |
+
# Si devuelve la estructura JSON del modelo
|
| 50 |
return respuesta.json()
|
|
|
|
| 51 |
else:
|
| 52 |
+
# Si devuelve los bytes directos de la imagen final
|
| 53 |
imagen_procesada_base64 = base64.b64encode(respuesta.content).decode("utf-8")
|
| 54 |
return {"data": f"data:image/jpeg;base64,{imagen_procesada_base64}"}
|
| 55 |
else:
|
|
|
|
| 62 |
print("=====================================================\n")
|
| 63 |
raise gr.Error(f"Crash interno en el servidor: {str(e)}")
|
| 64 |
|
| 65 |
+
# 2. Interfaz nativa de Gradio compatible con API REST externa
|
| 66 |
+
# Definimos una Interface clásica. Esto obliga a Gradio a exponer la ruta /api/predict automáticamente
|
| 67 |
+
demo = gr.Interface(
|
| 68 |
+
fn=fusionar_cosplay_bridge,
|
| 69 |
+
inputs=[gr.Textbox(label="Imagen Real Base64"), gr.Textbox(label="Imagen Referencia Base64")],
|
| 70 |
+
outputs=gr.JSON(label="Resultado"),
|
| 71 |
+
title="Cosplay API Bridge Active 🎭",
|
| 72 |
+
description="Servidor optimizado para peticiones móviles desde Android."
|
| 73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
demo.launch()
|