Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,24 @@ import traceback
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 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
|
| 14 |
raise gr.Error("Falta el token HF_TOKEN en el servidor.")
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
print("[+] Petición recibida desde Android
|
| 18 |
|
| 19 |
-
#
|
| 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
|
| 24 |
|
| 25 |
-
# Payload estructurado para el modelo de visión Qwen
|
| 26 |
json_payload = {
|
| 27 |
"inputs": {
|
| 28 |
"text": prompt_texto,
|
|
@@ -38,39 +37,36 @@ def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
|
| 38 |
"Content-Type": "application/json"
|
| 39 |
}
|
| 40 |
|
| 41 |
-
print("[+]
|
| 42 |
respuesta = requests.post(API_URL, headers=headers, json=json_payload, timeout=110)
|
| 43 |
-
print(f"[+]
|
| 44 |
|
| 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:
|
| 56 |
-
print(f"[-] Error de
|
| 57 |
raise gr.Error(f"HF API Error: {respuesta.status_code}")
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
print("\n================= CRASH EN EL PUENTE =================")
|
| 61 |
traceback.print_exc()
|
| 62 |
print("=====================================================\n")
|
| 63 |
-
raise gr.Error(f"Crash interno
|
| 64 |
|
| 65 |
-
#
|
| 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="
|
| 70 |
outputs=gr.JSON(label="Resultado"),
|
| 71 |
-
|
| 72 |
-
description="Servidor optimizado para peticiones móviles desde Android."
|
| 73 |
)
|
| 74 |
|
|
|
|
|
|
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
demo.launch()
|
|
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# 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 |
if not HF_TOKEN:
|
| 13 |
+
print("[-] ERROR: El HF_TOKEN no está configurado.")
|
| 14 |
raise gr.Error("Falta el token HF_TOKEN en el servidor.")
|
| 15 |
|
| 16 |
try:
|
| 17 |
+
print("[+] Petición recibida desde Android. Procesando cadenas...")
|
| 18 |
|
| 19 |
+
# Validamos y formateamos los prefijos Data URI requeridos por el modelo Qwen
|
| 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的真实人物,图1真实人物保持高度人脸一致性"
|
| 24 |
|
|
|
|
| 25 |
json_payload = {
|
| 26 |
"inputs": {
|
| 27 |
"text": prompt_texto,
|
|
|
|
| 37 |
"Content-Type": "application/json"
|
| 38 |
}
|
| 39 |
|
| 40 |
+
print("[+] Conectando con la API de Inferencia...")
|
| 41 |
respuesta = requests.post(API_URL, headers=headers, json=json_payload, timeout=110)
|
| 42 |
+
print(f"[+] API de Inferencia respondió con código: {respuesta.status_code}")
|
| 43 |
|
| 44 |
if respuesta.status_code == 200:
|
| 45 |
content_type = respuesta.headers.get("content-type", "").lower()
|
|
|
|
| 46 |
if "application/json" in content_type:
|
|
|
|
| 47 |
return respuesta.json()
|
| 48 |
else:
|
|
|
|
| 49 |
imagen_procesada_base64 = base64.b64encode(respuesta.content).decode("utf-8")
|
| 50 |
return {"data": f"data:image/jpeg;base64,{imagen_procesada_base64}"}
|
| 51 |
else:
|
| 52 |
+
print(f"[-] Error de Hugging Face: {respuesta.text}")
|
| 53 |
raise gr.Error(f"HF API Error: {respuesta.status_code}")
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print("\n================= CRASH EN EL PUENTE =================")
|
| 57 |
traceback.print_exc()
|
| 58 |
print("=====================================================\n")
|
| 59 |
+
raise gr.Error(f"Crash interno: {str(e)}")
|
| 60 |
|
| 61 |
+
# Montamos la interfaz tradicional para forzar la exposición de endpoints REST estándar
|
|
|
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=fusionar_cosplay_bridge,
|
| 64 |
+
inputs=[gr.Textbox(label="Real"), gr.Textbox(label="Ref")],
|
| 65 |
outputs=gr.JSON(label="Resultado"),
|
| 66 |
+
api_name="predict"
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# 🌟 CRÍTICO PARA GRADIO 6: Desactivamos el SSR experimental y la transpilación de funciones a JS
|
| 70 |
+
# Esto apaga el servidor Node.js interno y delega todo el tráfico directamente al proceso Python ASGI.
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
demo.launch(ssr_mode=False, show_api=True)
|