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