gstaff commited on
Commit
94421aa
β€’
1 Parent(s): b676f87

Fix hotkey events across tabs.

Browse files
Files changed (2) hide show
  1. app.py +8 -27
  2. hotkeys.js +34 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import logging
2
  import os
3
  import re
4
  import warnings
 
5
 
6
  import gradio as gr
7
  import requests
@@ -89,27 +90,7 @@ def copy_notify(code):
89
 
90
 
91
  def add_hotkeys() -> str:
92
- return """() => {
93
- function gradioApp() {
94
- const elems = document.getElementsByTagName('gradio-app');
95
- const elem = elems.length == 0 ? document : elems[0];
96
-
97
- if (elem !== document) {
98
- elem.getElementById = function(id) {
99
- return document.getElementById(id);
100
- };
101
- }
102
- return elem.shadowRoot ? elem.shadowRoot : elem;
103
- }
104
- window.addEventListener('keydown', (e) => {
105
- if (e.keyCode == 192 && e.ctrlKey) { // CTRL + ` key
106
- const recordButton = gradioApp().querySelector("div.mic-wrap > button");
107
- console.log(recordButton);
108
- recordButton.click();
109
- }
110
- });
111
- }
112
- """
113
 
114
 
115
  with gr.Blocks() as demo:
@@ -126,15 +107,15 @@ with gr.Blocks() as demo:
126
  with gr.Row():
127
  with gr.Column():
128
  with gr.Group():
129
- in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
130
  in_prompt = gr.Textbox(label="Or type a text request and press Enter",
131
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Put the reversed name output into a separate textbox")
132
  out_text = gr.TextArea(label="Chat Assistant Response")
133
  clear = gr.ClearButton([in_prompt, in_audio, out_text])
134
  with gr.Column():
135
- code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
136
  language='python', value=starting_app_code(DemoType.GRADIO))
137
- update_btn = gr.Button("Update App", variant="primary")
138
  code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
139
  '_js': update_iframe_js(DemoType.GRADIO)}
140
  gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
@@ -165,15 +146,15 @@ with gr.Blocks() as demo:
165
  with gr.Row():
166
  with gr.Column():
167
  with gr.Group():
168
- in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
169
  in_prompt = gr.Textbox(label="Or type a text request and press Enter",
170
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Make the button primary")
171
  out_text = gr.TextArea(label="Chat Assistant Response")
172
  clear_btn = gr.ClearButton([in_prompt, in_audio, out_text])
173
  with gr.Column():
174
- code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
175
  language='python', value=starting_app_code(DemoType.STREAMLIT))
176
- update_btn = gr.Button("Update App", variant="primary")
177
  code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
178
  '_js': update_iframe_js(DemoType.STREAMLIT)}
179
  gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
 
2
  import os
3
  import re
4
  import warnings
5
+ from pathlib import Path
6
 
7
  import gradio as gr
8
  import requests
 
90
 
91
 
92
  def add_hotkeys() -> str:
93
+ return Path("hotkeys.js").read_text()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
 
96
  with gr.Blocks() as demo:
 
107
  with gr.Row():
108
  with gr.Column():
109
  with gr.Group():
110
+ in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath', elem_classes=["record-btn"])
111
  in_prompt = gr.Textbox(label="Or type a text request and press Enter",
112
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Put the reversed name output into a separate textbox")
113
  out_text = gr.TextArea(label="Chat Assistant Response")
114
  clear = gr.ClearButton([in_prompt, in_audio, out_text])
115
  with gr.Column():
116
+ code_area = gr.Code(label="App Code - You can also edit directly and then click Update App or ctrl + space",
117
  language='python', value=starting_app_code(DemoType.GRADIO))
118
+ update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary", elem_classes=["update-btn"])
119
  code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
120
  '_js': update_iframe_js(DemoType.GRADIO)}
121
  gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
 
146
  with gr.Row():
147
  with gr.Column():
148
  with gr.Group():
149
+ in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath', elem_classes=["record-btn"])
150
  in_prompt = gr.Textbox(label="Or type a text request and press Enter",
151
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Make the button primary")
152
  out_text = gr.TextArea(label="Chat Assistant Response")
153
  clear_btn = gr.ClearButton([in_prompt, in_audio, out_text])
154
  with gr.Column():
155
+ code_area = gr.Code(label="App Code - You can also edit directly and then click Update App or ctrl + space",
156
  language='python', value=starting_app_code(DemoType.STREAMLIT))
157
+ update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary", elem_classes=["update-btn"])
158
  code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
159
  '_js': update_iframe_js(DemoType.STREAMLIT)}
160
  gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
hotkeys.js ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ () => {
2
+ function gradioApp() {
3
+ const elems = document.getElementsByTagName('gradio-app');
4
+ const elem = elems.length == 0 ? document : elems[0];
5
+
6
+ if (elem !== document) {
7
+ elem.getElementById = function(id) {
8
+ return document.getElementById(id);
9
+ };
10
+ }
11
+ return elem.shadowRoot ? elem.shadowRoot : elem;
12
+ }
13
+ window.addEventListener('keydown', (e) => {
14
+ if ((e.ctrlKey || e.metaKey) && e.key == "`") { // CTRL + ` key
15
+ const recordButtons = gradioApp().querySelectorAll(".record-btn button");
16
+ console.log(recordButtons);
17
+ for (let recordButton of recordButtons) {
18
+ if (recordButton.checkVisibility()) {
19
+ recordButton.click();
20
+ }
21
+ }
22
+ }
23
+ });
24
+ window.addEventListener('keydown', (e) => {
25
+ if ((e.ctrlKey || e.metaKey) && e.key === " ") { // CTRL + Space key
26
+ const updateButtons = gradioApp().querySelectorAll(".update-btn");
27
+ for (let updateButton of updateButtons) {
28
+ if (updateButton.checkVisibility()) {
29
+ updateButton.click();
30
+ }
31
+ }
32
+ }
33
+ });
34
+ }