awacke1 commited on
Commit
1ccc585
โ€ข
1 Parent(s): 8edf8d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -39
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
@@ -52,6 +52,142 @@ st.set_page_config(
52
  }
53
  )
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # 2. ๐ŸšฒBikeAI๐Ÿ† Load environment variables and initialize clients
56
  load_dotenv()
57
 
@@ -872,50 +1008,34 @@ def main():
872
  ["๐ŸŽค Voice Input", "๐Ÿ’ฌ Chat", "๐Ÿ“ธ Media Gallery", "๐Ÿ” Search ArXiv", "๐Ÿ“ File Editor"],
873
  horizontal=True)
874
 
875
- if tab_main == "๐ŸŽค Voice Input":
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
- # Display the transcript in a Streamlit text area
890
- st.markdown("### Processed Voice Input:")
891
- st.text_area("Voice Transcript", st.session_state.voice_transcript, height=100)
892
-
893
- # Add functionality to process the transcript
894
- if st.button("Process Transcript"):
895
- st.subheader("AI Response to Transcript")
896
- gpt_response = process_with_gpt(st.session_state.voice_transcript)
897
- st.markdown(gpt_response)
898
-
899
- # Option to clear the transcript
900
- if st.button("Clear Transcript"):
901
- st.session_state.voice_transcript = ""
902
- st.rerun()
903
-
904
-
905
- # Buttons to process the transcript
906
- if st.button("Search with GPT"):
907
  st.subheader("GPT-4o Response")
908
- gpt_response = process_with_gpt(st.session_state.voice_transcript)
909
  st.markdown(gpt_response)
910
-
911
- if st.button("Search with Claude"):
 
912
  st.subheader("Claude Response")
913
- claude_response = process_with_claude(st.session_state.voice_transcript)
914
  st.markdown(claude_response)
915
-
 
916
  if st.button("Search ArXiv"):
917
  st.subheader("ArXiv Search Results")
918
- arxiv_results = perform_ai_lookup(st.session_state.voice_transcript)
919
  st.markdown(arxiv_results)
920
 
921
 
 
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
 
52
  }
53
  )
54
 
