Spaces:
Running
Running
fschwartzer
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,27 @@ 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 response(user_question, table_data):
|
32 |
# Translate question to English
|
33 |
question_en = translate(user_question, pt_en_translator, tokenizer, source_lang="pt", target_lang="en")
|
@@ -39,6 +60,13 @@ def response(user_question, table_data):
|
|
39 |
|
40 |
# Translate response to Portuguese
|
41 |
response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return response_pt
|
43 |
|
44 |
# Streamlit interface
|
|
|
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 |
# Translate question to English
|
54 |
question_en = translate(user_question, pt_en_translator, tokenizer, source_lang="pt", target_lang="en")
|
|
|
60 |
|
61 |
# Translate response to Portuguese
|
62 |
response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
|
63 |
+
|
64 |
+
# Check if the response contains a request for an explanation
|
65 |
+
if "Explicação" in user_question:
|
66 |
+
row = table_data[table_data['Explicação'] == response_pt].iloc[0]
|
67 |
+
if not row['Explicação']:
|
68 |
+
response_pt = find_previous_explanation(row, table_data)
|
69 |
+
|
70 |
return response_pt
|
71 |
|
72 |
# Streamlit interface
|