LPX55 commited on
Commit
56e596b
·
1 Parent(s): 4cc700d

Refactor space loading functionality in space_loader_tab.py to streamline the selection and display of popular Hugging Face Spaces. Replace dropdown and button with a dynamic interface that shows the selected space directly, enhancing user experience and code clarity.

Browse files
Files changed (1) hide show
  1. space_loader_tab.py +10 -22
space_loader_tab.py CHANGED
@@ -2,35 +2,23 @@ import gradio as gr
2
 
3
  POPULAR_SPACES = [
4
  "gradio/calculator",
5
- "gradio/image-classification",
6
  "huggingface-projects/llama-2-7b-chat",
7
- "gradio/englishtranslator",
8
- "gradio/blocks-gallery"
9
  ]
10
 
11
  def gr_load_tab():
12
  gr.Markdown("## Load a Popular Hugging Face Space (gr.load)")
13
- space_dropdown = gr.Dropdown(POPULAR_SPACES, label="Popular Spaces", interactive=True)
14
- load_btn = gr.Button("Load Space")
15
- error_box = gr.Markdown(visible=False)
16
- with gr.Column():
17
- gr.Markdown("### gr.load() Interface")
18
- loaded_interface = gr.Blocks()
19
 
20
- def load_space(space):
21
- if not space or "/" not in space:
22
- return gr.update(visible=True, value="**Error:** Please select a valid space name."), None
23
- try:
24
- interface = gr.load(space, src="spaces")
25
- except Exception as e:
26
- return gr.update(visible=True, value=f"**Error loading space:** {e}"), None
27
- return gr.update(visible=False), interface
28
 
29
- load_btn.click(
30
- fn=load_space,
31
- inputs=space_dropdown,
32
- outputs=[error_box, loaded_interface]
33
- )
34
 
35
  def iframe_loader_tab():
36
  gr.Markdown("## Load Any Hugging Face Space (iframe)")
 
2
 
3
  POPULAR_SPACES = [
4
  "gradio/calculator",
 
5
  "huggingface-projects/llama-2-7b-chat",
 
 
6
  ]
7
 
8
  def gr_load_tab():
9
  gr.Markdown("## Load a Popular Hugging Face Space (gr.load)")
10
+ dropdown = gr.Dropdown(POPULAR_SPACES, label="Popular Spaces", value=POPULAR_SPACES[0])
11
+ space_blocks = []
12
+ for space in POPULAR_SPACES:
13
+ with gr.Column(visible=(space == POPULAR_SPACES[0])) as col:
14
+ gr.load(space, src="spaces")
15
+ space_blocks.append(col)
16
 
17
+ def show_space(selected):
18
+ return [gr.Column(visible=(selected == space)) for space in POPULAR_SPACES]
19
+
20
+ dropdown.change(fn=show_space, inputs=dropdown, outputs=space_blocks)
 
 
 
 
21
 
 
 
 
 
 
22
 
23
  def iframe_loader_tab():
24
  gr.Markdown("## Load Any Hugging Face Space (iframe)")