adowu commited on
Commit
e900f04
·
verified ·
1 Parent(s): b857964

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -56
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
- # 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()
 
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()