awacke1 commited on
Commit
8cb9f0c
·
verified ·
1 Parent(s): c4531c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -162
app.py CHANGED
@@ -30,6 +30,8 @@ from PyPDF2 import PdfReader
30
  from urllib.parse import quote
31
  from xml.etree import ElementTree as ET
32
  from openai import OpenAI
 
 
33
 
34
  # Configuration constants
35
  Site_Name = '🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI'
@@ -38,6 +40,19 @@ helpURL = 'https://huggingface.co/awacke1'
38
  bugURL = 'https://huggingface.co/spaces/awacke1'
39
  icons = '🚲🏆'
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Speech Recognition HTML Template
42
  speech_recognition_html = """
43
  <!DOCTYPE html>
@@ -68,91 +83,106 @@ speech_recognition_html = """
68
  <div id="output"></div>
69
 
70
  <script>
71
- const startBtn = document.getElementById('startBtn');
72
- const stopBtn = document.getElementById('stopBtn');
73
- const clearBtn = document.getElementById('clearBtn');
74
- const status = document.getElementById('status');
75
- const output = document.getElementById('output');
76
  let recognition;
77
  let fullTranscript = '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if ('webkitSpeechRecognition' in window) {
80
  recognition = new webkitSpeechRecognition();
81
  recognition.continuous = true;
82
  recognition.interimResults = true;
83
 
 
 
 
 
84
  recognition.onstart = () => {
85
- status.textContent = 'Listening...';
86
- startBtn.disabled = true;
87
- stopBtn.disabled = false;
88
  };
89
 
90
  recognition.onend = () => {
91
- status.textContent = 'Click Start to begin';
92
- startBtn.disabled = false;
93
- stopBtn.disabled = true;
94
  };
95
 
96
  recognition.onresult = (event) => {
97
- let interim = '';
98
- let final = '';
99
 
100
  for (let i = event.resultIndex; i < event.results.length; i++) {
 
101
  if (event.results[i].isFinal) {
102
- final += event.results[i][0].transcript + ' ';
103
- fullTranscript += event.results[i][0].transcript + ' ';
104
  } else {
105
- interim += event.results[i][0].transcript;
106
  }
107
  }
108
 
109
- if (final) {
110
- // Send to Streamlit
111
- window.parent.postMessage({
112
- type: 'final_transcript',
113
- text: fullTranscript
114
- }, '*');
115
  }
116
 
117
- output.textContent = fullTranscript + interim;
 
118
  output.scrollTop = output.scrollHeight;
119
  };
120
 
121
  recognition.onerror = (event) => {
122
- status.textContent = 'Error: ' + event.error;
123
- startBtn.disabled = false;
124
- stopBtn.disabled = true;
125
  };
126
 
127
  // Button handlers
128
- startBtn.onclick = () => {
129
  try {
130
  recognition.start();
131
  } catch (e) {
132
- status.textContent = 'Error starting: ' + e;
133
  }
134
  };
135
 
136
- stopBtn.onclick = () => recognition.stop();
137
 
138
- clearBtn.onclick = () => {
139
  fullTranscript = '';
140
- output.textContent = '';
141
- window.parent.postMessage({
142
- type: 'final_transcript',
143
- text: ''
144
- }, '*');
145
  };
146
 
147
- // Auto-start
148
- document.addEventListener('DOMContentLoaded', () => {
149
- setTimeout(() => startBtn.click(), 1000);
 
 
 
 
 
 
150
  });
151
-
152
  } else {
153
- status.textContent = 'Speech recognition not supported in this browser';
154
- startBtn.disabled = true;
155
- stopBtn.disabled = true;
156
  }
157
  </script>
158
  </body>
@@ -172,7 +202,10 @@ st.set_page_config(
172
  }
173
  )
174
 
175
- # Load environment variables
 
 
 
176
  load_dotenv()
177
 
178
  # OpenAI setup
@@ -203,7 +236,7 @@ if "messages" not in st.session_state:
203
  if 'voice_transcript' not in st.session_state:
204
  st.session_state.voice_transcript = ""
205
 
206
- # Main processing functions
207
  def process_with_gpt(text_input):
208
  """Process text with GPT-4."""
209
  if text_input:
@@ -256,6 +289,7 @@ def process_with_claude(text_input):
256
  return response_text
257
 
258
  def perform_ai_lookup(query):
 
259
  client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
260
  start_time = time.strftime("%Y-%m-%d %H:%M:%S")
261
 
@@ -271,26 +305,24 @@ def perform_ai_lookup(query):
271
  References = response1[0]
272
  ReferenceLinks = extract_urls(References)
273
 
