gstaff commited on
Commit
e4e66ed
β€’
1 Parent(s): fd3511e

Update to gradio v4.

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +39 -31
  3. hotkeys.js +4 -5
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸͺπŸƒ
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 3.50.2
8
  app_file: app.py
9
  pinned: true
10
  ---
 
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.10.0
8
  app_file: app.py
9
  pinned: true
10
  ---
app.py CHANGED
@@ -64,6 +64,7 @@ def query(payload: dict):
64
  return response.json()
65
 
66
 
 
67
  def generate_text(code: str, prompt: str) -> (str, str, str):
68
  logger.info(f"Calling API with prompt:\n{prompt}")
69
  prompt = f"```python\n{code}```\nGiven the code above return only updated code for the following request:\n{prompt}\n<|assistant|>"
@@ -99,21 +100,24 @@ def add_hotkeys() -> str:
99
  return Path("hotkeys.js").read_text()
100
 
101
 
102
- def apply_query_params(gradio_code: str, stlite_code: str, request: gr.Request) -> (
103
- str, str, [str], str, str, [str], typing.Any):
104
  params = dict(request.query_params)
105
  demo_type = params.get('type')
106
  if demo_type == 'gradio':
107
- return params.get('code') or gradio_code, params.get('requirements') or '', [
108
- params.get('code') or gradio_code], stlite_code, '', [stlite_code_area], gr.Tabs(selected=0)
 
109
  if demo_type == 'streamlit':
110
- return gradio_code, '', [gradio_code], params.get('code') or stlite_code, params.get('requirements') or '', [
111
- params.get('code') or stlite_code], gr.Tabs(selected=1)
112
- return gradio_code, '', [gradio_code], stlite_code, '', [stlite_code], gr.Tabs(selected=0)
 
 
 
113
 
114
 
115
  def update_state(code: str, requirements: [str], error: str, history: [str], current_index: int) -> (
116
- str, str, [str], int):
117
  # Only modify undo history if new code was added.
118
  if history[current_index] != code:
119
  history = history[:current_index + 1]
@@ -151,8 +155,9 @@ with gr.Blocks(title="KiteWind") as demo:
151
  with gr.Row():
152
  with gr.Column():
153
  with gr.Group():
154
- gradio_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)",
155
- source='microphone', type='filepath', elem_classes=["record-btn"])
 
156
  gradio_prompt = gr.Textbox(label="Or type a text request and press Enter",
157
  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")
158
  gradio_bot_text = gr.TextArea(label="πŸ€– Chat Assistant Response")
@@ -160,9 +165,9 @@ with gr.Blocks(title="KiteWind") as demo:
160
  with gr.Column():
161
  gradio_code_area = gr.Code(
162
  label="App Code - You can also edit directly and then click Update App or ctrl + space",
163
- language='python', value=starting_app_code(DemoType.GRADIO))
164
  gradio_requirements_area = gr.Code(
165
- label="App Requirements (additional modules pip installed for pyodide)")
166
  with gr.Group():
167
  gradio_update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary",
168
  elem_classes=["update-btn"])
@@ -176,7 +181,7 @@ with gr.Blocks(title="KiteWind") as demo:
176
  gradio_history, gradio_index],
177
  'outputs': [gradio_requirements_area, gradio_error, gradio_history,
178
  gradio_index],
179
- '_js': update_iframe_js(DemoType.GRADIO)}
180
  gradio_gen_text_params = {'fn': generate_text, 'inputs': [gradio_code_area, gradio_prompt],
181
  'outputs': [gradio_bot_text, gradio_code_area]}
