ar08 commited on
Commit
5053ef7
·
verified ·
1 Parent(s): 716173c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -53,8 +53,7 @@ def whisper_speech_to_text(audio):
53
  return None
54
  finally:
55
  # Clear CUDA cache
56
- #torch.cuda.empty_cache()
57
- print("1")
58
 
59
  async def chat_with_ai(message, history):
60
  global chat_history
@@ -109,21 +108,23 @@ with gr.Blocks() as demo:
109
  audio_button = gr.Button("Send Audio")
110
  text_button = gr.Button("Send Text")
111
 
112
- # JavaScript for handling space bar events for recording and playing output
113
- demo.load(_js="""
114
- document.addEventListener('keydown', function(event) {
115
- if (event.code === 'Space') {
116
- document.getElementById('audio_input').click();
117
- }
118
- });
119
-
120
- document.addEventListener('gradioAudioLoaded', function(event) {
121
- var audioElement = document.getElementById('audio_output');
122
- if (audioElement) {
123
- audioElement.play();
124
- }
125
- });
126
- """)
 
 
127
 
128
  audio_button.click(transcribe_and_chat, inputs=audio_input, outputs=[chat_output, audio_output])
129
  text_button.click(lambda x: asyncio.run(chat_with_ai(x, [])), inputs=text_input, outputs=[chat_output, audio_output])
 
53
  return None
54
  finally:
55
  # Clear CUDA cache
56
+ torch.cuda.empty_cache()
 
57
 
58
  async def chat_with_ai(message, history):
59
  global chat_history
 
108
  audio_button = gr.Button("Send Audio")
109
  text_button = gr.Button("Send Text")
110
 
111
+ # Add custom JavaScript to handle spacebar press and play audio automatically
112
+ demo.append(gr.HTML("""
113
+ <script>
114
+ document.addEventListener('keydown', function(event) {
115
+ if (event.code === 'Space') {
116
+ document.querySelector('input[type="file"]').click();
117
+ }
118
+ });
119
+
120
+ document.addEventListener('gradioAudioLoaded', function(event) {
121
+ var audioElement = document.querySelector('audio');
122
+ if (audioElement) {
123
+ audioElement.play();
124
+ }
125
+ });
126
+ </script>
127
+ """))
128
 
129
  audio_button.click(transcribe_and_chat, inputs=audio_input, outputs=[chat_output, audio_output])
130
  text_button.click(lambda x: asyncio.run(chat_with_ai(x, [])), inputs=text_input, outputs=[chat_output, audio_output])