Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from database import KodeksProcessor
|
3 |
from chatbot import Chatbot
|
4 |
import os
|
5 |
|
|
|
|
|
|
|
6 |
def initialize_session_state():
|
7 |
if 'chatbot' not in st.session_state:
|
8 |
st.session_state.chatbot = Chatbot()
|
@@ -11,9 +15,9 @@ def initialize_session_state():
|
|
11 |
|
12 |
def main():
|
13 |
st.title("Asystent Prawny")
|
14 |
-
|
15 |
initialize_session_state()
|
16 |
-
|
17 |
# Inicjalizacja bazy danych (jeśli potrzebna)
|
18 |
if 'db_initialized' not in st.session_state:
|
19 |
with st.spinner("Inicjalizacja bazy danych..."):
|
@@ -21,7 +25,8 @@ def main():
|
|
21 |
if not os.path.exists("chroma_db"):
|
22 |
processor.process_all_files("data/kodeksy")
|
23 |
st.session_state.db_initialized = True
|
24 |
-
|
|
|
25 |
# Przycisk do czyszczenia historii
|
26 |
if st.sidebar.button("Wyczyść historię"):
|
27 |
st.session_state.chatbot.clear_history()
|
@@ -37,29 +42,29 @@ def main():
|
|
37 |
if prompt := st.chat_input("Zadaj pytanie dotyczące prawa..."):
|
38 |
# Dodaj pytanie użytkownika do historii
|
39 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
40 |
-
|
41 |
with st.chat_message("user"):
|
42 |
st.markdown(prompt)
|
43 |
|
44 |
# Wyszukaj odpowiednie fragmenty w bazie
|
45 |
processor = KodeksProcessor()
|
46 |
relevant_chunks = processor.search(prompt)
|
47 |
-
|
48 |
# Wygeneruj odpowiedź
|
49 |
with st.chat_message("assistant"):
|
50 |
message_placeholder = st.empty()
|
51 |
full_response = ""
|
52 |
-
|
53 |
context = st.session_state.chatbot.generate_context(
|
54 |
[{"text": doc} for doc in relevant_chunks['documents'][0]]
|
55 |
)
|
56 |
-
|
57 |
for response_chunk in st.session_state.chatbot.get_response(prompt, context):
|
58 |
full_response += response_chunk
|
59 |
message_placeholder.markdown(full_response + "▌")
|
60 |
-
|
61 |
message_placeholder.markdown(full_response)
|
62 |
-
|
63 |
# Dodaj odpowiedź asystenta do historii
|
64 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
65 |
|
|
|
1 |
import streamlit as st
|
2 |
+
import logging
|
3 |
from database import KodeksProcessor
|
4 |
from chatbot import Chatbot
|
5 |
import os
|
6 |
|
7 |
+
# Konfiguracja logowania
|
8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
+
|
10 |
def initialize_session_state():
|
11 |
if 'chatbot' not in st.session_state:
|
12 |
st.session_state.chatbot = Chatbot()
|
|
|
15 |
|
16 |
def main():
|
17 |
st.title("Asystent Prawny")
|
18 |
+
|
19 |
initialize_session_state()
|
20 |
+
|
21 |
# Inicjalizacja bazy danych (jeśli potrzebna)
|
22 |
if 'db_initialized' not in st.session_state:
|
23 |
with st.spinner("Inicjalizacja bazy danych..."):
|
|
|
25 |
if not os.path.exists("chroma_db"):
|
26 |
processor.process_all_files("data/kodeksy")
|
27 |
st.session_state.db_initialized = True
|
28 |
+
logging.info("Baza danych została zainicjalizowana.")
|
29 |
+
|
30 |
# Przycisk do czyszczenia historii
|
31 |
if st.sidebar.button("Wyczyść historię"):
|
32 |
st.session_state.chatbot.clear_history()
|
|
|
42 |
if prompt := st.chat_input("Zadaj pytanie dotyczące prawa..."):
|
43 |
# Dodaj pytanie użytkownika do historii
|
44 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
45 |
+
|
46 |
with st.chat_message("user"):
|
47 |
st.markdown(prompt)
|
48 |
|
49 |
# Wyszukaj odpowiednie fragmenty w bazie
|
50 |
processor = KodeksProcessor()
|
51 |
relevant_chunks = processor.search(prompt)
|
52 |
+
|
53 |
# Wygeneruj odpowiedź
|
54 |
with st.chat_message("assistant"):
|
55 |
message_placeholder = st.empty()
|
56 |
full_response = ""
|
57 |
+
|
58 |
context = st.session_state.chatbot.generate_context(
|
59 |
[{"text": doc} for doc in relevant_chunks['documents'][0]]
|
60 |
)
|
61 |
+
|
62 |
for response_chunk in st.session_state.chatbot.get_response(prompt, context):
|
63 |
full_response += response_chunk
|
64 |
message_placeholder.markdown(full_response + "▌")
|
65 |
+
|
66 |
message_placeholder.markdown(full_response)
|
67 |
+
|
68 |
# Dodaj odpowiedź asystenta do historii
|
69 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
70 |
|