Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -28,26 +28,6 @@ def translate(text, model, tokenizer, source_lang="pt", target_lang="en"):
|
|
28 |
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
return translated_text
|
30 |
|
31 |
-
def find_previous_explanation(row, table_data):
|
32 |
-
current_date = pd.to_datetime(row['Ano e mês'])
|
33 |
-
current_group = row['Grupo']
|
34 |
-
|
35 |
-
while True:
|
36 |
-
# Subtract one year to look for the previous year's same month
|
37 |
-
current_date = current_date - pd.DateOffset(years=1)
|
38 |
-
previous_row = table_data[
|
39 |
-
(table_data['Ano e mês'] == current_date.strftime('%Y-%m-01')) &
|
40 |
-
(table_data['Grupo'] == current_group)
|
41 |
-
]
|
42 |
-
|
43 |
-
if not previous_row.empty and previous_row.iloc[0]['Explicação']:
|
44 |
-
return f"Em {current_date.strftime('%B de %Y')}, a explicação foi: {previous_row.iloc[0]['Explicação']}"
|
45 |
-
|
46 |
-
# Stop if we've searched 10 years back without finding anything
|
47 |
-
if current_date.year < pd.to_datetime(row['Ano e mês']).year - 10:
|
48 |
-
break
|
49 |
-
|
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
|
@@ -62,7 +42,7 @@ def response(user_question, table_data):
|
|
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
|
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:
|
@@ -70,11 +50,7 @@ def response(user_question, table_data):
|
|
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 |
|
80 |
# Streamlit interface
|
|
|
28 |
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
return translated_text
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def response(user_question, table_data):
|
33 |
# Traduz a pergunta para o inglês
|
|
|
42 |
response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
|
43 |
|
44 |
# Verifica se a resposta contém uma solicitação de explicação
|
45 |
+
if 'Explicação' or 'explicação' in user_question:
|
46 |
# Tenta encontrar a linha correspondente na tabela
|
47 |
matching_rows = table_data[table_data['Explicação'] == response_pt]
|
48 |
if not matching_rows.empty:
|
|
|
50 |
if not row['Explicação']:
|
51 |
response_pt = find_previous_explanation(row, table_data)
|
52 |
else:
|
53 |
+
response_pt = "Não foi possível encontrar uma explicação correspondente."
|
|
|
|
|
|
|
|
|
54 |
return response_pt
|
55 |
|
56 |
# Streamlit interface
|