adowu commited on
Commit
1a5536b
·
verified ·
1 Parent(s): e6eebe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -36
app.py CHANGED
@@ -1,11 +1,8 @@
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:
@@ -25,48 +22,71 @@ def main():
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()
33
- st.session_state.messages = []
34
- st.rerun()
35
 
36
- # Wyświetlenie historii czatu
37
- for message in st.session_state.messages:
38
- with st.chat_message(message["role"]):
39
- st.markdown(message["content"])
 
 
40
 
41
- # Input użytkownika
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
 
71
  if __name__ == "__main__":
72
  main()
 
1
  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:
 
22
  if not os.path.exists("chroma_db"):
23
  processor.process_all_files("data/kodeksy")
24
  st.session_state.db_initialized = True
 
25
 
26
+ # Sidebar do nawigacji
27
+ st.sidebar.title("Nawigacja")
28
+ page = st.sidebar.radio("Wybierz stronę:", ["Chatbot", "Podgląd danych"])
 
 
29
 
30
+ if page == "Chatbot":
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
+ # Input użytkownika
43
+ if prompt := st.chat_input("Zadaj pytanie dotyczące prawa..."):
44
+ # Dodaj pytanie użytkownika do historii
45
+ st.session_state.messages.append({"role": "user", "content": prompt})
46
 
47
+ with st.chat_message("user"):
48
+ st.markdown(prompt)
 
49
 
50
+ # Wyszukaj odpowiednie fragmenty w bazie
51
+ processor = KodeksProcessor()
52
+ relevant_chunks = processor.search(prompt)
 
53
 
54
+ # Wygeneruj odpowiedź
55
+ with st.chat_message("assistant"):
56
+ message_placeholder = st.empty()
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
+ for response_chunk in st.session_state.chatbot.get_response(prompt, context):
64
+ full_response += response_chunk
65
+ message_placeholder.markdown(full_response + "▌")
66
 
67
+ message_placeholder.markdown(full_response)
68
+
69
+ # Dodaj odpowiedź asystenta do historii
70
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
71
+
72
+ elif page == "Podgląd danych":
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
+ # Wyświetl dane w tabeli
85
+ if data:
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()