awacke1 commited on
Commit
e7880f0
·
verified ·
1 Parent(s): 7236ce8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -0
app.py CHANGED
@@ -405,6 +405,170 @@ async def perform_ai_lookup(query, vocal_summary=True, extended_refs=False, titl
405
  if audio_file:
406
  st.audio(audio_file)
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  # Delete all user files function
409
  def delete_user_files():
410
  protected_files = {'app.py', 'requirements.txt', 'README.md'}
 
405
  if audio_file:
406
  st.audio(audio_file)
407
 
408
+ # ASR Component HTML
409
+ ASR_HTML = """
410
+ <html>
411
+ <head>
412
+ <title>Continuous Speech Demo</title>
413
+ <style>
414
+ body {
415
+ font-family: sans-serif;
416
+ padding: 20px;
417
+ max-width: 800px;
418
+ margin: 0 auto;
419
+ }
420
+ button {
421
+ padding: 10px 20px;
422
+ margin: 10px 5px;
423
+ font-size: 16px;
424
+ }
425
+ #status {
426
+ margin: 10px 0;
427
+ padding: 10px;
428
+ background: #e8f5e9;
429
+ border-radius: 4px;
430
+ }
431
+ #output {
432
+ white-space: pre-wrap;
433
+ padding: 15px;
434
+ background: #f5f5f5;
435
+ border-radius: 4px;
436
+ margin: 10px 0;
437
+ min-height: 100px;
438
+ max-height: 400px;
439
+ overflow-y: auto;
440
+ }
441
+ .controls {
442
+ margin: 10px 0;
443
+ }
444
+ </style>
445
+ </head>
446
+ <body>
447
+ <div class="controls">
448
+ <button id="start">Start Listening</button>
449
+ <button id="stop" disabled>Stop Listening</button>
450
+ <button id="clear">Clear Text</button>
451
+ </div>
452
+ <div id="status">Ready</div>
453
+ <div id="output"></div>
454
+
455
+ <script>
456
+ if (!('webkitSpeechRecognition' in window)) {
457
+ alert('Speech recognition not supported');
458
+ } else {
459
+ const recognition = new webkitSpeechRecognition();
460
+ const startButton = document.getElementById('start');
461
+ const stopButton = document.getElementById('stop');
462
+ const clearButton = document.getElementById('clear');
463
+ const status = document.getElementById('status');
464
+ const output = document.getElementById('output');
465
+ let fullTranscript = '';
466
+ let lastUpdateTime = Date.now();
467
+
468
+ recognition.continuous = true;
469
+ recognition.interimResults = true;
470
+
471
+ const startRecognition = () => {
472
+ try {
473
+ recognition.start();
474
+ status.textContent = 'Listening...';
475
+ startButton.disabled = true;
476
+ stopButton.disabled = false;
477
+ } catch (e) {
478
+ console.error(e);
479
+ status.textContent = 'Error: ' + e.message;
480
+ }
481
+ };
482
+
483
+ window.addEventListener('load', () => {
484
+ setTimeout(startRecognition, 1000);
485
+ });
486
+
487
+ startButton.onclick = startRecognition;
488
+
489
+ stopButton.onclick = () => {
490
+ recognition.stop();
491
+ status.textContent = 'Stopped';
492
+ startButton.disabled = false;
493
+ stopButton.disabled = true;
494
+ };
495
+
496
+ clearButton.onclick = () => {
497
+ fullTranscript = '';
498
+ output.textContent = '';
499
+ sendDataToPython({value: '', dataType: "json"});
500
+ };
501
+
502
+ recognition.onresult = (event) => {
503
+ let interimTranscript = '';
504
+ let finalTranscript = '';
505
+
506
+ for (let i = event.resultIndex; i < event.results.length; i++) {
507
+ const transcript = event.results[i][0].transcript;
508
+ if (event.results[i].isFinal) {
509
+ finalTranscript += transcript + '\\n';
510
+ } else {
511
+ interimTranscript += transcript;
512
+ }
513
+ }
514
+
515
+ if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
516
+ if (finalTranscript) {
517
+ fullTranscript += finalTranscript;
518
+ }
519
+ lastUpdateTime = Date.now();
520
+ output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
521
+ output.scrollTop = output.scrollHeight;
522
+ sendDataToPython({value: fullTranscript, dataType: "json"});
523
+ }
524
+ };
525
+
526
+ recognition.onend = () => {
527
+ if (!stopButton.disabled) {
528
+ try {
529
+ recognition.start();
530
+ console.log('Restarted recognition');
531
+ } catch (e) {
532
+ console.error('Failed to restart recognition:', e);
533
+ status.textContent = 'Error restarting: ' + e.message;
534
+ startButton.disabled = false;
535
+ stopButton.disabled = true;
536
+ }
537
+ }
538
+ };
539
+
540
+ recognition.onerror = (event) => {
541
+ console.error('Recognition error:', event.error);
542
+ status.textContent = 'Error: ' + event.error;
543
+ if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
544
+ startButton.disabled = false;
545
+ stopButton.disabled = true;
546
+ }
547
+ };
548
+ }
549
+
550
+ function sendDataToPython(data) {
551
+ window.parent.postMessage({
552
+ isStreamlitMessage: true,
553
+ type: "streamlit:setComponentValue",
554
+ ...data
555
+ }, "*");
556
+ }
557
+
558
+ window.addEventListener('load', function() {
559
+ window.setTimeout(function() {
560
+ window.parent.postMessage({
561
+ isStreamlitMessage: true,
562
+ type: "streamlit:setFrameHeight",
563
+ height: document.documentElement.clientHeight
564
+ }, "*");
565
+ }, 0);
566
+ });
567
+ </script>
568
+ </body>
569
+ </html>
570
+ """
571
+
572
  # Delete all user files function
573
  def delete_user_files():
574
  protected_files = {'app.py', 'requirements.txt', 'README.md'}