Spaces:
Runtime error
Runtime error
LPX55
commited on
Commit
·
4478d67
1
Parent(s):
ce7438d
Refactor app.py to modularize tab functionality by introducing greeting_tab, calculator_tab, and sepia_tab functions for improved readability and maintainability
Browse files- app.py +32 -40
- calculator_tab.py +21 -0
- greeting_tab.py +22 -0
- sepia_tab.py +9 -0
app.py
CHANGED
@@ -4,6 +4,9 @@ import numpy as np
|
|
4 |
from style import js_func, css_func
|
5 |
import tempfile
|
6 |
import pandas as pd
|
|
|
|
|
|
|
7 |
|
8 |
def greet(name="Stranger", intensity=1, exclaim=False):
|
9 |
greeting = f"Hello, {name}{'!' * int(intensity)}"
|
@@ -94,50 +97,13 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
94 |
open_sam_tab_btn = gr.Button("Open SAM2.1 Masking Tool in New Tab", elem_id="open-sam-tab-btn")
|
95 |
|
96 |
with gr.Tab("Greeting"):
|
97 |
-
|
98 |
-
with gr.Row():
|
99 |
-
with gr.Column():
|
100 |
-
name = gr.Textbox(label="Name", info="Enter your name", placeholder="e.g. Alex")
|
101 |
-
intensity = gr.Slider(1, 20, value=3, step=1, label="Intensity", info="How excited should the greeting be?")
|
102 |
-
with gr.Accordion("Advanced Options", open=False):
|
103 |
-
exclaim = gr.Checkbox(label="Shout (all caps)", info="Make the greeting all uppercase and add exclamations")
|
104 |
-
greet_btn = gr.Button("Greet")
|
105 |
-
with gr.Column():
|
106 |
-
greet_output = gr.Textbox(label="Greeting", lines=2)
|
107 |
-
download_greet_btn = gr.DownloadButton(label="Download Greeting", value=download_text, inputs=greet_output)
|
108 |
-
gr.Examples(
|
109 |
-
[["Jill", 1, False], ["Sam", 3, True], ["Alex", 5, False]],
|
110 |
-
inputs=[name, intensity, exclaim],
|
111 |
-
outputs=greet_output,
|
112 |
-
fn=greet
|
113 |
-
)
|
114 |
-
greet_btn.click(greet, [name, intensity, exclaim], greet_output)
|
115 |
|
116 |
with gr.Tab("Calculator"):
|
117 |
-
|
118 |
-
with gr.Row():
|
119 |
-
with gr.Column():
|
120 |
-
num1 = gr.Number(label="Number 1", info="First number")
|
121 |
-
operation = gr.Radio(["add", "subtract", "multiply", "divide"], label="Operation", info="Choose the operation")
|
122 |
-
num2 = gr.Number(label="Number 2", info="Second number")
|
123 |
-
calc_btn = gr.Button("Calculate")
|
124 |
-
with gr.Column():
|
125 |
-
calc_output = gr.Number(label="Result")
|
126 |
-
download_calc_btn = gr.DownloadButton(label="Download Result", value=download_csv, inputs=calc_output)
|
127 |
-
gr.Examples(
|
128 |
-
[[45, "add", 3], [3.14, "divide", 2], [144, "multiply", 2.5], [0, "subtract", 1.2]],
|
129 |
-
inputs=[num1, operation, num2],
|
130 |
-
outputs=calc_output,
|
131 |
-
fn=calculator
|
132 |
-
)
|
133 |
-
calc_btn.click(calculator, [num1, operation, num2], calc_output)
|
134 |
|
135 |
with gr.Tab("Sepia Image"):
|
136 |
-
|
137 |
-
image_input = gr.Image(label="Input Image", type="numpy")
|
138 |
-
sepia_btn = gr.Button("Apply Sepia")
|
139 |
-
image_output = gr.Image(label="Sepia Image")
|
140 |
-
sepia_btn.click(sepia, image_input, image_output)
|
141 |
|
142 |
# Add state variables for each dynamic tab
|
143 |
extra_space_open = gr.State(True)
|
@@ -232,6 +198,32 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
232 |
return gr.Tab(visible=False), False
|
233 |
close_btn.click(fn=close_tab, outputs=[custom_iframe_tab, custom_iframe_tab_open])
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
def show_tab():
|
236 |
return gr.Tab(visible=True)
|
237 |
|
|
|
4 |
from style import js_func, css_func
|
5 |
import tempfile
|
6 |
import pandas as pd
|
7 |
+
from greeting_tab import greeting_tab
|
8 |
+
from calculator_tab import calculator_tab
|
9 |
+
from sepia_tab import sepia_tab
|
10 |
|
11 |
def greet(name="Stranger", intensity=1, exclaim=False):
|
12 |
greeting = f"Hello, {name}{'!' * int(intensity)}"
|
|
|
97 |
open_sam_tab_btn = gr.Button("Open SAM2.1 Masking Tool in New Tab", elem_id="open-sam-tab-btn")
|
98 |
|
99 |
with gr.Tab("Greeting"):
|
100 |
+
greeting_tab()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
with gr.Tab("Calculator"):
|
103 |
+
calculator_tab()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
with gr.Tab("Sepia Image"):
|
106 |
+
sepia_tab()
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# Add state variables for each dynamic tab
|
109 |
extra_space_open = gr.State(True)
|
|
|
198 |
return gr.Tab(visible=False), False
|
199 |
close_btn.click(fn=close_tab, outputs=[custom_iframe_tab, custom_iframe_tab_open])
|
200 |
|
201 |
+
# Iframe lazy load pattern
|
202 |
+
heavy_iframe_src = gr.State("")
|
203 |
+
with gr.Tab("Heavy Space (iframe - lazy load)", visible=False) as heavy_iframe_tab:
|
204 |
+
iframe_html = gr.HTML("")
|
205 |
+
close_btn = gr.Button("Close Tab")
|
206 |
+
def close_heavy_iframe_tab():
|
207 |
+
return gr.Tab(visible=False), ""
|
208 |
+
close_btn.click(fn=close_heavy_iframe_tab, outputs=[heavy_iframe_tab, iframe_html])
|
209 |
+
def open_heavy_iframe_tab():
|
210 |
+
src = '<iframe src="https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space" width="100%" height="800" style="border:none;"></iframe>'
|
211 |
+
return gr.Tab(visible=True), src
|
212 |
+
open_heavy_iframe_btn = gr.Button("Open Heavy Space (iframe - lazy load)")
|
213 |
+
open_heavy_iframe_btn.click(fn=open_heavy_iframe_tab, outputs=[heavy_iframe_tab, iframe_html])
|
214 |
+
|
215 |
+
# Iframe always loaded pattern
|
216 |
+
with gr.Tab("Heavy Space (iframe - always loaded)", visible=True) as heavy_iframe_always_tab:
|
217 |
+
gr.HTML('<iframe src="https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space" width="100%" height="800" style="border:none;"></iframe>')
|
218 |
+
close_btn2 = gr.Button("Close Tab")
|
219 |
+
def close_heavy_iframe_always_tab():
|
220 |
+
return gr.Tab(visible=False)
|
221 |
+
close_btn2.click(fn=close_heavy_iframe_always_tab, outputs=heavy_iframe_always_tab)
|
222 |
+
open_heavy_iframe_always_btn = gr.Button("Open Heavy Space (iframe - always loaded)")
|
223 |
+
def open_heavy_iframe_always_tab():
|
224 |
+
return gr.Tab(visible=True)
|
225 |
+
open_heavy_iframe_always_btn.click(fn=open_heavy_iframe_always_tab, outputs=heavy_iframe_always_tab)
|
226 |
+
|
227 |
def show_tab():
|
228 |
return gr.Tab(visible=True)
|
229 |
|
calculator_tab.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from app import calculator, download_csv
|
3 |
+
|
4 |
+
def calculator_tab():
|
5 |
+
gr.Markdown("## Toy Calculator")
|
6 |
+
with gr.Row():
|
7 |
+
with gr.Column():
|
8 |
+
num1 = gr.Number(label="Number 1", info="First number")
|
9 |
+
operation = gr.Radio(["add", "subtract", "multiply", "divide"], label="Operation", info="Choose the operation")
|
10 |
+
num2 = gr.Number(label="Number 2", info="Second number")
|
11 |
+
calc_btn = gr.Button("Calculate")
|
12 |
+
with gr.Column():
|
13 |
+
calc_output = gr.Number(label="Result")
|
14 |
+
download_calc_btn = gr.DownloadButton(label="Download Result", value=download_csv, inputs=calc_output)
|
15 |
+
gr.Examples(
|
16 |
+
[[45, "add", 3], [3.14, "divide", 2], [144, "multiply", 2.5], [0, "subtract", 1.2]],
|
17 |
+
inputs=[num1, operation, num2],
|
18 |
+
outputs=calc_output,
|
19 |
+
fn=calculator
|
20 |
+
)
|
21 |
+
calc_btn.click(calculator, [num1, operation, num2], calc_output)
|
greeting_tab.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from app import greet, download_text
|
3 |
+
|
4 |
+
def greeting_tab():
|
5 |
+
gr.Markdown("## Greeting Generator")
|
6 |
+
with gr.Row():
|
7 |
+
with gr.Column():
|
8 |
+
name = gr.Textbox(label="Name", info="Enter your name", placeholder="e.g. Alex")
|
9 |
+
intensity = gr.Slider(1, 20, value=3, step=1, label="Intensity", info="How excited should the greeting be?")
|
10 |
+
with gr.Accordion("Advanced Options", open=False):
|
11 |
+
exclaim = gr.Checkbox(label="Shout (all caps)", info="Make the greeting all uppercase and add exclamations")
|
12 |
+
greet_btn = gr.Button("Greet")
|
13 |
+
with gr.Column():
|
14 |
+
greet_output = gr.Textbox(label="Greeting", lines=2)
|
15 |
+
download_greet_btn = gr.DownloadButton(label="Download Greeting", value=download_text, inputs=greet_output)
|
16 |
+
gr.Examples(
|
17 |
+
[["Jill", 1, False], ["Sam", 3, True], ["Alex", 5, False]],
|
18 |
+
inputs=[name, intensity, exclaim],
|
19 |
+
outputs=greet_output,
|
20 |
+
fn=greet
|
21 |
+
)
|
22 |
+
greet_btn.click(greet, [name, intensity, exclaim], greet_output)
|
sepia_tab.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from app import sepia
|
3 |
+
|
4 |
+
def sepia_tab():
|
5 |
+
gr.Markdown("## Sepia Image Filter")
|
6 |
+
image_input = gr.Image(label="Input Image", type="numpy")
|
7 |
+
sepia_btn = gr.Button("Apply Sepia")
|
8 |
+
image_output = gr.Image(label="Sepia Image")
|
9 |
+
sepia_btn.click(sepia, image_input, image_output)
|