cryogenic22 commited on
Commit
00c3c06
·
verified ·
1 Parent(s): 3d796cc

Update components/document_viewer.py

Browse files
Files changed (1) hide show
  1. components/document_viewer.py +38 -12
components/document_viewer.py CHANGED
@@ -1,31 +1,31 @@
1
- # components/document_viewer.py
2
  import streamlit as st
3
  from typing import Dict, List
4
  import pandas as pd
5
 
 
6
  class DocumentViewer:
7
  def __init__(self, case_manager):
8
  self.case_manager = case_manager
9
 
10
  def render(self, case_id: str):
11
- """Render document viewer"""
12
  case = self.case_manager.get_case(case_id)
13
  if not case:
14
  st.error("Case not found")
15
  return
16
 
17
  st.subheader("Documents")
18
-
19
  # Document list
20
  if case['documents']:
21
  # Create DataFrame for documents
22
  docs_df = pd.DataFrame(case['documents'])
23
  docs_df['added_at'] = pd.to_datetime(docs_df['added_at'])
24
  docs_df = docs_df.sort_values('added_at', ascending=False)
25
-
26
  # Display documents in tabs
27
  tabs = st.tabs([f"📄 {doc['title']}" for doc in docs_df.to_dict('records')])
28
-
29
  for tab, doc in zip(tabs, docs_df.to_dict('records')):
30
  with tab:
31
  self._render_document_tab(doc)
@@ -33,17 +33,33 @@ class DocumentViewer:
33
  st.info("No documents uploaded yet")
34
 
35
  def _render_document_tab(self, doc: Dict):
36
- """Render single document tab"""
37
  col1, col2 = st.columns([2, 1])
38
-
39
  with col1:
40
  st.markdown(f"**{doc['title']}**")
41
  st.markdown(f"Added: {doc['added_at']}")
42
-
43
- # Display document content in expander
44
  with st.expander("View Content"):
45
- st.markdown(doc['content'][:1000] + "..." if len(doc['content']) > 1000 else doc['content'])
46
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  with col2:
48
  st.markdown("### Actions")
49
  if st.button("📥 Download", key=f"download_{doc['id']}"):
@@ -51,5 +67,15 @@ class DocumentViewer:
51
  "Download Document",
52
  doc['content'],
53
  file_name=doc['title'],
54
- mime="text/plain"
55
  )
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from typing import Dict, List
3
  import pandas as pd
4
 
5
+
6
  class DocumentViewer:
7
  def __init__(self, case_manager):
8
  self.case_manager = case_manager
9
 
10
  def render(self, case_id: str):
11
+ """Render document viewer with additional features."""
12
  case = self.case_manager.get_case(case_id)
13
  if not case:
14
  st.error("Case not found")
15
  return
16
 
17
  st.subheader("Documents")
18
+
19
  # Document list
20
  if case['documents']:
21
  # Create DataFrame for documents
22
  docs_df = pd.DataFrame(case['documents'])
23
  docs_df['added_at'] = pd.to_datetime(docs_df['added_at'])
24
  docs_df = docs_df.sort_values('added_at', ascending=False)
25
+
26
  # Display documents in tabs
27
  tabs = st.tabs([f"📄 {doc['title']}" for doc in docs_df.to_dict('records')])
28
+
29
  for tab, doc in zip(tabs, docs_df.to_dict('records')):
30
  with tab:
31
  self._render_document_tab(doc)
 
33
  st.info("No documents uploaded yet")
34
 
35
  def _render_document_tab(self, doc: Dict):
36
+ """Render a single document tab with chunk-level references."""
37
  col1, col2 = st.columns([2, 1])
38
+
39
  with col1:
40
  st.markdown(f"**{doc['title']}**")
41
  st.markdown(f"Added: {doc['added_at']}")
42
+
43
+ # Display document content in an expander
44
  with st.expander("View Content"):
45
+ # Include chunk references if available
46
+ content_with_chunks = (
47
+ "\n\n".join(
48
+ [
49
+ f"Chunk {idx + 1}:\n{chunk}"
50
+ for idx, chunk in enumerate(doc.get('chunks', [doc['content']]))
51
+ ]
52
+ )
53
+ if 'chunks' in doc
54
+ else doc['content']
55
+ )
56
+ st.text_area(
57
+ label="Document Content",
58
+ value=content_with_chunks[:3000] + "..." if len(content_with_chunks) > 3000 else content_with_chunks,
59
+ height=300,
60
+ disabled=True,
61
+ )
62
+
63
  with col2:
64
  st.markdown("### Actions")
65
  if st.button("📥 Download", key=f"download_{doc['id']}"):
 
67
  "Download Document",
68
  doc['content'],
69
  file_name=doc['title'],
70
+ mime="text/plain",
71
  )
72
+
73
+ # Add any additional actions (e.g., reprocess or annotate)
74
+
75
+ def render_chunk_references(self, references: List[Dict]):
76
+ """Render a section listing chunks referenced in responses."""
77
+ st.subheader("Referenced Chunks")
78
+ for ref in references:
79
+ st.markdown(
80
+ f"- **{ref['title']}** (Chunk {ref['chunk_id']}, Page {ref.get('page', 'N/A')}): {ref['snippet']}"
81
+ )