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