awacke1 commited on
Commit
a17be0f
·
verified ·
1 Parent(s): ca744db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -132,10 +132,9 @@ def load_file(file_name):
132
  file_extension = os.path.splitext(file_name)[1].lower()
133
  try:
134
  if file_extension == '.pdf':
135
- return convert_pdf_to_markdown(file_name)
136
  elif file_extension == '.html':
137
- with open(file_name, 'r', encoding='utf-8') as file:
138
- return file.read()
139
  else: # For .md and other text files
140
  with open(file_name, 'r', encoding='utf-8') as file:
141
  return file.read()
@@ -562,10 +561,11 @@ def compare_and_delete_files(files):
562
  def get_file_size(file_path):
563
  return os.path.getsize(file_path)
564
 
 
565
  def FileSidebar():
566
  # File Sidebar for files 🌐View, 📂Open, ▶️Run, and 🗑Delete per file
567
  all_files = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*_abstract.pdf")
568
- all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
569
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by filename length which puts similar prompts together
570
 
571
  # ⬇️ Download
@@ -593,12 +593,9 @@ def FileSidebar():
593
  if st.button("🌐", key=f"view_{timestamp}_{file}"): # view emoji button
594
  file_extension = os.path.splitext(file)[1].lower()
595
  if file_extension == '.pdf':
596
- markdown_content = convert_pdf_to_markdown(file)
597
- st.markdown(markdown_content)
598
  elif file_extension == '.html':
599
- with open(file, 'r', encoding='utf-8') as f:
600
- html_content = f.read()
601
- st.components.v1.html(html_content, height=600, scrolling=True)
602
  else:
603
  file_contents = load_file(file)
604
  st.markdown(file_contents)
@@ -631,6 +628,16 @@ def FileSidebar():
631
  st.session_state['next_action'] = next_action
632
 
633
 
 
 
 
 
 
 
 
 
 
 
634
 
635
 
636
  # 🚩File duplicate detector - useful to prune and view all. Pruning works well by file size detection of two similar and flags the duplicate.
 
132
  file_extension = os.path.splitext(file_name)[1].lower()
133
  try:
134
  if file_extension == '.pdf':
135
+ return "PDF file. Use 'View' button to display the content."
136
  elif file_extension == '.html':
137
+ return "HTML file. Use 'View' button to display the content."
 
138
  else: # For .md and other text files
139
  with open(file_name, 'r', encoding='utf-8') as file:
140
  return file.read()
 
561
  def get_file_size(file_path):
562
  return os.path.getsize(file_path)
563
 
564
+
565
  def FileSidebar():
566
  # File Sidebar for files 🌐View, 📂Open, ▶️Run, and 🗑Delete per file
567
  all_files = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*_abstract.pdf")
568
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10 and os.path.basename(file)] # exclude files with short names and empty names
569
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by filename length which puts similar prompts together
570
 
571
  # ⬇️ Download
 
593
  if st.button("🌐", key=f"view_{timestamp}_{file}"): # view emoji button
594
  file_extension = os.path.splitext(file)[1].lower()
595
  if file_extension == '.pdf':
596
+ display_pdf(file)
 
597
  elif file_extension == '.html':
598
+ display_html(file)
 
 
599
  else:
600
  file_contents = load_file(file)
601
  st.markdown(file_contents)
 
628
  st.session_state['next_action'] = next_action
629
 
630
 
631
+ def display_pdf(file_path):
632
+ with open(file_path, "rb") as f:
633
+ base64_pdf = base64.b64encode(f.read()).decode('utf-8')
634
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="800" height="800" type="application/pdf"></iframe>'
635
+ st.markdown(pdf_display, unsafe_allow_html=True)
636
+
637
+ def display_html(file_path):
638
+ with open(file_path, 'r', encoding='utf-8') as f:
639
+ html_content = f.read()
640
+ st.components.v1.html(html_content, height=800, scrolling=True)
641
 
642
 
643
  # 🚩File duplicate detector - useful to prune and view all. Pruning works well by file size detection of two similar and flags the duplicate.