Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
import base64
|
| 3 |
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 |
-
|
| 14 |
-
raise gr.Error("Falta el token HF_TOKEN en el servidor.")
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
print("[+] Petición recibida desde Android. Procesando
|
| 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的真实人物,图1真实人物保持高度人脸一致性"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
"inputs": {
|
| 27 |
-
"text": prompt_texto,
|
| 28 |
-
"images": [data_uri_real, data_uri_ref]
|
| 29 |
-
},
|
| 30 |
-
"parameters": {
|
| 31 |
-
"max_new_tokens": 512
|
| 32 |
-
}
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
headers = {
|
| 36 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 37 |
-
"Content-Type": "application/json"
|
| 38 |
-
}
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
#
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=fusionar_cosplay_bridge,
|
| 64 |
inputs=[gr.Textbox(label="Real"), gr.Textbox(label="Ref")],
|
|
@@ -66,6 +54,6 @@ demo = gr.Interface(
|
|
| 66 |
api_name="predict"
|
| 67 |
)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch(ssr_mode=False)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import traceback
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
from gradio_client import Client
|
| 5 |
|
| 6 |
+
# 1. Inicializamos el cliente oficial de Hugging Face que hereda las DNS del clúster
|
|
|
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
+
# Usamos la API Serverless mediante el cliente nativo de Gradio
|
| 10 |
+
try:
|
| 11 |
+
# Vincula directamente el modelo espacial sin usar peticiones HTTP crudas
|
| 12 |
+
hf_client = Client("joyfox/Qwen-Image-Edit-Cosplay", hf_token=HF_TOKEN)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"[-] Error inicializando el cliente de Hugging Face: {e}")
|
| 15 |
+
hf_client = None
|
| 16 |
+
|
| 17 |
def fusionar_cosplay_bridge(imagen_real, imagen_ref):
|
| 18 |
+
if not HF_TOKEN or hf_client is None:
|
| 19 |
+
raise gr.Error("El servidor no tiene configurado o validado el HF_TOKEN en los Secrets.")
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
print("[+] Petición recibida desde Android. Procesando a través del cliente nativo...")
|
| 23 |
|
| 24 |
+
# Aseguramos los prefijos Data URI limpios
|
| 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的真实人物,图1真实人物保持高度人脸一致性"
|
| 29 |
|
| 30 |
+
print("[+] Enviando parámetros al modelo Qwen...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Al usar .predict() del cliente nativo, Gradio resuelve internamente la red sin lanzar NameResolutionError
|
| 33 |
+
resultado = hf_client.predict(
|
| 34 |
+
parameter_0=data_uri_real,
|
| 35 |
+
parameter_1=data_uri_ref,
|
| 36 |
+
parameter_2=prompt_texto,
|
| 37 |
+
api_name="/predict"
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
print(f"[+] Procesamiento exitoso. Retornando estructura...")
|
| 41 |
+
return {"data": resultado}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
print("\n================= CRASH EN EL PUENTE =================")
|
| 45 |
traceback.print_exc()
|
| 46 |
print("=====================================================\n")
|
| 47 |
+
raise gr.Error(f"Crash interno en el puente: {str(e)}")
|
| 48 |
|
| 49 |
+
# 2. Interfaz limpia expuesta como API universal para Android
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=fusionar_cosplay_bridge,
|
| 52 |
inputs=[gr.Textbox(label="Real"), gr.Textbox(label="Ref")],
|
|
|
|
| 54 |
api_name="predict"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Desactivamos el SSR experimental para máxima compatibilidad con clientes HTTP crudos
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch(ssr_mode=False)
|