NavyDevilDoc commited on
Commit
a14f7cc
Β·
verified Β·
1 Parent(s): 7558bd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -88,9 +88,7 @@ with st.sidebar:
88
  # --- DUPLICATION CHECK ---
89
  if f.name in existing_files:
90
  st.toast(f"♻️ Updating existing file: {f.name}")
91
- # Remove the old version first (Cleans SQL and FAISS)
92
  st.session_state.db.delete_document(f.name)
93
- # -------------------------
94
 
95
  # A. Parse File
96
  text, filename, method = process_file(f)
@@ -102,14 +100,28 @@ with st.sidebar:
102
  # B. Chunk & ID
103
  chunks, doc_id = chunk_text(text, filename)
104
 
105
- # C. Save to SQLite
106
- st.session_state.db.add_document(doc_id, filename, text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  # D. Add to Vector Index
109
  st.session_state.search_engine.add_features(chunks)
110
 
111
- progress_bar.progress((i + 1) / len(uploaded_files))
112
-
113
  status.text("Syncing to Cloud...")
114
  SyncManager.push_data()
115
 
@@ -226,38 +238,38 @@ if query:
226
  with st.expander("πŸ” View Source Data used for this summary"):
227
  st.text(full_doc_text[:2000] + "...")
228
 
229
- # --- SEARCH RESULTS SECTION (Updated) ---
230
- # We wrap the results in an expander to keep the UI clean
231
- with st.expander("πŸ“š Reference Documents (Click to view)", expanded=False):
232
-
233
  if not results:
234
  st.info("No matching documents found.")
235
 
236
  for res in results:
237
  score = res['score']
238
- # Dynamic color coding based on relevance
239
  color = "#09ab3b" if score > 2 else "#ffbd45" if score > 0 else "#ff4b4b"
240
 
241
- # CSS FIX:
242
- # 1. We kept 'background-color: #f0f2f6' (Light Gray)
243
- # 2. We ADDED 'color: #1f1f1f' (Dark Gray) to force readability
244
- # regardless of whether the user is in Dark Mode or Light Mode.
245
 
246
  st.markdown(f"""
247
  <div style="
248
  border-left: 5px solid {color};
249
  padding: 15px;
250
  background-color: #f0f2f6;
251
- margin-bottom: 10px;
252
  border-radius: 5px;
253
  color: #1f1f1f;
254
  ">
255
- <div style="display: flex; justify-content: space-between; align-items: center;">
256
  <h4 style="margin:0; color: #0e1117;">πŸ“„ {res['source']}</h4>
257
- <span style="font-size: 0.8em; color: #555; background: #ddd; padding: 2px 6px; border-radius: 4px;">Score: {score:.2f}</span>
258
  </div>
259
- <p style="margin: 8px 0; font-style: italic; font-size: 0.95em; color: #333; line-height: 1.5;">
260
- "...{res['snippet']}..."
 
 
 
 
 
261
  </p>
262
  </div>
263
  """, unsafe_allow_html=True)
 
88
  # --- DUPLICATION CHECK ---
89
  if f.name in existing_files:
90
  st.toast(f"♻️ Updating existing file: {f.name}")
 
91
  st.session_state.db.delete_document(f.name)
 
92
 
93
  # A. Parse File
94
  text, filename, method = process_file(f)
 
100
  # B. Chunk & ID
101
  chunks, doc_id = chunk_text(text, filename)
102
 
103
+ # --- NEW STEP: Generate Abstract ---
104
+ # We skip this for tiny files to save time
105
+ abstract = "No summary generated."
106
+ if len(text) > 500:
107
+ with st.spinner(f"Writing abstract for {filename}..."):
108
+ # We utilize our flexible LLM client
109
+ # Note: We send only the first 30k chars to keep it fast
110
+ abstract = ask_llm(
111
+ query="Generate Abstract",
112
+ context=text[:30000],
113
+ mode="Abstract Generator",
114
+ model_provider="Gemini"
115
+ )
116
+ # -----------------------------------
117
+
118
+ # C. Save to SQLite (Now includes Abstract)
119
+ st.session_state.db.add_document(doc_id, filename, text, abstract=abstract)
120
 
121
  # D. Add to Vector Index
122
  st.session_state.search_engine.add_features(chunks)
123
 
124
+ progress_bar.progress((i + 1) / len(uploaded_files))
 
125
  status.text("Syncing to Cloud...")
126
  SyncManager.push_data()
127
 
 
238
  with st.expander("πŸ” View Source Data used for this summary"):
239
  st.text(full_doc_text[:2000] + "...")
240
 
241
+ # --- SEARCH RESULTS SECTION (Rich View) ---
242
+ with st.expander("πŸ“š Reference Documents (Click to view)", expanded=True):
 
 
243
  if not results:
244
  st.info("No matching documents found.")
245
 
246
  for res in results:
247
  score = res['score']
 
248
  color = "#09ab3b" if score > 2 else "#ffbd45" if score > 0 else "#ff4b4b"
249
 
250
+ # RETRIEVE THE ABSTRACT FROM DB
251
+ doc_abstract = st.session_state.db.get_doc_abstract(res['doc_id'])
 
 
252
 
253
  st.markdown(f"""
254
  <div style="
255
  border-left: 5px solid {color};
256
  padding: 15px;
257
  background-color: #f0f2f6;
258
+ margin-bottom: 15px;
259
  border-radius: 5px;
260
  color: #1f1f1f;
261
  ">
262
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
263
  <h4 style="margin:0; color: #0e1117;">πŸ“„ {res['source']}</h4>
264
+ <span style="font-size: 0.8em; color: #555; background: #ddd; padding: 2px 8px; border-radius: 4px;">Relevance: {score:.2f}</span>
265
  </div>
266
+
267
+ <div style="background: #e3e6ea; padding: 10px; border-radius: 5px; margin-bottom: 10px;">
268
+ <p style="margin: 0; font-size: 0.9em; color: #333;"><strong>πŸ€– Abstract:</strong> {doc_abstract}</p>
269
+ </div>
270
+
271
+ <p style="margin: 0; font-style: italic; font-size: 0.85em; color: #555;">
272
+ "Matching Chunk: ...{res['snippet']}..."
273
  </p>
274
  </div>
275
  """, unsafe_allow_html=True)