Spaces:
Runtime error
Runtime error
Raymond Weitekamp
commited on
Commit
·
f5f2ad3
1
Parent(s):
e3d7a16
ui improvements
Browse files
app.py
CHANGED
@@ -104,9 +104,12 @@ def create_gradio_interface():
|
|
104 |
|
105 |
with gr.Blocks() as demo:
|
106 |
gr.Markdown("## Crowdsourcing Handwriting OCR Dataset")
|
107 |
-
gr.
|
|
|
|
|
|
|
|
|
108 |
user_info = gr.Markdown()
|
109 |
-
# Hidden state to hold the user's OAuth profile as JSON.
|
110 |
profile_state = gr.JSON(visible=False)
|
111 |
|
112 |
gr.Markdown(
|
@@ -114,10 +117,24 @@ def create_gradio_interface():
|
|
114 |
"If you wish to skip the current text, click 'Skip'."
|
115 |
)
|
116 |
|
117 |
-
text_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
image_input = gr.Image(type="pil", label="Upload Handwritten Image", sources=["upload"], visible=False)
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
with gr.Row(visible=False) as button_row:
|
123 |
submit_btn = gr.Button("Submit")
|
@@ -129,13 +146,13 @@ def create_gradio_interface():
|
|
129 |
# Use the username provided by the profile (from the "profile" scope)
|
130 |
return f"Logged in as: {profile.username}", {"username": profile.username}
|
131 |
|
132 |
-
def handle_submit(profile,
|
133 |
if not profile or "username" not in profile:
|
134 |
raise gr.Error("Please log in to use this application")
|
135 |
username = profile["username"]
|
136 |
repo_id = f"{username}/handwriting-ocr-private"
|
137 |
|
138 |
-
if
|
139 |
# Remove all metadata for privacy
|
140 |
stripped_image = strip_metadata(image)
|
141 |
|
@@ -184,7 +201,7 @@ def create_gradio_interface():
|
|
184 |
|
185 |
new_text = collector.get_random_text_block()
|
186 |
return None, new_text
|
187 |
-
|
188 |
# Fallback to public submission
|
189 |
new_text = collector.submit_image(image, text, username)
|
190 |
return None, new_text
|
@@ -196,20 +213,27 @@ def create_gradio_interface():
|
|
196 |
|
197 |
def update_visibility(profile: gr.OAuthProfile | None):
|
198 |
is_visible = profile is not None
|
199 |
-
# Update the visibility of text_box, image_input, button_row, and dataset_radio.
|
200 |
return [
|
201 |
-
gr.update(visible=is_visible),
|
202 |
-
gr.update(visible=is_visible),
|
203 |
-
gr.update(visible=is_visible),
|
204 |
-
gr.update(visible=is_visible)
|
|
|
|
|
|
|
205 |
]
|
206 |
|
207 |
# On load, update both the display message and the hidden profile state.
|
208 |
demo.load(update_user_info, inputs=None, outputs=[user_info, profile_state])
|
209 |
-
demo.load(update_visibility, inputs=None,
|
|
|
|
|
|
|
210 |
|
211 |
# Bind the submit and skip actions with updated inputs.
|
212 |
-
submit_btn.click(handle_submit,
|
|
|
|
|
213 |
skip_btn.click(handle_skip, inputs=[profile_state, text_box], outputs=text_box)
|
214 |
|
215 |
return demo
|
|
|
104 |
|
105 |
with gr.Blocks() as demo:
|
106 |
gr.Markdown("## Crowdsourcing Handwriting OCR Dataset")
|
107 |
+
with gr.Row():
|
108 |
+
with gr.Column(scale=1, min_width=100): # Just use scale and min_width to constrain the column
|
109 |
+
gr.LoginButton()
|
110 |
+
with gr.Column(scale=4):
|
111 |
+
pass
|
112 |
user_info = gr.Markdown()
|
|
|
113 |
profile_state = gr.JSON(visible=False)
|
114 |
|
115 |
gr.Markdown(
|
|
|
117 |
"If you wish to skip the current text, click 'Skip'."
|
118 |
)
|
119 |
|
120 |
+
text_box = gr.Textbox(
|
121 |
+
value=collector.current_text_block,
|
122 |
+
label="Text to Handwrite",
|
123 |
+
interactive=False,
|
124 |
+
visible=False,
|
125 |
+
lines=10, # Allow multiple lines
|
126 |
+
show_copy_button=True # Optional: allows users to easily copy the text
|
127 |
+
)
|
128 |
image_input = gr.Image(type="pil", label="Upload Handwritten Image", sources=["upload"], visible=False)
|
129 |
+
|
130 |
+
# Add checkboxes and explanation text
|
131 |
+
gr.Markdown("### Select Datasets")
|
132 |
+
with gr.Column():
|
133 |
+
private_checkbox = gr.Checkbox(value=True, label="Private", interactive=True, visible=False)
|
134 |
+
private_explanation = gr.Markdown("*Private: Creates a new dataset on your account named '/handwriting-ocr-private' and appends data there.*", visible=False)
|
135 |
+
|
136 |
+
public_checkbox = gr.Checkbox(value=True, label="Public", interactive=True, visible=False)
|
137 |
+
public_explanation = gr.Markdown("*Public: Will be added to our public dataset. By submitting, you are giving permission to be added to the dataset.*", visible=False)
|
138 |
|
139 |
with gr.Row(visible=False) as button_row:
|
140 |
submit_btn = gr.Button("Submit")
|
|
|
146 |
# Use the username provided by the profile (from the "profile" scope)
|
147 |
return f"Logged in as: {profile.username}", {"username": profile.username}
|
148 |
|
149 |
+
def handle_submit(profile, private_checkbox, public_checkbox, image, text):
|
150 |
if not profile or "username" not in profile:
|
151 |
raise gr.Error("Please log in to use this application")
|
152 |
username = profile["username"]
|
153 |
repo_id = f"{username}/handwriting-ocr-private"
|
154 |
|
155 |
+
if private_checkbox:
|
156 |
# Remove all metadata for privacy
|
157 |
stripped_image = strip_metadata(image)
|
158 |
|
|
|
201 |
|
202 |
new_text = collector.get_random_text_block()
|
203 |
return None, new_text
|
204 |
+
elif public_checkbox:
|
205 |
# Fallback to public submission
|
206 |
new_text = collector.submit_image(image, text, username)
|
207 |
return None, new_text
|
|
|
213 |
|
214 |
def update_visibility(profile: gr.OAuthProfile | None):
|
215 |
is_visible = profile is not None
|
|
|
216 |
return [
|
217 |
+
gr.update(visible=is_visible), # text_box
|
218 |
+
gr.update(visible=is_visible), # image_input
|
219 |
+
gr.update(visible=is_visible), # button_row
|
220 |
+
gr.update(visible=is_visible), # private_checkbox
|
221 |
+
gr.update(visible=is_visible), # public_checkbox
|
222 |
+
gr.update(visible=is_visible), # private explanation
|
223 |
+
gr.update(visible=is_visible), # public explanation
|
224 |
]
|
225 |
|
226 |
# On load, update both the display message and the hidden profile state.
|
227 |
demo.load(update_user_info, inputs=None, outputs=[user_info, profile_state])
|
228 |
+
demo.load(update_visibility, inputs=None,
|
229 |
+
outputs=[text_box, image_input, button_row,
|
230 |
+
private_checkbox, public_checkbox,
|
231 |
+
private_explanation, public_explanation])
|
232 |
|
233 |
# Bind the submit and skip actions with updated inputs.
|
234 |
+
submit_btn.click(handle_submit,
|
235 |
+
inputs=[profile_state, private_checkbox, public_checkbox, image_input, text_box],
|
236 |
+
outputs=[image_input, text_box])
|
237 |
skip_btn.click(handle_skip, inputs=[profile_state, text_box], outputs=text_box)
|
238 |
|
239 |
return demo
|