Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from transformers import TapasConfig, TapasForQuestionAnswering
|
5 |
+
from transformers import pipeline
|
6 |
+
import datetime
|
7 |
+
|
8 |
+
# Load the data
|
9 |
+
df = pd.read_excel('discrepantes.xlsx')
|
10 |
+
df.fillna(0, inplace=True)
|
11 |
+
table_data = df.astype(str)
|
12 |
+
print(table_data.head())
|
13 |
+
|
14 |
+
# Function to generate a response using the TAPEX model
|
15 |
+
def response(user_question, table_data):
|
16 |
+
a = datetime.datetime.now()
|
17 |
+
|
18 |
+
model = TapasForQuestionAnswering.from_pretrained("google/tapas-base")
|
19 |
+
tqa = pipeline(task="table-question-answering", model="google/tapas-large-finetuned-wtq")
|
20 |
+
answer = tqa(table=table_data, query=user_question)['answer']
|
21 |
+
query_result = {
|
22 |
+
"Resposta": answer
|
23 |
+
}
|
24 |
+
|
25 |
+
b = datetime.datetime.now()
|
26 |
+
print(b - a)
|
27 |
+
|
28 |
+
return query_result
|
29 |
+
|
30 |
+
# Streamlit interface
|
31 |
+
st.markdown("""
|
32 |
+
<div style='display: flex; align-items: center;'>
|
33 |
+
<div style='width: 40px; height: 40px; background-color: green; border-radius: 50%; margin-right: 5px;'></div>
|
34 |
+
<div style='width: 40px; height: 40px; background-color: red; border-radius: 50%; margin-right: 5px;'></div>
|
35 |
+
<div style='width: 40px; height: 40px; background-color: yellow; border-radius: 50%; margin-right: 5px;'></div>
|
36 |
+
<span style='font-size: 40px; font-weight: bold;'>Chatbot do Tesouro RS</span>
|
37 |
+
</div>
|
38 |
+
""", unsafe_allow_html=True)
|
39 |
+
|
40 |
+
# Chat history
|
41 |
+
if 'history' not in st.session_state:
|
42 |
+
st.session_state['history'] = []
|
43 |
+
|
44 |
+
# Input box for user question
|
45 |
+
user_question = st.text_input("Escreva sua questão aqui:", "")
|
46 |
+
|
47 |
+
if user_question:
|
48 |
+
# Add human emoji when user asks a question
|
49 |
+
st.session_state['history'].append(('👤', user_question))
|
50 |
+
st.markdown(f"**👤 {user_question}**")
|
51 |
+
|
52 |
+
# Generate the response
|
53 |
+
bot_response = response(user_question, table_data)["Resposta"]
|
54 |
+
|
55 |
+
# Add robot emoji when generating response and align to the right
|
56 |
+
st.session_state['history'].append(('🤖', bot_response))
|
57 |
+
st.markdown(f"<div style='text-align: right'>**🤖 {bot_response}**</div>", unsafe_allow_html=True)
|
58 |
+
|
59 |
+
# Clear history button
|
60 |
+
if st.button("Limpar"):
|
61 |
+
st.session_state['history'] = []
|
62 |
+
|
63 |
+
# Display chat history
|
64 |
+
for sender, message in st.session_state['history']:
|
65 |
+
if sender == '👤':
|
66 |
+
st.markdown(f"**👤 {message}**")
|
67 |
+
elif sender == '🤖':
|
68 |
+
st.markdown(f"<div style='text-align: right'>**🤖 {message}**</div>", unsafe_allow_html=True)
|