Stable-X commited on
Commit
eb16685
·
verified ·
1 Parent(s): 10198a8

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +67 -21
app.py CHANGED
@@ -1,17 +1,18 @@
1
  import gradio as gr
2
 
3
- def greet(name: str) -> str:
4
  """
5
- Simple greeting function.
6
  """
7
- if not name:
8
  return "Please enter a name!"
9
- return f"Hello, {name}! Welcome to your Gradio 6 application."
 
 
10
 
11
- # Gradio 6 Syntax: NO parameters in gr.Blocks() constructor!
12
  with gr.Blocks() as demo:
13
 
14
- # Header with required link
15
  gr.HTML(
16
  """
17
  <div style="text-align: center; margin-bottom: 20px;">
@@ -22,34 +23,75 @@ with gr.Blocks() as demo:
22
  """
23
  )
24
 
25
- gr.Markdown("# 🚀 Gradio 6 Starter App")
26
- gr.Markdown("Enter your name below to receive a friendly greeting. This app demonstrates the new Gradio 6 syntax.")
27
-
28
  with gr.Row():
29
- with gr.Column(scale=1):
30
  name_input = gr.Textbox(
31
  label="Your Name",
32
  placeholder="Type your name here...",
33
- autofocus=True
 
 
 
 
 
 
 
 
 
34
  )
35
  submit_btn = gr.Button("Generate Greeting", variant="primary", size="lg")
36
-
37
- with gr.Column(scale=1):
 
 
 
 
 
 
 
 
 
 
 
38
  greeting_output = gr.Textbox(
39
  label="Output",
40
  interactive=False,
41
- lines=2
 
42
  )
43
-
44
- # Event listener using api_visibility (Gradio 6 syntax)
 
 
 
 
 
 
45
  submit_btn.click(
46
- fn=greet,
47
- inputs=[name_input],
48
- outputs=[greeting_output],
49
  api_visibility="public"
50
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Gradio 6 Syntax: ALL app-level parameters go in demo.launch()!
53
  demo.launch(
54
  theme=gr.themes.Soft(
55
  primary_hue="blue",
@@ -59,9 +101,13 @@ demo.launch(
59
  text_size="lg",
60
  spacing_size="lg",
61
  radius_size="md"
 
 
 
62
  ),
63
  footer_links=[
64
  {"label": "Gradio", "url": "https://gradio.app"},
65
  {"label": "GitHub", "url": "https://github.com/gradio-app/gradio"}
66
- ]
 
67
  )
 
1
  import gradio as gr
2
 
3
+ def greet(name: str, enthusiasm: int = 1) -> str:
4
  """
5
+ Enhanced greeting function with customizable enthusiasm.
6
  """
7
+ if not name.strip():
8
  return "Please enter a name!"
9
+ greeting = f"Hello, {name}!"
10
+ exclamation = "!" * enthusiasm
11
+ return f"{greeting}{exclamation} Welcome to your Gradio 6 application."
12
 
13
+ # Version 1: Enhanced with Examples and Validation
14
  with gr.Blocks() as demo:
15
 
 
16
  gr.HTML(
17
  """
18
  <div style="text-align: center; margin-bottom: 20px;">
 
23
  """
24
  )
25
 
26
+ gr.Markdown("# 🚀 Enhanced Gradio 6 App")
27
+ gr.Markdown("Enter your name and choose enthusiasm level for a personalized greeting.")
28
+
29
  with gr.Row():
30
+ with gr.Column():
31
  name_input = gr.Textbox(
32
  label="Your Name",
33
  placeholder="Type your name here...",
34
+ autofocus=True,
35
+ max_length=50
36
+ )
37
+ enthusiasm_slider = gr.Slider(
38
+ label="Enthusiasm Level",
39
+ minimum=1,
40
+ maximum=5,
41
+ value=1,
42
+ step=1,
43
+ info="How excited should the greeting be?"
44
  )
45
  submit_btn = gr.Button("Generate Greeting", variant="primary", size="lg")
46
+
47
+ # Add examples
48
+ examples = gr.Examples(
49
+ examples=[
50
+ ["Alice", 3],
51
+ ["Bob", 1],
52
+ ["Charlie", 5]
53
+ ],
54
+ inputs=[name_input, enthusiasm_slider],
55
+ label="Quick Examples"
56
+ )
57
+
58
+ with gr.Column():
59
  greeting_output = gr.Textbox(
60
  label="Output",
61
  interactive=False,
62
+ lines=2,
63
+ show_copy_button=True
64
  )
65
+ # Add a stats component
66
+ char_count = gr.Number(label="Character Count", interactive=False)
67
+
68
+ # Event listeners with Gradio 6 syntax
69
+ def process_greeting(name, enthusiasm):
70
+ result = greet(name, enthusiasm)
71
+ return result, len(result)
72
+
73
  submit_btn.click(
74
+ fn=process_greeting,
75
+ inputs=[name_input, enthusiasm_slider],
76
+ outputs=[greeting_output, char_count],
77
  api_visibility="public"
78
  )
79
+
80
+ # Real-time updates
81
+ name_input.change(
82
+ fn=process_greeting,
83
+ inputs=[name_input, enthusiasm_slider],
84
+ outputs=[greeting_output, char_count],
85
+ api_visibility="private"
86
+ )
87
+
88
+ enthusiasm_slider.change(
89
+ fn=process_greeting,
90
+ inputs=[name_input, enthusiasm_slider],
91
+ outputs=[greeting_output, char_count],
92
+ api_visibility="private"
93
+ )
94
 
 
95
  demo.launch(
96
  theme=gr.themes.Soft(
97
  primary_hue="blue",
 
101
  text_size="lg",
102
  spacing_size="lg",
103
  radius_size="md"
104
+ ).set(
105
+ button_primary_background_fill="*primary_600",
106
+ button_primary_background_fill_hover="*primary_700"
107
  ),
108
  footer_links=[
109
  {"label": "Gradio", "url": "https://gradio.app"},
110
  {"label": "GitHub", "url": "https://github.com/gradio-app/gradio"}
111
+ ],
112
+ show_error=True
113
  )