182
  gradio_transcribe_params = {'fn': transcribe, 'inputs': [gradio_audio],
@@ -194,13 +199,13 @@ with gr.Blocks(title="KiteWind") as demo:
194
  gr.Markdown("## 3. Export your app to share!")
195
  gradio_share_link_btn = gr.Button("πŸ”— Copy share link to clipboard")
196
  gradio_share_link_btn.click(link_copy_notify, [gradio_code_area, gradio_requirements_area], None,
197
- _js=copy_share_link_js(DemoType.GRADIO))
198
  gradio_copy_snippet_btn = gr.Button("βœ‚οΈ Copy app snippet to paste into another page")
199
  gradio_copy_snippet_btn.click(copy_notify, [gradio_code_area, gradio_requirements_area], None,
200
- _js=copy_snippet_js(DemoType.GRADIO))
201
  gradio_download_btn = gr.Button("πŸ—Ž Download app as a standalone file")
202
  gradio_download_btn.click(None, [gradio_code_area, gradio_requirements_area], None,
203
- _js=download_code_js(DemoType.GRADIO))
204
  with gr.Row():
205
  with gr.Column():
206
  gr.Markdown("## Current limitations")
@@ -216,8 +221,9 @@ with gr.Blocks(title="KiteWind") as demo:
216
  with gr.Row():
217
  with gr.Column():
218
  with gr.Group():
219
- stlite_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)",
220
- source='microphone', type='filepath', elem_classes=["record-btn"])
 
221
  stlite_prompt = gr.Textbox(label="Or type a text request and press Enter",
222
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Change the theme to soft")
223
  stlite_bot_text = gr.TextArea(label="πŸ€– Chat Assistant Response")
@@ -225,9 +231,9 @@ with gr.Blocks(title="KiteWind") as demo:
225
  with gr.Column():
226
  stlite_code_area = gr.Code(
227
  label="App Code - You can also edit directly and then click Update App or ctrl + space",
228
- language='python', value=starting_app_code(DemoType.STREAMLIT))
229
  stlite_requirements_area = gr.Code(
230
- label="App Requirements (additional modules pip installed for pyodide)")
231
  with gr.Group():
232
  stlite_update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary",
233
  elem_classes=["update-btn"])
@@ -241,7 +247,7 @@ with gr.Blocks(title="KiteWind") as demo:
241
  stlite_history, stlite_index],
242
  'outputs': [stlite_requirements_area, stlite_error, stlite_history,
243
  stlite_index],
244
- '_js': update_iframe_js(DemoType.STREAMLIT)}
245
  stlite_gen_text_params = {'fn': generate_text, 'inputs': [stlite_code_area, stlite_prompt],
246
  'outputs': [stlite_bot_text, stlite_code_area]}
