awacke1 commited on
Commit
8193e89
Β·
verified Β·
1 Parent(s): f0e1c91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -21
app.py CHANGED
@@ -982,29 +982,112 @@ def display_file_history_in_sidebar():
982
  # β–Ά Sort newest first
983
  all_files = sorted(all_files, key=os.path.getmtime, reverse=True)
984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985
  for f in all_files:
986
  fname = os.path.basename(f)
987
- ext = os.path.splitext(fname)[1].lower().strip('.')
988
- emoji = FILE_EMOJIS.get(ext, 'πŸ“¦')
989
- time_str = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
990
-
991
- with st.sidebar.expander(f"{emoji} {fname}"):
992
- st.write(f"**Modified:** {time_str}")
993
- if ext == "md":
994
- with open(f, "r", encoding="utf-8") as file_in:
995
- snippet = file_in.read(200).replace("\n", " ")
996
- if len(snippet) == 200:
997
- snippet += "..."
998
- st.write(snippet)
999
- dl_link = create_download_link_with_cache(f, file_type="md")
1000
- st.markdown(dl_link, unsafe_allow_html=True)
1001
- elif ext in ["mp3","wav"]:
1002
- st.audio(f)
1003
- dl_link = create_download_link_with_cache(f, file_type=ext)
1004
- st.markdown(dl_link, unsafe_allow_html=True)
1005
- else:
1006
- dl_link = create_download_link_with_cache(f)
1007
- st.markdown(dl_link, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1008
 
1009
  # ─────────────────────────────────────────────────────────
1010
  # MAIN APP
 
982
  # β–Ά Sort newest first
983
  all_files = sorted(all_files, key=os.path.getmtime, reverse=True)
984
 
985
+ #for f in all_files:
986
+ # fname = os.path.basename(f)
987
+ # ext = os.path.splitext(fname)[1].lower().strip('.')
988
+ # emoji = FILE_EMOJIS.get(ext, 'πŸ“¦')
989
+ # time_str = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
990
+
991
+ #with st.sidebar.expander(f"{emoji} {fname}"):
992
+ # st.write(f"**Modified:** {time_str}")
993
+ # if ext == "md":
994
+ # with open(f, "r", encoding="utf-8") as file_in:
995
+ # snippet = file_in.read(200).replace("\n", " ")
996
+ # if len(snippet) == 200:
997
+ # snippet += "..."
998
+ # st.write(snippet)
999
+ # dl_link = create_download_link_with_cache(f, file_type="md")
1000
+ # st.markdown(dl_link, unsafe_allow_html=True)
1001
+ # elif ext in ["mp3","wav"]:
1002
+ # st.audio(f)
1003
+ # dl_link = create_download_link_with_cache(f, file_type=ext)
1004
+ # st.markdown(dl_link, unsafe_allow_html=True)
1005
+ # else:
1006
+ # dl_link = create_download_link_with_cache(f)
1007
+ # st.markdown(dl_link, unsafe_allow_html=True)
1008
+
1009
+
1010
+
1011
+ # Group files by their query prefix (timestamp_query)
1012
+ grouped_files = {}
1013
  for f in all_files:
1014
  fname = os.path.basename(f)
1015
+ prefix = '_'.join(fname.split('_')[:6]) # Get timestamp part
1016
+ if prefix not in grouped_files:
1017
+ grouped_files[prefix] = {'md': [], 'audio': [], 'loaded': False}
1018
+
1019
+ ext = os.path.splitext(fname)[1].lower()
1020
+ if ext == '.md':
1021
+ grouped_files[prefix]['md'].append(f)
1022
+ elif ext in ['.mp3', '.wav']:
1023
+ grouped_files[prefix]['audio'].append(f)
1024
+
1025
+ # Sort groups by timestamp (newest first)
1026
+ sorted_groups = sorted(grouped_files.items(), key=lambda x: x[0], reverse=True)
1027
+
1028
+ # πŸ—‘β¬‡οΈ Sidebar delete all and zip all download
1029
+ col1, col4 = st.sidebar.columns(2)
1030
+ with col1:
1031
+ if st.button("πŸ—‘ Delete All"):
1032
+ for f in all_files:
1033
+ os.remove(f)
1034
+ st.rerun()
1035
+ st.session_state.should_rerun = True
1036
+ with col4:
1037
+ if st.button("⬇️ Zip All"):
1038
+ zip_name = create_zip_of_files(md_files, mp3_files, wav_files,
1039
+ st.session_state.get('last_query', ''))
1040
+ if zip_name:
1041
+ st.sidebar.markdown(get_download_link(zip_name, "zip"),
1042
+ unsafe_allow_html=True)
1043
+
1044
+ # Display grouped files
1045
+ for prefix, files in sorted_groups:
1046
+ # Get a preview of content from first MD file
1047
+ preview = ""
1048
+ if files['md']:
1049
+ with open(files['md'][0], "r", encoding="utf-8") as f:
1050
+ preview = f.read(200).replace("\n", " ")
1051
+ if len(preview) > 200:
1052
+ preview += "..."
1053
+
1054
+ # Create unique key for this group
1055
+ group_key = f"group_{prefix}"
1056
+ if group_key not in st.session_state:
1057
+ st.session_state[group_key] = False
1058
+
1059
+ # Display group expander
1060
+ with st.sidebar.expander(f"πŸ“‘ Query Group: {prefix}"):
1061
+ st.write("**Preview:**")
1062
+ st.write(preview)
1063
+
1064
+ # Load full content button
1065
+ if st.button("πŸ“– View Full Content", key=f"btn_{prefix}"):
1066
+ st.session_state[group_key] = True
1067
+
1068
+ # Only show full content and audio if button was clicked
1069
+ if st.session_state[group_key]:
1070
+ # Display markdown files
1071
+ for md_file in files['md']:
1072
+ with open(md_file, "r", encoding="utf-8") as f:
1073
+ content = f.read()
1074
+ st.markdown("**Full Content:**")
1075
+ st.markdown(content)
1076
+ st.markdown(get_download_link(md_file, file_type="md"),
1077
+ unsafe_allow_html=True)
1078
+
1079
+ # Display audio files
1080
+ usePlaySidebar=False
1081
+ if usePlaySidebar:
1082
+ for audio_file in files['audio']:
1083
+ ext = os.path.splitext(audio_file)[1].replace('.', '')
1084
+ st.audio(audio_file)
1085
+ st.markdown(get_download_link(audio_file, file_type=ext),
1086
+ unsafe_allow_html=True)
1087
+
1088
+
1089
+
1090
+
1091
 
1092
  # ─────────────────────────────────────────────────────────
1093
  # MAIN APP