Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,22 +50,30 @@ def find_previous_explanation(row, table_data):
|
|
| 50 |
return "Não foi encontrada nenhuma explicação em anos anteriores."
|
| 51 |
|
| 52 |
def response(user_question, table_data):
|
| 53 |
-
#
|
| 54 |
question_en = translate(user_question, pt_en_translator, tokenizer, source_lang="pt", target_lang="en")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
encoding = tapex_tokenizer(table=table_data, query=[question_en], padding=True, return_tensors="pt", truncation=True)
|
| 58 |
outputs = tapex_model.generate(**encoding)
|
| 59 |
response_en = tapex_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 60 |
|
| 61 |
-
#
|
| 62 |
response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
if "Explicação" in user_question:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
return response_pt
|
| 71 |
|
|
|
|
| 50 |
return "Não foi encontrada nenhuma explicação em anos anteriores."
|
| 51 |
|
| 52 |
def response(user_question, table_data):
|
| 53 |
+
# Traduz a pergunta para o inglês
|
| 54 |
question_en = translate(user_question, pt_en_translator, tokenizer, source_lang="pt", target_lang="en")
|
| 55 |
|
| 56 |
+
# Gera a resposta em inglês
|
| 57 |
encoding = tapex_tokenizer(table=table_data, query=[question_en], padding=True, return_tensors="pt", truncation=True)
|
| 58 |
outputs = tapex_model.generate(**encoding)
|
| 59 |
response_en = tapex_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 60 |
|
| 61 |
+
# Traduz a resposta para o português
|
| 62 |
response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
|
| 63 |
+
|
| 64 |
+
# Verifica se a resposta contém uma solicitação de explicação
|
| 65 |
if "Explicação" in user_question:
|
| 66 |
+
# Tenta encontrar a linha correspondente na tabela
|
| 67 |
+
matching_rows = table_data[table_data['Explicação'] == response_pt]
|
| 68 |
+
if not matching_rows.empty:
|
| 69 |
+
row = matching_rows.iloc[0]
|
| 70 |
+
if not row['Explicação']:
|
| 71 |
+
response_pt = find_previous_explanation(row, table_data)
|
| 72 |
+
else:
|
| 73 |
+
response_pt = "Não foi possível encontrar uma explicação correspondente."
|
| 74 |
+
|
| 75 |
+
return response_pt
|
| 76 |
+
|
| 77 |
|
| 78 |
return response_pt
|
| 79 |
|