Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,8 @@ if "history" not in st.session_state:
|
|
| 33 |
st.session_state.history = []
|
| 34 |
if "authenticated" not in st.session_state:
|
| 35 |
st.session_state.authenticated = False
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# File processing logic
|
| 38 |
def process_input(input_data):
|
|
@@ -44,7 +46,8 @@ def process_input(input_data):
|
|
| 44 |
status.text("Reading file...")
|
| 45 |
progress_bar.progress(0.20)
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
documents = ""
|
| 49 |
|
| 50 |
# Step 2: Extract text based on file type
|
|
@@ -78,7 +81,7 @@ def process_input(input_data):
|
|
| 78 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 79 |
texts = text_splitter.split_text(documents)
|
| 80 |
|
| 81 |
-
# Step 4: Create embeddings
|
| 82 |
status.text("Creating embeddings...")
|
| 83 |
progress_bar.progress(0.80)
|
| 84 |
|
|
@@ -87,18 +90,21 @@ def process_input(input_data):
|
|
| 87 |
model_kwargs={'device': 'cpu'}
|
| 88 |
)
|
| 89 |
|
| 90 |
-
# Step 5: Initialize FAISS vector store
|
| 91 |
-
status.text("Building vector store...")
|
| 92 |
progress_bar.progress(1.0)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# Add texts to vector store
|
| 104 |
uuids = [str(uuid.uuid4()) for _ in texts]
|
|
@@ -106,6 +112,7 @@ def process_input(input_data):
|
|
| 106 |
|
| 107 |
# Complete processing
|
| 108 |
status.text("Processing complete!")
|
|
|
|
| 109 |
|
| 110 |
return vector_store
|
| 111 |
|
|
@@ -143,7 +150,7 @@ def answer_question(vectorstore, query):
|
|
| 143 |
except requests.exceptions.HTTPError as e:
|
| 144 |
raise RuntimeError(f"Error querying LLM: {str(e)}. Please try again or check model endpoint.")
|
| 145 |
|
| 146 |
-
# Sidebar with BSNL logo and
|
| 147 |
with st.sidebar:
|
| 148 |
try:
|
| 149 |
st.image("bsnl_logo.png", width=200)
|
|
@@ -153,10 +160,10 @@ with st.sidebar:
|
|
| 153 |
st.header("RAG Control Panel")
|
| 154 |
api_key_input = st.text_input("Enter RAG Access Key", type="password")
|
| 155 |
|
| 156 |
-
# Blue
|
| 157 |
st.markdown("""
|
| 158 |
<style>
|
| 159 |
-
.auth-button button {
|
| 160 |
background-color: #007BFF !important;
|
| 161 |
color: white !important;
|
| 162 |
font-weight: bold;
|
|
@@ -166,13 +173,14 @@ with st.sidebar:
|
|
| 166 |
transition: all 0.3s ease;
|
| 167 |
width: 100%;
|
| 168 |
}
|
| 169 |
-
.auth-button button:hover {
|
| 170 |
background-color: #0056b3 !important;
|
| 171 |
transform: scale(1.05);
|
| 172 |
}
|
| 173 |
</style>
|
| 174 |
""", unsafe_allow_html=True)
|
| 175 |
|
|
|
|
| 176 |
with st.container():
|
| 177 |
st.markdown('<div class="auth-button">', unsafe_allow_html=True)
|
| 178 |
if st.button("Authenticate"):
|
|
@@ -184,21 +192,40 @@ with st.sidebar:
|
|
| 184 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 185 |
|
| 186 |
if st.session_state.authenticated:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
input_data = st.file_uploader("Upload a file (PDF, XLS/XLSX, DOC/DOCX, TXT)", type=["pdf", "xls", "xlsx", "doc", "docx", "txt"])
|
| 188 |
|
| 189 |
if st.button("Process File") and input_data is not None:
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
st.subheader("Chat History")
|
| 204 |
for i, (q, a) in enumerate(st.session_state.history):
|
|
@@ -246,7 +273,7 @@ def main():
|
|
| 246 |
""", unsafe_allow_html=True)
|
| 247 |
|
| 248 |
st.title("RAG Q&A App with Mistral AI")
|
| 249 |
-
st.markdown("Welcome to the BSNL RAG App! Upload a PDF, XLS/XLSX, DOC/DOCX, or TXT file and ask questions.", unsafe_allow_html=True)
|
| 250 |
|
| 251 |
if not st.session_state.authenticated:
|
| 252 |
st.warning("Please authenticate using the sidebar.")
|
|
|
|
| 33 |
st.session_state.history = []
|
| 34 |
if "authenticated" not in st.session_state:
|
| 35 |
st.session_state.authenticated = False
|
| 36 |
+
if "uploaded_files" not in st.session_state:
|
| 37 |
+
st.session_state.uploaded_files = []
|
| 38 |
|
| 39 |
# File processing logic
|
| 40 |
def process_input(input_data):
|
|
|
|
| 46 |
status.text("Reading file...")
|
| 47 |
progress_bar.progress(0.20)
|
| 48 |
|
| 49 |
+
file_name = input_data.name
|
| 50 |
+
file_extension = file_name.lower().split('.')[-1]
|
| 51 |
documents = ""
|
| 52 |
|
| 53 |
# Step 2: Extract text based on file type
|
|
|
|
| 81 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 82 |
texts = text_splitter.split_text(documents)
|
| 83 |
|
| 84 |
+
# Step 4: Create or update embeddings
|
| 85 |
status.text("Creating embeddings...")
|
| 86 |
progress_bar.progress(0.80)
|
| 87 |
|
|
|
|
| 90 |
model_kwargs={'device': 'cpu'}
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# Step 5: Initialize or append to FAISS vector store
|
| 94 |
+
status.text("Building or updating vector store...")
|
| 95 |
progress_bar.progress(1.0)
|
| 96 |
|
| 97 |
+
if st.session_state.vectorstore is None:
|
| 98 |
+
dimension = len(hf_embeddings.embed_query("test"))
|
| 99 |
+
index = faiss.IndexFlatL2(dimension)
|
| 100 |
+
vector_store = FAISS(
|
| 101 |
+
embedding_function=hf_embeddings,
|
| 102 |
+
index=index,
|
| 103 |
+
docstore=InMemoryDocstore({}),
|
| 104 |
+
index_to_docstore_id={}
|
| 105 |
+
)
|
| 106 |
+
else:
|
| 107 |
+
vector_store = st.session_state.vectorstore
|
| 108 |
|
| 109 |
# Add texts to vector store
|
| 110 |
uuids = [str(uuid.uuid4()) for _ in texts]
|
|
|
|
| 112 |
|
| 113 |
# Complete processing
|
| 114 |
status.text("Processing complete!")
|
| 115 |
+
st.session_state.uploaded_files.append(file_name)
|
| 116 |
|
| 117 |
return vector_store
|
| 118 |
|
|
|
|
| 150 |
except requests.exceptions.HTTPError as e:
|
| 151 |
raise RuntimeError(f"Error querying LLM: {str(e)}. Please try again or check model endpoint.")
|
| 152 |
|
| 153 |
+
# Sidebar with BSNL logo, authentication, and controls
|
| 154 |
with st.sidebar:
|
| 155 |
try:
|
| 156 |
st.image("bsnl_logo.png", width=200)
|
|
|
|
| 160 |
st.header("RAG Control Panel")
|
| 161 |
api_key_input = st.text_input("Enter RAG Access Key", type="password")
|
| 162 |
|
| 163 |
+
# Blue button styles
|
| 164 |
st.markdown("""
|
| 165 |
<style>
|
| 166 |
+
.auth-button button, .delete-button button {
|
| 167 |
background-color: #007BFF !important;
|
| 168 |
color: white !important;
|
| 169 |
font-weight: bold;
|
|
|
|
| 173 |
transition: all 0.3s ease;
|
| 174 |
width: 100%;
|
| 175 |
}
|
| 176 |
+
.auth-button button:hover, .delete-button button:hover {
|
| 177 |
background-color: #0056b3 !important;
|
| 178 |
transform: scale(1.05);
|
| 179 |
}
|
| 180 |
</style>
|
| 181 |
""", unsafe_allow_html=True)
|
| 182 |
|
| 183 |
+
# Authenticate button
|
| 184 |
with st.container():
|
| 185 |
st.markdown('<div class="auth-button">', unsafe_allow_html=True)
|
| 186 |
if st.button("Authenticate"):
|
|
|
|
| 192 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 193 |
|
| 194 |
if st.session_state.authenticated:
|
| 195 |
+
# Display uploaded files
|
| 196 |
+
if st.session_state.uploaded_files:
|
| 197 |
+
st.subheader("Uploaded Files")
|
| 198 |
+
for file_name in st.session_state.uploaded_files:
|
| 199 |
+
st.write(f"- {file_name}")
|
| 200 |
+
|
| 201 |
+
# File uploader
|
| 202 |
input_data = st.file_uploader("Upload a file (PDF, XLS/XLSX, DOC/DOCX, TXT)", type=["pdf", "xls", "xlsx", "doc", "docx", "txt"])
|
| 203 |
|
| 204 |
if st.button("Process File") and input_data is not None:
|
| 205 |
+
if input_data.name in st.session_state.uploaded_files:
|
| 206 |
+
st.warning(f"File '{input_data.name}' has already been processed. Please upload a different file or delete the vector store.")
|
| 207 |
+
else:
|
| 208 |
+
try:
|
| 209 |
+
vector_store = process_input(input_data)
|
| 210 |
+
st.session_state.vectorstore = vector_store
|
| 211 |
+
st.success("File processed successfully. You can now ask questions.")
|
| 212 |
+
except PermissionError as e:
|
| 213 |
+
st.error(f"File upload failed: Permission error - {str(e)}. Check file system access.")
|
| 214 |
+
except OSError as e:
|
| 215 |
+
st.error(f"File upload failed: OS error - {str(e)}. Check server configuration.")
|
| 216 |
+
except ValueError as e:
|
| 217 |
+
st.error(f"File upload failed: {str(e)} (Invalid file format).")
|
| 218 |
+
except Exception as e:
|
| 219 |
+
st.error(f"File upload failed: {str(e)} (Exception type: {type(e).__name__}). Please try again or check server logs.")
|
| 220 |
+
|
| 221 |
+
# Delete vector store button
|
| 222 |
+
if st.session_state.vectorstore is not None:
|
| 223 |
+
st.markdown('<div class="delete-button">', unsafe_allow_html=True)
|
| 224 |
+
if st.button("Delete Vector Store"):
|
| 225 |
+
st.session_state.vectorstore = None
|
| 226 |
+
st.session_state.uploaded_files = []
|
| 227 |
+
st.success("Vector store deleted successfully.")
|
| 228 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 229 |
|
| 230 |
st.subheader("Chat History")
|
| 231 |
for i, (q, a) in enumerate(st.session_state.history):
|
|
|
|
| 273 |
""", unsafe_allow_html=True)
|
| 274 |
|
| 275 |
st.title("RAG Q&A App with Mistral AI")
|
| 276 |
+
st.markdown("Welcome to the BSNL RAG App! Upload a PDF, XLS/XLSX, DOC/DOCX, or TXT file and ask questions. Files are stored in the vector store until explicitly deleted.", unsafe_allow_html=True)
|
| 277 |
|
| 278 |
if not st.session_state.authenticated:
|
| 279 |
st.warning("Please authenticate using the sidebar.")
|