hmb HF Staff commited on
Commit
b313159
·
verified ·
1 Parent(s): 68f94a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -27
app.py CHANGED
@@ -1,27 +1,164 @@
1
- import gradio as gr
2
-
3
- def get_theme(theme_name):
4
- themes = {
5
- "base": gr.themes.Base(),
6
- "default": gr.themes.Default(),
7
- "soft": gr.themes.Soft(),
8
- "monochrome": gr.themes.Monochrome(),
9
- "glass": gr.themes.Glass(),
10
- "origin": gr.themes.Origin(),
11
- "citrus": gr.themes.Citrus(),
12
- "ocean": gr.themes.Ocean(),
13
- "spark": gr.themes.Spark(),
14
- }
15
- return themes.get(theme_name, gr.themes.Default())
16
-
17
- theme_name = gr.request.query_params.get("theme", "default")
18
-
19
- with gr.Blocks(theme=get_theme(theme_name)) as demo:
20
- gr.Markdown("# Theme Preview")
21
- with gr.Row():
22
- gr.Textbox(label="Input", value="Sample text")
23
- gr.Slider(label="Slider", value=50)
24
- gr.Button("Submit", variant="primary")
25
- gr.Checkbox(label="Option", value=True)
26
-
27
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastapi import FastAPI, Request
3
+ from fastapi.responses import RedirectResponse
4
+ import uvicorn
5
+
6
+ BUILTIN_THEMES = ["default", "base", "soft", "monochrome", "glass", "origin", "citrus", "ocean", "spark"]
7
+
8
+
9
+ def load_theme(theme_id: str):
10
+ """Load theme by ID. Supports built-in and Hub themes."""
11
+ if not theme_id or theme_id == "default":
12
+ return gr.themes.Default()
13
+
14
+ # Community theme from Hub (e.g., "gradio/seafoam")
15
+ if "/" in theme_id:
16
+ try:
17
+ return gr.Theme.from_hub(theme_id)
18
+ except Exception as e:
19
+ print(f"Failed to load '{theme_id}' from Hub: {e}")
20
+ return gr.themes.Default()
21
+
22
+ # Built-in theme
23
+ builtin = {
24
+ "base": gr.themes.Base,
25
+ "soft": gr.themes.Soft,
26
+ "monochrome": gr.themes.Monochrome,
27
+ "glass": gr.themes.Glass,
28
+ "origin": gr.themes.Origin,
29
+ "citrus": gr.themes.Citrus,
30
+ "ocean": gr.themes.Ocean,
31
+ "spark": gr.themes.Spark,
32
+ }
33
+ theme_cls = builtin.get(theme_id.lower())
34
+ return theme_cls() if theme_cls else gr.themes.Default()
35
+
36
+
37
+ def process(text, number, checkbox, radio, dropdown):
38
+ return f"""**Results:**
39
+ - Text: {text}
40
+ - Number: {number}
41
+ - Checkbox: {checkbox}
42
+ - Radio: {radio}
43
+ - Dropdown: {dropdown}"""
44
+
45
+
46
+ def make_demo(theme_id: str):
47
+ """Create a demo app with the specified theme."""
48
+ theme = load_theme(theme_id)
49
+ display_name = theme_id if "/" in theme_id else theme_id.capitalize()
50
+
51
+ with gr.Blocks(theme=theme, title=f"Theme: {display_name}") as demo:
52
+ gr.Markdown(f"# {display_name} Theme")
53
+ gr.Markdown("Preview of Gradio components with this theme.")
54
+
55
+ with gr.Row():
56
+ with gr.Column():
57
+ text_input = gr.Textbox(
58
+ label="Text Input",
59
+ placeholder="Type something here...",
60
+ info="A standard text input field"
61
+ )
62
+ number_input = gr.Slider(
63
+ minimum=0,
64
+ maximum=100,
65
+ value=50,
66
+ label="Slider",
67
+ info="Drag to select a value"
68
+ )
69
+ checkbox = gr.Checkbox(
70
+ value=True,
71
+ label="Enable feature",
72
+ info="Toggle this option"
73
+ )
74
+
75
+ with gr.Row():
76
+ submit_btn = gr.Button("Submit", variant="primary")
77
+ clear_btn = gr.Button("Clear", variant="secondary")
78
+ stop_btn = gr.Button("Stop", variant="stop")
79
+
80
+ with gr.Column():
81
+ output = gr.Markdown(label="Output")
82
+ radio = gr.Radio(
83
+ choices=["Option A", "Option B", "Option C"],
84
+ value="Option A",
85
+ label="Radio Selection"
86
+ )
87
+ dropdown = gr.Dropdown(
88
+ choices=["Choice 1", "Choice 2", "Choice 3"],
89
+ value="Choice 1",
90
+ label="Dropdown Menu"
91
+ )
92
+
93
+ with gr.Row():
94
+ with gr.Column():
95
+ gr.Markdown("### Additional Components")
96
+ with gr.Tabs():
97
+ with gr.Tab("Tab 1"):
98
+ gr.Textbox(label="Tab 1 Content", value="Content in first tab")
99
+ with gr.Tab("Tab 2"):
100
+ gr.Textbox(label="Tab 2 Content", value="Content in second tab")
101
+
102
+ with gr.Column():
103
+ with gr.Accordion("Advanced Settings", open=False):
104
+ gr.Slider(0, 10, 5, label="Advanced Option 1")
105
+ gr.Slider(0, 10, 3, label="Advanced Option 2")
106
+ gr.Checkbox(label="Advanced Toggle")
107
+
108
+ with gr.Row():
109
+ gr.Number(label="Number Input", value=42)
110
+ gr.ColorPicker(label="Color", value="#ff7700")
111
+
112
+ submit_btn.click(
113
+ fn=process,
114
+ inputs=[text_input, number_input, checkbox, radio, dropdown],
115
+ outputs=output
116
+ )
117
+ clear_btn.click(
118
+ fn=lambda: ("", 50, True, "Option A", "Choice 1", ""),
119
+ outputs=[text_input, number_input, checkbox, radio, dropdown, output]
120
+ )
121
+
122
+ return demo
123
+
124
+
125
+ # Pre-build demos for all built-in themes
126
+ DEMOS = {tid: make_demo(tid) for tid in BUILTIN_THEMES}
127
+
128
+ # Hub theme cache
129
+ HUB_DEMOS = {}
130
+
131
+ app = FastAPI()
132
+
133
+
134
+ @app.get("/")
135
+ async def root(request: Request):
136
+ """Redirect to appropriate theme path based on query param."""
137
+ theme = request.query_params.get("theme", "default").lower()
138
+
139
+ # Handle hub themes
140
+ if "/" in theme:
141
+ # URL encode the slash for the path
142
+ safe_theme = theme.replace("/", "--")
143
+ if safe_theme not in HUB_DEMOS:
144
+ try:
145
+ HUB_DEMOS[safe_theme] = make_demo(theme)
146
+ gr.mount_gradio_app(app, HUB_DEMOS[safe_theme], path=f"/{safe_theme}")
147
+ except Exception as e:
148
+ print(f"Error creating demo for {theme}: {e}")
149
+ return RedirectResponse("/default/")
150
+ return RedirectResponse(f"/{safe_theme}/")
151
+
152
+ # Handle built-in themes
153
+ if theme not in DEMOS:
154
+ theme = "default"
155
+ return RedirectResponse(f"/{theme}/")
156
+
157
+
158
+ # Mount all built-in theme demos
159
+ for theme_id, demo in DEMOS.items():
160
+ app = gr.mount_gradio_app(app, demo, path=f"/{theme_id}")
161
+
162
+
163
+ if __name__ == "__main__":
164
+ uvicorn.run(app, host="0.0.0.0", port=7860)