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

Update routers/inference.py

Browse files
Files changed (1) hide show
  1. routers/inference.py +64 -26
routers/inference.py CHANGED
@@ -169,10 +169,16 @@ def extract_text_from_response(response):
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:
@@ -188,33 +194,65 @@ def extract_text_from_response(response):
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
 
 
169
  """
170
  logger.info(f"Tipo da resposta: {type(response)}")
171
 
172
+ # Método 1: Tentar acessar response.text diretamente
173
+ try:
174
+ text_content = getattr(response, 'text', None)
175
+ if text_content:
176
+ logger.info(f"Texto extraído via response.text: {len(text_content)} caracteres")
177
+ return text_content
178
+ else:
179
+ logger.info("response.text existe mas está vazio/None")
180
+ except Exception as e:
181
+ logger.error(f"Erro ao acessar response.text: {e}")
182
 
183
  # Método 2: Verificar candidates
184
  if hasattr(response, 'candidates') and response.candidates:
 
194
 
195
  # Verificar se tem parts
196
  if hasattr(content, 'parts') and content.parts:
197
+ try:
198
+ parts_list = list(content.parts)
199
+ logger.info(f"Content tem {len(parts_list)} parts")
 
 
200
 
201
+ response_text = ""
202
+ for j, part in enumerate(parts_list):
203
+ logger.info(f"Processando part {j}, tipo: {type(part)}")
204
+
205
+ # Tentar várias formas de acessar o texto
206
+ part_text = None
207
+ if hasattr(part, 'text'):
208
+ part_text = getattr(part, 'text', None)
209
+
210
+ if part_text:
211
+ logger.info(f"Part {j} tem texto: {len(part_text)} caracteres")
212
+ response_text += part_text
213
+ else:
214
+ logger.info(f"Part {j} não tem texto ou está vazio")
215
+
216
+ if response_text:
217
+ return response_text
218
+ except Exception as e:
219
+ logger.error(f"Erro ao processar parts do candidate {i}: {e}")
220
 
221
+ # Método 3: Tentar usar método _get_text() se existir
222
  try:
223
+ if hasattr(response, '_get_text'):
224
+ text_content = response._get_text()
225
+ if text_content:
226
+ logger.info(f"Texto extraído via _get_text(): {len(text_content)} caracteres")
227
+ return text_content
 
 
228
  except Exception as e:
229
+ logger.error(f"Erro ao usar _get_text(): {e}")
230
 
231
+ # Método 4: Debug - tentar inspecionar a estrutura real
232
+ try:
233
+ logger.info("Tentando debug da estrutura:")
234
+ if hasattr(response, 'candidates') and response.candidates:
235
+ candidate = response.candidates[0]
236
+ logger.info(f"Primeiro candidate: {type(candidate)}")
237
+ logger.info(f"Atributos do candidate: {dir(candidate)}")
238
+
239
+ if hasattr(candidate, 'content'):
240
+ content = candidate.content
241
+ logger.info(f"Content: {type(content)}")
242
+ logger.info(f"Atributos do content: {dir(content)}")
243
+
244
+ if hasattr(content, 'parts'):
245
+ logger.info(f"Parts: {type(content.parts)}")
246
+ try:
247
+ parts_list = list(content.parts)
248
+ if parts_list:
249
+ first_part = parts_list[0]
250
+ logger.info(f"Primeiro part: {type(first_part)}")
251
+ logger.info(f"Atributos do part: {dir(first_part)}")
252
+ except Exception as e:
253
+ logger.error(f"Erro ao inspecionar parts: {e}")
254
+ except Exception as e:
255
+ logger.error(f"Erro no debug da estrutura: {e}")
256
 
257
  return ""
258