awacke1 commited on
Commit
b79858a
1 Parent(s): 68d92ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +183 -0
app.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import anthropic
3
+ import os
4
+ import base64
5
+ import glob
6
+ import json
7
+ import pytz
8
+ from datetime import datetime
9
+ from streamlit.components.v1 import html
10
+ from PIL import Image
11
+ import re
12
+ from urllib.parse import quote
13
+
14
+ # Set up the Anthropic client
15
+ client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
16
+
17
+ # Initialize session state
18
+ if "chat_history" not in st.session_state:
19
+ st.session_state.chat_history = []
20
+
21
+ # Function to get file download link
22
+ def get_download_link(file_path):
23
+ with open(file_path, "rb") as file:
24
+ contents = file.read()
25
+ b64 = base64.b64encode(contents).decode()
26
+ file_name = os.path.basename(file_path)
27
+ return f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}</a>'
28
+
29
+ # Function to generate filename
30
+ def generate_filename(prompt, file_type):
31
+ central = pytz.timezone('US/Central')
32
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
33
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
34
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
35
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
36
+
37
+ # Function to create file
38
+ def create_file(filename, prompt, response, should_save=True):
39
+ if not should_save:
40
+ return
41
+ with open(filename, 'w', encoding='utf-8') as file:
42
+ file.write(prompt + "\n\n" + response)
43
+
44
+ # Function to load file content
45
+ def load_file(file_name):
46
+ with open(file_name, "r", encoding='utf-8') as file:
47
+ content = file.read()
48
+ return content
49
+
50
+ # Function to display glossary entity
51
+ def display_glossary_entity(k):
52
+ search_urls = {
53
+ "🚀🌌ArXiv": lambda k: f"/?q={quote(k)}",
54
+ "📖": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
55
+ "🔍": lambda k: f"https://www.google.com/search?q={quote(k)}",
56
+ "🎥": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
57
+ }
58
+ links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()])
59
+ st.markdown(f"**{k}** <small>{links_md}</small>", unsafe_allow_html=True)
60
+
61
+ # Function to create zip of files
62
+ def create_zip_of_files(files):
63
+ import zipfile
64
+ zip_name = "all_files.zip"
65
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
66
+ for file in files:
67
+ zipf.write(file)
68
+ return zip_name
69
+
70
+ # Function to get zip download link
71
+ def get_zip_download_link(zip_file):
72
+ with open(zip_file, 'rb') as f:
73
+ data = f.read()
74
+ b64 = base64.b64encode(data).decode()
75
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
76
+ return href
77
+
78
+ # Function to create HTML for autoplaying and looping video
79
+ def get_video_html(video_path):
80
+ video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
81
+ return f'''
82
+ <video width="100%" controls autoplay muted loop>
83
+ <source src="{video_url}" type="video/mp4">
84
+ Your browser does not support the video tag.
85
+ </video>
86
+ '''
87
+
88
+ # Streamlit app
89
+ def main():
90
+ st.title("Claude 3.5 Sonnet API Demo")
91
+
92
+ # Sidebar
93
+ st.sidebar.title("File Operations")
94
+
95
+ # File management
96
+ all_files = glob.glob("*.md")
97
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10]
98
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
99
+
100
+ if st.sidebar.button("🗑 Delete All"):
101
+ for file in all_files:
102
+ os.remove(file)
103
+ st.rerun()
104
+
105
+ if st.sidebar.button("⬇️ Download All"):
106
+ zip_file = create_zip_of_files(all_files)
107
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
108
+
109
+ for file in all_files:
110
+ col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
111
+ with col1:
112
+ if st.button("🌐", key="view_"+file):
113
+ st.session_state.current_file = file
114
+ st.session_state.file_content = load_file(file)
115
+ with col2:
116
+ st.markdown(get_download_link(file), unsafe_allow_html=True)
117
+ with col3:
118
+ if st.button("📂", key="edit_"+file):
119
+ st.session_state.current_file = file
120
+ st.session_state.file_content = load_file(file)
121
+ with col4:
122
+ if st.button("🗑", key="delete_"+file):
123
+ os.remove(file)
124
+ st.rerun()
125
+
126
+ # Main content area
127
+ user_input = st.text_area("Enter your message:", height=100)
128
+
129
+ if st.button("Send"):
130
+ if user_input:
131
+ response = client.messages.create(
132
+ model="claude-3-sonnet-20240229",
133
+ max_tokens=1000,
134
+ messages=[
135
+ {"role": "user", "content": user_input}
136
+ ]
137
+ )
138
+
139
+ st.write("Claude's response:")
140
+ st.write(response.content[0].text)
141
+
142
+ # Save response to file
143
+ filename = generate_filename(user_input, "md")
144
+ create_file(filename, user_input, response.content[0].text)
145
+
146
+ # Add to chat history
147
+ st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text})
148
+
149
+ # Display chat history
150
+ st.subheader("Chat History")
151
+ for chat in st.session_state.chat_history:
152
+ st.text_area("You:", chat["user"], height=100, disabled=True)
153
+ st.text_area("Claude:", chat["claude"], height=200, disabled=True)
154
+ st.markdown("---")
155
+
156
+ # File content viewer/editor
157
+ if hasattr(st.session_state, 'current_file'):
158
+ st.subheader(f"Editing: {st.session_state.current_file}")
159
+ new_content = st.text_area("File Content:", st.session_state.file_content, height=300)
160
+ if st.button("Save Changes"):
161
+ with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
162
+ file.write(new_content)
163
+ st.success("File updated successfully!")
164
+
165
+ # Image Gallery
166
+ st.subheader("Image Gallery")
167
+ image_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
168
+ cols = st.columns(3)
169
+ for idx, image_file in enumerate(image_files):
170
+ with cols[idx % 3]:
171
+ img = Image.open(image_file)
172
+ st.image(img, caption=image_file, use_column_width=True)
173
+ display_glossary_entity(os.path.splitext(image_file)[0])
174
+
175
+ # Video Gallery
176
+ st.subheader("Video Gallery")
177
+ video_files = glob.glob("*.mp4")
178
+ for video_file in video_files:
179
+ st.markdown(get_video_html(video_file), unsafe_allow_html=True)
180
+ display_glossary_entity(os.path.splitext(video_file)[0])
181
+
182
+ if __name__ == "__main__":
183
+ main()