awacke1 commited on
Commit
0d727b1
·
verified ·
1 Parent(s): 4aa7cbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -3
app.py CHANGED
@@ -19,7 +19,62 @@ client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
19
  if "chat_history" not in st.session_state:
20
  st.session_state.chat_history = []
21
 
22
- # ... (previous helper functions remain unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # Function to create HTML for autoplaying and looping video
25
  def get_video_html(video_path, width="100%"):
@@ -64,7 +119,36 @@ def main():
64
  # Sidebar
65
  st.sidebar.title("File Operations")
66
 
67
- # ... (file management code remains unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  # Main content area
70
  user_input = st.text_area("Enter your message:", height=100)
@@ -96,7 +180,21 @@ def main():
96
  # Add to chat history
97
  st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text})
98
 
99
- # ... (chat history and file content viewer/editor remain unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Image Gallery
102
  st.subheader("Image Gallery")
 
19
  if "chat_history" not in st.session_state:
20
  st.session_state.chat_history = []
21
 
22
+ # Function to get file download link
23
+ def get_download_link(file_path):
24
+ with open(file_path, "rb") as file:
25
+ contents = file.read()
26
+ b64 = base64.b64encode(contents).decode()
27
+ file_name = os.path.basename(file_path)
28
+ return f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}</a>'
29
+
30
+ # Function to generate filename
31
+ def generate_filename(prompt, file_type):
32
+ central = pytz.timezone('US/Central')
33
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
34
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
35
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
36
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
37
+
38
+ # Function to create file
39
+ def create_file(filename, prompt, response, should_save=True):
40
+ if not should_save:
41
+ return
42
+ with open(filename, 'w', encoding='utf-8') as file:
43
+ file.write(prompt + "\n\n" + response)
44
+
45
+ # Function to load file content
46
+ def load_file(file_name):
47
+ with open(file_name, "r", encoding='utf-8') as file:
48
+ content = file.read()
49
+ return content
50
+
51
+ # Function to display glossary entity
52
+ def display_glossary_entity(k):
53
+ search_urls = {
54
+ "🚀🌌ArXiv": lambda k: f"/?q={quote(k)}",
55
+ "📖": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
56
+ "🔍": lambda k: f"https://www.google.com/search?q={quote(k)}",
57
+ "🎥": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
58
+ }
59
+ links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()])
60
+ st.markdown(f"**{k}** <small>{links_md}</small>", unsafe_allow_html=True)
61
+
62
+ # Function to create zip of files
63
+ def create_zip_of_files(files):
64
+ import zipfile
65
+ zip_name = "all_files.zip"
66
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
67
+ for file in files:
68
+ zipf.write(file)
69
+ return zip_name
70
+
71
+ # Function to get zip download link
72
+ def get_zip_download_link(zip_file):
73
+ with open(zip_file, 'rb') as f:
74
+ data = f.read()
75
+ b64 = base64.b64encode(data).decode()
76
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
77
+ return href
78
 
79
  # Function to create HTML for autoplaying and looping video
80
  def get_video_html(video_path, width="100%"):
 
119
  # Sidebar
120
  st.sidebar.title("File Operations")
121
 
122
+ # File management
123
+ all_files = glob.glob("*.md")
124
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10]
125
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
126
+
127
+ if st.sidebar.button("🗑 Delete All"):
128
+ for file in all_files:
129
+ os.remove(file)
130
+ st.rerun()
131
+
132
+ if st.sidebar.button("⬇️ Download All"):
133
+ zip_file = create_zip_of_files(all_files)
134
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
135
+
136
+ for file in all_files:
137
+ col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
138
+ with col1:
139
+ if st.button("🌐", key="view_"+file):
140
+ st.session_state.current_file = file
141
+ st.session_state.file_content = load_file(file)
142
+ with col2:
143
+ st.markdown(get_download_link(file), unsafe_allow_html=True)
144
+ with col3:
145
+ if st.button("📂", key="edit_"+file):
146
+ st.session_state.current_file = file
147
+ st.session_state.file_content = load_file(file)
148
+ with col4:
149
+ if st.button("🗑", key="delete_"+file):
150
+ os.remove(file)
151
+ st.rerun()
152
 
153
  # Main content area
154
  user_input = st.text_area("Enter your message:", height=100)
 
180
  # Add to chat history
181
  st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text})
182
 
183
+ # Display chat history
184
+ st.subheader("Chat History")
185
+ for chat in st.session_state.chat_history:
186
+ st.text_area("You:", chat["user"], height=100, disabled=True)
187
+ st.text_area("Claude:", chat["claude"], height=200, disabled=True)
188
+ st.markdown("---")
189
+
190
+ # File content viewer/editor
191
+ if hasattr(st.session_state, 'current_file'):
192
+ st.subheader(f"Editing: {st.session_state.current_file}")
193
+ new_content = st.text_area("File Content:", st.session_state.file_content, height=300)
194
+ if st.button("Save Changes"):
195
+ with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
196
+ file.write(new_content)
197
+ st.success("File updated successfully!")
198
 
199
  # Image Gallery
200
  st.subheader("Image Gallery")