Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
|
|
2 |
from database import KodeksProcessor
|
3 |
from chatbot import Chatbot
|
4 |
import os
|
5 |
-
import pandas as pd
|
6 |
|
7 |
def initialize_session_state():
|
8 |
if 'chatbot' not in st.session_state:
|
@@ -23,70 +22,46 @@ def main():
|
|
23 |
processor.process_all_files("data/kodeksy")
|
24 |
st.session_state.db_initialized = True
|
25 |
|
26 |
-
#
|
27 |
-
st.sidebar.
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
# Przycisk do czyszczenia historii
|
32 |
-
if st.sidebar.button("Wyczyść historię"):
|
33 |
-
st.session_state.chatbot.clear_history()
|
34 |
-
st.session_state.messages = []
|
35 |
-
st.experimental_rerun()
|
36 |
-
|
37 |
-
# Wyświetlenie historii czatu
|
38 |
-
for message in st.session_state.messages:
|
39 |
-
with st.chat_message(message["role"]):
|
40 |
-
st.markdown(message["content"])
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
st.
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
relevant_chunks = processor.search(prompt)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
full_response = ""
|
58 |
-
|
59 |
-
context = st.session_state.chatbot.generate_context(
|
60 |
-
[{"text": doc} for doc in relevant_chunks['documents'][0]]
|
61 |
-
)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
|
|
71 |
|
72 |
-
|
73 |
-
st.subheader("Dane w bazie")
|
74 |
-
processor = KodeksProcessor()
|
75 |
-
|
76 |
-
# Pobierz wszystkie dokumenty z bazy
|
77 |
-
all_docs = processor.collection.query(query_texts=[""], n_results=1000)
|
78 |
-
|
79 |
-
# Przygotuj dane do wyświetlenia
|
80 |
-
data = []
|
81 |
-
for doc in all_docs['documents'][0]:
|
82 |
-
data.append(doc)
|
83 |
|
84 |
-
#
|
85 |
-
|
86 |
-
df = pd.DataFrame(data)
|
87 |
-
st.dataframe(df)
|
88 |
-
else:
|
89 |
-
st.write("Brak danych w bazie.")
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
main()
|
|
|
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:
|
|
|
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()
|
28 |
+
st.session_state.messages = []
|
29 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Wyświetlenie historii czatu
|
32 |
+
for message in st.session_state.messages:
|
33 |
+
with st.chat_message(message["role"]):
|
34 |
+
st.markdown(message["content"])
|
35 |
|
36 |
+
# Input użytkownika
|
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 |
|
66 |
if __name__ == "__main__":
|
67 |
main()
|