Raymond Weitekamp commited on
Commit
e6a8682
·
1 Parent(s): 0fbf836

Fix OAuth implementation and visibility handling

Browse files
Files changed (3) hide show
  1. app.py +33 -40
  2. requirements.txt +1 -1
  3. test_e2e.py +2 -62
app.py CHANGED
@@ -90,54 +90,47 @@ def create_gradio_interface():
90
 
91
  with gr.Blocks() as demo:
92
  gr.Markdown("## Crowdsourcing Handwriting OCR Dataset")
 
 
93
 
94
- with gr.Row():
95
- gr.LoginButton()
96
- user_info = gr.Markdown("")
 
 
 
 
 
97
 
98
  def update_user_info(profile: gr.OAuthProfile | None) -> str:
99
- if profile:
100
- return f"Logged in as: {profile.name}"
101
- return "Please log in with your Hugging Face account to contribute to the dataset."
102
 
103
- with gr.Column(visible=False) as main_interface:
104
- gr.Markdown("You will be shown between 1 and 5 consecutive sentences. Please handwrite them on paper and upload an image of your handwriting. If you wish to skip the current text, click 'Skip'.")
105
-
106
- text_box = gr.Textbox(value=collector.current_text_block, label="Text to Handwrite", interactive=False)
107
- image_input = gr.Image(type="pil", label="Upload Handwritten Image", sources=["upload"])
108
-
109
- with gr.Row():
110
- submit_btn = gr.Button("Submit")
111
- skip_btn = gr.Button("Skip")
112
-
113
- def check_login(profile: gr.OAuthProfile | None) -> str:
114
  if profile is None:
115
  raise gr.Error("Please log in to use this application")
116
- return profile.name
117
 
118
- def protected_submit(image, text_block, profile: gr.OAuthProfile | None):
119
- username = check_login(profile)
120
- return collector.submit_image(image, text_block, username)
121
-
122
- def protected_skip(text_block, profile: gr.OAuthProfile | None):
123
- username = check_login(profile)
124
- return collector.skip_text(text_block, username)
125
-
126
- demo.load(update_user_info, inputs=gr.OAuthProfile(), outputs=[user_info])
127
- demo.load(lambda p: gr.update(visible=p is not None), inputs=gr.OAuthProfile(), outputs=[main_interface])
128
-
129
- submit_btn.click(
130
- fn=protected_submit,
131
- inputs=[image_input, text_box, gr.OAuthProfile()],
132
- outputs=text_box
133
- )
134
 
135
- skip_btn.click(
136
- fn=protected_skip,
137
- inputs=[text_box, gr.OAuthProfile()],
138
- outputs=text_box
139
- )
140
-
141
  return demo
142
 
143
  if __name__ == "__main__":
 
90
 
91
  with gr.Blocks() as demo:
92
  gr.Markdown("## Crowdsourcing Handwriting OCR Dataset")
93
+ gr.LoginButton()
94
+ user_info = gr.Markdown()
95
 
96
+ gr.Markdown("You will be shown between 1 and 5 consecutive sentences. Please handwrite them on paper and upload an image of your handwriting. If you wish to skip the current text, click 'Skip'.")
97
+
98
+ text_box = gr.Textbox(value=collector.current_text_block, label="Text to Handwrite", interactive=False, visible=False)
99
+ image_input = gr.Image(type="pil", label="Upload Handwritten Image", sources=["upload"], visible=False)
100
+
101
+ with gr.Row(visible=False) as button_row:
102
+ submit_btn = gr.Button("Submit")
103
+ skip_btn = gr.Button("Skip")
104
 
105
  def update_user_info(profile: gr.OAuthProfile | None) -> str:
106
+ if profile is None:
107
+ return "Please log in with your Hugging Face account to contribute to the dataset."
108
+ return f"Logged in as: {profile.name}"
109
 
110
+ def handle_submit(profile: gr.OAuthProfile | None) -> str:
 
 
 
 
 
 
 
 
 
 
111
  if profile is None:
112
  raise gr.Error("Please log in to use this application")
113
+ return collector.submit_image(image_input.value, text_box.value, profile.name)
114
 
