Spaces:
Running
Running
Update backup12.app.py
Browse files- backup12.app.py +164 -31
backup12.app.py
CHANGED
@@ -511,6 +511,168 @@ def preprocess_text(text):
|
|
511 |
return text
|
512 |
|
513 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
514 |
|
515 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
516 |
def main():
|
@@ -1091,7 +1253,7 @@ def main():
|
|
1091 |
Label = '#### π¬ Chat functionality - Every chat is a chance to learn'
|
1092 |
st.markdown(Label)
|
1093 |
if user_input:
|
1094 |
-
response =
|
1095 |
model="claude-3-sonnet-20240229",
|
1096 |
max_tokens=1000,
|
1097 |
messages=[
|
@@ -1132,36 +1294,7 @@ def main():
|
|
1132 |
st.success("File updated successfully! π")
|
1133 |
|
1134 |
# π File management - "Manage many, maintain order"
|
1135 |
-
|
1136 |
-
|
1137 |
-
all_files = glob.glob("*.md")
|
1138 |
-
all_files.sort(reverse=True)
|
1139 |
-
|
1140 |
-
if st.sidebar.button("π Delete All Files"):
|
1141 |
-
for file in all_files:
|
1142 |
-
os.remove(file)
|
1143 |
-
st.rerun()
|
1144 |
-
|
1145 |
-
if st.sidebar.button("β¬οΈ Download All Files"):
|
1146 |
-
zip_file = create_zip_of_files(all_files)
|
1147 |
-
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
1148 |
-
|
1149 |
-
for file in all_files:
|
1150 |
-
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
1151 |
-
with col1:
|
1152 |
-
if st.button("π", key="view_"+file):
|
1153 |
-
st.session_state.current_file = file
|
1154 |
-
st.session_state.file_content = load_file(file)
|
1155 |
-
with col2:
|
1156 |
-
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
1157 |
-
with col3:
|
1158 |
-
if st.button("π", key="edit_"+file):
|
1159 |
-
st.session_state.current_file = file
|
1160 |
-
st.session_state.file_content = load_file(file)
|
1161 |
-
with col4:
|
1162 |
-
if st.button("π", key="delete_"+file):
|
1163 |
-
os.remove(file)
|
1164 |
-
st.rerun()
|
1165 |
|
1166 |
except exceptions.CosmosHttpResponseError as e:
|
1167 |
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} π¨")
|
|
|
511 |
return text
|
512 |
|
513 |
|
514 |
+
|
515 |
+
def load_file_content(file_path):
|
516 |
+
"""Load and return file content with error handling"""
|
517 |
+
try:
|
518 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
519 |
+
return file.read()
|
520 |
+
except Exception as e:
|
521 |
+
st.error(f"Error loading file: {str(e)}")
|
522 |
+
return None
|
523 |
+
|
524 |
+
def save_file_content(file_path, content):
|
525 |
+
"""Save file content with error handling"""
|
526 |
+
try:
|
527 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
528 |
+
file.write(content)
|
529 |
+
return True
|
530 |
+
except Exception as e:
|
531 |
+
st.error(f"Error saving file: {str(e)}")
|
532 |
+
return False
|
533 |
+
|
534 |
+
def display_file_viewer(file_path):
|
535 |
+
"""Display file content in markdown viewer"""
|
536 |
+
content = load_file_content(file_path)
|
537 |
+
if content:
|
538 |
+
st.markdown("### π File Viewer")
|
539 |
+
st.markdown(f"**Viewing:** {file_path}")
|
540 |
+
|
541 |
+
# Add file metadata
|
542 |
+
file_stats = os.stat(file_path)
|
543 |
+
st.markdown(f"**Last modified:** {datetime.fromtimestamp(file_stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')}")
|
544 |
+
st.markdown(f"**Size:** {file_stats.st_size} bytes")
|
545 |
+
|
546 |
+
# Display content in markdown
|
547 |
+
st.markdown("---")
|
548 |
+
st.markdown(content)
|
549 |
+
|
550 |
+
# Add download button
|
551 |
+
st.download_button(
|
552 |
+
label="β¬οΈ Download File",
|
553 |
+
data=content,
|
554 |
+
file_name=os.path.basename(file_path),
|
555 |
+
mime="text/markdown"
|
556 |
+
)
|
557 |
+
|
558 |
+
def display_file_editor(file_path):
|
559 |
+
"""Display file content in editor with save functionality"""
|
560 |
+
# Initialize file content in session state if not already present
|
561 |
+
if 'file_content' not in st.session_state:
|
562 |
+
st.session_state.file_content = {}
|
563 |
+
|
564 |
+
# Load content if not in session state or if it's a different file
|
565 |
+
if file_path not in st.session_state.file_content:
|
566 |
+
content = load_file_content(file_path)
|
567 |
+
if content is not None:
|
568 |
+
st.session_state.file_content[file_path] = content
|
569 |
+
else:
|
570 |
+
return
|
571 |
+
|
572 |
+
st.markdown("### βοΈ File Editor")
|
573 |
+
st.markdown(f"**Editing:** {file_path}")
|
574 |
+
|
575 |
+
# Create a unique key for the text area
|
576 |
+
editor_key = f"editor_{hash(file_path)}"
|
577 |
+
|
578 |
+
# Editor with syntax highlighting for markdown
|
579 |
+
new_content = st.text_area(
|
580 |
+
"Edit content below:",
|
581 |
+
value=st.session_state.file_content[file_path],
|
582 |
+
height=400,
|
583 |
+
key=editor_key
|
584 |
+
)
|
585 |
+
|
586 |
+
col1, col2 = st.columns([1, 5])
|
587 |
+
with col1:
|
588 |
+
if st.button("πΎ Save Changes"):
|
589 |
+
if save_file_content(file_path, new_content):
|
590 |
+
st.session_state.file_content[file_path] = new_content
|
591 |
+
st.success("File saved successfully! π")
|
592 |
+
time.sleep(1)
|
593 |
+
st.rerun()
|
594 |
+
|
595 |
+
with col2:
|
596 |
+
st.download_button(
|
597 |
+
label="β¬οΈ Download File",
|
598 |
+
data=new_content,
|
599 |
+
file_name=os.path.basename(file_path),
|
600 |
+
mime="text/markdown"
|
601 |
+
)
|
602 |
+
|
603 |
+
def update_file_management_section():
|
604 |
+
# Initialize session state variables
|
605 |
+
if 'file_view_mode' not in st.session_state:
|
606 |
+
st.session_state.file_view_mode = None
|
607 |
+
if 'current_file' not in st.session_state:
|
608 |
+
st.session_state.current_file = None
|
609 |
+
if 'file_content' not in st.session_state:
|
610 |
+
st.session_state.file_content = {}
|
611 |
+
|
612 |
+
all_files = glob.glob("*.md")
|
613 |
+
all_files.sort(reverse=True)
|
614 |
+
|
615 |
+
# File management buttons in sidebar
|
616 |
+
st.sidebar.title("π File Management")
|
617 |
+
|
618 |
+
if st.sidebar.button("π Delete All Files"):
|
619 |
+
for file in all_files:
|
620 |
+
os.remove(file)
|
621 |
+
st.session_state.file_content = {} # Clear the file content cache
|
622 |
+
st.session_state.current_file = None
|
623 |
+
st.session_state.file_view_mode = None
|
624 |
+
st.rerun()
|
625 |
+
|
626 |
+
if st.sidebar.button("β¬οΈ Download All Files"):
|
627 |
+
zip_file = create_zip_of_files(all_files)
|
628 |
+
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
629 |
+
|
630 |
+
# Display files in sidebar with action buttons
|
631 |
+
for file in all_files:
|
632 |
+
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
633 |
+
|
634 |
+
with col1:
|
635 |
+
if st.button("π", key=f"view_{file}"):
|
636 |
+
st.session_state.current_file = file
|
637 |
+
st.session_state.file_view_mode = 'view'
|
638 |
+
if file not in st.session_state.file_content:
|
639 |
+
content = load_file_content(file)
|
640 |
+
if content is not None:
|
641 |
+
st.session_state.file_content[file] = content
|
642 |
+
st.rerun()
|
643 |
+
|
644 |
+
with col2:
|
645 |
+
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
646 |
+
|
647 |
+
with col3:
|
648 |
+
if st.button("π", key=f"edit_{file}"):
|
649 |
+
st.session_state.current_file = file
|
650 |
+
st.session_state.file_view_mode = 'edit'
|
651 |
+
if file not in st.session_state.file_content:
|
652 |
+
content = load_file_content(file)
|
653 |
+
if content is not None:
|
654 |
+
st.session_state.file_content[file] = content
|
655 |
+
st.rerun()
|
656 |
+
|
657 |
+
with col4:
|
658 |
+
if st.button("π", key=f"delete_{file}"):
|
659 |
+
os.remove(file)
|
660 |
+
if file in st.session_state.file_content:
|
661 |
+
del st.session_state.file_content[file]
|
662 |
+
if st.session_state.current_file == file:
|
663 |
+
st.session_state.current_file = None
|
664 |
+
st.session_state.file_view_mode = None
|
665 |
+
st.rerun()
|
666 |
+
|
667 |
+
# Display viewer or editor in main area based on mode
|
668 |
+
if st.session_state.current_file:
|
669 |
+
if st.session_state.file_view_mode == 'view':
|
670 |
+
display_file_viewer(st.session_state.current_file)
|
671 |
+
elif st.session_state.file_view_mode == 'edit':
|
672 |
+
display_file_editor(st.session_state.current_file)
|
673 |
+
|
674 |
+
|
675 |
+
|
676 |
|
677 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
678 |
def main():
|
|
|
1253 |
Label = '#### π¬ Chat functionality - Every chat is a chance to learn'
|
1254 |
st.markdown(Label)
|
1255 |
if user_input:
|
1256 |
+
response = anthropicclient.messages.create(
|
1257 |
model="claude-3-sonnet-20240229",
|
1258 |
max_tokens=1000,
|
1259 |
messages=[
|
|
|
1294 |
st.success("File updated successfully! π")
|
1295 |
|
1296 |
# π File management - "Manage many, maintain order"
|
1297 |
+
update_file_management_section()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1298 |
|
1299 |
except exceptions.CosmosHttpResponseError as e:
|
1300 |
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} π¨")
|