gradio库是一个用于创建简单web应用程序的库,允许用户通过浏览器和程序进行交互。
1.gr.Blocks(),使用with创建,在with中创建的任何组件都会自动添加到应用程序中,组件按创建顺序垂直显示在应用中。Blocks()模块用于组件在页面中的位置,处理复杂的数据流(输出可以作为其他函数的输入),基于用户交互更新组件的属性/可见性。
with gr.Blocks() as demo:
pass
使用with语句创建了名为demo的上下文管理器。这个上下文管理器中创建了名为Blocks()的对象,该对象的内部包含GUI组件。
2.gr.grow()下的元素全部水平显示。行布局:将组件水平排列在一行中。
with gr.Row():
gr.Markdown("""# Official Implementation of [DragDiffusion](https://arxiv.org/abs/2306.14435)""")
即以下部分:gr.Markdown()这里面可以放Markdown格式的内容,或gr.HTML()这个就是常见的HTML内容了。
3.gr.Tab()标签页,用于创建多个标签页,使界面更有组织、易于导航。
with gr.Tab(label="Editing Real Image"):
pass
4.gr.state(),创建了3个gradio状态对象mask、selected_points、original_image,用于存储mask、point和原始输入图像的。它的参数None、[]、None分别是每一个对象的初始值。
mask = gr.State(value=None) # store mask
selected_points = gr.State([]) # store points
original_image = gr.State(value=None) # store original input image
5.gr.Column()列布局,将组件垂直排列在一列中。
gr.Image()用于处理图像输入和输出。
gr.Button(按钮)用于创建按钮,用户可以点击按钮执行特定的操作。
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Draw Mask</p>""")
canvas = gr.Image(type="numpy", tool="sketch", label="Draw Mask",show_label=True, height=LENGTH, width=LENGTH) # for mask painting
train_lora_button = gr.Button("Train LoRA")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Click Points</p>""")
input_image = gr.Image(type="numpy", label="Click Points",
show_label=True, height=LENGTH, width=LENGTH) # for points clicking
undo_button = gr.Button("Undo point")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Editing Results</p>""")
output_image = gr.Image(type="numpy", label="Editing Results",show_label=True, height=LENGTH, width=LENGTH)
with gr.Row():
run_button = gr.Button("Run")
clear_all_button = gr.Button("Clear All")
分别对应一个小块
6.gr.Textbox()文本框用于接收和显示文本输入和输出。
# general parameters
with gr.Row():
prompt = gr.Textbox(label="Prompt")
lora_path = gr.Textbox(value="./lora_tmp", label="LoRA path")
lora_status_bar = gr.Textbox(label="display LoRA training status")