Spaces:
Running
Running
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 | |
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 constants | |
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 = 'π²π' | |
# Speech Recognition HTML Template | |
speech_recognition_html = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { font-family: sans-serif; padding: 20px; } | |
button { padding: 10px 20px; margin: 10px 5px; } | |
#status { margin: 10px 0; padding: 10px; background: #e8f5e9; } | |
#output { | |
white-space: pre-wrap; | |
padding: 15px; | |
background: #f5f5f5; | |
margin: 10px 0; | |
min-height: 100px; | |
max-height: 300px; | |
overflow-y: auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<button id="startBtn">Start</button> | |
<button id="stopBtn" disabled>Stop</button> | |
<button id="clearBtn">Clear</button> | |
</div> | |
<div id="status">Ready</div> | |
<div id="output"></div> | |
<script> | |
const startBtn = document.getElementById('startBtn'); | |
const stopBtn = document.getElementById('stopBtn'); | |
const clearBtn = document.getElementById('clearBtn'); | |
const status = document.getElementById('status'); | |
const output = document.getElementById('output'); | |
let recognition; | |
let fullTranscript = ''; | |
if ('webkitSpeechRecognition' in window) { | |
recognition = new webkitSpeechRecognition(); | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.onstart = () => { | |
status.textContent = 'Listening...'; | |
startBtn.disabled = true; | |
stopBtn.disabled = false; | |
}; | |
recognition.onend = () => { | |
status.textContent = 'Click Start to begin'; | |
startBtn.disabled = false; | |
stopBtn.disabled = true; | |
}; | |
recognition.onresult = (event) => { | |
let interim = ''; | |
let final = ''; | |
for (let i = event.resultIndex; i < event.results.length; i++) { | |
if (event.results[i].isFinal) { | |
final += event.results[i][0].transcript + ' '; | |
fullTranscript += event.results[i][0].transcript + ' '; | |
} else { | |
interim += event.results[i][0].transcript; | |
} | |
} | |
if (final) { | |
// Send to Streamlit | |
window.parent.postMessage({ | |
type: 'final_transcript', | |
text: fullTranscript | |
}, '*'); | |
} | |
output.textContent = fullTranscript + interim; | |
output.scrollTop = output.scrollHeight; | |
}; | |
recognition.onerror = (event) => { | |
status.textContent = 'Error: ' + event.error; | |
startBtn.disabled = false; | |
stopBtn.disabled = true; | |
}; | |
// Button handlers | |
startBtn.onclick = () => { | |
try { | |
recognition.start(); | |
} catch (e) { | |
status.textContent = 'Error starting: ' + e; | |
} | |
}; | |
stopBtn.onclick = () => recognition.stop(); | |
clearBtn.onclick = () => { | |
fullTranscript = ''; | |
output.textContent = ''; | |
window.parent.postMessage({ | |
type: 'final_transcript', | |
text: '' | |
}, '*'); | |
}; | |
// Auto-start | |
document.addEventListener('DOMContentLoaded', () => { | |
setTimeout(() => startBtn.click(), 1000); | |
}); | |
} else { | |
status.textContent = 'Speech recognition not supported in this browser'; | |
startBtn.disabled = true; | |
stopBtn.disabled = true; | |
} | |
</script> | |
</body> | |
</html> | |
""" | |
# Streamlit page configuration | |
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 | |
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-4-vision-preview" | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
if 'voice_transcript' not in st.session_state: | |
st.session_state.voice_transcript = "" | |
# Main processing functions | |
def process_with_gpt(text_input): | |
"""Process text with GPT-4.""" | |
if text_input: | |
st.session_state.messages.append({"role": "user", "content": text_input}) | |
with st.chat_message("user"): | |
st.markdown(text_input) | |
with st.chat_message("assistant"): | |
completion = openai_client.chat.completions.create( | |
model=st.session_state["openai_model"], | |
messages=[ | |
{"role": m["role"], "content": m["content"]} | |
for m in st.session_state.messages | |
], | |
stream=False | |
) | |
return_text = completion.choices[0].message.content | |
st.write("GPT-4: " + return_text) | |
filename = generate_filename("GPT-4: " + return_text, "md") | |
create_file(filename, text_input, return_text) | |
st.session_state.messages.append({"role": "assistant", "content": return_text}) | |
return return_text | |
def process_with_claude(text_input): | |
"""Process text with Claude.""" | |
if text_input: | |
with st.chat_message("user"): | |
st.markdown(text_input) | |
with st.chat_message("assistant"): | |
response = claude_client.messages.create( | |
model="claude-3-sonnet-20240229", | |
max_tokens=1000, | |
messages=[ | |
{"role": "user", "content": text_input} | |
] | |
) | |
response_text = response.content[0].text | |
st.write("Claude: " + response_text) | |
filename = generate_filename("Claude: " + response_text, "md") | |
create_file(filename, text_input, response_text) | |
st.session_state.chat_history.append({ | |
"user": text_input, | |
"claude": response_text | |
}) | |
return response_text | |
def perform_ai_lookup(query): | |
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern") | |
start_time = time.strftime("%Y-%m-%d %H:%M:%S") | |
response1 = client.predict( | |
query, | |
20, | |
"Semantic Search", | |
"mistralai/Mixtral-8x7B-Instruct-v0.1", | |
api_name="/update_with_rag_md" | |
) | |
Question = '### π ' + query + '\r\n' | |
References = response1[0] | |
ReferenceLinks = extract_urls(References) | |
RunSecondQuery = True | |
results = '' | |
if RunSecondQuery: | |
response2 = client.predict( | |
query, | |
"mistralai/Mixtral-8x7B-Instruct-v0.1", | |
True, | |
api_name="/ask_llm" | |
) | |
if len(response2) > 10: | |
Answer = response2 | |
results = Question + '\r\n' + Answer + '\r\n' + References + '\r\n' + ReferenceLinks | |
st.markdown(results) | |
end_time = time.strftime("%Y-%m-%d %H:%M:%S") | |
start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S")) | |
end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S")) | |
elapsed_seconds = end_timestamp - start_timestamp | |
st.write('πRun of Multi-Agent System Paper Summary Spec is Complete') | |
st.write(f"Start time: {start_time}") | |
st.write(f"Finish time: {end_time}") | |
st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds") | |
filename = generate_filename(query, "md") | |
create_file(filename, query, results) | |
return results | |
# Main function | |
def main(): | |
st.sidebar.markdown("### π²BikeAIπ Claude and GPT Multi-Agent Research AI") | |
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 | |
st.components.v1.html(speech_recognition_html, height=400) | |
# Transcript receiver | |
transcript_receiver = st.components.v1.html(""" | |
<script> | |
window.addEventListener('message', function(e) { | |
if (e.data && e.data.type === 'final_transcript') { | |
window.Streamlit.setComponentValue(e.data.text); | |
} | |
}); | |
</script> | |
""", height=0) | |
# Update session state if new transcript received | |
if transcript_receiver: | |
st.session_state.voice_transcript = transcript_receiver | |
# Display transcript | |
st.markdown("### Processed Voice Input:") | |
st.text_area( | |
"Voice Transcript", | |
value=st.session_state.voice_transcript if isinstance(st.session_state.voice_transcript, str) else "", | |
height=100 | |
) | |
# Process buttons | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
if st.button("Process with GPT"): | |
if st.session_state.voice_transcript: | |
st.markdown("### GPT Response:") | |
gpt_response = process_with_gpt(st.session_state.voice_transcript) | |
st.markdown(gpt_response) | |
with col2: | |
if st.button("Process with Claude"): | |
if st.session_state.voice_transcript: | |
st.markdown("### Claude Response:") | |
claude_response = process_with_claude(st.session_state.voice_transcript) | |
st.markdown(claude_response) | |
with col3: | |
if st.button("Clear Transcript"): | |
st.session_state.voice_transcript = "" | |
st.experimental_rerun() | |
if st.session_state.voice_transcript: | |
if st.button("Search ArXiv"): | |
st.markdown("### ArXiv Search Results:") | |
arxiv_results = perform_ai_lookup(st.session_state.voice_transcript) | |
st.markdown(arxiv_results) | |
elif tab_main == "π¬ Chat": | |
# Model Selection | |
model_choice = st.sidebar.radio( | |
"Choose AI Model:", | |
["GPT-4", "Claude-3", "GPT+Claude+Arxiv"] | |
) | |
# Chat Interface | |
user_input = st.text_area("Message:", height=100) | |
if st.button("Send π¨"): | |
if user_input: | |
if model_choice == "GPT-4": | |
gpt_response = process_with_gpt(user_input) | |
elif model_choice == "Claude-3": | |
claude_response = process_with_claude(user_input) | |
else: # Both + Arxiv | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
st.subheader("GPT-4:") | |
try: | |
gpt_response = process_with_gpt(user_input) | |
except: | |
st.write('GPT-4 out of tokens') | |
with col2: | |
st.subheader("Claude-3:") | |
try: | |
claude_response = process_with_claude(user_input) | |
except: | |
st.write('Claude-3 out of tokens') | |
with col3: | |
st.subheader("Arxiv Search:") | |
with st.spinner("Searching ArXiv..."): | |
results = perform_ai_lookup(user_input) | |
st.markdown(results) | |
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 = perform_ai_lookup(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() | |
def create_media_gallery(): | |
"""Create the media gallery interface.""" | |
st.header("π¬ Media Gallery") | |
tabs = st.tabs(["πΌοΈ Images", "π΅ Audio", "π₯ Video"]) | |
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) | |
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) | |
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 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=f"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=f"edit_{file}"): | |
st.session_state.current_file = file | |
st.session_state.file_content = load_file(file) | |
with col4: | |
if st.button("π", key=f"delete_{file}"): | |
os.remove(file) | |
st.rerun() | |
def generate_filename(prompt, file_type): | |
"""Generate a filename based on prompt and time.""" | |
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}" | |
def create_file(filename, prompt, response): | |
"""Create and save a file.""" | |
with open(filename, 'w', encoding='utf-8') as file: | |
file.write(prompt + "\n\n" + response) | |
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 extract_urls(text): | |
"""Extract URLs from text.""" | |
try: | |
date_pattern = re.compile(r'### (\d{2} \w{3} \d{4})') | |
abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)') | |
pdf_link_pattern = re.compile(r'\[β¬οΈ\]\((https://arxiv\.org/pdf/\d+\.\d+)\)') | |
title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]') | |
date_matches = date_pattern.findall(text) | |
abs_link_matches = abs_link_pattern.findall(text) | |
pdf_link_matches = pdf_link_pattern.findall(text) | |
title_matches = title_pattern.findall(text) | |
markdown_text = "" | |
for i in range(len(date_matches)): | |
date = date_matches[i] | |
title = title_matches[i] | |
abs_link = abs_link_matches[i][1] | |
pdf_link = pdf_link_matches[i] | |
markdown_text += f"**Date:** {date}\n\n" | |
markdown_text += f"**Title:** {title}\n\n" | |
markdown_text += f"**Abstract Link:** [{abs_link}]({abs_link})\n\n" | |
markdown_text += f"**PDF Link:** [{pdf_link}]({pdf_link})\n\n" | |
markdown_text += "---\n\n" | |
return markdown_text | |
except: | |
return '' | |
# Run the application | |
if __name__ == "__main__": | |
main() |