fschwartzer commited on
Commit
8df29f4
1 Parent(s): ee399ee

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from transformers import BartForConditionalGeneration, TapexTokenizer, T5ForConditionalGeneration, T5Tokenizer
4
+ import datetime
5
+
6
+ # Load CSV file
7
+ df = pd.read_csv("anomalies.csv", quotechar='"')
8
+ df.rename(columns={"ds": "Ano e mês", "real": "Valor Monetário", "Group": "Grupo"}, inplace=True)
9
+ df.sort_values(by=['Ano e mês', 'Valor Monetário'], ascending=False, inplace=True)
10
+ df = df[df['Valor Monetário'] >= 1000000.]
11
+ df['Valor Monetário'] = df['Valor Monetário'].apply(lambda x: f"{x:.2f}")
12
+ df = df.fillna('').astype(str)
13
+
14
+ # Load translation models
15
+ pt_en_translator = T5ForConditionalGeneration.from_pretrained("unicamp-dl/translation-pt-en-t5")
16
+ en_pt_translator = T5ForConditionalGeneration.from_pretrained("unicamp-dl/translation-en-pt-t5")
17
+ tokenizer = T5Tokenizer.from_pretrained("unicamp-dl/translation-pt-en-t5")
18
+
19
+ # Load TAPEX model
20
+ tapex_model = BartForConditionalGeneration.from_pretrained("microsoft/tapex-large-finetuned-wtq")
21
+ tapex_tokenizer = TapexTokenizer.from_pretrained("microsoft/tapex-large-finetuned-wtq")
22
+
23
+ def translate(text, model, tokenizer, source_lang="pt", target_lang="en"):
24
+ input_ids = tokenizer.encode(text, return_tensors="pt", add_special_tokens=True)
25
+ outputs = model.generate(input_ids)
26
+ translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+ return translated_text
28
+
29
+ def response(user_question, table_data):
30
+ # Translate question to English
31
+ question_en = translate(user_question, pt_en_translator, tokenizer, source_lang="pt", target_lang="en")
32
+
33
+ # Generate response in English
34
+ encoding = tapex_tokenizer(table=table_data, query=[question_en], padding=True, return_tensors="pt", truncation=True)
35
+ outputs = tapex_model.generate(**encoding)
36
+ response_en = tapex_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
37
+
38
+ # Translate response to Portuguese
39
+ response_pt = translate(response_en, en_pt_translator, tokenizer, source_lang="en", target_lang="pt")
40
+ return response_pt
41
+
42
+ # Streamlit interface
43
+
44
+ st.dataframe(table_data.head())
45
+
46
+ st.markdown("""
47
+ <div style='display: flex; align-items: center;'>
48
+ <div style='width: 40px; height: 40px; background-color: green; border-radius: 50%; margin-right: 5px;'></div>
49
+ <div style='width: 40px; height: 40px; background-color: red; border-radius: 50%; margin-right: 5px;'></div>
50
+ <div style='width: 40px; height: 40px; background-color: yellow; border-radius: 50%; margin-right: 5px;'></div>
51
+ <span style='font-size: 40px; font-weight: bold;'>Chatbot do Tesouro RS</span>
52
+ </div>
53
+ """, unsafe_allow_html=True)
54
+
55
+ # Chat history
56
+ if 'history' not in st.session_state:
57
+ st.session_state['history'] = []
58
+
59
+ # Input box for user question
60
+ user_question = st.text_input("Escreva sua questão aqui:", "")
61
+
62
+ if user_question:
63
+ # Add human emoji when user asks a question
64
+ st.session_state['history'].append(('👤', user_question))
65
+ st.markdown(f"**👤 {user_question}**")
66
+
67
+ # Generate the response
68
+ bot_response = response(user_question, table_data)["Resposta"]
69
+
70
+ # Add robot emoji when generating response and align to the right
71
+ st.session_state['history'].append(('🤖', bot_response))
72
+ st.markdown(f"<div style='text-align: right'>**🤖 {bot_response}**</div>", unsafe_allow_html=True)
73
+
74
+ # Clear history button
75
+ if st.button("Limpar"):
76
+ st.session_state['history'] = []
77
+
78
+ # Display chat history
79
+ for sender, message in st.session_state['history']:
80
+ if sender == '👤':
81
+ st.markdown(f"**👤 {message}**")
82
+ elif sender == '🤖':
83
+ st.markdown(f"<div style='text-align: right'>**🤖 {message}**</div>", unsafe_allow_html=True)