274
- RunSecondQuery = True
275
  results = ''
276
- if RunSecondQuery:
277
- response2 = client.predict(
278
- query,
279
- "mistralai/Mixtral-8x7B-Instruct-v0.1",
280
- True,
281
- api_name="/ask_llm"
282
- )
283
- if len(response2) > 10:
284
- Answer = response2
285
- results = Question + '\r\n' + Answer + '\r\n' + References + '\r\n' + ReferenceLinks
286
- st.markdown(results)
287
 
288
  end_time = time.strftime("%Y-%m-%d %H:%M:%S")
289
  start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))
290
  end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))
291
  elapsed_seconds = end_timestamp - start_timestamp
292
 
293
- st.write('🔍Run of Multi-Agent System Paper Summary Spec is Complete')
294
  st.write(f"Start time: {start_time}")
295
  st.write(f"Finish time: {end_time}")
296
  st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds")
@@ -299,10 +331,12 @@ def perform_ai_lookup(query):
299
  create_file(filename, query, results)
300
  return results
301
 
302
- # Main function
303
  def main():
304
  st.sidebar.markdown("### 🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI")
305
 
 
 
 
306
  tab_main = st.radio("Choose Action:",
307
  ["🎤 Voice Input", "💬 Chat", "📸 Media Gallery", "🔍 Search ArXiv", "📝 File Editor"],
308
  horizontal=True)
@@ -310,30 +344,44 @@ def main():
310
  if tab_main == "🎤 Voice Input":
311
  st.subheader("Voice Recognition")
312
 
 
 
 
 
 
313
  # Display speech recognition component
314
  st.components.v1.html(speech_recognition_html, height=400)
315
 
316
- # Transcript receiver
317
  transcript_receiver = st.components.v1.html("""
318
  <script>
319
  window.addEventListener('message', function(e) {
320
- if (e.data && e.data.type === 'final_transcript') {
321
- window.Streamlit.setComponentValue(e.data.text);
 
 
 
 
 
 
 
322
  }
323
  });
324
  </script>
325
  """, height=0)
326
 
327
- # Update session state if new transcript received
328
- if transcript_receiver:
329
- st.session_state.voice_transcript = transcript_receiver
 
330
 
331
  # Display transcript
332
  st.markdown("### Processed Voice Input:")
333
- st.text_area(
334
  "Voice Transcript",
335
  value=st.session_state.voice_transcript if isinstance(st.session_state.voice_transcript, str) else "",
336
- height=100
 
337
  )
338
 
339
  # Process buttons
@@ -354,10 +402,10 @@ def main():
354
 
355
  with col3:
356
  if st.button("Clear Transcript"):
357
- st.session_state.voice_transcript = ""
358
  st.experimental_rerun()
359
 
360
- if st.session_state.voice_transcript:
361
  if st.button("Search ArXiv"):
362
  st.markdown("### ArXiv Search Results:")
363
  arxiv_results = perform_ai_lookup(st.session_state.voice_transcript)
@@ -398,6 +446,21 @@ def main():
398
  with st.spinner("Searching ArXiv..."):
399
  results = perform_ai_lookup(user_input)
