Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,9 +19,6 @@ import pandas as pd
|
|
19 |
import pydeck as pdk
|
20 |
from urllib.error import URLError
|
21 |
|
22 |
-
|
23 |
-
st.set_page_config(layout="initial_sidebar_state="collapsed"")
|
24 |
-
|
25 |
# Step 1: Clone the Dataset Repository
|
26 |
repo = Repository(
|
27 |
local_dir="Private_Book", # Local directory to clone the repository
|
@@ -34,67 +31,17 @@ repo.git_pull() # Pull the latest changes (if any)
|
|
34 |
# Step 2: Load the PDF File
|
35 |
pdf_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
|
40 |
-
|
41 |
-
add_vertical_space(1) # Adjust as per the desired spacing
|
42 |
-
|
43 |
-
st.markdown("""
|
44 |
-
Hello! I’m here to assist you with:<br><br>
|
45 |
-
📘 **Glossary Inquiries:**<br>
|
46 |
-
I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
|
47 |
-
🆘 **Help Page Navigation:**<br>
|
48 |
-
Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
|
49 |
-
📰 **Latest Whitepapers Insights:**<br>
|
50 |
-
Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
|
51 |
-
""", unsafe_allow_html=True)
|
52 |
-
|
53 |
-
add_vertical_space(1) # Adjust as per the desired spacing
|
54 |
-
|
55 |
-
st.write('Made with ❤️ by BinDoc GmbH')
|
56 |
-
|
57 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
58 |
-
# Retrieve the API key from st.secrets
|
59 |
|
60 |
# Updated caching mechanism using st.cache_data
|
61 |
@st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
|
62 |
|
63 |
|
64 |
-
def load_vector_store(file_path, store_name, force_reload=False):
|
65 |
-
# Check if we need to force reload the vector store (e.g., when the PDF changes)
|
66 |
-
if force_reload or not os.path.exists(f"{store_name}.pkl"):
|
67 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
68 |
-
chunk_size=1000,
|
69 |
-
chunk_overlap=200,
|
70 |
-
length_function=len
|
71 |
-
)
|
72 |
-
|
73 |
-
text = load_pdf_text(file_path)
|
74 |
-
chunks = text_splitter.split_text(text=text)
|
75 |
-
|
76 |
-
embeddings = OpenAIEmbeddings()
|
77 |
-
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
78 |
-
with open(f"{store_name}.pkl", "wb") as f:
|
79 |
-
pickle.dump(VectorStore, f)
|
80 |
-
else:
|
81 |
-
with open(f"{store_name}.pkl", "rb") as f:
|
82 |
-
VectorStore = pickle.load(f)
|
83 |
-
|
84 |
-
return VectorStore
|
85 |
|
86 |
-
|
87 |
-
def load_pdf_text(file_path):
|
88 |
-
pdf_reader = PdfReader(file_path)
|
89 |
-
text = ""
|
90 |
-
for page in pdf_reader.pages:
|
91 |
-
text += page.extract_text() or "" # Add fallback for pages where text extraction fails
|
92 |
-
return text
|
93 |
-
|
94 |
-
def load_chatbot():
|
95 |
-
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
96 |
|
97 |
-
def
|
98 |
try:
|
99 |
hide_streamlit_style = """
|
100 |
<style>
|
@@ -206,12 +153,69 @@ def main():
|
|
206 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
207 |
# Optionally log the exception details to a file or error tracking service
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
def display_chat_history(chat_history):
|
211 |
for chat in chat_history:
|
212 |
background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
|
213 |
st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
|
|
216 |
if __name__ == "__main__":
|
217 |
main()
|
|
|
19 |
import pydeck as pdk
|
20 |
from urllib.error import URLError
|
21 |
|
|
|
|
|
|
|
22 |
# Step 1: Clone the Dataset Repository
|
23 |
repo = Repository(
|
24 |
local_dir="Private_Book", # Local directory to clone the repository
|
|
|
31 |
# Step 2: Load the PDF File
|
32 |
pdf_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
|
33 |
|
34 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
35 |
+
# Retrieve the API key from st.secrets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Updated caching mechanism using st.cache_data
|
38 |
@st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
st.set_page_config(layout="initial_sidebar_state="collapsed"")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
def page1():
|
45 |
try:
|
46 |
hide_streamlit_style = """
|
47 |
<style>
|
|
|
153 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
154 |
# Optionally log the exception details to a file or error tracking service
|
155 |
|
156 |
+
def load_vector_store(file_path, store_name, force_reload=False):
|
157 |
+
# Check if we need to force reload the vector store (e.g., when the PDF changes)
|
158 |
+
if force_reload or not os.path.exists(f"{store_name}.pkl"):
|
159 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
160 |
+
chunk_size=1000,
|
161 |
+
chunk_overlap=200,
|
162 |
+
length_function=len
|
163 |
+
)
|
164 |
+
|
165 |
+
text = load_pdf_text(file_path)
|
166 |
+
chunks = text_splitter.split_text(text=text)
|
167 |
+
|
168 |
+
embeddings = OpenAIEmbeddings()
|
169 |
+
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
170 |
+
with open(f"{store_name}.pkl", "wb") as f:
|
171 |
+
pickle.dump(VectorStore, f)
|
172 |
+
else:
|
173 |
+
with open(f"{store_name}.pkl", "rb") as f:
|
174 |
+
VectorStore = pickle.load(f)
|
175 |
+
|
176 |
+
return VectorStore
|
177 |
+
|
178 |
+
# Utility function to load text from a PDF
|
179 |
+
def load_pdf_text(file_path):
|
180 |
+
pdf_reader = PdfReader(file_path)
|
181 |
+
text = ""
|
182 |
+
for page in pdf_reader.pages:
|
183 |
+
text += page.extract_text() or "" # Add fallback for pages where text extraction fails
|
184 |
+
return text
|
185 |
+
|
186 |
+
def load_chatbot():
|
187 |
+
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
188 |
+
|
189 |
|
190 |
def display_chat_history(chat_history):
|
191 |
for chat in chat_history:
|
192 |
background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
|
193 |
st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
194 |
|
195 |
+
def page2():
|
196 |
+
st.title("New Page")
|
197 |
+
st.write("This is the content of the new page.")
|
198 |
+
|
199 |
+
def main():
|
200 |
+
with st.sidebar:
|
201 |
+
st.title('BinDoc GmbH')
|
202 |
+
st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
|
203 |
+
|
204 |
+
add_vertical_space(1) # Adjust as per the desired spacing
|
205 |
+
|
206 |
+
st.sidebar.title('Navigation')
|
207 |
+
page = st.sidebar.selectbox("Choose a page", ["Page 1", "Page 2"])
|
208 |
+
|
209 |
+
if page == "Page 1":
|
210 |
+
page1()
|
211 |
+
elif page == "Page 2":
|
212 |
+
page2()
|
213 |
+
|
214 |
+
add_vertical_space(1) # Adjust as per the desired spacing
|
215 |
+
|
216 |
+
st.write('Made with ❤️ by BinDoc GmbH')
|
217 |
+
|
218 |
|
219 |
+
|
220 |
if __name__ == "__main__":
|
221 |
main()
|