awacke1 commited on
Commit
e3c75a1
·
verified ·
1 Parent(s): 0394322

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -18
app.py CHANGED
@@ -31,6 +31,7 @@ from xml.etree import ElementTree as ET
31
 
32
  import openai
33
  from openai import OpenAI
 
34
 
35
 
36
 
@@ -63,6 +64,16 @@ if st.button("Clear Session"):
63
  st.session_state.messages = []
64
 
65
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  def download_and_save_abstract(url, title):
68
  response = requests.get(url)
@@ -118,10 +129,18 @@ def parse_to_markdown(text):
118
  return text
119
 
120
  def load_file(file_name):
121
- with open(file_name, "r", encoding='utf-8') as file:
122
- #with open(file_name, "r") as file:
123
- content = file.read()
124
- return content
 
 
 
 
 
 
 
 
125
 
126
  def extract_urls_old(text):
127
  try:
@@ -543,10 +562,9 @@ def compare_and_delete_files(files):
543
  def get_file_size(file_path):
544
  return os.path.getsize(file_path)
545
 
546
-
547
  def FileSidebar():
548
  # File Sidebar for files 🌐View, 📂Open, ▶️Run, and 🗑Delete per file
549
- all_files = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*.pdf")
550
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
551
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by filename length which puts similar prompts together
552
 
@@ -561,9 +579,9 @@ def FileSidebar():
561
  if st.button("⬇️ Download"):
562
  zip_file = create_zip_of_files(all_files)
563
  st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
564
- file_contents=''
565
- file_name=''
566
- next_action=''
567
 
568
  # Add files 🌐View, 📂Open, ▶️Run, and 🗑Delete per file
569
  for file in all_files:
@@ -573,17 +591,27 @@ def FileSidebar():
573
  col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
574
  with col1:
575
  if st.button("🌐", key=f"view_{timestamp}_{file}"): # view emoji button
576
- file_contents = load_file(file)
577
- file_name=file
578
- next_action='md'
 
 
 
 
 
 
 
 
 
 
579
  st.session_state['next_action'] = next_action
580
  with col2:
581
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
582
  with col3:
583
  if st.button("📂", key=f"open_{timestamp}_{file}"): # open emoji button
584
  file_contents = load_file(file)
585
- file_name=file
586
- next_action='open'
587
  st.session_state['lastfilename'] = file
588
  st.session_state['filename'] = file
589
  st.session_state['filetext'] = file_contents
@@ -591,15 +619,15 @@ def FileSidebar():
591
  with col4:
592
  if st.button("▶️", key=f"read_{timestamp}_{file}"): # search emoji button
593
  file_contents = load_file(file)
594
- file_name=file
595
- next_action='search'
596
  st.session_state['next_action'] = next_action
597
  with col5:
598
  if st.button("🗑", key=f"delete_{timestamp}_{file}"):
599
  os.remove(file)
600
- file_name=file
601
  st.rerun()
602
- next_action='delete'
603
  st.session_state['next_action'] = next_action
604
 
605
 
 
31
 
32
  import openai
33
  from openai import OpenAI
34
+ import fitz # pymupdf
35
 
36
 
37
 
 
64
  st.session_state.messages = []
65
 
66
 
67
+ def convert_pdf_to_markdown(pdf_path):
68
+ try:
69
+ doc = fitz.open(pdf_path)
70
+ markdown_text = ""
71
+ for page in doc:
72
+ markdown_text += page.get_text("markdown")
73
+ doc.close()
74
+ return markdown_text
75
+ except Exception as e:
76
+ return f"Error converting PDF to markdown: {str(e)}"
77
 
78
  def download_and_save_abstract(url, title):
79
  response = requests.get(url)
 
129
  return text
130
 
131
  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()
142
+ except Exception as e:
143
+ return f"Error loading file {file_name}: {str(e)}"
144
 
145
  def extract_urls_old(text):
146
  try:
 
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
 
 
579
  if st.button("⬇️ Download"):
580
  zip_file = create_zip_of_files(all_files)
581
  st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
582
+ file_contents = ''
583
+ file_name = ''
584
+ next_action = ''
585
 
586
  # Add files 🌐View, 📂Open, ▶️Run, and 🗑Delete per file
587
  for file in all_files:
 
591
  col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
592
  with col1:
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)
605
+ file_name = file
606
+ next_action = 'md'
607
  st.session_state['next_action'] = next_action
608
  with col2:
609
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
610
  with col3:
611
  if st.button("📂", key=f"open_{timestamp}_{file}"): # open emoji button
612
  file_contents = load_file(file)
613
+ file_name = file
614
+ next_action = 'open'
615
  st.session_state['lastfilename'] = file
616
  st.session_state['filename'] = file
617
  st.session_state['filetext'] = file_contents
 
619
  with col4:
620
  if st.button("▶️", key=f"read_{timestamp}_{file}"): # search emoji button
621
  file_contents = load_file(file)
622
+ file_name = file
623
+ next_action = 'search'
624
  st.session_state['next_action'] = next_action
625
  with col5:
626
  if st.button("🗑", key=f"delete_{timestamp}_{file}"):
627
  os.remove(file)
628
+ file_name = file
629
  st.rerun()
630
+ next_action = 'delete'
631
  st.session_state['next_action'] = next_action
632
 
633