gtani commited on
Commit
6b20de6
Β·
verified Β·
1 Parent(s): 2fb7115

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -86
app.py CHANGED
@@ -5,6 +5,7 @@ from typing import Dict, List, Tuple
5
  import os
6
  import base64
7
 
 
8
  PROCESSED_DATA_DIR = Path(".")
9
  DATA_DIR = Path("./data")
10
  # Embed logo as a base64 data URI to avoid Gradio toolbar interactions
@@ -201,102 +202,81 @@ def download_annotations() -> str:
201
  # --------------
202
  def build_gradio_app():
203
  with gr.Blocks() as demo:
204
- with gr.Row():
205
- with gr.Column(scale=1):
206
- # Static logo, non-interactive
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  gr.HTML(
208
- f'<img src="data:image/png;base64,{logo_b64}" width="200" style="pointer-events:none; user-select:none; display:block;" />'
 
209
  )
210
- with gr.Row():
211
  gr.Markdown("# ISCO Annotation", elem_id="isco-title")
212
- gr.HTML("""
213
- <style>
214
- #isco-title {
215
- text-align: center;
216
- width: 100%;
217
- margin: 0.5em 0;
218
- }
219
- footer { display: none !important; }
220
- .gradio-container .api-link, .gradio-container .share-link { display: none !important; }
221
- </style>
222
- """)
223
 
224
- idx_state = gr.State(0)
225
-
226
- with gr.Group():
227
  record_md = gr.Markdown()
228
  status_md = gr.Markdown()
229
-
230
- with gr.Row():
231
  prev_btn = gr.Button("β¬… Previous")
232
  next_btn = gr.Button("βœ… Next")
233
-
234
- with gr.Row():
235
- with gr.Column():
236
- major_radio = gr.Radio(label="Level 1: Major", choices=[], interactive=True)
237
- with gr.Column():
238
- sub_radio = gr.Radio(label="Level 2: Sub-major", choices=[], interactive=True)
239
- with gr.Column():
240
- minor_radio = gr.Radio(label="Level 3: Minor", choices=[], interactive=True)
241
- with gr.Column():
242
- unit_radio = gr.Radio(label="Level 4: Unit", choices=[], interactive=True)
243
-
244
- download_btn = gr.Button("πŸ“₯ Download Annotations")
245
- download_file = gr.File(label="Annotated CSV", visible=False)
246
-
247
- # Initial load
248
- demo.load(
249
- lambda: (0,) + load_record(0),
250
- outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio],
251
- )
252
-
253
- next_btn.click(lambda i: save_and_jump(i, "next"),
254
- inputs=[idx_state],
255
- outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio])
256
-
257
- prev_btn.click(lambda i: save_and_jump(i, "prev"),
258
- inputs=[idx_state],
259
- outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio])
260
-
261
- # Change handlers (also update status)
262
- major_radio.change(
263
- on_major_change,
264
- inputs=[major_radio, idx_state],
265
- outputs=[major_radio, sub_radio, minor_radio, unit_radio, status_md],
266
- )
267
-
268
- sub_radio.change(
269
- on_sub_change,
270
- inputs=[sub_radio, idx_state, major_radio],
271
- outputs=[sub_radio, minor_radio, unit_radio, status_md],
272
- )
273
-
274
- minor_radio.change(
275
- on_minor_change,
276
- inputs=[minor_radio, idx_state, major_radio, sub_radio],
277
- outputs=[minor_radio, unit_radio, status_md],
278
- )
279
-
280
- unit_radio.change(
281
- on_unit_change,
282
- inputs=[unit_radio, idx_state, major_radio, sub_radio, minor_radio],
283
- outputs=[unit_radio, status_md],
284
- )
285
-
286
- # Download
287
- download_btn.click(download_annotations, outputs=[download_file]).then(
288
- lambda: gr.update(visible=True), None, [download_file]
289
- )
290
 
291
  return demo
292
 
293
  if __name__ == "__main__":
294
- USER = os.environ.get("APP_USER", "")
295
- PWD = os.environ.get("APP_PASS", "")
296
  demo = build_gradio_app()
297
- demo.queue().launch(
298
- server_name="0.0.0.0", # in a space or your own server
299
- show_api=False, # hides the β€œUse via API” link
300
- #auth=(USER, PWD), # basic-auth from env
301
- share=True # generate a public link
302
- )
 
