awacke1 commited on
Commit
1025dd3
1 Parent(s): 4020d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -102
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  import anthropic
3
  import openai
4
  import base64
@@ -13,7 +14,6 @@ import pytz
13
  import random
14
  import re
15
  import requests
16
- import streamlit.components.v1 as components
17
  import textract
18
  import time
19
  import zipfile
@@ -31,6 +31,7 @@ from xml.etree import ElementTree as ET
31
  from openai import OpenAI
32
  import extra_streamlit_components as stx
33
  from streamlit.runtime.scriptrunner import get_script_run_ctx
 
34
 
35
 
36
  # 1. 🚲BikeAI🏆 Configuration and Setup
@@ -52,6 +53,146 @@ st.set_page_config(
52
  }
53
  )
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # 2. 🚲BikeAI🏆 Load environment variables and initialize clients
56
  load_dotenv()
57
 
@@ -863,117 +1004,48 @@ def get_media_html(media_path, media_type="video", width="100%"):
863
  def set_transcript(text):
864
  """Set transcript in session state."""
865
  st.session_state.voice_transcript = text
866
-
867
  def main():
868
  st.sidebar.markdown("### 🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI")
869
 
870
- # Main navigation
871
  tab_main = st.radio("Choose Action:",
872
- ["🎤 Voice Input 💬 Chat", "📸 Media Gallery", "🔍 Search ArXiv", "📝 File Editor"],
873
- horizontal=True)
874
 
875
- if tab_main == "🎤 Voice Input 💬 Chat":
876
  st.subheader("Voice Recognition")
877
 
878
- # Initialize session state for the transcript
879
- if 'voice_transcript' not in st.session_state:
880
- st.session_state.voice_transcript = ""
881
-
882
- # Display speech recognition component and capture returned value
883
- transcript = st.components.v1.html(speech_recognition_html, height=400)
884
-
885
- # Update session state if there's new data
886
- if transcript is not None and transcript != "":
887
- st.session_state.voice_transcript = transcript
888
-
889
-
890
- # Model Selection
891
- model_choice = st.sidebar.radio(
892
- "Choose AI Model:",
893
- ["GPT+Claude+Arxiv", "GPT-4o", "Claude-3"]
894
- )
895
-
896
- # Display Chat History
897
- st.subheader("Chat History 📜")
898
- tab1, tab2 = st.tabs(["Claude History", "GPT-4o History"])
899
-
900
- with tab1:
901
- for chat in st.session_state.chat_history:
902
- st.text_area("You:", chat["user"], height=100)
903
- st.text_area("Claude:", chat["claude"], height=200)
904
- st.markdown(chat["claude"])
905
-
906
- with tab2:
907
- for message in st.session_state.messages:
908
- with st.chat_message(message["role"]):
909
- st.markdown(message["content"])
910
-
911
-
912
- # Chat Interface
913
- user_input = st.text_area("Message:", height=100)
914
-
915
- if st.button("Send 📨"):
916
- if user_input:
917
- if model_choice == "GPT-4o":
918
- gpt_response = process_with_gpt(user_input)
919
- elif model_choice == "Claude-3":
920
- claude_response = process_with_claude(user_input)
921
- else: # Both
922
- col1, col2, col3 = st.columns(3)
923
- with col2:
924
- st.subheader("Claude-3.5 Sonnet:")
925
- try:
926
- claude_response = process_with_claude(user_input)
927
- except:
928
- st.write('Claude 3.5 Sonnet out of tokens.')
929
- with col1:
930
- st.subheader("GPT-4o Omni:")
931
- try:
932
- gpt_response = process_with_gpt(user_input)
933
- except:
934
- st.write('GPT 4o out of tokens')
935
- with col3:
936
- st.subheader("Arxiv and Mistral Research:")
937
  with st.spinner("Searching ArXiv..."):
938
- #results = search_arxiv(user_input)
939
- results = perform_ai_lookup(user_input)
940
-
941
  st.markdown(results)
942
-
943
- # Display Chat History
944
- st.subheader("Chat History 📜")
945
- tab1, tab2 = st.tabs(["Claude History", "GPT-4o History"])
946
 
