awacke1's picture
Update app.py
23488d1 verified
raw
history blame
17.3 kB
import streamlit as st
import anthropic
import openai
import base64
from datetime import datetime
import plotly.graph_objects as go
import cv2
import glob
import json
import math
import os
import pytz
import random
import re
import requests
import streamlit.components.v1 as components
import textract
import time
import zipfile
from audio_recorder_streamlit import audio_recorder
from bs4 import BeautifulSoup
from collections import deque
from dotenv import load_dotenv
from gradio_client import Client, handle_file
from huggingface_hub import InferenceClient
from io import BytesIO
from moviepy.editor import VideoFileClip
from PIL import Image
from PyPDF2 import PdfReader
from urllib.parse import quote
from xml.etree import ElementTree as ET
from openai import OpenAI
# Configuration and Setup
Site_Name = '🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI'
title = "🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI"
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
}
)
# Load environment variables and initialize clients
load_dotenv()
# OpenAI setup
openai.api_key = os.getenv('OPENAI_API_KEY')
if openai.api_key == None:
openai.api_key = st.secrets['OPENAI_API_KEY']
openai_client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY'),
organization=os.getenv('OPENAI_ORG_ID')
)
# Claude setup
anthropic_key = os.getenv("ANTHROPIC_API_KEY_3")
if anthropic_key == None:
anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
claude_client = anthropic.Anthropic(api_key=anthropic_key)
# Initialize session states
if 'transcript_history' not in st.session_state:
st.session_state.transcript_history = []
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-4o-2024-05-13"
if "messages" not in st.session_state:
st.session_state.messages = []
if 'last_voice_input' not in st.session_state:
st.session_state.last_voice_input = ""
# Speech Recognition HTML Component
speech_recognition_html = """
<!DOCTYPE html>
<html>
<head>
<title>Continuous Speech Demo</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
max-width: 800px;
margin: 0 auto;
}
button {
padding: 10px 20px;
margin: 10px 5px;
font-size: 16px;
}
#status {
margin: 10px 0;
padding: 10px;
background: #e8f5e9;
border-radius: 4px;
}
#output {
white-space: pre-wrap;
padding: 15px;
background: #f5f5f5;
border-radius: 4px;
margin: 10px 0;
min-height: 100px;
max-height: 400px;
overflow-y: auto;
}
.controls {
margin: 10px 0;
}
</style>
</head>
<body>
<div class="controls">
<button id="start">Start Listening</button>
<button id="stop" disabled>Stop Listening</button>
<button id="clear">Clear Text</button>
</div>
<div id="status">Ready</div>
<div id="output"></div>
<script>
if (!('webkitSpeechRecognition' in window)) {
alert('Speech recognition not supported');
} else {
const recognition = new webkitSpeechRecognition();
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const clearButton = document.getElementById('clear');
const status = document.getElementById('status');
const output = document.getElementById('output');
let fullTranscript = '';
let lastUpdateTime = Date.now();
// Configure recognition
recognition.continuous = true;
recognition.interimResults = true;
// Function to start recognition
const startRecognition = () => {
try {
recognition.start();
status.textContent = 'Listening...';
startButton.disabled = true;
stopButton.disabled = false;
} catch (e) {
console.error(e);
status.textContent = 'Error: ' + e.message;
}
};
// Auto-start on load
window.addEventListener('load', () => {
setTimeout(startRecognition, 1000);
});
startButton.onclick = startRecognition;
stopButton.onclick = () => {
recognition.stop();
status.textContent = 'Stopped';
startButton.disabled = false;
stopButton.disabled = true;
};
clearButton.onclick = () => {
fullTranscript = '';
output.textContent = '';
window.parent.postMessage({
type: 'clear_transcript',
}, '*');
};
recognition.onresult = (event) => {
let interimTranscript = '';
let finalTranscript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const transcript = event.results[i][0].transcript;
if (event.results[i].isFinal) {
finalTranscript += transcript + '\\n';
} else {
interimTranscript += transcript;
}
}
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
if (finalTranscript) {
fullTranscript += finalTranscript;
// Send to Streamlit
window.parent.postMessage({
type: 'final_transcript',
text: finalTranscript
}, '*');
}
lastUpdateTime = Date.now();
}
output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
output.scrollTop = output.scrollHeight;
};
recognition.onend = () => {
if (!stopButton.disabled) {
try {
recognition.start();
console.log('Restarted recognition');
} catch (e) {
console.error('Failed to restart recognition:', e);
status.textContent = 'Error restarting: ' + e.message;
startButton.disabled = false;
stopButton.disabled = true;
}
}
};
recognition.onerror = (event) => {
console.error('Recognition error:', event.error);
status.textContent = 'Error: ' + event.error;
if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
startButton.disabled = false;
stopButton.disabled = true;
}
};
}
</script>
</body>
</html>
"""
# Helper Functions
def generate_filename(prompt, file_type):
central = pytz.timezone('US/Central')
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
replaced_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt)
safe_prompt = re.sub(r'\s+', ' ', replaced_prompt).strip()[:230]
return f"{safe_date_time}_{safe_prompt}.{file_type}"
# File Management Functions
def load_file(file_name):
"""Load file content."""
with open(file_name, "r", encoding='utf-8') as file:
content = file.read()
return content
def create_zip_of_files(files):
"""Create zip archive of files."""
zip_name = "all_files.zip"
with zipfile.ZipFile(zip_name, 'w') as zipf:
for file in files:
zipf.write(file)
return zip_name
def get_download_link(file):
"""Create download link for file."""
with open(file, "rb") as f:
contents = f.read()
b64 = base64.b64encode(contents).decode()
return f'<a href="data:file/txt;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}πŸ“‚</a>'
def display_file_manager():
"""Display file management sidebar."""
st.sidebar.title("πŸ“ File Management")
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()
def create_media_gallery():
"""Create the media gallery interface."""
st.header("🎬 Media Gallery")
tabs = st.tabs(["πŸ–ΌοΈ Images", "🎡 Audio", "πŸŽ₯ Video", "🎨 Scene Generator"])
with tabs[0]:
image_files = glob.glob("*.png") + glob.glob("*.jpg")
if image_files:
num_cols = st.slider("Number of columns", 1, 5, 3)
cols = st.columns(num_cols)
for idx, image_file in enumerate(image_files):
with cols[idx % num_cols]:
img = Image.open(image_file)
st.image(img, use_container_width=True)
# Add GPT vision analysis option
if st.button(f"Analyze {os.path.basename(image_file)}"):
analysis = process_image(image_file,
"Describe this image in detail and identify key elements.")
st.markdown(analysis)
with tabs[1]:
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
for audio_file in audio_files:
with st.expander(f"🎡 {os.path.basename(audio_file)}"):
st.markdown(get_media_html(audio_file, "audio"), unsafe_allow_html=True)
if st.button(f"Transcribe {os.path.basename(audio_file)}"):
with open(audio_file, "rb") as f:
transcription = process_audio(f)
st.write(transcription)
with tabs[2]:
video_files = glob.glob("*.mp4")
for video_file in video_files:
with st.expander(f"πŸŽ₯ {os.path.basename(video_file)}"):
st.markdown(get_media_html(video_file, "video"), unsafe_allow_html=True)
if st.button(f"Analyze {os.path.basename(video_file)}"):
analysis = process_video_with_gpt(video_file,
"Describe what's happening in this video.")
st.markdown(analysis)
with tabs[3]:
for collection_name, bikes in bike_collections.items():
st.subheader(collection_name)
cols = st.columns(len(bikes))
for idx, (bike_name, details) in enumerate(bikes.items()):
with cols[idx]:
st.markdown(f"""
<div class='bike-card'>
<h3>{details['emoji']} {bike_name}</h3>
<p>{details['prompt']}</p>
</div>
""", unsafe_allow_html=True)
if st.button(f"Generate {bike_name} Scene"):
prompt = details['prompt']
# Here you could integrate with image generation API
st.write(f"Generated scene description for {bike_name}:")
st.write(prompt)
def get_media_html(media_path, media_type="video", width="100%"):
"""Generate HTML for media player."""
media_data = base64.b64encode(open(media_path, 'rb').read()).decode()
if media_type == "video":
return f'''
<video width="{width}" controls autoplay muted loop>
<source src="data:video/mp4;base64,{media_data}" type="video/mp4">
Your browser does not support the video tag.
</video>
'''
else: # audio
return f'''
<audio controls style="width: {width};">
<source src="data:audio/mpeg;base64,{media_data}" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
'''
def main():
st.sidebar.markdown("### 🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI")
# Main navigation
tab_main = st.radio("Choose Action:",
["🎀 Voice Input", "πŸ’¬ Chat", "πŸ“Έ Media Gallery", "πŸ” Search ArXiv", "πŸ“ File Editor"],
horizontal=True)
if tab_main == "🎀 Voice Input":
st.subheader("Voice Recognition")
# Display speech recognition component
speech_component = st.components.v1.html(speech_recognition_html, height=400)
# Handle speech recognition output
if speech_component:
try:
data = speech_component
if isinstance(data, dict):
if data.get('type') == 'final_transcript':
text = data.get('text', '').strip()
if text:
st.session_state.last_voice_input = text
# Process voice input with AI
st.subheader("AI Response to Voice Input:")
col1, col2, col3 = st.columns(3)
with col2:
st.write("Claude-3.5 Sonnet:")
try:
claude_response = process_with_claude(text)
except:
st.write('Claude 3.5 Sonnet out of tokens.')
with col1:
st.write("GPT-4o Omni:")
try:
gpt_response = process_with_gpt(text)
except:
st.write('GPT 4o out of tokens')
with col3:
st.write("Arxiv and Mistral Research:")
with st.spinner("Searching ArXiv..."):
results = perform_ai_lookup(text)
st.markdown(results)
elif data.get('type') == 'clear_transcript':
st.session_state.last_voice_input = ""
st.experimental_rerun()
except Exception as e:
st.error(f"Error processing voice input: {e}")
# Display last voice input
if st.session_state.last_voice_input:
st.text_area("Last Voice Input:", st.session_state.last_voice_input, height=100)
# [Rest of the main function remains the same]
elif tab_main == "πŸ’¬ Chat":
# [Previous chat interface code]
pass
elif tab_main == "πŸ“Έ Media Gallery":
create_media_gallery()
elif tab_main == "πŸ” Search ArXiv":
query = st.text_input("Enter your research query:")
if query:
with st.spinner("Searching ArXiv..."):
results = search_arxiv(query)
st.markdown(results)
elif tab_main == "πŸ“ File Editor":
if hasattr(st.session_state, 'current_file'):
st.subheader(f"Editing: {st.session_state.current_file}")
new_content = st.text_area("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!")
# Always show file manager in sidebar
display_file_manager()
if __name__ == "__main__":
main()