|
import streamlit as st |
|
import requests |
|
from PIL import Image |
|
import base64 |
|
|
|
import streamlit as st |
|
import streamlit_authenticator as stauth |
|
|
|
import yaml |
|
from yaml.loader import SafeLoader |
|
|
|
|
|
class ChatbotApp: |
|
def __init__(self): |
|
|
|
self.backend_url = "http://localhost:5001/chat" |
|
self.title = "Chatbot Carômetro" |
|
self.description = "Este assistente virtual pode te ajudar com informações sobre carômetros da Sicoob." |
|
|
|
def stream_chat(self, user_input): |
|
""" |
|
Faz a comunicação com o backend e retorna a resposta como streaming de tokens. |
|
""" |
|
try: |
|
response = requests.post( |
|
self.backend_url, |
|
json={"message": user_input}, |
|
stream=True |
|
) |
|
response.raise_for_status() |
|
|
|
|
|
for chunk in response.iter_content(chunk_size=512): |
|
if chunk: |
|
yield chunk.decode("utf-8") |
|
except Exception as e: |
|
yield f"Erro ao conectar ao servidor: {e}" |
|
|
|
def render_sidebar(self): |
|
""" |
|
Exibe opções na barra lateral e renderiza a logo do Sicoob. |
|
""" |
|
st.sidebar.title("Configuração de LLM") |
|
sidebar_option = st.sidebar.radio("Selecione o LLM", ["gpt-3.5-turbo"]) |
|
if sidebar_option != "gpt-3.5-turbo": |
|
raise Exception("Opção de LLM inválida!") |
|
|
|
|
|
with open("sicoob-logo.png", "rb") as f: |
|
data = base64.b64encode(f.read()).decode("utf-8") |
|
st.sidebar.markdown( |
|
f""" |
|
<div style="display:table;margin-top:-80%;margin-left:0%;"> |
|
<img src="data:image/png;base64,{data}" width="250" height="70"> |
|
</div> |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
def render(self): |
|
""" |
|
Renderiza a interface do chatbot. |
|
""" |
|
|
|
im = Image.open("pngegg.png") |
|
st.set_page_config(page_title="Chatbot Carômetro", page_icon=im, layout="wide") |
|
|
|
with open('./config.yaml') as file: |
|
config = yaml.load(file, Loader=SafeLoader) |
|
|
|
|
|
|
|
|
|
authenticator = stauth.Authenticate( |
|
config['credentials'], |
|
config['cookie']['name'], |
|
config['cookie']['key'], |
|
config['cookie']['expiry_days'] |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('./config.yaml', 'w', encoding='utf-8') as file: |
|
yaml.dump(config, file, default_flow_style=False) |
|
|
|
|
|
authentication_status = authenticator.login() |
|
|
|
if st.session_state["authentication_status"]: |
|
authenticator.logout('Logout', 'main') |
|
|
|
|
|
self.render_sidebar() |
|
|
|
|
|
st.title(self.title) |
|
st.write(self.description) |
|
|
|
|
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
|
|
for message in st.session_state.chat_history: |
|
role, text = message.split(":", 1) |
|
with st.chat_message(role.strip().lower()): |
|
st.write(text.strip()) |
|
|
|
|
|
user_input = st.chat_input("Digite sua pergunta") |
|
if user_input: |
|
|
|
with st.chat_message("user"): |
|
st.write(user_input) |
|
st.session_state.chat_history.append(f"user: {user_input}") |
|
|
|
|
|
with st.chat_message("assistant"): |
|
message_placeholder = st.empty() |
|
assistant_message = "" |
|
|
|
|
|
for token in self.stream_chat(user_input): |
|
assistant_message += token |
|
message_placeholder.markdown(assistant_message + "▌") |
|
|
|
|
|
message_placeholder.markdown(assistant_message) |
|
st.session_state.chat_history.append(f"assistant: {assistant_message}") |
|
elif st.session_state["authentication_status"] == False: |
|
st.error('Username/password is incorrect') |
|
elif st.session_state["authentication_status"] == None: |
|
st.warning('Please enter your username and password') |
|
|
|
if __name__ == "__main__": |
|
chatbot_app = ChatbotApp() |
|
chatbot_app.render() |
|
|