import streamlit as st
import pandas as pd
import torch
from transformers import TapexTokenizer, BartForConditionalGeneration
import datetime

# Load the CSV file
df = pd.read_csv("anomalies.csv", quotechar='"')

df.rename(columns={"ds": "Ano e mês", "real": "Valor Monetário", "Group": "Grupo"}, inplace=True)

df.sort_values(by=['Ano e mês', 'Valor Monetário'], ascending=False, inplace=True)

print(df)

# Filter 'real' higher than 10 Million
df= df[df['Valor Monetário'] >= 1000000.]

# Convert 'real' column to standard float format and then to strings
df['Valor Monetário'] = df['Valor Monetário'].apply(lambda x: f"{x:.2f}")

# Fill NaN values and convert all columns to strings
df = df.fillna('').astype(str)

table_data = df

# Function to generate a response using the TAPEX model
def response(user_question, table_data):
    a = datetime.datetime.now()

    model_name = "microsoft/tapex-large-finetuned-wtq"
    model = BartForConditionalGeneration.from_pretrained(model_name)
    tokenizer = TapexTokenizer.from_pretrained(model_name)

    queries = [user_question]

    encoding = tokenizer(table=table_data, query=queries, padding=True, return_tensors="pt", truncation=True)

    # Experiment with generation parameters
    outputs = model.generate(
        **encoding
    )

    ans = tokenizer.batch_decode(outputs, skip_special_tokens=True)

    query_result = {
        "Resposta": ans[0]
    }

    b = datetime.datetime.now()
    print(b - a)

    return query_result

# Streamlit interface

st.dataframe(table_data.head())

st.markdown("""
<div style='display: flex; align-items: center;'>
    <div style='width: 40px; height: 40px; background-color: green; border-radius: 50%; margin-right: 5px;'></div>
    <div style='width: 40px; height: 40px; background-color: red; border-radius: 50%; margin-right: 5px;'></div>
    <div style='width: 40px; height: 40px; background-color: yellow; border-radius: 50%; margin-right: 5px;'></div>
    <span style='font-size: 40px; font-weight: bold;'>Chatbot do Tesouro RS</span>
</div>
""", unsafe_allow_html=True)

# Chat history
if 'history' not in st.session_state:
    st.session_state['history'] = []

# Input box for user question
user_question = st.text_input("Escreva sua questão aqui:", "")

if user_question:
    # Add human emoji when user asks a question
    st.session_state['history'].append(('👤', user_question))
    st.markdown(f"**👤 {user_question}**")
    
    # Generate the response
    bot_response = response(user_question, table_data)["Resposta"]
    
    # Add robot emoji when generating response and align to the right
    st.session_state['history'].append(('🤖', bot_response))
    st.markdown(f"<div style='text-align: right'>**🤖 {bot_response}**</div>", unsafe_allow_html=True)

# Clear history button
if st.button("Limpar"):
    st.session_state['history'] = []

# Display chat history
for sender, message in st.session_state['history']:
    if sender == '👤':
        st.markdown(f"**👤 {message}**")
    elif sender == '🤖':
        st.markdown(f"<div style='text-align: right'>**🤖 {message}**</div>", unsafe_allow_html=True)