Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -563,183 +563,110 @@ def get_file_size(file_path):
|
|
| 563 |
|
| 564 |
|
| 565 |
def FileSidebar():
|
| 566 |
-
# File Sidebar for files 🌐View, 📂
|
| 567 |
all_files = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*_abstract.pdf")
|
| 568 |
-
all_files = [file for file in all_files if
|
| 569 |
-
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
|
| 570 |
|
| 571 |
# ⬇️ Download
|
| 572 |
Files1, Files2 = st.sidebar.columns(2)
|
| 573 |
with Files1:
|
| 574 |
if st.button("🗑 Delete All"):
|
| 575 |
for file in all_files:
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
| 577 |
st.rerun()
|
| 578 |
with Files2:
|
| 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, 📂
|
| 587 |
for file in all_files:
|
| 588 |
-
# Generate a unique timestamp for each iteration
|
| 589 |
timestamp = time.strftime("%H%M%S%f")[:-4] # HHMMSSNN format
|
| 590 |
|
| 591 |
-
col1, col2, col3, col4
|
| 592 |
with col1:
|
| 593 |
-
|
| 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)
|
| 602 |
-
file_name = file
|
| 603 |
-
next_action = 'md'
|
| 604 |
-
st.session_state['next_action'] = next_action
|
| 605 |
with col2:
|
| 606 |
-
st.
|
|
|
|
| 607 |
with col3:
|
| 608 |
-
if st.button("📂", key=f"
|
| 609 |
-
|
| 610 |
-
file_name = file
|
| 611 |
-
next_action = 'open'
|
| 612 |
-
st.session_state['lastfilename'] = file
|
| 613 |
-
st.session_state['filename'] = file
|
| 614 |
-
st.session_state['filetext'] = file_contents
|
| 615 |
-
st.session_state['next_action'] = next_action
|
| 616 |
with col4:
|
| 617 |
-
if st.button("▶️", key=f"read_{timestamp}_{file}"): # search emoji button
|
| 618 |
-
file_contents = load_file(file)
|
| 619 |
-
file_name = file
|
| 620 |
-
next_action = 'search'
|
| 621 |
-
st.session_state['next_action'] = next_action
|
| 622 |
-
with col5:
|
| 623 |
if st.button("🗑", key=f"delete_{timestamp}_{file}"):
|
| 624 |
-
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
| 626 |
st.rerun()
|
| 627 |
-
next_action = 'delete'
|
| 628 |
-
st.session_state['next_action'] = next_action
|
| 629 |
|
| 630 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
def display_pdf(file_path):
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
|
|
|
|
|
|
|
|
|
| 636 |
|
| 637 |
def display_html(file_path):
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
file_sizes = [get_file_size(file) for file in all_files]
|
| 645 |
-
previous_size = None
|
| 646 |
-
st.sidebar.title("File Operations")
|
| 647 |
-
for file, size in zip(all_files, file_sizes):
|
| 648 |
-
duplicate_flag = "🚩" if size == previous_size else ""
|
| 649 |
-
with st.sidebar.expander(f"File: {file} {duplicate_flag}"):
|
| 650 |
-
st.text(f"Size: {size} bytes")
|
| 651 |
-
|
| 652 |
-
if st.button("View", key=f"view_{file}"):
|
| 653 |
-
try:
|
| 654 |
-
with open(file, "r", encoding='utf-8') as f: # Ensure the file is read with UTF-8 encoding
|
| 655 |
-
file_content = f.read()
|
| 656 |
-
st.code(file_content, language="markdown")
|
| 657 |
-
except UnicodeDecodeError:
|
| 658 |
-
st.error("Failed to decode the file with UTF-8. It might contain non-UTF-8 encoded characters.")
|
| 659 |
-
|
| 660 |
-
if st.button("Delete", key=f"delete3_{file}"):
|
| 661 |
-
os.remove(file)
|
| 662 |
-
st.rerun()
|
| 663 |
-
previous_size = size # Update previous size for the next iteration
|
| 664 |
-
|
| 665 |
-
if len(file_contents) > 0:
|
| 666 |
-
if next_action=='open': # For "open", prep session state if it hasn't been yet
|
| 667 |
-
if 'lastfilename' not in st.session_state:
|
| 668 |
-
st.session_state['lastfilename'] = ''
|
| 669 |
-
if 'filename' not in st.session_state:
|
| 670 |
-
st.session_state['filename'] = ''
|
| 671 |
-
if 'filetext' not in st.session_state:
|
| 672 |
-
st.session_state['filetext'] = ''
|
| 673 |
-
open1, open2 = st.columns(spec=[.8,.2])
|
| 674 |
-
|
| 675 |
-
with open1:
|
| 676 |
-
# Use onchange functions to autoexecute file name and text save functions.
|
| 677 |
-
file_name_input = st.text_input(key='file_name_input', on_change=SaveFileNameClicked, label="File Name:",value=file_name )
|
| 678 |
-
file_content_area = st.text_area(key='file_content_area', on_change=SaveFileTextClicked, label="File Contents:", value=file_contents, height=300)
|
| 679 |
-
|
| 680 |
-
ShowButtons = False # Having buttons is redundant. They work but if on change event seals the deal so be it - faster save is less impedence - less context breaking
|
| 681 |
-
if ShowButtons:
|
| 682 |
-
bp1,bp2 = st.columns([.5,.5])
|
| 683 |
-
with bp1:
|
| 684 |
-
if st.button(label='💾 Save Name'):
|
| 685 |
-
SaveFileNameClicked()
|
| 686 |
-
with bp2:
|
| 687 |
-
if st.button(label='💾 Save File'):
|
| 688 |
-
SaveFileTextClicked()
|
| 689 |
-
|
| 690 |
-
new_file_content_area = st.session_state['file_content_area']
|
| 691 |
-
if new_file_content_area != file_contents:
|
| 692 |
-
st.markdown(new_file_content_area) #changed
|
| 693 |
-
|
| 694 |
-
if st.button("🔍 Run AI Meta Strategy", key="filecontentssearch"):
|
| 695 |
-
#search_glossary(file_content_area)
|
| 696 |
-
filesearch = PromptPrefix + file_content_area
|
| 697 |
-
st.markdown(filesearch)
|
| 698 |
-
|
| 699 |
-
if st.button(key=rerun, label='🔍AI Search' ):
|
| 700 |
-
search_glossary(filesearch)
|
| 701 |
-
|
| 702 |
-
if next_action=='md':
|
| 703 |
-
st.markdown(file_contents)
|
| 704 |
-
SpeechSynthesis(file_contents)
|
| 705 |
-
|
| 706 |
-
buttonlabel = '🔍Run'
|
| 707 |
-
if st.button(key='Runmd', label = buttonlabel):
|
| 708 |
-
MODEL = "gpt-4o-2024-05-13"
|
| 709 |
-
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 710 |
-
openai.organization = os.getenv('OPENAI_ORG_ID')
|
| 711 |
-
client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
|
| 712 |
-
st.session_state.messages.append({"role": "user", "content": transcript})
|
| 713 |
-
with st.chat_message("user"):
|
| 714 |
-
st.markdown(transcript)
|
| 715 |
-
with st.chat_message("assistant"):
|
| 716 |
-
completion = client.chat.completions.create(
|
| 717 |
-
model=MODEL,
|
| 718 |
-
messages = st.session_state.messages,
|
| 719 |
-
stream=True
|
| 720 |
-
)
|
| 721 |
-
response = process_text2(text_input=prompt)
|
| 722 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 723 |
-
#try:
|
| 724 |
-
#search_glossary(file_contents)
|
| 725 |
-
#except:
|
| 726 |
-
#st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
|
| 727 |
-
|
| 728 |
-
if next_action=='search':
|
| 729 |
-
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
| 730 |
-
user_prompt = file_contents
|
| 731 |
-
#try:
|
| 732 |
-
#search_glossary(file_contents)
|
| 733 |
-
filesearch = PromptPrefix2 + file_content_area
|
| 734 |
-
st.markdown(filesearch)
|
| 735 |
-
if st.button(key=rerun, label='🔍Re-Code' ):
|
| 736 |
-
#search_glossary(filesearch)
|
| 737 |
-
search_arxiv(filesearch)
|
| 738 |
-
|
| 739 |
-
#except:
|
| 740 |
-
#st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
|
| 741 |
-
# ----------------------------------------------------- File Sidebar for Jump Gates ------------------------------------------
|
| 742 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
# Randomly select a title
|
| 744 |
titles = [
|
| 745 |
"🧠🎭 Semantic Symphonies 🎹🎸 & Episodic Encores 🥁🎻",
|
|
|
|
| 563 |
|
| 564 |
|
| 565 |
def FileSidebar():
|
| 566 |
+
# File Sidebar for files 🌐View, 📂Edit, 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 os.path.basename(file) and len(os.path.splitext(file)[0]) >= 10]
|
| 569 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
|
| 570 |
|
| 571 |
# ⬇️ Download
|
| 572 |
Files1, Files2 = st.sidebar.columns(2)
|
| 573 |
with Files1:
|
| 574 |
if st.button("🗑 Delete All"):
|
| 575 |
for file in all_files:
|
| 576 |
+
try:
|
| 577 |
+
os.remove(file)
|
| 578 |
+
except Exception as e:
|
| 579 |
+
st.error(f"Error deleting {file}: {str(e)}")
|
| 580 |
st.rerun()
|
| 581 |
with Files2:
|
| 582 |
if st.button("⬇️ Download"):
|
| 583 |
zip_file = create_zip_of_files(all_files)
|
| 584 |
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
+
# Add files 🌐View, 📂Edit, and 🗑Delete per file
|
| 587 |
for file in all_files:
|
|
|
|
| 588 |
timestamp = time.strftime("%H%M%S%f")[:-4] # HHMMSSNN format
|
| 589 |
|
| 590 |
+
col1, col2, col3, col4 = st.sidebar.columns([3,1,1,1])
|
| 591 |
with col1:
|
| 592 |
+
st.markdown(f"**{file}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
with col2:
|
| 594 |
+
if st.button("🌐", key=f"view_{timestamp}_{file}"):
|
| 595 |
+
view_file(file)
|
| 596 |
with col3:
|
| 597 |
+
if st.button("📂", key=f"edit_{timestamp}_{file}"):
|
| 598 |
+
edit_file(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
with col4:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
if st.button("🗑", key=f"delete_{timestamp}_{file}"):
|
| 601 |
+
try:
|
| 602 |
+
os.remove(file)
|
| 603 |
+
st.success(f"Deleted {file}")
|
| 604 |
+
except Exception as e:
|
| 605 |
+
st.error(f"Error deleting {file}: {str(e)}")
|
| 606 |
st.rerun()
|
|
|
|
|
|
|
| 607 |
|
| 608 |
+
def view_file(file):
|
| 609 |
+
file_extension = os.path.splitext(file)[1].lower()
|
| 610 |
+
try:
|
| 611 |
+
if file_extension == '.pdf':
|
| 612 |
+
display_pdf(file)
|
| 613 |
+
elif file_extension == '.html':
|
| 614 |
+
display_html(file)
|
| 615 |
+
else: # For .md and other text files
|
| 616 |
+
with open(file, 'r', encoding='utf-8') as f:
|
| 617 |
+
content = f.read()
|
| 618 |
+
st.markdown(content)
|
| 619 |
+
except Exception as e:
|
| 620 |
+
st.error(f"Error viewing {file}: {str(e)}")
|
| 621 |
+
|
| 622 |
+
def edit_file(file):
|
| 623 |
+
file_extension = os.path.splitext(file)[1].lower()
|
| 624 |
+
try:
|
| 625 |
+
if file_extension == '.pdf':
|
| 626 |
+
st.warning("PDF files cannot be edited directly. Please download and edit locally.")
|
| 627 |
+
elif file_extension == '.html':
|
| 628 |
+
with open(file, 'r', encoding='utf-8') as f:
|
| 629 |
+
content = f.read()
|
| 630 |
+
new_content = st.text_area("Edit HTML content", content, height=400)
|
| 631 |
+
if st.button("Save HTML"):
|
| 632 |
+
with open(file, 'w', encoding='utf-8') as f:
|
| 633 |
+
f.write(new_content)
|
| 634 |
+
st.success("File updated successfully!")
|
| 635 |
+
else: # For .md and other text files
|
| 636 |
+
with open(file, 'r', encoding='utf-8') as f:
|
| 637 |
+
content = f.read()
|
| 638 |
+
new_content = st.text_area("Edit content", content, height=400)
|
| 639 |
+
if st.button("Save"):
|
| 640 |
+
with open(file, 'w', encoding='utf-8') as f:
|
| 641 |
+
f.write(new_content)
|
| 642 |
+
st.success("File updated successfully!")
|
| 643 |
+
except Exception as e:
|
| 644 |
+
st.error(f"Error editing {file}: {str(e)}")
|
| 645 |
+
|
| 646 |
def display_pdf(file_path):
|
| 647 |
+
try:
|
| 648 |
+
with open(file_path, "rb") as f:
|
| 649 |
+
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
|
| 650 |
+
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="800" type="application/pdf"></iframe>'
|
| 651 |
+
st.markdown(pdf_display, unsafe_allow_html=True)
|
| 652 |
+
except Exception as e:
|
| 653 |
+
st.error(f"Error displaying PDF {file_path}: {str(e)}")
|
| 654 |
|
| 655 |
def display_html(file_path):
|
| 656 |
+
try:
|
| 657 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 658 |
+
html_content = f.read()
|
| 659 |
+
st.components.v1.html(html_content, height=800, scrolling=True)
|
| 660 |
+
except Exception as e:
|
| 661 |
+
st.error(f"Error displaying HTML {file_path}: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
|
| 663 |
+
def get_file_size(file_path):
|
| 664 |
+
try:
|
| 665 |
+
return os.path.getsize(file_path)
|
| 666 |
+
except Exception as e:
|
| 667 |
+
print(f"Error getting file size for {file_path}: {str(e)}")
|
| 668 |
+
return 0
|
| 669 |
+
|
| 670 |
# Randomly select a title
|
| 671 |
titles = [
|
| 672 |
"🧠🎭 Semantic Symphonies 🎹🎸 & Episodic Encores 🥁🎻",
|