Docfile commited on
Commit
09be1bb
·
verified ·
1 Parent(s): 1c16ef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -5
app.py CHANGED
@@ -296,19 +296,58 @@ def api_francais():
296
  def generate_stream():
297
  try:
298
  logger.info("Démarrage du streaming de génération...")
 
 
299
 
300
  for chunk in client.models.generate_content_stream(
301
  model=model_name,
302
  contents=[user_prompt],
303
  config=config
304
  ):
 
 
 
 
 
 
 
 
 
 
 
 
305
  if not chunk.candidates:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  continue
307
 
308
- for part in chunk.candidates[0].content.parts:
309
- if not part.text:
310
  continue
311
-
 
 
312
  if hasattr(part, 'thought') and part.thought:
313
  # Pensées du modèle (mode deepthink)
314
  yield "data: " + json.dumps({
@@ -322,8 +361,16 @@ def api_francais():
322
  'content': part.text
323
  }) + "\n\n"
324
 
325
- logger.info("Fin du streaming de génération.")
326
- yield "data: " + json.dumps({'type': 'done'}) + "\n\n"
 
 
 
 
 
 
 
 
327
 
328
  except Exception as e:
329
  logger.exception("Erreur pendant la génération de contenu.")
 
296
  def generate_stream():
297
  try:
298
  logger.info("Démarrage du streaming de génération...")
299
+ content_generated = False
300
+ chunk_count = 0
301
 
302
  for chunk in client.models.generate_content_stream(
303
  model=model_name,
304
  contents=[user_prompt],
305
  config=config
306
  ):
307
+ chunk_count += 1
308
+
309
+ # Vérifier si la réponse a été bloquée
310
+ if hasattr(chunk, 'prompt_feedback') and chunk.prompt_feedback:
311
+ logger.warning(f"Prompt feedback: {chunk.prompt_feedback}")
312
+ if hasattr(chunk.prompt_feedback, 'block_reason'):
313
+ yield "data: " + json.dumps({
314
+ 'type': 'error',
315
+ 'content': f'Contenu bloqué par les filtres de sécurité: {chunk.prompt_feedback.block_reason}'
316
+ }) + "\n\n"
317
+ return
318
+
319
  if not chunk.candidates:
320
+ logger.warning(f"Chunk {chunk_count} sans candidats")
321
+ continue
322
+
323
+ candidate = chunk.candidates[0]
324
+
325
+ # Vérifier le statut du candidat
326
+ if hasattr(candidate, 'finish_reason') and candidate.finish_reason:
327
+ finish_reason = str(candidate.finish_reason)
328
+ if finish_reason != 'STOP' and finish_reason != '0':
329
+ logger.warning(f"Génération terminée avec raison: {finish_reason}")
330
+ if 'SAFETY' in finish_reason:
331
+ yield "data: " + json.dumps({
332
+ 'type': 'error',
333
+ 'content': 'Contenu bloqué par les filtres de sécurité.'
334
+ }) + "\n\n"
335
+ return
336
+
337
+ if not hasattr(candidate, 'content') or not candidate.content:
338
+ logger.warning(f"Chunk {chunk_count} sans contenu")
339
+ continue
340
+
341
+ if not hasattr(candidate.content, 'parts') or not candidate.content.parts:
342
+ logger.warning(f"Chunk {chunk_count} sans parts")
343
  continue
344
 
345
+ for part in candidate.content.parts:
346
+ if not hasattr(part, 'text') or not part.text:
347
  continue
348
+
349
+ content_generated = True
350
+
351
  if hasattr(part, 'thought') and part.thought:
352
  # Pensées du modèle (mode deepthink)
353
  yield "data: " + json.dumps({
 
361
  'content': part.text
362
  }) + "\n\n"
363
 
364
+ logger.info(f"Fin du streaming. Chunks traités: {chunk_count}, Contenu généré: {content_generated}")
365
+
366
+ if not content_generated:
367
+ logger.error("Aucun contenu n'a été généré!")
368
+ yield "data: " + json.dumps({
369
+ 'type': 'error',
370
+ 'content': 'Aucun contenu généré. Le modèle n\'a pas produit de réponse.'
371
+ }) + "\n\n"
372
+ else:
373
+ yield "data: " + json.dumps({'type': 'done'}) + "\n\n"
374
 
375
  except Exception as e:
376
  logger.exception("Erreur pendant la génération de contenu.")