947
- with tab1:
948
- for chat in st.session_state.chat_history:
949
- st.text_area("You:", chat["user"], height=100)
950
- st.text_area("Claude:", chat["claude"], height=200)
951
- st.markdown(chat["claude"])
952
-
953
- with tab2:
954
- for message in st.session_state.messages:
955
- with st.chat_message(message["role"]):
956
- st.markdown(message["content"])
957
-
958
- elif tab_main == "📸 Media Gallery":
959
- create_media_gallery()
960
-
961
- elif tab_main == "🔍 Search ArXiv":
962
- query = st.text_input("Enter your research query:")
963
- if query:
964
- with st.spinner("Searching ArXiv..."):
965
- results = search_arxiv(query)
966
- st.markdown(results)
967
-
968
- elif tab_main == "📝 File Editor":
969
- if hasattr(st.session_state, 'current_file'):
970
- st.subheader(f"Editing: {st.session_state.current_file}")
971
- new_content = st.text_area("Content:", st.session_state.file_content, height=300)
972
- if st.button("Save Changes"):
973
- with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
974
- file.write(new_content)
975
- st.success("File updated successfully!")
976
-
977
 
978
  # Always show file manager in sidebar
979
  display_file_manager()
 
1
  import streamlit as st
2
+ import streamlit.components.v1 as components
3
  import anthropic
4
  import openai
5
  import base64
 
14
  import random
15
  import re
16
  import requests
 
17
  import textract
18
  import time
19
  import zipfile
 
31
  from openai import OpenAI
32
  import extra_streamlit_components as stx
33
  from streamlit.runtime.scriptrunner import get_script_run_ctx
34
+ import extra_streamlit_components as stx
35
 
36
 
37
  # 1. 🚲BikeAI🏆 Configuration and Setup
 
53
  }
54
  )
55
 
56
+
57
+
58
+ def create_speech_component():
59
+ """Create speech recognition component using postMessage for communication."""
60
+
61
+ speech_recognition_html = """
62
+ <div style="padding: 20px;">
63
+ <div class="controls">
64
+ <button id="start">Start Listening</button>
65
+ <button id="stop" disabled>Stop Listening</button>
66
+ <button id="clear">Clear Text</button>
67
+ </div>
68
+ <div id="status" style="margin: 10px 0; padding: 10px; background: #e8f5e9;">Ready</div>
69
+ <div id="output" style="white-space: pre-wrap; padding: 15px; background: #f5f5f5; min-height: 100px; max-height: 400px; overflow-y: auto;"></div>
70
+ <div id="debug" style="margin-top: 10px; color: #666;"></div>
71
+
72
+ <script>
73
+ let currentTranscript = '';
74
+ const debug = document.getElementById('debug');
75
+
76
+ function sendTranscriptUpdate() {
77
+ // Send transcript to parent (Streamlit)
78
+ window.parent.postMessage({
79
+ type: 'transcript_update',
80
+ data: currentTranscript
81
+ }, '*');
82
+ debug.textContent = `Last update: ${new Date().toLocaleTimeString()} - Length: ${currentTranscript.length}`;
83
+ }
84
+
85
+ // Set up periodic updates
86
+ setInterval(sendTranscriptUpdate, 3000); // Send update every 3 seconds
87
+
88
+ const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
89
+ const startButton = document.getElementById('start');
90
+ const stopButton = document.getElementById('stop');
91
+ const clearButton = document.getElementById('clear');
92
+ const status = document.getElementById('status');
93
+ const output = document.getElementById('output');
94
+
95
+ recognition.continuous = true;
96
+ recognition.interimResults = true;
97
+
98
+ startButton.onclick = () => {
99
+ recognition.start();
100
+ status.textContent = '🎤 Listening...';
101
+ startButton.disabled = true;
102
+ stopButton.disabled = false;
103
+ };
104
+
105
+ stopButton.onclick = () => {
106
+ recognition.stop();
107
+ status.textContent = 'Stopped';
108
+ startButton.disabled = false;
109
+ stopButton.disabled = true;
110
+ sendTranscriptUpdate(); // Send final update when stopped
111
+ };
112
+
113
+ clearButton.onclick = () => {
114
+ currentTranscript = '';
115
+ output.textContent = '';
116
+ sendTranscriptUpdate(); // Send empty transcript
117
+ };
118
+
119
+ recognition.onresult = (event) => {
120
+ let interimTranscript = '';
121
+ let finalTranscript = '';
122
+
123
+ for (let i = event.resultIndex; i < event.results.length; i++) {
124
+ const transcript = event.results[i][0].transcript;
125
+ if (event.results[i].isFinal) {
126
+ finalTranscript += transcript + ' ';
127
+ currentTranscript += transcript + ' ';
128
+ } else {
129
+ interimTranscript += transcript;
130
+ }
131
+ }
132
+
133
+ output.textContent = currentTranscript + (interimTranscript ? '... ' + interimTranscript : '');
134
+ output.scrollTop = output.scrollHeight;
135
+
136
+ if (finalTranscript) {
137
+ sendTranscriptUpdate(); // Send update when we have final transcript
138
+ }
139
+ };
140
+
141
+ recognition.onend = () => {
142
+ if (!stopButton.disabled) {
143
+ recognition.start();
144
+ }
145
+ };
146
+
147
+ // Auto-start on load
148
+ window.addEventListener('load', () => {
149
+ setTimeout(() => startButton.click(), 1000);
150
+ });
151
+ </script>
152
+ </div>
153
+ """
154
+
155
+ # Return both the component value
156
+ return components.html(
157
+ speech_recognition_html,
158
+ height=400,
159
+ )
160
+
161
+ def integrate_speech_component():
162
+ """Integrate speech component with session state management."""
163
+ if "voice_transcript" not in st.session_state:
164
+ st.session_state.voice_transcript = ""
165
+ if "last_update" not in st.session_state:
166
+ st.session_state.last_update = time.time()
167
+
168
+ # Create placeholders for display
169
+ transcript_container = st.empty()
170
+ status_container = st.empty()
171
+
172
+ # Create component
173
+ component_val = create_speech_component()
174
+
175
+ # Display current transcript
176
+ current_transcript = st.session_state.voice_transcript
177
+ transcript_container.text_area(
178
+ "Voice Transcript:",
179
+ value=current_transcript,
180
+ height=100,
181
+ key=f"transcript_display_{int(time.time())}"
182
+ )
183
+
184
+ # Show status
185
+ status_container.text(
186
+ f"Last updated: {datetime.fromtimestamp(st.session_state.last_update).strftime('%H:%M:%S')}"
187
+ )
188
+
189
+ return current_transcript
190
+
191
+
192
+
193
+
194
+
195
+
196
  # 2. 🚲BikeAI🏆 Load environment variables and initialize clients