400
  st.markdown(results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
 
402
  elif tab_main == "📸 Media Gallery":
403
  create_media_gallery()
@@ -421,97 +484,7 @@ def main():
421
  # Always show file manager in sidebar
422
  display_file_manager()
423
 
424
- def create_media_gallery():
425
- """Create the media gallery interface."""
426
- st.header("🎬 Media Gallery")
427
-
428
- tabs = st.tabs(["🖼️ Images", "🎵 Audio", "🎥 Video"])
429
-
430
- with tabs[0]:
431
- image_files = glob.glob("*.png") + glob.glob("*.jpg")
432
- if image_files:
433
- num_cols = st.slider("Number of columns", 1, 5, 3)
434
- cols = st.columns(num_cols)
435
- for idx, image_file in enumerate(image_files):
436
- with cols[idx % num_cols]:
437
- img = Image.open(image_file)
438
- st.image(img, use_container_width=True)
439
- if st.button(f"Analyze {os.path.basename(image_file)}"):
440
- analysis = process_image(image_file,
441
- "Describe this image in detail and identify key elements.")
442
- st.markdown(analysis)
443
-
444
- with tabs[1]:
445
- audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
446
- for audio_file in audio_files:
447
- with st.expander(f"🎵 {os.path.basename(audio_file)}"):
448
- st.markdown(get_media_html(audio_file, "audio"), unsafe_allow_html=True)
449
- if st.button(f"Transcribe {os.path.basename(audio_file)}"):
450
- with open(audio_file, "rb") as f:
451
- transcription = process_audio(f)
452
- st.write(transcription)
453
-
454
- with tabs[2]:
455
- video_files = glob.glob("*.mp4")
456
- for video_file in video_files:
457
- with st.expander(f"🎥 {os.path.basename(video_file)}"):
458
- st.markdown(get_media_html(video_file, "video"), unsafe_allow_html=True)
459
- if st.button(f"Analyze {os.path.basename(video_file)}"):
460
- analysis = process_video_with_gpt(video_file,
461
- "Describe what's happening in this video.")
462
- st.markdown(analysis)
463
-
464
- def get_media_html(media_path, media_type="video", width="100%"):
465
- """Generate HTML for media player."""
466
- media_data = base64.b64encode(open(media_path, 'rb').read()).decode()
467
- if media_type == "video":
468
- return f'''
469
- <video width="{width}" controls autoplay muted loop>
470
- <source src="data:video/mp4;base64,{media_data}" type="video/mp4">
471
- Your browser does not support the video tag.
472
- </video>
473
- '''
474
- else: # audio
475
- return f'''
476
- <audio controls style="width: {width};">
477
- <source src="data:audio/mpeg;base64,{media_data}" type="audio/mpeg">
478
- Your browser does not support the audio element.
479
- </audio>
480
- '''
481
-
482
- def display_file_manager():
483
- """Display file management sidebar."""
484
- st.sidebar.title("📁 File Management")
485
-
486
- all_files = glob.glob("*.md")
487
- all_files.sort(reverse=True)
488
-
489
- if st.sidebar.button("🗑 Delete All"):
490
- for file in all_files:
491
- os.remove(file)
492
- st.rerun()
493
-
494
- if st.sidebar.button("⬇️ Download All"):
495
- zip_file = create_zip_of_files(all_files)
496
- st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
497
-
498
- for file in all_files:
499
- col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
500
- with col1:
501
- if st.button("🌐", key=f"view_{file}"):
502
- st.session_state.current_file = file
503
- st.session_state.file_content = load_file(file)
504
- with col2:
505
- st.markdown(get_download_link(file), unsafe_allow_html=True)
506
- with col3:
507
- if st.button("📂", key=f"edit_{file}"):
508
- st.session_state.current_file = file
509
- st.session_state.file_content = load_file(file)
510
- with col4:
511
- if st.button("🗑", key=f"delete_{file}"):
512
- os.remove(file)
513
- st.rerun()
514
-
515
  def generate_filename(prompt, file_type):
516
  """Generate a filename based on prompt and time."""
517
  central = pytz.timezone('US/Central')
@@ -528,8 +501,7 @@ def create_file(filename, prompt, response):
528
  def load_file(file_name):
529
  """Load file content."""
530
  with open(file_name, "r", encoding='utf-8') as file:
531
- content = file.read()
532
- return content
533
 
534
  def create_zip_of_files(files):
535
  """Create zip archive of files."""
@@ -576,4 +548,7 @@ def extract_urls(text):
576
 
577
  # Run the application
578
  if __name__ == "__main__":
579
- main()
 
 
 
 
30
  from urllib.parse import quote
31
  from xml.etree import ElementTree as ET
32
  from openai import OpenAI
33
+ import extra_streamlit_components as stx
34
+ from streamlit.runtime.scriptrunner import get_script_run_ctx
35
 
36
  # Configuration constants
37
  Site_Name = '🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI'
 
40
  bugURL = 'https://huggingface.co/spaces/awacke1'
41
  icons = '🚲🏆'
42
 
43
+ # Cookie Management
44
+ def get_cookie_manager():
45
+ return stx.CookieManager()
46
+
47
+ # State Management
48
+ def update_transcript_state(new_transcript):
49
+ """Update transcript in both session state and cookie"""
50
+ cookie_manager = get_cookie_manager()
51
+ if new_transcript is not None:
52
+ st.session_state.voice_transcript = new_transcript
53
+ cookie_manager.set('voice_transcript', new_transcript)
54
+ return new_transcript
55
+
56
  # Speech Recognition HTML Template
57
  speech_recognition_html = """
58
  <!DOCTYPE html>
 
83
  <div id="output"></div>
84
 
85
  <script>
 
 
 
 
 
86
  let recognition;
87
  let fullTranscript = '';
88
+
89
+ // Function to save state to localStorage
90
+ function saveTranscriptState(text) {
91
+ localStorage.setItem('voiceTranscript', text);
92
+ // Send to Streamlit
93
+ window.parent.postMessage({
94
+ type: 'streamlit:setComponentValue',
95
+ value: JSON.stringify({ transcript: text })
96
+ }, '*');
97
+ }
98
+
99
+ // Function to load state from localStorage
100
+ function loadTranscriptState() {
101
+ return localStorage.getItem('voiceTranscript') || '';
102
+ }
103
 
104
  if ('webkitSpeechRecognition' in window) {
105
  recognition = new webkitSpeechRecognition();
106
  recognition.continuous = true;
107
  recognition.interimResults = true;
108
 
109
+ // Load any existing transcript
110
+ fullTranscript = loadTranscriptState();
111
+ document.getElementById('output').textContent = fullTranscript;
112
+
113
  recognition.onstart = () => {
114
+ document.getElementById('status').textContent = 'Listening...';
115
+ document.getElementById('startBtn').disabled = true;
116
+ document.getElementById('stopBtn').disabled = false;
117
  };
118
 
119
  recognition.onend = () => {
120
+ document.getElementById('status').textContent = 'Click Start to begin';
121
+ document.getElementById('startBtn').disabled = false;
122
+ document.getElementById('stopBtn').disabled = true;
123
  };
124
 
125
  recognition.onresult = (event) => {
126
+ let interimTranscript = '';
127
+ let finalTranscript = '';
128
 
129
  for (let i = event.resultIndex; i < event.results.length; i++) {
130
+ const transcript = event.results[i][0].transcript;
131
  if (event.results[i].isFinal) {
132
+ finalTranscript += transcript + ' ';
133
+ fullTranscript += transcript + ' ';
134
  } else {
135
+ interimTranscript += transcript;
136
  }
137
  }
138
 
139
+ if (finalTranscript) {
140
+ saveTranscriptState(fullTranscript);
 
 
 
 
141
  }
142
 
143
+ const output = document.getElementById('output');
144
+ output.textContent = fullTranscript + interimTranscript;
145
  output.scrollTop = output.scrollHeight;
146
  };
147
 
148
  recognition.onerror = (event) => {
149
+ document.getElementById('status').textContent = 'Error: ' + event.error;
150
+ document.getElementById('startBtn').disabled = false;
151
+ document.getElementById('stopBtn').disabled = true;
152
  };
153
 
154
  // Button handlers
155
+ document.getElementById('startBtn').onclick = () => {
156
  try {
157
  recognition.start();
158
  } catch (e) {
159
+ document.getElementById('status').textContent = 'Error starting: ' + e;
160
  }
161
  };
162
 
163
+ document.getElementById('stopBtn').onclick = () => recognition.stop();
164
 
165
+ document.getElementById('clearBtn').onclick = () => {
166
  fullTranscript = '';
167
+ document.getElementById('output').textContent = '';
168
+ saveTranscriptState('');
169
+ localStorage.removeItem('voiceTranscript');
 
 
170
  };
171
 
172
+ // Auto-start and load saved transcript on page load
173
+ window.addEventListener('load', () => {
174
+ const savedTranscript = loadTranscriptState();
175
+ if (savedTranscript) {
176
+ fullTranscript = savedTranscript;
177
+ document.getElementById('output').textContent = savedTranscript;
178
+ saveTranscriptState(savedTranscript);
179
+ }
180
+ setTimeout(() => document.getElementById('startBtn').click(), 1000);
181
  });
 
182
  } else {
183
+ document.getElementById('status').textContent = 'Speech recognition not supported in this browser';
184
+ document.getElementById('startBtn').disabled = true;
185
+ document.getElementById('stopBtn').disabled = true;
186
  }
187
  </script>
188
  </body>
 
202
  }
203
  )
204
 
205
+
206
+
207
+
208
+ # Load environment variables and initialize clients
209
  load_dotenv()
210
 
211
  # OpenAI setup
 
236
  if 'voice_transcript' not in st.session_state:
237
  st.session_state.voice_transcript = ""
238
 
239
+ # Processing Functions
240
  def process_with_gpt(text_input):
241
  """Process text with GPT-4."""
242
  if text_input:
 
289
  return response_text
290
 
291
  def perform_ai_lookup(query):
292
+ """Perform ArXiv search and analysis."""
293
  client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
294
  start_time = time.strftime("%Y-%m-%d %H:%M:%S")
295
 
 
305
  References = response1[0]
306
  ReferenceLinks = extract_urls(References)
307
 
 
308
  results = ''
309
+ response2 = client.predict(
310
+ query,
311
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
312
+ True,
313
+ api_name="/ask_llm"
314
+ )
315
+ if len(response2) > 10:
316
+ Answer = response2
317
+ results = Question + '\r\n' + Answer + '\r\n' + References + '\r\n' + ReferenceLinks
318
+ st.markdown(results)
 
319
 
320
  end_time = time.strftime("%Y-%m-%d %H:%M:%S")
321
  start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))
322
  end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))
323
  elapsed_seconds = end_timestamp - start_timestamp
324
 
325
+ st.write('🔍Run Complete')
326
  st.write(f"Start time: {start_time}")
327
  st.write(f"Finish time: {end_time}")
328
  st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds")
 
331
  create_file(filename, query, results)
332
  return results
333
 
 
334
  def main():
335
  st.sidebar.markdown("### 🚲BikeAI🏆 Claude and GPT Multi-Agent Research AI")
336
 
337
+ # Initialize cookie manager
338
+ cookie_manager = get_cookie_manager()
339
+
340
  tab_main = st.radio("Choose Action:",
341
  ["🎤 Voice Input", "💬 Chat", "📸 Media Gallery", "🔍 Search ArXiv", "📝 File Editor"],
342
  horizontal=True)
 
344
  if tab_main == "🎤 Voice Input":
345
  st.subheader("Voice Recognition")
346
 
347
+ # Initialize/restore state from cookie
348
+ saved_transcript = cookie_manager.get('voice_transcript')
349
+ if saved_transcript and not st.session_state.voice_transcript:
350
+ st.session_state.voice_transcript = saved_transcript
351
+
352
  # Display speech recognition component
353
  st.components.v1.html(speech_recognition_html, height=400)
354
 
355
+ # Handle transcript updates with JSON parsing
356
  transcript_receiver = st.components.v1.html("""
357
  <script>
358
  window.addEventListener('message', function(e) {
359
+ if (e.data && e.data.type === 'streamlit:setComponentValue') {
360
+ try {
361
+ const data = JSON.parse(e.data.value);
362
+ if (data.transcript !== undefined) {
363
+ window.Streamlit.setComponentValue(data);
364
+ }
365
+ } catch (err) {
366
+ console.error('Error parsing transcript data:', err);
367
+ }
368
  }
369
  });
370
  </script>
371
  """, height=0)
372
 
373
+ # Update state if we received new transcript
374
+ if transcript_receiver and isinstance(transcript_receiver, dict):
375
+ new_transcript = transcript_receiver.get('transcript', '')
376
+ update_transcript_state(new_transcript)
377
 
378
  # Display transcript
379
  st.markdown("### Processed Voice Input:")
380
+ transcript_text = st.text_area(
381
  "Voice Transcript",
382
  value=st.session_state.voice_transcript if isinstance(st.session_state.voice_transcript, str) else "",
383
+ height=100,
384
+ key=f"voice_transcript_display_{int(time.time())}" # Unique key to force refresh
385
  )
386
 
387
  # Process buttons
 
402
 
403
  with col3:
404
  if st.button("Clear Transcript"):
405
+ update_transcript_state("")
406
  st.experimental_rerun()
407
 
408
+ if st.session_state.voice_transcriptif st.session_state.voice_transcript:
409
  if st.button("Search ArXiv"):
410
  st.markdown("### ArXiv Search Results:")
411
  arxiv_results = perform_ai_lookup(st.session_state.voice_transcript)
 
446
  with st.spinner("Searching ArXiv..."):
447
  results = perform_ai_lookup(user_input)
448
  st.markdown(results)
449
+
450
+ # Display Chat History
451
+ st.subheader("Chat History 📜")
452
+ tab1, tab2 = st.tabs(["Claude History", "GPT-4 History"])
453
+
454
+ with tab1:
455
+ for chat in st.session_state.chat_history:
456
+ st.text_area("You:", chat["user"], height=100)
457
+ st.text_area("Claude:", chat["claude"], height=200)
458
+ st.markdown(chat["claude"])
459
+
460
+ with tab2:
461
+ for message in st.session_state.messages:
462
+ with st.chat_message(message["role"]):
463
+ st.markdown(message["content"])
464
 
465
  elif tab_main == "📸 Media Gallery":
466
  create_media_gallery()
 
484
  # Always show file manager in sidebar
485
  display_file_manager()
486
 
487
+ # Utility functions for file management
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
  def generate_filename(prompt, file_type):
489
  """Generate a filename based on prompt and time."""
490
  central = pytz.timezone('US/Central')
 
501
  def load_file(file_name):
502
  """Load file content."""
503
  with open(file_name, "r", encoding='utf-8') as file:
504
+ return file.read()
 
505
 
506
  def create_zip_of_files(files):
507
  """Create zip archive of files."""
 
548
 
549
  # Run the application
550
  if __name__ == "__main__":
551
+ main()
552
+
553
+
554
+