Spaces:
Build error
Build error
File size: 3,510 Bytes
f3ff4f1 eb16685 f3ff4f1 eb16685 f3ff4f1 eb16685 10198a8 eb16685 10198a8 eb16685 10198a8 f3ff4f1 eb16685 f3ff4f1 eb16685 10198a8 eb16685 10198a8 eb16685 10198a8 eb16685 10198a8 eb16685 10198a8 eb16685 10198a8 4c99c56 eb16685 f3ff4f1 10198a8 eb16685 10198a8 eb16685 10198a8 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import gradio as gr
def greet(name: str, enthusiasm: int = 1) -> str:
"""
Enhanced greeting function with customizable enthusiasm.
"""
if not name.strip():
return "Please enter a name!"
greeting = f"Hello, {name}!"
exclamation = "!" * enthusiasm
return f"{greeting}{exclamation} Welcome to your Gradio 6 application."
# Version 1: Enhanced with Examples and Validation
with gr.Blocks() as demo:
gr.HTML(
"""
<div style="text-align: center; margin-bottom: 20px;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #007bff; font-weight: bold;">
Built with anycoder
</a>
</div>
"""
)
gr.Markdown("# 🚀 Enhanced Gradio 6 App")
gr.Markdown("Enter your name and choose enthusiasm level for a personalized greeting.")
with gr.Row():
with gr.Column():
name_input = gr.Textbox(
label="Your Name",
placeholder="Type your name here...",
autofocus=True,
max_length=50
)
enthusiasm_slider = gr.Slider(
label="Enthusiasm Level",
minimum=1,
maximum=5,
value=1,
step=1,
info="How excited should the greeting be?"
)
submit_btn = gr.Button("Generate Greeting", variant="primary", size="lg")
# Add examples
examples = gr.Examples(
examples=[
["Alice", 3],
["Bob", 1],
["Charlie", 5]
],
inputs=[name_input, enthusiasm_slider],
label="Quick Examples"
)
with gr.Column():
greeting_output = gr.Textbox(
label="Output",
interactive=False,
lines=2,
show_copy_button=True
)
# Add a stats component
char_count = gr.Number(label="Character Count", interactive=False)
# Event listeners with Gradio 6 syntax
def process_greeting(name, enthusiasm):
result = greet(name, enthusiasm)
return result, len(result)
submit_btn.click(
fn=process_greeting,
inputs=[name_input, enthusiasm_slider],
outputs=[greeting_output, char_count],
api_visibility="public"
)
# Real-time updates
name_input.change(
fn=process_greeting,
inputs=[name_input, enthusiasm_slider],
outputs=[greeting_output, char_count],
api_visibility="private"
)
enthusiasm_slider.change(
fn=process_greeting,
inputs=[name_input, enthusiasm_slider],
outputs=[greeting_output, char_count],
api_visibility="private"
)
demo.launch(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="cyan",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700"
),
footer_links=[
{"label": "Gradio", "url": "https://gradio.app"},
{"label": "GitHub", "url": "https://github.com/gradio-app/gradio"}
],
show_error=True
) |