Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,109 +1,98 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
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 |
-
download_zip_button.click(
|
102 |
-
lambda zip_path: zip_path,
|
103 |
-
inputs=[output_zip],
|
104 |
-
outputs=[output_zip],
|
105 |
-
)
|
106 |
-
|
107 |
-
return interface
|
108 |
-
|
109 |
-
main_interface().launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import io
|
3 |
+
import zipfile
|
4 |
+
|
5 |
+
# 自定义CSS,固定左右面板的大小 & 按钮样式
|
6 |
+
CUSTOM_CSS = """
|
7 |
+
/* 左侧面板 */
|
8 |
+
#left-panel {
|
9 |
+
width: 300px;
|
10 |
+
height: 400px; /* 固定高度,你可根据需求调整 */
|
11 |
+
overflow-y: auto; /* 超出时滚动 */
|
12 |
+
border-right: 1px solid #ccc;
|
13 |
+
padding: 10px;
|
14 |
+
box-sizing: border-box;
|
15 |
+
}
|
16 |
+
|
17 |
+
/* 右侧面板 */
|
18 |
+
#right-panel {
|
19 |
+
width: 300px;
|
20 |
+
height: 400px; /* 固定高度 */
|
21 |
+
overflow-y: auto;
|
22 |
+
border-left: 1px solid #ccc;
|
23 |
+
padding: 10px;
|
24 |
+
box-sizing: border-box;
|
25 |
+
}
|
26 |
+
|
27 |
+
/* 合并按钮 */
|
28 |
+
#combine-button {
|
29 |
+
background-color: orange;
|
30 |
+
color: white;
|
31 |
+
font-size: 16px;
|
32 |
+
border-radius: 5px;
|
33 |
+
border: none;
|
34 |
+
padding: 10px 20px;
|
35 |
+
cursor: pointer;
|
36 |
+
}
|
37 |
+
|
38 |
+
/* 下载ZIP按钮 */
|
39 |
+
#download-zip {
|
40 |
+
background-color: blue;
|
41 |
+
color: white;
|
42 |
+
font-size: 16px;
|
43 |
+
border-radius: 5px;
|
44 |
+
border: none;
|
45 |
+
padding: 10px 20px;
|
46 |
+
cursor: pointer;
|
47 |
+
}
|
48 |
+
"""
|
49 |
+
|
50 |
+
def combine_action():
|
51 |
+
"""
|
52 |
+
合并按钮点击后,你可以在这里执行实际逻辑。
|
53 |
+
下面仅示例返回一个字符串或做一个简单处理。
|
54 |
+
"""
|
55 |
+
print("合并逻辑被触发!")
|
56 |
+
return "已合并(此处可替换为实际处理逻辑)"
|
57 |
+
|
58 |
+
def create_zip():
|
59 |
+
"""
|
60 |
+
动态生成并返回一个 zip 文件(内存中)。
|
61 |
+
Gradio 会将返回的 BytesIO 作为 File 下载。
|
62 |
+
"""
|
63 |
+
buf = io.BytesIO()
|
64 |
+
with zipfile.ZipFile(buf, 'w') as zf:
|
65 |
+
# 往 zip 中写一个示例文件
|
66 |
+
zf.writestr("test.txt", "这是一个测试文件!\nHello World!")
|
67 |
+
buf.seek(0)
|
68 |
+
return buf # 返回内存中的 zip
|
69 |
+
|
70 |
+
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
71 |
+
# 主布局
|
72 |
+
with gr.Row():
|
73 |
+
# 左侧面板
|
74 |
+
with gr.Box(elem_id="left-panel"):
|
75 |
+
gr.Markdown("**上传区域**")
|
76 |
+
uploader = gr.File(label="在此处上传文件")
|
77 |
+
|
78 |
+
# 右侧面板
|
79 |
+
with gr.Box(elem_id="right-panel"):
|
80 |
+
gr.Markdown("**结果缩略图**")
|
81 |
+
gr.Markdown("这里可以放置处理后的缩略图或任何输出")
|
82 |
+
|
83 |
+
# 底部按钮
|
84 |
+
with gr.Row():
|
85 |
+
combine_btn = gr.Button("合并", elem_id="combine-button")
|
86 |
+
download_btn = gr.Button("下载 Zip", elem_id="download-zip")
|
87 |
+
|
88 |
+
# 点击“合并”时(这里仅做一个演示)
|
89 |
+
combine_btn.click(fn=combine_action, inputs=[], outputs=[])
|
90 |
+
|
91 |
+
# 准备一个隐藏的 File 组件,用来承载生成的 zip 文件
|
92 |
+
# 一旦函数返回文件,该组件就会显示“下载链接”
|
93 |
+
zip_file = gr.File(label="点此下载生成的 ZIP", visible=False)
|
94 |
+
|
95 |
+
# 点击“下载 Zip”时,调用 create_zip,输出到 zip_file
|
96 |
+
download_btn.click(fn=create_zip, inputs=[], outputs=zip_file)
|
97 |
+
|
98 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|