Spaces:
Runtime error
Runtime error
File size: 4,513 Bytes
4a11f4c | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import gradio as gr
import time
def greet(name, greeting_style, emoji_enabled):
"""Generate a personalized greeting based on user input."""
if not name.strip():
return "Please enter your name!"
greetings = {
"Formal": f"Good day, {name}! It's a pleasure to meet you.",
"Casual": f"Hey {name}! What's up?",
"Friendly": f"Hello there, {name}! Great to see you!",
"Professional": f"Dear {name}, welcome to our application.",
"Enthusiastic": f"Wow, {name}! So excited to have you here!"
}
base_greeting = greetings.get(greeting_style, f"Hello, {name}!")
if emoji_enabled:
emojis = ["π", "π", "π", "β¨", "π", "π«"]
emoji = time.time() % len(emojis)
base_greeting += f" {emojis[int(emoji)]}"
return base_greeting
def get_current_time():
"""Get current time for display."""
return time.strftime("%H:%M:%S")
# Create the Gradio 6 application
with gr.Blocks(
title="Greeting App",
theme=gr.themes.Soft(),
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) as demo:
# Header with branding
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1>π Welcome to the Greeting App!</h1>
<p style="color: #666;">Create personalized greetings with different styles</p>
<p><a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007bff; text-decoration: underline;">Built with anycoder</a></p>
</div>
""")
with gr.Row():
with gr.Column(scale=2):
# Input section
name_input = gr.Textbox(
label="Your Name",
placeholder="Enter your name here...",
autofocus=True,
lines=1
)
style_radio = gr.Radio(
choices=["Formal", "Casual", "Friendly", "Professional", "Enthusiastic"],
value="Friendly",
label="Greeting Style",
info="Choose how you'd like to be greeted"
)
emoji_checkbox = gr.Checkbox(
label="Add Emoji",
value=True,
info="Include a fun emoji with your greeting"
)
# Buttons
with gr.Row():
greet_btn = gr.Button("Generate Greeting", variant="primary", size="lg")
clear_btn = gr.Button("Clear", variant="secondary")
with gr.Column(scale=3):
# Output section
output_text = gr.Textbox(
label="Your Personalized Greeting",
lines=3,
interactive=False,
show_copy_button=True
)
# Current time display
time_display = gr.Textbox(
label="Current Time",
value=get_current_time(),
interactive=False,
every=1
)
# Example section
gr.Examples(
examples=[
["Alice", "Casual", True],
["Bob", "Professional", False],
["Charlie", "Enthusiastic", True],
["Diana", "Formal", False]
],
inputs=[name_input, style_radio, emoji_checkbox],
label="Try these examples:",
examples_per_page=4
)
# Event listeners with Gradio 6 syntax
greet_btn.click(
fn=greet,
inputs=[name_input, style_radio, emoji_checkbox],
outputs=output_text,
api_visibility="public"
)
# Also trigger on Enter key for name input
name_input.submit(
fn=greet,
inputs=[name_input, style_radio, emoji_checkbox],
outputs=output_text,
api_visibility="public"
)
# Clear button functionality
clear_btn.click(
fn=lambda: ["", "Friendly", True, ""],
outputs=[name_input, style_radio, emoji_checkbox, output_text],
api_visibility="private"
)
# Footer info
gr.HTML("""
<div style="text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee;">
<p style="color: #888; font-size: 0.9em;">
π‘ Tip: Try different greeting styles and toggle emojis for fun variations!
</p>
</div>
""")
# Launch the application
if __name__ == "__main__":
demo.launch() |