awacke1 commited on
Commit
b7f62c2
β€’
1 Parent(s): f393c10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -415
app.py CHANGED
@@ -63,25 +63,13 @@ openai_client = OpenAI(
63
  organization=os.getenv('OPENAI_ORG_ID')
64
  )
65
 
66
- # 3. Claude setup
67
  anthropic_key = os.getenv("ANTHROPIC_API_KEY_3")
68
  if anthropic_key == None:
69
  anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
70
  claude_client = anthropic.Anthropic(api_key=anthropic_key)
71
 
72
- # 4. Initialize session states
73
- if 'transcript_history' not in st.session_state:
74
- st.session_state.transcript_history = []
75
- if "chat_history" not in st.session_state:
76
- st.session_state.chat_history = []
77
- if "openai_model" not in st.session_state:
78
- st.session_state["openai_model"] = "gpt-4o-2024-05-13"
79
- if "messages" not in st.session_state:
80
- st.session_state.messages = []
81
- if 'last_voice_input' not in st.session_state:
82
- st.session_state.last_voice_input = ""
83
-
84
- # 5. # HuggingFace setup
85
  API_URL = os.getenv('API_URL')
86
  HF_KEY = os.getenv('HF_KEY')
87
  MODEL1 = "meta-llama/Llama-2-7b-chat-hf"
@@ -140,7 +128,6 @@ st.markdown("""
140
  </style>
141
  """, unsafe_allow_html=True)
142
 
143
-
144
  # Bike Collections