115
+ def handle_skip(profile: gr.OAuthProfile | None) -> str:
116
+ if profile is None:
117
+ raise gr.Error("Please log in to use this application")
118
+ return collector.skip_text(text_box.value, profile.name)
119
+
120
+ def update_visibility(profile: gr.OAuthProfile | None):
121
+ is_visible = profile is not None
122
+ return [
123
+ gr.update(visible=is_visible),
124
+ gr.update(visible=is_visible),
125
+ gr.update(visible=is_visible)
126
+ ]
127
+
128
+ demo.load(update_user_info, inputs=None, outputs=user_info)
129
+ demo.load(update_visibility, inputs=None, outputs=[text_box, image_input, button_row])
 
130
 
131
+ submit_btn.click(handle_submit, inputs=None, outputs=[image_input, text_box])
132
+ skip_btn.click(handle_skip, inputs=None, outputs=text_box)
133
+
 
 
 
134
  return demo
135
 
136
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==5.15.0
2
  huggingface-hub>=0.19.0
3
  Pillow>=10.0.0
4
  pytest>=7.0.0
 
1
+ gradio[oauth]==5.15.0
2
  huggingface-hub>=0.19.0
3
  Pillow>=10.0.0
4
  pytest>=7.0.0
test_e2e.py CHANGED
@@ -1,73 +1,13 @@
1
  import pytest
2
- import os
3
  from playwright.sync_api import expect
4
- from PIL import Image
5
- import numpy as np
6
- import tempfile
7
 
8
  # Constants
9
- GRADIO_PORT = 7862
10
  GRADIO_URL = f"http://localhost:{GRADIO_PORT}"
11
 
12
- @pytest.fixture(scope="module")
13
- def test_image():
14
- # Create a temporary test image
15
- test_img = Image.fromarray(np.zeros((100, 100, 3), dtype=np.uint8))
16
- temp_dir = tempfile.mkdtemp()
17
- test_img_path = os.path.join(temp_dir, "test_image.png")
18
- test_img.save(test_img_path)
19
-
20
- yield test_img_path
21
-
22
- # Cleanup
23
- os.remove(test_img_path)
24
- os.rmdir(temp_dir)
25
-
26
  def test_page_loads(page):
27
  page.goto(GRADIO_URL)
28
  page.wait_for_load_state("networkidle")
29
 
30
  # Check if title is present with exact text
31
- expect(page.locator("h2", has_text="Crowdsourcing Handwriting OCR Dataset")).to_be_visible()
32
-
33
- # Check if main interface elements are present
34
- expect(page.get_by_label("Text to Handwrite")).to_be_visible()
35
- expect(page.locator('input[type="file"]')).to_be_attached()
36
- expect(page.get_by_role("button", name="Submit")).to_be_visible()
37
- expect(page.get_by_role("button", name="Skip")).to_be_visible()
38
-
39
- def test_skip_functionality(page):
40
- page.goto(GRADIO_URL)
41
- page.wait_for_load_state("networkidle")
42
-
43
- # Get initial text
44
- text_box = page.get_by_label("Text to Handwrite")
45
- initial_text = text_box.input_value()
46
-
47
- # Click skip button
48
- page.get_by_role("button", name="Skip").click()
49
- page.wait_for_timeout(2000) # Wait for response
50
-
51
- # Get new text and verify it changed
52
- new_text = text_box.input_value()
53
- assert initial_text != new_text
54
-
55
- def test_upload_image(page, test_image):
56
- page.goto(GRADIO_URL)
57
- page.wait_for_load_state("networkidle")
58
-
59
- # Get initial text
60
- text_box = page.get_by_label("Text to Handwrite")
61
- initial_text = text_box.input_value()
62
-
63
- # Upload image - file input is hidden, but we can still set its value
64
- page.locator('input[type="file"]').set_input_files(test_image)
65
- page.wait_for_timeout(2000) # Wait for upload
66
-
67
- # Click submit to complete the upload
68
- page.get_by_role("button", name="Submit").click()
69
- page.wait_for_timeout(2000) # Wait for response
70
-
71
- # Verify text changed after submission
72
- new_text = text_box.input_value()
73
- assert initial_text != new_text
 
1
  import pytest
 
2
  from playwright.sync_api import expect
 
 
 
3
 
4
  # Constants
5
+ GRADIO_PORT = 7861
6
  GRADIO_URL = f"http://localhost:{GRADIO_PORT}"
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def test_page_loads(page):
9
  page.goto(GRADIO_URL)
10
  page.wait_for_load_state("networkidle")
11
 
12
  # Check if title is present with exact text
13
+ expect(page.locator("h2", has_text="Crowdsourcing Handwriting OCR Dataset")).to_be_visible()