197
  load_dotenv()
198
 
 
1004
  def set_transcript(text):
1005
  """Set transcript in session state."""
1006
  st.session_state.voice_transcript = text
 
1007
  def main():
1008
  st.sidebar.markdown("### 🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI")
1009
 
 
1010
  tab_main = st.radio("Choose Action:",
1011
+ ["🎤 Voice Input", "💬 Chat", "📸 Media Gallery", "🔍 Search ArXiv", "📝 File Editor"],
1012
+ horizontal=True)
1013
 
1014
+ if tab_main == "🎤 Voice Input":
1015
  st.subheader("Voice Recognition")
1016
 
1017
+ try:
1018
+ # Initialize speech component
1019
+ current_transcript = integrate_speech_component()
1020
+
1021
+ # Show last update time
1022
+ st.text(f"Last updated: {datetime.fromtimestamp(st.session_state.last_update).strftime('%H:%M:%S')}")
1023
+
1024
+ # Process buttons if we have a transcript
1025
+ if current_transcript:
1026
+ col1, col2, col3 = st.columns(3)
1027
+
1028
+ with col1:
1029
+ if st.button("Process with GPT"):
1030
+ with st.spinner("Processing with GPT..."):
1031
+ response = process_with_gpt(current_transcript)
1032
+ st.markdown(response)
1033
+
1034
+ with col2:
1035
+ if st.button("Process with Claude"):
1036
+ with st.spinner("Processing with Claude..."):
1037
+ response = process_with_claude(current_transcript)
1038
+ st.markdown(response)
1039
+
1040
+ with col3:
1041
+ if st.button("Search ArXiv"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1042
  with st.spinner("Searching ArXiv..."):
1043
+ results = perform_ai_lookup(current_transcript)
 
 
1044
  st.markdown(results)
 
 
 
 
1045
 
1046
+ except Exception as e:
1047
+ st.error(f"Error in voice input: {str(e)}")
1048
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1049
 
1050
  # Always show file manager in sidebar
1051
  display_file_manager()