Spaces:
Sleeping
Sleeping
Commit
·
a62b0d6
1
Parent(s):
62028bb
Improve GUI
Browse files
app.py
CHANGED
|
@@ -23,7 +23,9 @@ dataset = None
|
|
| 23 |
def load_merit_dataset():
|
| 24 |
global dataset
|
| 25 |
if dataset is None:
|
| 26 |
-
dataset = load_dataset(
|
|
|
|
|
|
|
| 27 |
return dataset
|
| 28 |
|
| 29 |
|
|
@@ -98,34 +100,39 @@ def process_image_donut(model, processor, image):
|
|
| 98 |
|
| 99 |
|
| 100 |
@spaces.GPU
|
| 101 |
-
def process_image(model_name,
|
| 102 |
-
if
|
| 103 |
-
image = custom_image
|
| 104 |
-
else:
|
| 105 |
image = get_image_from_dataset(dataset_image_index)
|
| 106 |
|
| 107 |
if model_name == "de-Rodrigo/donut-merit":
|
| 108 |
model, processor = get_donut()
|
| 109 |
result = process_image_donut(model, processor, image)
|
| 110 |
else:
|
|
|
|
| 111 |
result = f"Processing for model {model_name} not implemented"
|
| 112 |
|
| 113 |
return image, result
|
| 114 |
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if __name__ == "__main__":
|
|
|
|
| 117 |
load_merit_dataset()
|
|
|
|
| 118 |
models = get_collection_models("saliency")
|
| 119 |
models.append("de-Rodrigo/donut-merit")
|
| 120 |
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
gr.Markdown("# Document Understanding with Donut")
|
| 123 |
gr.Markdown(
|
| 124 |
-
"Select an image from the dataset or
|
| 125 |
)
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
-
with gr.Column(
|
| 129 |
model_dropdown = gr.Dropdown(choices=models, label="Select Model")
|
| 130 |
dataset_slider = gr.Slider(
|
| 131 |
minimum=0,
|
|
@@ -133,11 +140,9 @@ if __name__ == "__main__":
|
|
| 133 |
step=1,
|
| 134 |
label="Dataset Image Index",
|
| 135 |
)
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
custom_image = gr.Image(
|
| 139 |
-
type="pil", label="Optional: Upload Custom Image"
|
| 140 |
-
)
|
| 141 |
|
| 142 |
process_button = gr.Button("Process Image")
|
| 143 |
|
|
@@ -145,19 +150,21 @@ if __name__ == "__main__":
|
|
| 145 |
output_image = gr.Image(label="Processed Image")
|
| 146 |
output_text = gr.Textbox(label="Result")
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
process_button.click(
|
| 149 |
fn=process_image,
|
| 150 |
-
inputs=[model_dropdown,
|
| 151 |
outputs=[output_image, output_text],
|
| 152 |
)
|
| 153 |
|
| 154 |
-
gr.Examples(
|
| 155 |
-
examples=[
|
| 156 |
-
["de-Rodrigo/donut-merit", 0],
|
| 157 |
-
["de-Rodrigo/donut-merit", 10],
|
| 158 |
-
["de-Rodrigo/donut-merit", 20],
|
| 159 |
-
],
|
| 160 |
-
inputs=[model_dropdown, dataset_slider],
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
demo.launch()
|
|
|
|
| 23 |
def load_merit_dataset():
|
| 24 |
global dataset
|
| 25 |
if dataset is None:
|
| 26 |
+
dataset = load_dataset(
|
| 27 |
+
"de-Rodrigo/merit", name="en-digital-seq", split="test", num_proc=8
|
| 28 |
+
)
|
| 29 |
return dataset
|
| 30 |
|
| 31 |
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
@spaces.GPU
|
| 103 |
+
def process_image(model_name, image=None, dataset_image_index=None):
|
| 104 |
+
if dataset_image_index is not None:
|
|
|
|
|
|
|
| 105 |
image = get_image_from_dataset(dataset_image_index)
|
| 106 |
|
| 107 |
if model_name == "de-Rodrigo/donut-merit":
|
| 108 |
model, processor = get_donut()
|
| 109 |
result = process_image_donut(model, processor, image)
|
| 110 |
else:
|
| 111 |
+
# Here you should implement processing for other models
|
| 112 |
result = f"Processing for model {model_name} not implemented"
|
| 113 |
|
| 114 |
return image, result
|
| 115 |
|
| 116 |
|
| 117 |
+
def update_image(dataset_image_index):
|
| 118 |
+
return get_image_from_dataset(dataset_image_index)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
if __name__ == "__main__":
|
| 122 |
+
# Load the dataset
|
| 123 |
load_merit_dataset()
|
| 124 |
+
|
| 125 |
models = get_collection_models("saliency")
|
| 126 |
models.append("de-Rodrigo/donut-merit")
|
| 127 |
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
gr.Markdown("# Document Understanding with Donut")
|
| 130 |
gr.Markdown(
|
| 131 |
+
"Select a model and an image from the dataset, or upload your own image."
|
| 132 |
)
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
+
with gr.Column():
|
| 136 |
model_dropdown = gr.Dropdown(choices=models, label="Select Model")
|
| 137 |
dataset_slider = gr.Slider(
|
| 138 |
minimum=0,
|
|
|
|
| 140 |
step=1,
|
| 141 |
label="Dataset Image Index",
|
| 142 |
)
|
| 143 |
+
upload_image = gr.Image(type="pil", label="Or Upload Your Own Image")
|
| 144 |
|
| 145 |
+
preview_image = gr.Image(label="Selected/Uploaded Image")
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
process_button = gr.Button("Process Image")
|
| 148 |
|
|
|
|
| 150 |
output_image = gr.Image(label="Processed Image")
|
| 151 |
output_text = gr.Textbox(label="Result")
|
| 152 |
|
| 153 |
+
# Update preview image when slider changes
|
| 154 |
+
dataset_slider.change(
|
| 155 |
+
fn=update_image, inputs=[dataset_slider], outputs=[preview_image]
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Update preview image when an image is uploaded
|
| 159 |
+
upload_image.change(
|
| 160 |
+
fn=lambda x: x, inputs=[upload_image], outputs=[preview_image]
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Process image when button is clicked
|
| 164 |
process_button.click(
|
| 165 |
fn=process_image,
|
| 166 |
+
inputs=[model_dropdown, upload_image, dataset_slider],
|
| 167 |
outputs=[output_image, output_text],
|
| 168 |
)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
demo.launch()
|