habulaj commited on
Commit
75bb18b
·
verified ·
1 Parent(s): b06a19f

Update routers/inference.py

Browse files
Files changed (1) hide show
  1. routers/inference.py +55 -18
routers/inference.py CHANGED
@@ -165,34 +165,58 @@ def load_sources_file(file_id: str) -> str:
165
 
166
  def extract_text_from_response(response):
167
  """
168
- Extrai o texto da resposta de forma robusta.
169
  """
170
- response_text = ""
171
 
 
172
  if hasattr(response, 'text') and response.text:
 
173
  return response.text
174
 
 
175
  if hasattr(response, 'candidates') and response.candidates:
176
- for candidate in response.candidates:
177
- if not hasattr(candidate, 'content') or not candidate.content:
178
- continue
179
-
180
- content = candidate.content
181
 
182
- if not hasattr(content, 'parts') or content.parts is None:
183
- continue
 
 
184
 
185
- try:
186
- parts_list = list(content.parts) if content.parts else []
187
-
188
- for part in parts_list:
189
- if hasattr(part, 'text') and part.text:
190
- response_text += part.text
 
191
 
192
- except Exception:
193
- continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
- return response_text
 
 
 
196
 
197
  def extract_sources_from_response(response):
198
  """
@@ -373,12 +397,23 @@ Daniels serves as a director on the show alongside Ken Kwapis, Yana Gorskaya, Pa
373
  config=config
374
  )
375
 
 
 
376
  # Extrair texto e fontes
377
  response_text = extract_text_from_response(response)
378
  sources = extract_sources_from_response(response)
379
 
 
 
380
  # Verificar se o texto está vazio
381
  if not response_text or response_text.strip() == "":
 
 
 
 
 
 
 
382
  raise HTTPException(
383
  status_code=500,
384
  detail="Modelo não retornou conteúdo válido"
@@ -401,6 +436,8 @@ Daniels serves as a director on the show alongside Ken Kwapis, Yana Gorskaya, Pa
401
  else:
402
  content = "Conteúdo não encontrado"
403
 
 
 
404
  return NewsResponse(title=title, subhead=subhead, content=content, sources=sources)
405
 
406
  except HTTPException:
 
165
 
166
  def extract_text_from_response(response):
167
  """
168
+ Extrai o texto da resposta de forma robusta com debug.
169
  """
170
+ logger.info(f"Tipo da resposta: {type(response)}")
171
 
172
+ # Método 1: Verificar se tem atributo 'text' diretamente
173
  if hasattr(response, 'text') and response.text:
174
+ logger.info("Texto extraído via response.text")
175
  return response.text
176
 
177
+ # Método 2: Verificar candidates
178
  if hasattr(response, 'candidates') and response.candidates:
179
+ logger.info(f"Encontrados {len(response.candidates)} candidates")
180
+
181
+ for i, candidate in enumerate(response.candidates):
182
+ logger.info(f"Processando candidate {i}")
 
183
 
184
+ # Verificar se tem content
185
+ if hasattr(candidate, 'content') and candidate.content:
186
+ content = candidate.content
187
+ logger.info(f"Candidate {i} tem content")
188
 
189
+ # Verificar se tem parts
190
+ if hasattr(content, 'parts') and content.parts:
191
+ logger.info(f"Content tem {len(list(content.parts))} parts")
192
+
193
+ response_text = ""
194
+ for j, part in enumerate(content.parts):
195
+ logger.info(f"Processando part {j}, tipo: {type(part)}")
196
 
197
+ if hasattr(part, 'text') and part.text:
198
+ logger.info(f"Part {j} tem texto: {len(part.text)} caracteres")
199
+ response_text += part.text
200
+
201
+ if response_text:
202
+ return response_text
203
+
204
+ # Método 3: Tentar acessar via string conversion
205
+ try:
206
+ response_str = str(response)
207
+ logger.info(f"Resposta como string (primeiros 200 chars): {response_str[:200]}")
208
+
209
+ # Se a string contém as tags esperadas, retornar ela
210
+ if '<headline>' in response_str and '<body>' in response_str:
211
+ return response_str
212
+
213
+ except Exception as e:
214
+ logger.error(f"Erro ao converter resposta para string: {e}")
215
 
216
+ # Debug: Logar atributos da resposta
217
+ logger.info(f"Atributos da resposta: {dir(response)}")
218
+
219
+ return ""
220
 
221
  def extract_sources_from_response(response):
222
  """
 
397
  config=config
398
  )
399
 
400
+ logger.info("Resposta do modelo recebida com sucesso")
401
+
402
  # Extrair texto e fontes
403
  response_text = extract_text_from_response(response)
404
  sources = extract_sources_from_response(response)
405
 
406
+ logger.info(f"Texto extraído: {len(response_text) if response_text else 0} caracteres")
407
+
408
  # Verificar se o texto está vazio
409
  if not response_text or response_text.strip() == "":
410
+ logger.error("Texto extraído está vazio")
411
+ # Debug adicional: tentar logar a resposta crua
412
+ try:
413
+ logger.error(f"Resposta crua (primeiros 500 chars): {str(response)[:500]}")
414
+ except:
415
+ logger.error("Não foi possível converter resposta para string")
416
+
417
  raise HTTPException(
418
  status_code=500,
419
  detail="Modelo não retornou conteúdo válido"
 
436
  else:
437
  content = "Conteúdo não encontrado"
438
 
439
+ logger.info(f"Processamento concluído com sucesso - Título: {title[:50]}...")
440
+
441
  return NewsResponse(title=title, subhead=subhead, content=content, sources=sources)
442
 
443
  except HTTPException: