Spaces:
Runtime error
Runtime error
File size: 2,347 Bytes
4cc700d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import gradio as gr
POPULAR_SPACES = [
"gradio/calculator",
"gradio/image-classification",
"huggingface-projects/llama-2-7b-chat",
"gradio/englishtranslator",
"gradio/blocks-gallery"
]
def gr_load_tab():
gr.Markdown("## Load a Popular Hugging Face Space (gr.load)")
space_dropdown = gr.Dropdown(POPULAR_SPACES, label="Popular Spaces", interactive=True)
load_btn = gr.Button("Load Space")
error_box = gr.Markdown(visible=False)
with gr.Column():
gr.Markdown("### gr.load() Interface")
loaded_interface = gr.Blocks()
def load_space(space):
if not space or "/" not in space:
return gr.update(visible=True, value="**Error:** Please select a valid space name."), None
try:
interface = gr.load(space, src="spaces")
except Exception as e:
return gr.update(visible=True, value=f"**Error loading space:** {e}"), None
return gr.update(visible=False), interface
load_btn.click(
fn=load_space,
inputs=space_dropdown,
outputs=[error_box, loaded_interface]
)
def iframe_loader_tab():
gr.Markdown("## Load Any Hugging Face Space (iframe)")
with gr.Row():
space_name = gr.Textbox(label="Space Name", placeholder="e.g. gradio/calculator")
space_dropdown = gr.Dropdown(POPULAR_SPACES, label="Popular Spaces", interactive=True)
load_btn = gr.Button("Load Space")
error_box = gr.Markdown(visible=False)
with gr.Column():
gr.Markdown("### Space IFrame")
iframe_html = gr.HTML("")
def get_space_name(text, dropdown):
return text if text else dropdown
def load_space(space):
if not space or "/" not in space:
return gr.update(visible=True, value="**Error:** Please enter a valid space name (e.g. gradio/calculator)."), ""
space_id = space.replace("/", "-")
iframe_url = f"https://{space_id}.hf.space?__theme=dark"
iframe = f'<iframe src="{iframe_url}" width="100%" height="600" style="border:none;"></iframe>'
return gr.update(visible=False), iframe
load_btn.click(
fn=get_space_name,
inputs=[space_name, space_dropdown],
outputs=space_name
).then(
fn=load_space,
inputs=space_name,
outputs=[error_box, iframe_html]
) |