Spaces:
Sleeping
Sleeping
Commit
·
62028bb
1
Parent(s):
1dd7eb5
Improve GUI
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ dataset = None
|
|
23 |
def load_merit_dataset():
|
24 |
global dataset
|
25 |
if dataset is None:
|
26 |
-
dataset = load_dataset("de-Rodrigo/merit", name="en-digital-seq", split="
|
27 |
return dataset
|
28 |
|
29 |
|
@@ -98,39 +98,66 @@ def process_image_donut(model, processor, image):
|
|
98 |
|
99 |
|
100 |
@spaces.GPU
|
101 |
-
def process_image(model_name,
|
102 |
-
if
|
|
|
|
|
103 |
image = get_image_from_dataset(dataset_image_index)
|
104 |
|
105 |
if model_name == "de-Rodrigo/donut-merit":
|
106 |
model, processor = get_donut()
|
107 |
result = process_image_donut(model, processor, image)
|
108 |
else:
|
109 |
-
# Here you should implement processing for other models
|
110 |
result = f"Processing for model {model_name} not implemented"
|
111 |
|
112 |
return image, result
|
113 |
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
-
# Load the dataset
|
117 |
load_merit_dataset()
|
118 |
-
|
119 |
models = get_collection_models("saliency")
|
120 |
models.append("de-Rodrigo/donut-merit")
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
)
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
demo.launch()
|
|
|
23 |
def load_merit_dataset():
|
24 |
global dataset
|
25 |
if dataset is None:
|
26 |
+
dataset = load_dataset("de-Rodrigo/merit", name="en-digital-seq", split="test")
|
27 |
return dataset
|
28 |
|
29 |
|
|
|
98 |
|
99 |
|
100 |
@spaces.GPU
|
101 |
+
def process_image(model_name, dataset_image_index, custom_image=None):
|
102 |
+
if custom_image is not None:
|
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 optionally upload your own image."
|
125 |
+
)
|
126 |
+
|
127 |
+
with gr.Row():
|
128 |
+
with gr.Column(scale=2):
|
129 |
+
model_dropdown = gr.Dropdown(choices=models, label="Select Model")
|
130 |
+
dataset_slider = gr.Slider(
|
131 |
+
minimum=0,
|
132 |
+
maximum=len(dataset) - 1,
|
133 |
+
step=1,
|
134 |
+
label="Dataset Image Index",
|
135 |
+
)
|
136 |
+
|
137 |
+
with gr.Column(scale=1):
|
138 |
+
custom_image = gr.Image(
|
139 |
+
type="pil", label="Optional: Upload Custom Image"
|
140 |
+
)
|
141 |
+
|
142 |
+
process_button = gr.Button("Process Image")
|
143 |
+
|
144 |
+
with gr.Row():
|
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, dataset_slider, custom_image],
|
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()
|