habulaj commited on
Commit
6243d62
·
verified ·
1 Parent(s): 0693215

Update routers/inference.py

Browse files
Files changed (1) hide show
  1. routers/inference.py +20 -33
routers/inference.py CHANGED
@@ -25,9 +25,8 @@ class NewsResponse(BaseModel):
25
  title: str
26
  subhead: str
27
  content: str
28
- sources: list[str] # Lista de URLs/links utilizados
29
- title_instagram: str # Adicionado campo Instagram title
30
- content_instagram: str # Adicionado campo Instagram description
31
 
32
  # Referência ao diretório de arquivos temporários (deve ser o mesmo do outro módulo)
33
  TEMP_DIR = Path("/tmp")
@@ -260,33 +259,9 @@ def extract_text_from_response(response):
260
 
261
  def extract_sources_from_response(response):
262
  """
263
- Extrai as fontes (URLs) do grounding metadata.
264
  """
265
- sources = []
266
-
267
- if not (hasattr(response, 'candidates') and response.candidates):
268
- return sources
269
-
270
- for candidate in response.candidates:
271
- if not (hasattr(candidate, 'grounding_metadata') and candidate.grounding_metadata):
272
- continue
273
-
274
- grounding_metadata = candidate.grounding_metadata
275
-
276
- if hasattr(grounding_metadata, 'grounding_chunks') and grounding_metadata.grounding_chunks:
277
- for chunk in grounding_metadata.grounding_chunks:
278
- try:
279
- if (hasattr(chunk, 'web') and chunk.web and
280
- hasattr(chunk.web, 'uri') and chunk.web.uri):
281
-
282
- uri = chunk.web.uri
283
- if uri and uri not in sources:
284
- sources.append(uri)
285
-
286
- except Exception:
287
- continue
288
-
289
- return sources
290
 
291
  @router.post("/rewrite-news", response_model=NewsResponse)
292
  async def rewrite_news(news: NewsRequest):
@@ -486,11 +461,15 @@ News base: Noah Centineo Attached to Play Rambo in Prequel Movie 'John Rambo'
486
 
487
  logger.info("Resposta do modelo recebida com sucesso")
488
 
489
- # Extrair texto e fontes
490
  response_text = extract_text_from_response(response)
491
- sources = extract_sources_from_response(response)
492
 
493
  logger.info(f"Texto extraído: {len(response_text) if response_text else 0} caracteres")
 
 
 
 
 
494
 
495
  # Verificar se o texto está vazio
496
  if not response_text or response_text.strip() == "":
@@ -523,20 +502,28 @@ News base: Noah Centineo Attached to Play Rambo in Prequel Movie 'John Rambo'
523
  else:
524
  content = "Conteúdo não encontrado"
525
 
526
- # Campos do Instagram (corrigindo os nomes das variáveis)
527
  insta_title_match = re.search(r"<instagram_title>(.*?)</instagram_title>", response_text, re.DOTALL)
528
  title_instagram = insta_title_match.group(1).strip() if insta_title_match else "Título Instagram não encontrado"
529
 
530
  insta_desc_match = re.search(r"<instagram_description>(.*?)</instagram_description>", response_text, re.DOTALL)
531
  content_instagram = insta_desc_match.group(1).strip() if insta_desc_match else "Descrição Instagram não encontrada"
532
 
 
 
 
 
 
 
 
 
 
533
  logger.info(f"Processamento concluído com sucesso - Título: {title[:50]}...")
534
 
535
  return NewsResponse(
536
  title=title,
537
  subhead=subhead,
538
  content=content,
539
- sources=sources,
540
  title_instagram=title_instagram,
541
  content_instagram=content_instagram
542
  )
 
25
  title: str
26
  subhead: str
27
  content: str
28
+ title_instagram: str # Campo Instagram title
29
+ content_instagram: str # Campo Instagram description
 
30
 
31
  # Referência ao diretório de arquivos temporários (deve ser o mesmo do outro módulo)
32
  TEMP_DIR = Path("/tmp")
 
259
 
260
  def extract_sources_from_response(response):
261
  """
262
+ Função removida - sources não são mais necessárias.
263
  """
264
+ return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
 
266
  @router.post("/rewrite-news", response_model=NewsResponse)
267
  async def rewrite_news(news: NewsRequest):
 
461
 
462
  logger.info("Resposta do modelo recebida com sucesso")
463
 
464
+ # Extrair texto
465
  response_text = extract_text_from_response(response)
 
466
 
467
  logger.info(f"Texto extraído: {len(response_text) if response_text else 0} caracteres")
468
+
469
+ # Log da resposta bruta completa para debug
470
+ logger.info("=== RESPOSTA BRUTA DA API ===")
471
+ logger.info(f"Resposta completa: {response_text}")
472
+ logger.info("=== FIM RESPOSTA BRUTA ===")
473
 
474
  # Verificar se o texto está vazio
475
  if not response_text or response_text.strip() == "":
 
502
  else:
503
  content = "Conteúdo não encontrado"
504
 
505
+ # Campos do Instagram com debug adicional
506
  insta_title_match = re.search(r"<instagram_title>(.*?)</instagram_title>", response_text, re.DOTALL)
507
  title_instagram = insta_title_match.group(1).strip() if insta_title_match else "Título Instagram não encontrado"
508
 
509
  insta_desc_match = re.search(r"<instagram_description>(.*?)</instagram_description>", response_text, re.DOTALL)
510
  content_instagram = insta_desc_match.group(1).strip() if insta_desc_match else "Descrição Instagram não encontrada"
511
 
512
+ # Debug específico para Instagram fields
513
+ logger.info(f"Instagram Title Match: {bool(insta_title_match)}")
514
+ logger.info(f"Instagram Description Match: {bool(insta_desc_match)}")
515
+
516
+ if insta_title_match:
517
+ logger.info(f"Instagram Title encontrado: {title_instagram[:100]}...")
518
+ if insta_desc_match:
519
+ logger.info(f"Instagram Description encontrado: {content_instagram[:100]}...")
520
+
521
  logger.info(f"Processamento concluído com sucesso - Título: {title[:50]}...")
522
 
523
  return NewsResponse(
524
  title=title,
525
  subhead=subhead,
526
  content=content,
 
527
  title_instagram=title_instagram,
528
  content_instagram=content_instagram
529
  )