|
import streamlit as st |
|
from database import KodeksProcessor |
|
from chatbot import Chatbot |
|
import os |
|
import pandas as pd |
|
|
|
def initialize_session_state(): |
|
if 'chatbot' not in st.session_state: |
|
st.session_state.chatbot = Chatbot() |
|
if 'messages' not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
def main(): |
|
st.title("Asystent Prawny") |
|
|
|
initialize_session_state() |
|
|
|
|
|
if 'db_initialized' not in st.session_state: |
|
with st.spinner("Inicjalizacja bazy danych..."): |
|
processor = KodeksProcessor() |
|
if not os.path.exists("chroma_db"): |
|
processor.process_all_files("data/kodeksy") |
|
st.session_state.db_initialized = True |
|
|
|
|
|
st.sidebar.title("Nawigacja") |
|
page = st.sidebar.radio("Wybierz stronę:", ["Chatbot", "Podgląd danych"]) |
|
|
|
if page == "Chatbot": |
|
|
|
if st.sidebar.button("Wyczyść historię"): |
|
st.session_state.chatbot.clear_history() |
|
st.session_state.messages = [] |
|
st.experimental_rerun() |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
if prompt := st.chat_input("Zadaj pytanie dotyczące prawa..."): |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
|
|
processor = KodeksProcessor() |
|
relevant_chunks = processor.search(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
message_placeholder = st.empty() |
|
full_response = "" |
|
|
|
context = st.session_state.chatbot.generate_context( |
|
[{"text": doc} for doc in relevant_chunks['documents'][0]] |
|
) |
|
|
|
for response_chunk in st.session_state.chatbot.get_response(prompt, context): |
|
full_response += response_chunk |
|
message_placeholder.markdown(full_response + "▌") |
|
|
|
message_placeholder.markdown(full_response) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": full_response}) |
|
|
|
elif page == "Podgląd danych": |
|
st.subheader("Dane w bazie") |
|
processor = KodeksProcessor() |
|
|
|
|
|
all_docs = processor.collection.query(query_texts=[""], n_results=1000) |
|
|
|
|
|
data = [] |
|
for doc in all_docs['documents'][0]: |
|
data.append(doc) |
|
|
|
|
|
if data: |
|
df = pd.DataFrame(data) |
|
st.dataframe(df) |
|
else: |
|
st.write("Brak danych w bazie.") |
|
|
|
if __name__ == "__main__": |
|
main() |