247
  stlite_transcribe_params = {'fn': transcribe, 'inputs': [stlite_audio],
@@ -259,13 +265,13 @@ with gr.Blocks(title="KiteWind") as demo:
259
  gr.Markdown("## 3. Export your app to share!")
260
  stlite_share_link_btn = gr.Button("πŸ”— Copy share link to clipboard")
261
  stlite_share_link_btn.click(link_copy_notify, [stlite_code_area, stlite_requirements_area], None,
262
- _js=copy_share_link_js(DemoType.STREAMLIT))
263
  stlite_copy_snippet_btn = gr.Button("βœ‚οΈ Copy app snippet into paste in another page")
264
  stlite_copy_snippet_btn.click(copy_notify, [stlite_code_area, stlite_requirements_area], None,
265
- _js=copy_snippet_js(DemoType.STREAMLIT))
266
  stlite_download_btn = gr.Button("πŸ—Ž Download app as a standalone file")
267
  stlite_download_btn.click(None, [stlite_code_area, stlite_requirements_area], None,
268
- _js=download_code_js(DemoType.STREAMLIT))
269
  with gr.Row():
270
  with gr.Column():
271
  gr.Markdown("## Current limitations")
@@ -273,13 +279,15 @@ with gr.Blocks(title="KiteWind") as demo:
273
  gr.Markdown(
274
  "- Only Streamlit apps using libraries available in pyodide are supported\n- The chat hasn't been tuned on Streamlit library data; it may make mistakes")
275
  gradio_lite_tab.select(lambda: "gradio-lite", None, selectedTab).then(None, None, None,
276
- _js=load_js(DemoType.GRADIO))
277
- stlite_tab.select(lambda: "stlite", None, selectedTab).then(None, None, None, _js=load_js(DemoType.STREAMLIT))
278
- demo.load(None, None, None, _js=add_hotkeys())
279
- demo.load(apply_query_params, [gradio_code_area, stlite_code_area],
280
- [gradio_code_area, gradio_requirements_area, gradio_history, stlite_code_area, stlite_requirements_area,
281
- stlite_history, tabs])
 
 
282
  demo.css = "footer {visibility: hidden}"
283
 
284
  if __name__ == "__main__":
285
- demo.queue().launch(favicon_path='favicon-96x96.png')
 
64
  return response.json()
65
 
66
 
67
+ # TODO: Use chat template with more instructions
68
  def generate_text(code: str, prompt: str) -> (str, str, str):
69
  logger.info(f"Calling API with prompt:\n{prompt}")
70
  prompt = f"```python\n{code}```\nGiven the code above return only updated code for the following request:\n{prompt}\n<|assistant|>"
 
100
  return Path("hotkeys.js").read_text()
101
 
102
 
103
+ def apply_query_params(request: gr.Request) -> ([str], str, [str], str, typing.Any):
 
104
  params = dict(request.query_params)
105
  demo_type = params.get('type')
106
  if demo_type == 'gradio':
107
+ return [params.get('code') or starting_app_code(DemoType.GRADIO)], params.get('requirements') or '', [
108
+ starting_app_code(
109
+ DemoType.STREAMLIT)], '', gr.Tabs(selected=0)
110
  if demo_type == 'streamlit':
111
+ return [starting_app_code(DemoType.GRADIO)], '', [
112
+ params.get('code') or starting_app_code(DemoType.STREAMLIT)], params.get('requirements') or '', gr.Tabs(
113
+ selected=1)
114
+ return [params.get('code') or starting_app_code(DemoType.GRADIO)], params.get('requirements') or '', [
115
+ starting_app_code(DemoType.STREAMLIT)], '', gr.Tabs(
116
+ selected=0)
117
 
118
 
119
  def update_state(code: str, requirements: [str], error: str, history: [str], current_index: int) -> (
120
+ str, str, [str], int):
121
  # Only modify undo history if new code was added.
122
  if history[current_index] != code:
123
  history = history[:current_index + 1]
 
155
  with gr.Row():
156
  with gr.Column():
157
  with gr.Group():
158
+ gradio_audio = gr.Microphone(
159
+ label="Record a voice request (click or press ctrl + ` to start/stop)",
160
+ type='filepath', elem_classes=["record-btn"])
161
  gradio_prompt = gr.Textbox(label="Or type a text request and press Enter",
162
  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")
163
  gradio_bot_text = gr.TextArea(label="πŸ€– Chat Assistant Response")
 
165
  with gr.Column():
166
  gradio_code_area = gr.Code(
167
  label="App Code - You can also edit directly and then click Update App or ctrl + space",
168
+ language='python', value=starting_app_code(DemoType.GRADIO), interactive=True)
169
  gradio_requirements_area = gr.Code(
170
+ label="App Requirements (additional modules pip installed for pyodide)", interactive=True)
171
  with gr.Group():
172
  gradio_update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary",
173
  elem_classes=["update-btn"])
 
181
  gradio_history, gradio_index],
182
  'outputs': [gradio_requirements_area, gradio_error, gradio_history,
183
  gradio_index],
184
+ 'js': update_iframe_js(DemoType.GRADIO)}
185
  gradio_gen_text_params = {'fn': generate_text, 'inputs': [gradio_code_area, gradio_prompt],
186
  'outputs': [gradio_bot_text, gradio_code_area]}