145
  bike_collections = {
146
  "Celestial Collection 🌌": {
@@ -193,7 +180,6 @@ bike_collections = {
193
  }
194
  }
195
 
196
-
197
  # Helper Functions
198
  def generate_filename(prompt, file_type):
199
  """Generate a safe filename using the prompt and file type."""
@@ -624,44 +610,38 @@ def create_media_gallery():
624
  st.write(prompt)
625
 
626
  def display_file_manager():
627
- """Display file management sidebar with guaranteed unique button keys."""
628
  st.sidebar.title("πŸ“ File Management")
629
 
630
  all_files = glob.glob("*.md")
631
  all_files.sort(reverse=True)
632
 
633
- if st.sidebar.button("πŸ—‘ Delete All", key="delete_all_files_button"):
634
  for file in all_files:
635
  os.remove(file)
636
  st.rerun()
637
 
638
- if st.sidebar.button("⬇️ Download All", key="download_all_files_button"):
639
  zip_file = create_zip_of_files(all_files)
640
  st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
641
 
642
- # Create unique keys using file attributes
643
- for idx, file in enumerate(all_files):
644
- # Get file stats for unique identification
645
- file_stat = os.stat(file)
646
- unique_id = f"{idx}_{file_stat.st_size}_{file_stat.st_mtime}"
647
-
648
  col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
649
  with col1:
650
- if st.button("🌐", key=f"view_{unique_id}"):
651
  st.session_state.current_file = file
652
  st.session_state.file_content = load_file(file)
653
  with col2:
654
  st.markdown(get_download_link(file), unsafe_allow_html=True)
655
  with col3:
656
- if st.button("πŸ“‚", key=f"edit_{unique_id}"):
657
  st.session_state.current_file = file
658
  st.session_state.file_content = load_file(file)
659
  with col4:
660
- if st.button("πŸ—‘", key=f"delete_{unique_id}"):
661
  os.remove(file)
662
  st.rerun()
663
 
664
-
665
  def main():
666
  st.sidebar.markdown("### 🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI")
667
 
@@ -745,391 +725,5 @@ def main():
745
  # Always show file manager in sidebar
746
  display_file_manager()
747
 
748
-
749
- # Speech Recognition HTML Component
750
- speech_recognition_html = """
751
- <!DOCTYPE html>
752
- <html>
753
- <head>
754
- <title>Continuous Speech Demo</title>
755
- <style>
756
- body {
757
- font-family: sans-serif;
758
- padding: 20px;
759
- max-width: 800px;
760
- margin: 0 auto;
761
- }
762
- button {
763
- padding: 10px 20px;
764
- margin: 10px 5px;
765
- font-size: 16px;
766
- }
767
- #status {
768
- margin: 10px 0;
769
- padding: 10px;
770
- background: #e8f5e9;
771
- border-radius: 4px;
772
- }
773
- #output {
774
- white-space: pre-wrap;
775
- padding: 15px;
776
- background: #f5f5f5;
777
- border-radius: 4px;
778
- margin: 10px 0;
779
- min-height: 100px;
780
- max-height: 400px;
781
- overflow-y: auto;
782
- }
783
- .controls {
784
- margin: 10px 0;
785
- }
786
- </style>
787
- </head>
788
- <body>
789
- <div class="controls">
790
- <button id="start">Start Listening</button>
791
- <button id="stop" disabled>Stop Listening</button>
792
- <button id="clear">Clear Text</button>
793
- </div>
794
- <div id="status">Ready</div>
795
- <div id="output"></div>
796
-
797
- <script>
798
- if (!('webkitSpeechRecognition' in window)) {
799
- alert('Speech recognition not supported');
800
- } else {
801
- const recognition = new webkitSpeechRecognition();
802
- const startButton = document.getElementById('start');
803
- const stopButton = document.getElementById('stop');
804
- const clearButton = document.getElementById('clear');
805
- const status = document.getElementById('status');
806
- const output = document.getElementById('output');
807
- let fullTranscript = '';
808
- let lastUpdateTime = Date.now();
809
-
810
- // Configure recognition
811
- recognition.continuous = true;
812
- recognition.interimResults = true;
813
-
814
- // Function to start recognition
815
- const startRecognition = () => {
816
- try {
817
- recognition.start();
818
- status.textContent = 'Listening...';
819
- startButton.disabled = true;
820
- stopButton.disabled = false;
821
- } catch (e) {
822
- console.error(e);
823
- status.textContent = 'Error: ' + e.message;
824
- }
825
- };
826
-
827
- // Auto-start on load
828
- window.addEventListener('load', () => {
829
- setTimeout(startRecognition, 1000);
830
- });
831
-
832
- startButton.onclick = startRecognition;
833
-
834
- stopButton.onclick = () => {
835
- recognition.stop();
836
- status.textContent = 'Stopped';
837
- startButton.disabled = false;
838
- stopButton.disabled = true;
839
- };
840
-
841
- clearButton.onclick = () => {
842
- fullTranscript = '';
843
- output.textContent = '';
844
- window.parent.postMessage({
845
- type: 'clear_transcript',
846
- }, '*');
847
- };
848
-
849
- recognition.onresult = (event) => {
850
- let interimTranscript = '';
851
- let finalTranscript = '';
852
-
853
- for (let i = event.resultIndex; i < event.results.length; i++) {
854
- const transcript = event.results[i][0].transcript;
855
- if (event.results[i].isFinal) {
856
- finalTranscript += transcript + '\\n';
857
- } else {
858
- interimTranscript += transcript;
859
- }
860
- }
861
-
862
- if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
863
- if (finalTranscript) {
864
- fullTranscript += finalTranscript;
865
- // Send to Streamlit
866
- window.parent.postMessage({
867
- type: 'final_transcript',
868
- text: finalTranscript
869
- }, '*');
870
- }
871
- lastUpdateTime = Date.now();
872
- }
873
-
874
- output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
875
- output.scrollTop = output.scrollHeight;
876
- };
877
-
878
- recognition.onend = () => {
879
- if (!stopButton.disabled) {
880
- try {
881
- recognition.start();
882
- console.log('Restarted recognition');
883
- } catch (e) {
884
- console.error('Failed to restart recognition:', e);
885
- status.textContent = 'Error restarting: ' + e.message;
886
- startButton.disabled = false;
887
- stopButton.disabled = true;
888
- }
889
- }
890
- };
891
-
892
- recognition.onerror = (event) => {
893
- console.error('Recognition error:', event.error);
894
- status.textContent = 'Error: ' + event.error;
895
-
896
- if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
897
- startButton.disabled = false;
898
- stopButton.disabled = true;
899
- }
900
- };
901
- }
902
- </script>
903
- </body>
904
- </html>
905
- """
906
-
907
- # Helper Functions
908
- def generate_filename(prompt, file_type):
909
- central = pytz.timezone('US/Central')
910
- safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
911
- replaced_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt)
912
- safe_prompt = re.sub(r'\s+', ' ', replaced_prompt).strip()[:230]
913
- return f"{safe_date_time}_{safe_prompt}.{file_type}"
914
-
915
- # File Management Functions
916
- def load_file(file_name):
917
- """Load file content."""
918
- with open(file_name, "r", encoding='utf-8') as file:
919
- content = file.read()
920
- return content
921
-
922
- def create_zip_of_files(files):
923
- """Create zip archive of files."""
924
- zip_name = "all_files.zip"
925
- with zipfile.ZipFile(zip_name, 'w') as zipf:
926
- for file in files:
927
- zipf.write(file)
928
- return zip_name
929
-
930
- def get_download_link(file):
931
- """Create download link for file."""
932
- with open(file, "rb") as f:
933
- contents = f.read()
934
- b64 = base64.b64encode(contents).decode()
935
- return f'<a href="data:file/txt;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}πŸ“‚</a>'
936
-
937
- def display_file_manager():
938
- """Display file management sidebar."""
939
- st.sidebar.title("πŸ“ File Management")
940
-
941
- all_files = glob.glob("*.md")
942
- all_files.sort(reverse=True)
943
-
944
- if st.sidebar.button("πŸ—‘ Delete All"):
945
- for file in all_files:
946
- os.remove(file)
947
- st.rerun()
948
-
949
- if st.sidebar.button("⬇️ Download All"):
950
- zip_file = create_zip_of_files(all_files)
951
- st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
952
-
953
- for file in all_files:
954
- col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
955
- with col1:
956
- if st.button("🌐", key="view_"+file):
957
- st.session_state.current_file = file
958
- st.session_state.file_content = load_file(file)
959
- with col2:
960
- st.markdown(get_download_link(file), unsafe_allow_html=True)
961
- with col3:
962
- if st.button("πŸ“‚", key="edit_"+file):
963
- st.session_state.current_file = file
964
- st.session_state.file_content = load_file(file)
965
- with col4:
966
- if st.button("πŸ—‘", key="delete_"+file):
967
- os.remove(file)
968
- st.rerun()
969
-
970
- def create_media_gallery():
971
- """Create the media gallery interface."""
972
- st.header("🎬 Media Gallery")
973
-
974
- tabs = st.tabs(["πŸ–ΌοΈ Images", "🎡 Audio", "πŸŽ₯ Video", "🎨 Scene Generator"])
975
-
976
- with tabs[0]:
977
- image_files = glob.glob("*.png") + glob.glob("*.jpg")
978
- if image_files:
979
- num_cols = st.slider("Number of columns", 1, 5, 3)
980
- cols = st.columns(num_cols)
981
- for idx, image_file in enumerate(image_files):
982
- with cols[idx % num_cols]:
983
- img = Image.open(image_file)
984
- st.image(img, use_container_width=True)
985
-
986
- # Add GPT vision analysis option
987
- if st.button(f"Analyze {os.path.basename(image_file)}"):
988
- analysis = process_image(image_file,
989
- "Describe this image in detail and identify key elements.")
990
- st.markdown(analysis)
991
-
992
- with tabs[1]:
993
- audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
994
- for audio_file in audio_files:
995
- with st.expander(f"🎡 {os.path.basename(audio_file)}"):
996
- st.markdown(get_media_html(audio_file, "audio"), unsafe_allow_html=True)
997
- if st.button(f"Transcribe {os.path.basename(audio_file)}"):
998
- with open(audio_file, "rb") as f:
999
- transcription = process_audio(f)
1000
- st.write(transcription)
1001
-
1002
- with tabs[2]:
1003
- video_files = glob.glob("*.mp4")
1004
- for video_file in video_files:
1005
- with st.expander(f"πŸŽ₯ {os.path.basename(video_file)}"):
1006
- st.markdown(get_media_html(video_file, "video"), unsafe_allow_html=True)
1007
- if st.button(f"Analyze {os.path.basename(video_file)}"):
1008
- analysis = process_video_with_gpt(video_file,
1009
- "Describe what's happening in this video.")
1010
- st.markdown(analysis)
1011
-
1012
- with tabs[3]:
1013
- for collection_name, bikes in bike_collections.items():
1014
- st.subheader(collection_name)
1015
- cols = st.columns(len(bikes))
1016
-
1017
- for idx, (bike_name, details) in enumerate(bikes.items()):
1018
- with cols[idx]:
1019
- st.markdown(f"""
1020
- <div class='bike-card'>
1021
- <h3>{details['emoji']} {bike_name}</h3>
1022
- <p>{details['prompt']}</p>
1023
- </div>
1024
- """, unsafe_allow_html=True)
1025
-
1026
- if st.button(f"Generate {bike_name} Scene"):
1027
- prompt = details['prompt']
1028
- # Here you could integrate with image generation API
1029
- st.write(f"Generated scene description for {bike_name}:")
1030
- st.write(prompt)
1031
-
1032
- def get_media_html(media_path, media_type="video", width="100%"):
1033
- """Generate HTML for media player."""
1034
- media_data = base64.b64encode(open(media_path, 'rb').read()).decode()
1035
- if media_type == "video":
1036
- return f'''
1037
- <video width="{width}" controls autoplay muted loop>
1038
- <source src="data:video/mp4;base64,{media_data}" type="video/mp4">
1039
- Your browser does not support the video tag.
1040
- </video>
1041
- '''
1042
- else: # audio
1043
- return f'''
1044
- <audio controls style="width: {width};">
1045
- <source src="data:audio/mpeg;base64,{media_data}" type="audio/mpeg">
1046
- Your browser does not support the audio element.
1047
- </audio>
1048
- '''
1049
-
1050
- def main():
1051
- st.sidebar.markdown("### 🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI")
1052
-
1053
- # Main navigation
1054
- tab_main = st.radio("Choose Action:",
1055
- ["🎀 Voice Input", "πŸ’¬ Chat", "πŸ“Έ Media Gallery", "πŸ” Search ArXiv", "οΏ½οΏ½ File Editor"],
1056
- horizontal=True)
1057
-
1058
- if tab_main == "🎀 Voice Input":
1059
- st.subheader("Voice Recognition")
1060
-
1061
- # Display speech recognition component
1062
- speech_component = st.components.v1.html(speech_recognition_html, height=400)
1063
-
1064
- # Handle speech recognition output
1065
- if speech_component:
1066
- try:
1067
- data = speech_component
1068
- if isinstance(data, dict):
1069
- if data.get('type') == 'final_transcript':
1070
- text = data.get('text', '').strip()
1071
- if text:
1072
- st.session_state.last_voice_input = text
1073
-
1074
- # Process voice input with AI
1075
- st.subheader("AI Response to Voice Input:")
1076
-
1077
- col1, col2, col3 = st.columns(3)
1078
- with col2:
1079
- st.write("Claude-3.5 Sonnet:")
1080
- try:
1081
- claude_response = process_with_claude(text)
1082
- except:
1083
- st.write('Claude 3.5 Sonnet out of tokens.')
1084
- with col1:
1085
- st.write("GPT-4o Omni:")
1086
- try:
1087
- gpt_response = process_with_gpt(text)
1088
- except:
1089
- st.write('GPT 4o out of tokens')
1090
- with col3:
1091
- st.write("Arxiv and Mistral Research:")
1092
- with st.spinner("Searching ArXiv..."):
1093
- results = perform_ai_lookup(text)
1094
- st.markdown(results)
1095
-
1096
- elif data.get('type') == 'clear_transcript':
1097
- st.session_state.last_voice_input = ""
1098
- st.experimental_rerun()
1099
-
1100
- except Exception as e:
1101
- st.error(f"Error processing voice input: {e}")
1102
-
1103
- # Display last voice input
1104
- if st.session_state.last_voice_input:
1105
- st.text_area("Last Voice Input:", st.session_state.last_voice_input, height=100)
1106
-
1107
- # [Rest of the main function remains the same]
1108
- elif tab_main == "πŸ’¬ Chat":
1109
- # [Previous chat interface code]
1110
- pass
1111
-
1112
- elif tab_main == "πŸ“Έ Media Gallery":
1113
- create_media_gallery()
1114
-
1115
- elif tab_main == "πŸ” Search ArXiv":
1116
- query = st.text_input("Enter your research query:")
1117
- if query:
1118
- with st.spinner("Searching ArXiv..."):
1119
- results = search_arxiv(query)
1120
- st.markdown(results)
1121
-
1122
- elif tab_main == "πŸ“ File Editor":
1123
- if hasattr(st.session_state, 'current_file'):
1124
- st.subheader(f"Editing: {st.session_state.current_file}")
1125
- new_content = st.text_area("Content:", st.session_state.file_content, height=300)
1126
- if st.button("Save Changes"):
1127
- with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
1128
- file.write(new_content)
1129
- st.success("File updated successfully!")
1130
-
1131
- # Always show file manager in sidebar
1132
- display_file_manager()
1133
-
1134
  if __name__ == "__main__":
