Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,8 +19,11 @@ import pandas as pd
|
|
19 |
import pydeck as pdk
|
20 |
from urllib.error import URLError
|
21 |
|
22 |
-
#
|
|
|
|
|
23 |
|
|
|
24 |
|
25 |
# Step 1: Clone the Dataset Repository
|
26 |
repo = Repository(
|
@@ -37,9 +40,48 @@ pdf_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
|
|
37 |
api_key = os.getenv("OPENAI_API_KEY")
|
38 |
# Retrieve the API key from st.secrets
|
39 |
|
|
|
|
|
40 |
# Updated caching mechanism using st.cache_data
|
41 |
@st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
|
45 |
|
@@ -65,9 +107,7 @@ def page1():
|
|
65 |
image = Image.open('BinDoc Logo (Quadratisch).png')
|
66 |
st.image(image, use_column_width='always')
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
# Start tracking user interactions
|
72 |
with streamlit_analytics.track():
|
73 |
if not os.path.exists(pdf_path):
|
@@ -156,44 +196,8 @@ def page1():
|
|
156 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
157 |
# Optionally log the exception details to a file or error tracking service
|
158 |
|
159 |
-
def load_vector_store(file_path, store_name, force_reload=False):
|
160 |
-
# Check if we need to force reload the vector store (e.g., when the PDF changes)
|
161 |
-
if force_reload or not os.path.exists(f"{store_name}.pkl"):
|
162 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
163 |
-
chunk_size=1000,
|
164 |
-
chunk_overlap=200,
|
165 |
-
length_function=len
|
166 |
-
)
|
167 |
-
|
168 |
-
text = load_pdf_text(file_path)
|
169 |
-
chunks = text_splitter.split_text(text=text)
|
170 |
-
|
171 |
-
embeddings = OpenAIEmbeddings()
|
172 |
-
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
173 |
-
with open(f"{store_name}.pkl", "wb") as f:
|
174 |
-
pickle.dump(VectorStore, f)
|
175 |
-
else:
|
176 |
-
with open(f"{store_name}.pkl", "rb") as f:
|
177 |
-
VectorStore = pickle.load(f)
|
178 |
-
|
179 |
-
return VectorStore
|
180 |
-
|
181 |
-
# Utility function to load text from a PDF
|
182 |
-
def load_pdf_text(file_path):
|
183 |
-
pdf_reader = PdfReader(file_path)
|
184 |
-
text = ""
|
185 |
-
for page in pdf_reader.pages:
|
186 |
-
text += page.extract_text() or "" # Add fallback for pages where text extraction fails
|
187 |
-
return text
|
188 |
-
|
189 |
-
def load_chatbot():
|
190 |
-
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
191 |
|
192 |
|
193 |
-
def display_chat_history(chat_history):
|
194 |
-
for chat in chat_history:
|
195 |
-
background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
|
196 |
-
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)
|
197 |
|
198 |
def page2():
|
199 |
st.title("New Page")
|
|
|
19 |
import pydeck as pdk
|
20 |
from urllib.error import URLError
|
21 |
|
22 |
+
# Initialize session state variables
|
23 |
+
if 'chat_history' not in st.session_state:
|
24 |
+
st.session_state['chat_history'] = []
|
25 |
|
26 |
+
st.set_page_config(layout="wide")
|
27 |
|
28 |
# Step 1: Clone the Dataset Repository
|
29 |
repo = Repository(
|
|
|
40 |
api_key = os.getenv("OPENAI_API_KEY")
|
41 |
# Retrieve the API key from st.secrets
|
42 |
|
43 |
+
|
44 |
+
|
45 |
# Updated caching mechanism using st.cache_data
|
46 |
@st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
|
47 |
+
def load_vector_store(file_path, store_name, force_reload=False):
|
48 |
+
# Check if we need to force reload the vector store (e.g., when the PDF changes)
|
49 |
+
if force_reload or not os.path.exists(f"{store_name}.pkl"):
|
50 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
51 |
+
chunk_size=1000,
|
52 |
+
chunk_overlap=200,
|
53 |
+
length_function=len
|
54 |
+
)
|
55 |
+
|
56 |
+
text = load_pdf_text(file_path)
|
57 |
+
chunks = text_splitter.split_text(text=text)
|
58 |
+
|
59 |
+
embeddings = OpenAIEmbeddings()
|
60 |
+
VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
|
61 |
+
with open(f"{store_name}.pkl", "wb") as f:
|
62 |
+
pickle.dump(VectorStore, f)
|
63 |
+
else:
|
64 |
+
with open(f"{store_name}.pkl", "rb") as f:
|
65 |
+
VectorStore = pickle.load(f)
|
66 |
+
|
67 |
+
return VectorStore
|
68 |
|
69 |
+
# Utility function to load text from a PDF
|
70 |
+
def load_pdf_text(file_path):
|
71 |
+
pdf_reader = PdfReader(file_path)
|
72 |
+
text = ""
|
73 |
+
for page in pdf_reader.pages:
|
74 |
+
text += page.extract_text() or "" # Add fallback for pages where text extraction fails
|
75 |
+
return text
|
76 |
+
|
77 |
+
def load_chatbot():
|
78 |
+
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
79 |
+
|
80 |
+
|
81 |
+
def display_chat_history(chat_history):
|
82 |
+
for chat in chat_history:
|
83 |
+
background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
|
84 |
+
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)
|
85 |
|
86 |
|
87 |
|
|
|
107 |
image = Image.open('BinDoc Logo (Quadratisch).png')
|
108 |
st.image(image, use_column_width='always')
|
109 |
|
110 |
+
|
|
|
|
|
111 |
# Start tracking user interactions
|
112 |
with streamlit_analytics.track():
|
113 |
if not os.path.exists(pdf_path):
|
|
|
196 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
197 |
# Optionally log the exception details to a file or error tracking service
|
198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
|
|
|
|
|
|
|
|
|
201 |
|
202 |
def page2():
|
203 |
st.title("New Page")
|