187
  gradio_transcribe_params = {'fn': transcribe, 'inputs': [gradio_audio],
 
199
  gr.Markdown("## 3. Export your app to share!")
200
  gradio_share_link_btn = gr.Button("πŸ”— Copy share link to clipboard")
201
  gradio_share_link_btn.click(link_copy_notify, [gradio_code_area, gradio_requirements_area], None,
202
+ js=copy_share_link_js(DemoType.GRADIO))
203
  gradio_copy_snippet_btn = gr.Button("βœ‚οΈ Copy app snippet to paste into another page")
204
  gradio_copy_snippet_btn.click(copy_notify, [gradio_code_area, gradio_requirements_area], None,
205
+ js=copy_snippet_js(DemoType.GRADIO))
206
  gradio_download_btn = gr.Button("πŸ—Ž Download app as a standalone file")
207
  gradio_download_btn.click(None, [gradio_code_area, gradio_requirements_area], None,
208
+ js=download_code_js(DemoType.GRADIO))
209
  with gr.Row():
210
  with gr.Column():
211
  gr.Markdown("## Current limitations")
 
221
  with gr.Row():
222
  with gr.Column():
223
  with gr.Group():
224
+ stlite_audio = gr.Microphone(
225
+ label="Record a voice request (click or press ctrl + ` to start/stop)",
226
+ type='filepath', elem_classes=["record-btn"])
227
  stlite_prompt = gr.Textbox(label="Or type a text request and press Enter",
228
  placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Change the theme to soft")
229
  stlite_bot_text = gr.TextArea(label="πŸ€– Chat Assistant Response")
 
231
  with gr.Column():
232
  stlite_code_area = gr.Code(
233
  label="App Code - You can also edit directly and then click Update App or ctrl + space",
234
+ language='python', value=starting_app_code(DemoType.STREAMLIT), interactive=True)
235
  stlite_requirements_area = gr.Code(
236
+ label="App Requirements (additional modules pip installed for pyodide)", interactive=True)
237
  with gr.Group():
238
  stlite_update_btn = gr.Button("Update App (Ctrl + Space)", variant="primary",
239
  elem_classes=["update-btn"])
 
247
  stlite_history, stlite_index],
248
  'outputs': [stlite_requirements_area, stlite_error, stlite_history,
249
  stlite_index],
250
+ 'js': update_iframe_js(DemoType.STREAMLIT)}
251
  stlite_gen_text_params = {'fn': generate_text, 'inputs': [stlite_code_area, stlite_prompt],
252
  'outputs': [stlite_bot_text, stlite_code_area]}
253
  stlite_transcribe_params = {'fn': transcribe, 'inputs': [stlite_audio],
 
265
  gr.Markdown("## 3. Export your app to share!")
266
  stlite_share_link_btn = gr.Button("πŸ”— Copy share link to clipboard")
267
  stlite_share_link_btn.click(link_copy_notify, [stlite_code_area, stlite_requirements_area], None,
268
+ js=copy_share_link_js(DemoType.STREAMLIT))
269
  stlite_copy_snippet_btn = gr.Button("βœ‚οΈ Copy app snippet into paste in another page")
270
  stlite_copy_snippet_btn.click(copy_notify, [stlite_code_area, stlite_requirements_area], None,
271
+ js=copy_snippet_js(DemoType.STREAMLIT))
272
  stlite_download_btn = gr.Button("πŸ—Ž Download app as a standalone file")
273
  stlite_download_btn.click(None, [stlite_code_area, stlite_requirements_area], None,
274
+ js=download_code_js(DemoType.STREAMLIT))
275
  with gr.Row():
276
  with gr.Column():
277
  gr.Markdown("## Current limitations")
 
279
  gr.Markdown(
280
  "- Only Streamlit apps using libraries available in pyodide are supported\n- The chat hasn't been tuned on Streamlit library data; it may make mistakes")
281
  gradio_lite_tab.select(lambda: "gradio-lite", None, selectedTab).then(None, None, None,
282
+ js=load_js(DemoType.GRADIO))
283
+ stlite_tab.select(lambda: "stlite", None, selectedTab).then(None, None, None, js=load_js(DemoType.STREAMLIT))
284
+ demo.load(None, None, None, js=add_hotkeys())
285
+ demo.load(apply_query_params, [],
286
+ [gradio_history, gradio_requirements_area, stlite_history, stlite_requirements_area, tabs]).then(
287
+ lambda x, y: [x[0], y[0]],
288
+ [gradio_history, stlite_history],
289
+ [gradio_code_area, stlite_code_area])
290
  demo.css = "footer {visibility: hidden}"
291
 
292
  if __name__ == "__main__":
293
+ demo.queue().launch(favicon_path='favicon-96x96.png', show_api=False)
hotkeys.js CHANGED
@@ -12,11 +12,10 @@
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
- for (let recordButton of recordButtons) {
17
- if (recordButton.checkVisibility()) {
18
- recordButton.click();
19
- }
20
  }
21
  }
22
  });
 
12
  }
13
  window.addEventListener('keydown', (e) => {
14
  if ((e.ctrlKey || e.metaKey) && e.key == "`") { // CTRL + ` key
15
+ const recordButtons = [...gradioApp().querySelectorAll('button.record.record-button')].filter(x => x.checkVisibility());
16
+ const stopButtons = [...gradioApp().querySelectorAll('button.stop-button')].filter(x => x.checkVisibility());
17
+ for (let button of recordButtons.concat(stopButtons)) {
18
+ button.click();
 
19
  }
20
  }
21
  });