Update app.py
Browse files
app.py
CHANGED
|
@@ -371,26 +371,68 @@ def create_interface():
|
|
| 371 |
image_output = gr.Image(label="Aperçu")
|
| 372 |
status = gr.Textbox(label="Statut", interactive=False)
|
| 373 |
|
| 374 |
-
def generate(
|
| 375 |
-
|
| 376 |
-
params =
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
"
|
| 380 |
-
"
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
}
|
| 391 |
-
|
| 392 |
-
logger.
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
generate_btn.click(
|
| 396 |
generate,
|
|
|
|
| 371 |
image_output = gr.Image(label="Aperçu")
|
| 372 |
status = gr.Textbox(label="Statut", interactive=False)
|
| 373 |
|
| 374 |
+
def generate(self, params: Dict[str, Any]) -> Tuple[Optional[Image.Image], str]:
|
| 375 |
+
try:
|
| 376 |
+
logger.info(f"Début de génération avec paramètres: {json.dumps(params, indent=2)}")
|
| 377 |
+
|
| 378 |
+
if 'Bearer None' in self.headers['Authorization']:
|
| 379 |
+
logger.error("Token Hugging Face manquant ou invalide")
|
| 380 |
+
return None, "⚠️ Erreur: Token Hugging Face non configuré"
|
| 381 |
+
|
| 382 |
+
prompt = self._build_prompt(params)
|
| 383 |
+
logger.info(f"Prompt généré: {prompt}")
|
| 384 |
+
|
| 385 |
+
content_type = self._detect_content_type(params["subject"])
|
| 386 |
+
logger.info(f"Type de contenu détecté: {content_type}")
|
| 387 |
+
|
| 388 |
+
payload = {
|
| 389 |
+
"inputs": prompt,
|
| 390 |
+
"parameters": {
|
| 391 |
+
"negative_prompt": ART_STYLES[params["style"]]["negative_prompt"],
|
| 392 |
+
"num_inference_steps": min(int(35 * (params["quality"]/100)), 40),
|
| 393 |
+
"guidance_scale": min(7.5 * (params["creativity"]/10), 10.0),
|
| 394 |
+
"width": 768,
|
| 395 |
+
"height": 768 if params["orientation"] == "Portrait" else 512
|
| 396 |
+
}
|
| 397 |
}
|
| 398 |
+
|
| 399 |
+
logger.debug(f"Payload pour l'API: {json.dumps(payload, indent=2)}")
|
| 400 |
+
|
| 401 |
+
try:
|
| 402 |
+
response = requests.post(
|
| 403 |
+
self.API_URL,
|
| 404 |
+
headers=self.headers,
|
| 405 |
+
json=payload,
|
| 406 |
+
timeout=30
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
logger.info(f"Statut de la réponse API: {response.status_code}")
|
| 410 |
+
if response.status_code != 200:
|
| 411 |
+
logger.error(f"Contenu de la réponse en erreur: {response.text}")
|
| 412 |
+
|
| 413 |
+
if response.status_code == 200:
|
| 414 |
+
image = Image.open(io.BytesIO(response.content))
|
| 415 |
+
return image, "✨ Création réussie!"
|
| 416 |
+
else:
|
| 417 |
+
error_msg = f"⚠️ Erreur API {response.status_code}: {response.text}"
|
| 418 |
+
logger.error(error_msg)
|
| 419 |
+
return None, error_msg
|
| 420 |
+
|
| 421 |
+
except requests.exceptions.Timeout:
|
| 422 |
+
error_msg = "⚠️ Erreur: Le serveur met trop de temps à répondre"
|
| 423 |
+
logger.error(error_msg)
|
| 424 |
+
return None, error_msg
|
| 425 |
+
except requests.exceptions.RequestException as e:
|
| 426 |
+
error_msg = f"⚠️ Erreur de connexion: {str(e)}"
|
| 427 |
+
logger.error(error_msg)
|
| 428 |
+
return None, error_msg
|
| 429 |
+
|
| 430 |
+
except Exception as e:
|
| 431 |
+
error_msg = f"⚠️ Erreur inattendue: {str(e)}"
|
| 432 |
+
logger.exception("Erreur détaillée pendant la génération:")
|
| 433 |
+
return None, error_msg
|
| 434 |
+
finally:
|
| 435 |
+
gc.collect()
|
| 436 |
|
| 437 |
generate_btn.click(
|
| 438 |
generate,
|