1135
  main()
 
63
  organization=os.getenv('OPENAI_ORG_ID')
64
  )
65
 
66
+ # Claude setup
67
  anthropic_key = os.getenv("ANTHROPIC_API_KEY_3")
68
  if anthropic_key == None:
69
  anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
70
  claude_client = anthropic.Anthropic(api_key=anthropic_key)
71
 
72
+ # HuggingFace setup
 
 
 
 
 
 
 
 
 
 
 
 
73
  API_URL = os.getenv('API_URL')
74
  HF_KEY = os.getenv('HF_KEY')
75
  MODEL1 = "meta-llama/Llama-2-7b-chat-hf"
 
128
  </style>
129
  """, unsafe_allow_html=True)
130
 
 
131
  # Bike Collections
132
  bike_collections = {
133
  "Celestial Collection 🌌": {
 
180
  }
181
  }
182
 
 
183
  # Helper Functions
184
  def generate_filename(prompt, file_type):
185
  """Generate a safe filename using the prompt and file type."""
 
610
  st.write(prompt)
611
 
612
  def display_file_manager():
613
+ """Display file management sidebar."""
614
  st.sidebar.title("πŸ“ File Management")
615
 
616
  all_files = glob.glob("*.md")
617
  all_files.sort(reverse=True)
618
 
619
+ if st.sidebar.button("πŸ—‘ Delete All"):
620
  for file in all_files:
621
  os.remove(file)
622
  st.rerun()
623
 
624
+ if st.sidebar.button("⬇️ Download All"):
625
  zip_file = create_zip_of_files(all_files)
626
  st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
627
 
628
+ for file in all_files:
 
 
 
 
 
629
  col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
630
  with col1:
631
+ if st.button("🌐", key="view_"+file):
632
  st.session_state.current_file = file
633
  st.session_state.file_content = load_file(file)
634
  with col2:
635
  st.markdown(get_download_link(file), unsafe_allow_html=True)
636
  with col3:
637
+ if st.button("πŸ“‚", key="edit_"+file):
638
  st.session_state.current_file = file
639
  st.session_state.file_content = load_file(file)
640
  with col4:
641
+ if st.button("πŸ—‘", key="delete_"+file):
642
  os.remove(file)
643
  st.rerun()
644
 
 
645
  def main():
646
  st.sidebar.markdown("### 🚲BikeAIπŸ† Claude and GPT Multi-Agent Research AI")
647
 
 
725
  # Always show file manager in sidebar
726
  display_file_manager()
727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  if __name__ == "__main__":
729
  main()