5
  import os
6
  import base64
7
 
8
+
9
  PROCESSED_DATA_DIR = Path(".")
10
  DATA_DIR = Path("./data")
11
  # Embed logo as a base64 data URI to avoid Gradio toolbar interactions
 
202
  # --------------
203
  def build_gradio_app():
204
  with gr.Blocks() as demo:
205
+ # a flag in State for whether the user is logged in
206
+ is_auth = gr.State(False)
207
+
208
+ # β€”β€”β€”β€”β€”β€”β€”β€”
209
+ # LOGIN SCREEN
210
+ # β€”β€”β€”β€”β€”β€”β€”β€”
211
+ with gr.Column(visible=not is_auth.value) as login_box:
212
+ user_in = gr.Textbox(label="Username")
213
+ pass_in = gr.Textbox(label="Password", type="password")
214
+ login_btn= gr.Button("πŸ”’ Log me in")
215
+ login_msg= gr.Markdown("", visible=False)
216
+
217
+ def check_creds(u, p):
218
+ # load your secrets from env
219
+ USER = os.environ.get("APP_USER", "")
220
+ PWD = os.environ.get("APP_PASS", "")
221
+ if u == USER and p == PWD:
222
+ return gr.update(visible=False), gr.update(visible=True), gr.update(value=True, visible=False)
223
+ else:
224
+ return gr.update(visible=True, value="❌ Bad credentials!"), None, None
225
+
226
+ login_btn.click(
227
+ check_creds,
228
+ inputs=[user_in, pass_in],
229
+ outputs=[login_msg, login_box, is_auth]
230
+ )
231
+
232
+ # β€”β€”β€”β€”β€”β€”β€”β€”
233
+ # MAIN APP (hidden until auth)
234
+ # β€”β€”β€”β€”β€”β€”β€”β€”
235
+ with gr.Column(visible=is_auth.value) as app_box:
236
+ # your logo + title + everything else goes here…
237
+ with gr.Row():
238
+ # embed your base64 logo exactly where you want
239
  gr.HTML(
240
+ f'<img src="data:image/png;base64,{logo_b64}" '
241
+ 'style="pointer-events:none; user-select:none; width:200px; display:block;" />'
242
  )
 
243
  gr.Markdown("# ISCO Annotation", elem_id="isco-title")
 
 
 
 
 
 
 
 
 
 
 
244
 
245
+ # then all your States, buttons, radios, callbacks…
246
+ idx_state = gr.State(0)
 
247
  record_md = gr.Markdown()
248
  status_md = gr.Markdown()
 
 
249
  prev_btn = gr.Button("β¬… Previous")
250
  next_btn = gr.Button("βœ… Next")
251
+ major_radio = gr.Radio(label="Major", choices=[], interactive=True)
252
+ sub_radio = gr.Radio(label="Sub-major", choices=[], interactive=True)
253
+ minor_radio = gr.Radio(label="Minor", choices=[], interactive=True)
254
+ unit_radio = gr.Radio(label="Unit", choices=[], interactive=True)
255
+ download_btn = gr.Button("πŸ“₯ Download")
256
+ download_file= gr.File(visible=False)
257
+
258
+ # wire up your existing load_record, on_change, save_and_jump, etc.
259
+ demo.load(lambda: (0,) + load_record(0),
260
+ outputs=[idx_state, record_md, status_md,
261
+ major_radio, sub_radio, minor_radio, unit_radio])
262
+ next_btn.click(lambda i: save_and_jump(i, "next"),
263
+ inputs=[idx_state],
264
+ outputs=[idx_state, record_md, status_md,
265
+ major_radio, sub_radio, minor_radio, unit_radio])
266
+ # … and so on for prev_btn, radio.change handlers, download_btn …
267
+
268
+ # hide Gradio footer & share links:
269
+ gr.HTML("""
270
+ <style>
271
+ footer { display: none !important; }
272
+ .gradio-container .api-link,
273
+ .gradio-container .share-link { display: none !important; }
274
+ #isco-title { text-align: center; margin-top: 1em; }
275
+ </style>
276
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
278
  return demo
279
 
280
  if __name__ == "__main__":
 
 
281
  demo = build_gradio_app()
282
+ demo.queue().launch(show_api=False, share=True, server_name="0.0.0.0")