Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# 1. App Configuration
|
15 |
+
Site_Name = 'π€π§ Antπ¬Claudeπ'
|
16 |
+
title="π€π§ Antπ¬Claudeπ"
|
17 |
+
helpURL='https://huggingface.co/awacke1'
|
18 |
+
bugURL='https://huggingface.co/spaces/awacke1'
|
19 |
+
icons='π€π§ π¬π'
|
20 |
+
|
21 |
+
st.set_page_config(
|
22 |
+
page_title=title,
|
23 |
+
page_icon=icons,
|
24 |
+
layout="wide",
|
25 |
+
initial_sidebar_state="auto",
|
26 |
+
menu_items={
|
27 |
+
'Get Help': helpURL,
|
28 |
+
'Report a bug': bugURL,
|
29 |
+
'About': title
|
30 |
+
}
|
31 |
+
)
|
32 |
+
|
33 |
+
# Set up the Anthropic client
|
34 |
+
client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
35 |
+
|
36 |
+
# Initialize session state
|
37 |
+
if "chat_history" not in st.session_state:
|
38 |
+
st.session_state.chat_history = []
|
39 |
+
|
40 |
+
# Helper Functions: All Your Essentials π
|
41 |
+
|
42 |
+
# Function to get a file download link (because you deserve easy downloads π)
|
43 |
+
def get_download_link(file_path):
|
44 |
+
with open(file_path, "rb") as file:
|
45 |
+
contents = file.read()
|
46 |
+
b64 = base64.b64encode(contents).decode()
|
47 |
+
file_name = os.path.basename(file_path)
|
48 |
+
return f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}π</a>'
|
49 |
+
|
50 |
+
# Function to generate a filename based on prompt and time (because names matter π)
|
51 |
+
def generate_filename(prompt, file_type):
|
52 |
+
central = pytz.timezone('US/Central')
|
53 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
54 |
+
safe_prompt = re.sub(r'\W+', '_', prompt)[:90]
|
55 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
56 |
+
|
57 |
+
# Function to create and save a file (and avoid the black hole of lost data π³)
|
58 |
+
def create_file(filename, prompt, response, should_save=True):
|
59 |
+
if not should_save:
|
60 |
+
return
|
61 |
+
with open(filename, 'w', encoding='utf-8') as file:
|
62 |
+
file.write(prompt + "\n\n" + response)
|
63 |
+
|
64 |
+
# Function to load file content (for revisiting the past π)
|
65 |
+
def load_file(file_name):
|
66 |
+
with open(file_name, "r", encoding='utf-8') as file:
|
67 |
+
content = file.read()
|
68 |
+
return content
|
69 |
+
|
70 |
+
# Function to display handy glossary entity links (search like a pro π)
|
71 |
+
def display_glossary_entity(k):
|
72 |
+
search_urls = {
|
73 |
+
"ππArXiv": lambda k: f"/?q={quote(k)}",
|
74 |
+
"π": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
|
75 |
+
"π": lambda k: f"https://www.google.com/search?q={quote(k)}",
|
76 |
+
"π₯": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
|
77 |
+
}
|
78 |
+
links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()])
|
79 |
+
st.markdown(f"**{k}** <small>{links_md}</small>", unsafe_allow_html=True)
|
80 |
+
|
81 |
+
# Function to create zip of files (because more is more π§³)
|
82 |
+
def create_zip_of_files(files):
|
83 |
+
import zipfile
|
84 |
+
zip_name = "all_files.zip"
|
85 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
86 |
+
for file in files:
|
87 |
+
zipf.write(file)
|
88 |
+
return zip_name
|
89 |
+
|
90 |
+
# Function to create HTML for autoplaying and looping video (for the full cinematic effect π₯)
|
91 |
+
def get_video_html(video_path, width="100%"):
|
92 |
+
video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
|
93 |
+
return f'''
|
94 |
+
<video width="{width}" controls autoplay muted loop>
|
95 |
+
<source src="{video_url}" type="video/mp4">
|
96 |
+
Your browser does not support the video tag.
|
97 |
+
</video>
|
98 |
+
'''
|
99 |
+
|
100 |
+
# Function to create HTML for audio player (when life needs a soundtrack πΆ)
|
101 |
+
def get_audio_html(audio_path, width="100%"):
|
102 |
+
audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
|
103 |
+
return f'''
|
104 |
+
<audio controls style="width: {width};">
|
105 |
+
<source src="{audio_url}" type="audio/mpeg">
|
106 |
+
Your browser does not support the audio element.
|
107 |
+
</audio>
|
108 |
+
'''
|
109 |
+
|
110 |
+
|
111 |
+
# Streamlit App Layout (like a house with better flow π‘)
|
112 |
+
def main():
|
113 |
+
|
114 |
+
# Sidebar with Useful Controls (All the VIP actions π)
|
115 |
+
st.sidebar.title("π€π§ Antπ¬Claudeππ")
|
116 |
+
|
117 |
+
all_files = glob.glob("*.md")
|
118 |
+
all_files.sort(reverse=True)
|
119 |
+
|
120 |
+
if st.sidebar.button("π Delete All"):
|
121 |
+
for file in all_files:
|
122 |
+
os.remove(file)
|
123 |
+
st.rerun()
|
124 |
+
|
125 |
+
if st.sidebar.button("β¬οΈ Download All"):
|
126 |
+
zip_file = create_zip_of_files(all_files)
|
127 |
+
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
128 |
+
|
129 |
+
for file in all_files:
|
130 |
+
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
131 |
+
with col1:
|
132 |
+
if st.button("π", key="view_"+file):
|
133 |
+
st.session_state.current_file = file
|
134 |
+
st.session_state.file_content = load_file(file)
|
135 |
+
with col2:
|
136 |
+
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
137 |
+
with col3:
|
138 |
+
if st.button("π", key="edit_"+file):
|
139 |
+
st.session_state.current_file = file
|
140 |
+
st.session_state.file_content = load_file(file)
|
141 |
+
with col4:
|
142 |
+
if st.button("π", key="delete_"+file):
|
143 |
+
os.remove(file)
|
144 |
+
st.rerun()
|
145 |
+
|
146 |
+
# Main Area: Chat with Claude (Heβs a good listener π¬)
|
147 |
+
user_input = st.text_area("Message π¨:", height=100)
|
148 |
+
|
149 |
+
if st.button("Send π¨"):
|
150 |
+
if user_input:
|
151 |
+
response = client.messages.create(
|
152 |
+
model="claude-3-sonnet-20240229",
|
153 |
+
max_tokens=1000,
|
154 |
+
messages=[
|
155 |
+
{"role": "user", "content": user_input}
|
156 |
+
]
|
157 |
+
)
|
158 |
+
st.write("Claude's reply π§ :")
|
159 |
+
st.write(response.content[0].text)
|
160 |
+
|
161 |
+
filename = generate_filename(user_input, "md")
|
162 |
+
create_file(filename, user_input, response.content[0].text)
|
163 |
+
|
164 |
+
st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text})
|
165 |
+
|
166 |
+
# Display Chat History (Never forget a good chat π)
|
167 |
+
st.subheader("Past Conversations π")
|
168 |
+
for chat in st.session_state.chat_history:
|
169 |
+
st.text_area("You said π¬:", chat["user"], height=100, disabled=True)
|
170 |
+
st.text_area("Claude replied π€:", chat["claude"], height=200, disabled=True)
|
171 |
+
st.markdown("---")
|
172 |
+
|
173 |
+
# File Editor (When you need to tweak things βοΈ)
|
174 |
+
if hasattr(st.session_state, 'current_file'):
|
175 |
+
st.subheader(f"Editing: {st.session_state.current_file} π ")
|
176 |
+
new_content = st.text_area("File Content βοΈ:", st.session_state.file_content, height=300)
|
177 |
+
if st.button("Save Changes πΎ"):
|
178 |
+
with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
|
179 |
+
file.write(new_content)
|
180 |
+
st.success("File updated successfully! π")
|
181 |
+
|
182 |
+
# Image Gallery (For your viewing pleasure πΈ)
|
183 |
+
st.subheader("Image Gallery πΌ")
|
184 |
+
image_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
|
185 |
+
image_cols = st.slider("Gallery Columns πΌ", min_value=1, max_value=5, value=3)
|
186 |
+
cols = st.columns(image_cols)
|
187 |
+
for idx, image_file in enumerate(image_files):
|
188 |
+
with cols[idx % image_cols]:
|
189 |
+
img = Image.open(image_file)
|
190 |
+
st.image(img, caption=image_file, use_column_width=True)
|
191 |
+
display_glossary_entity(os.path.splitext(image_file)[0])
|
192 |
+
|
193 |
+
# Video Gallery (Letβs roll the tapes π¬)
|
194 |
+
st.subheader("Video Gallery π₯")
|
195 |
+
video_files = glob.glob("*.mp4")
|
196 |
+
video_cols = st.slider("Gallery Columns π¬", min_value=1, max_value=5, value=3)
|
197 |
+
cols = st.columns(video_cols)
|
198 |
+
for idx, video_file in enumerate(video_files):
|
199 |
+
with cols[idx % video_cols]:
|
200 |
+
st.markdown(get_video_html(video_file, width="100%"), unsafe_allow_html=True)
|
201 |
+
display_glossary_entity(os.path.splitext(video_file)[0])
|
202 |
+
|
203 |
+
# Audio Gallery (Tunes for the mood πΆ)
|
204 |
+
st.subheader("Audio Gallery π§")
|
205 |
+
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
206 |
+
audio_cols = st.slider("Gallery Columns πΆ", min_value=1, max_value=5, value=3)
|
207 |
+
cols = st.columns(audio_cols)
|
208 |
+
for idx, audio_file in enumerate(audio_files):
|
209 |
+
with cols[idx % audio_cols]:
|
210 |
+
st.markdown(get_audio_html(audio_file, width="100%"), unsafe_allow_html=True)
|
211 |
+
display_glossary_entity(os.path.splitext(audio_file)[0])
|
212 |
+
|
213 |
+
if __name__ == "__main__":
|
214 |
+
main()
|