55
+
56
+
57
+
58
+
59
+ def create_speech_component():
60
+ """Create a custom speech recognition component with bidirectional communication."""
61
+
62
+ speech_recognition_html = """
63
+ <!DOCTYPE html>
64
+ <html>
65
+ <head>
66
+ <title>Continuous Speech Demo</title>
67
+ <style>
68
+ /* Your existing styles here */
69
+ </style>
70
+ </head>
71
+ <body>
72
+ <div class="controls">
73
+ <button id="start">Start Listening</button>
74
+ <button id="stop" disabled>Stop Listening</button>
75
+ <button id="clear">Clear Text</button>
76
+ </div>
77
+ <div id="status">Ready</div>
78
+ <div id="output"></div>
79
+
80
+ <script>
81
+ // Function to send data back to Streamlit
82
+ function sendToStreamlit(data) {
83
+ window.parent.postMessage({
84
+ type: 'streamlit:setComponentValue',
85
+ value: data
86
+ }, '*');
87
+ }
88
+
89
+ if (!('webkitSpeechRecognition' in window)) {
90
+ alert('Speech recognition not supported');
91
+ } else {
92
+ const recognition = new webkitSpeechRecognition();
93
+ const startButton = document.getElementById('start');
94
+ const stopButton = document.getElementById('stop');
95
+ const clearButton = document.getElementById('clear');
96
+ const status = document.getElementById('status');
97
+ const output = document.getElementById('output');
98
+ let fullTranscript = '';
99
+
100
+ // Configure recognition
101
+ recognition.continuous = true;
102
+ recognition.interimResults = true;
103
+
104
+ startButton.onclick = () => {
105
+ recognition.start();
106
+ status.textContent = 'Listening...';
107
+ startButton.disabled = true;
108
+ stopButton.disabled = false;
109
+ };
110
+
111
+ stopButton.onclick = () => {
112
+ recognition.stop();
113
+ status.textContent = 'Stopped';
114
+ startButton.disabled = false;
115
+ stopButton.disabled = true;
116
+ // Send final transcript to Streamlit
117
+ sendToStreamlit(fullTranscript);
118
+ };
119
+
120
+ clearButton.onclick = () => {
121
+ fullTranscript = '';
122
+ output.textContent = '';
123
+ sendToStreamlit('');
124
+ };
125
+
126
+ recognition.onresult = (event) => {
127
+ let interimTranscript = '';
128
+ let finalTranscript = '';
129
+
130
+ for (let i = event.resultIndex; i < event.results.length; i++) {
131
+ const transcript = event.results[i][0].transcript;
132
+ if (event.results[i].isFinal) {
133
+ finalTranscript += transcript + '\\n';
134
+ fullTranscript += transcript + '\\n';
135
+ // Send update to Streamlit
136
+ sendToStreamlit(fullTranscript);
137
+ } else {
138
+ interimTranscript += transcript;
139
+ }
140
+ }
141
+
142
+ output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
143
+ output.scrollTop = output.scrollHeight;
144
+ };
145
+
146
+ recognition.onend = () => {
147
+ if (!stopButton.disabled) {
148
+ recognition.start();
149
+ }
150
+ };
151
+
152
+ // Auto-start on load
153
+ window.addEventListener('load', () => {
154
+ setTimeout(() => {
155
+ startButton.click();
156
+ }, 1000);
157
+ });
158
+ }
159
+ </script>
160
+ </body>
161
+ </html>
162
+ """
163
+
164
+ # Create the component with a key
165
+ component_value = components.html(
166
+ speech_recognition_html,
167
+ height=400,
168
+ key="speech_recognition"
169
+ )
170
+
171
+ return component_value
172
+
173
+ def integrate_speech_component():
174
+ """Integrate the speech component into the main app."""
175
+ if "voice_transcript" not in st.session_state:
176
+ st.session_state.voice_transcript = ""
177
+
178
+ # Get the transcript from the component
179
+ transcript = create_speech_component()
180
+
181
+ # Update session state if there's new data
182
+ if transcript is not None and transcript != "":
183
+ st.session_state.voice_transcript = transcript
184
+
185
+ return st.session_state.voice_transcript
186
+
187
+
188
+
189
+
190
+
191
  # 2. ๐ŸšฒBikeAI๐Ÿ† Load environment variables and initialize clients
192
  load_dotenv()
193
 
 
1008
  ["๐ŸŽค Voice Input", "๐Ÿ’ฌ Chat", "๐Ÿ“ธ Media Gallery", "๐Ÿ” Search ArXiv", "๐Ÿ“ File Editor"],
1009
  horizontal=True)
1010
 
1011
+ if tab_main == "๐ŸŽค Voice Input":
1012
+ st.subheader("Voice Recognition")
1013
+
1014
+ # Get transcript from the speech component
1015
+ current_transcript = integrate_speech_component()
1016
+
1017
+ # Display the transcript in a Streamlit text area
1018
+ st.markdown("### Processed Voice Input:")
1019
+ st.text_area("Voice Transcript", current_transcript, height=100)
1020
+
1021
+ # Process buttons
1022
+ col1, col2, col3 = st.columns(3)
1023
+ with col1:
1024
+ if st.button("Process with GPT"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1025
  st.subheader("GPT-4o Response")
1026
+ gpt_response = process_with_gpt(current_transcript)
1027
  st.markdown(gpt_response)
1028
+
1029
+ with col2:
1030
+ if st.button("Process with Claude"):
1031
  st.subheader("Claude Response")
1032
+ claude_response = process_with_claude(current_transcript)
1033
  st.markdown(claude_response)
1034
+
1035
+ with col3:
1036
  if st.button("Search ArXiv"):
1037
  st.subheader("ArXiv Search Results")
1038
+ arxiv_results = perform_ai_lookup(current_transcript)
1039
  st.markdown(arxiv_results)
1040
 
1041