Spaces:
Runtime error
Runtime error
| 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() |