SeaWolf-AI commited on
Commit
d38bbef
·
verified ·
1 Parent(s): 020204d

feat: instant language re-render of displayed results (cache scan in State)

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -350,33 +350,34 @@ def _loading(lang):
350
 
351
  def free_scan(url, lang):
352
  if not (url or "").strip():
353
- return _note(_L("진단할 주소를 입력하세요. 예: example.com", "Enter a URL to scan. e.g. example.com", lang), "mid")
354
- return build_teaser(_api("/scan", {"url": url}), lang)
 
355
 
356
 
357
  def deep_scan(url, lang, profile: gr.OAuthProfile | None = None, oauth_token: gr.OAuthToken | None = None):
358
  if profile is None or oauth_token is None:
359
  return _note(_L('먼저 위의 <b>"Sign in with Hugging Face"</b> 버튼으로 로그인하세요. (회사/조직 도메인 메일로 가입한 계정)',
360
- 'First sign in with the <b>"Sign in with Hugging Face"</b> button above. (an account registered on your company/org domain email)', lang), "mid"), gr.update(visible=False)
361
  if not (url or "").strip():
362
- return _note(_L("진단할 주소를 입력하세요.", "Enter a URL to scan.", lang), "mid"), gr.update(visible=False)
363
  try:
364
  email = (whoami(token=oauth_token.token).get("email") or "").lower()
365
  except Exception:
366
  email = ""
367
  if not email:
368
  return _note(_L("로그인 계정의 인증된 이메일을 읽지 못했습니다. HF 계정 이메일 인증 후 다시 시도하세요.",
369
- "Could not read a verified email from your account. Verify your HF account email and try again.", lang), "hi"), gr.update(visible=False)
370
  edom = email.split("@")[-1]
371
  tdom = _domain(url)
372
  if edom != tdom and not tdom.endswith("." + edom) and not edom.endswith("." + tdom):
373
  return _note(_L("도메인 소유 확인 실패. 진단 대상(<b>%s</b>)의 도메인 이메일로 가입한 HF 계정으로 로그인해야 합니다. 현재 계정 도메인: <b>%s</b>" % (html.escape(tdom), html.escape(edom)),
374
- "Ownership check failed. Sign in with an HF account on the target domain (<b>%s</b>). Your account domain: <b>%s</b>" % (html.escape(tdom), html.escape(edom)), lang), "hi"), gr.update(visible=False)
375
  d = _api("/deep", {"url": url})
376
  body = build_report(d, deep=True, verified_email=email, lang=lang)
377
  if d.get("error") or d.get("blocked"):
378
- return body, gr.update(visible=False)
379
- return body, gr.update(value=_save_html(body, d.get("host", "site"), True), visible=True)
380
 
381
 
382
  def consult(area, name, company, email, phone, msg, lang):
@@ -590,15 +591,19 @@ def footer_html(lang):
590
  "Non-intrusive & non-destructive · no unauthorized deep scans of others' sites · active testing only after domain verification/authorization · Service: QuantID · Engine: CHITOS", lang) + '</div>'
591
 
592
 
593
- def set_lang(label):
594
  lang = "en" if str(label).startswith("Eng") else "ko"
 
 
595
  return (lang, gr.update(value=label), header_html(lang), desc1(lang), desc2(lang), desc3(lang),
596
- area_help(lang), guideline_html(lang), footer_html(lang))
597
 
598
 
599
  CSS = "#title{text-align:center}.gradio-container{max-width:980px!important;margin:0 auto}"
600
  with gr.Blocks(title="CHITOS 인스턴트 스캔", css=CSS, theme=gr.themes.Soft(primary_hue="purple")) as demo:
601
  lang_state = gr.State("ko")
 
 
602
  with gr.Row():
603
  gr.HTML("")
604
  lang_radio = gr.Radio(["한국어", "English"], value="한국어", label="", container=False, scale=0, min_width=220)
@@ -610,8 +615,8 @@ with gr.Blocks(title="CHITOS 인스턴트 스캔", css=CSS, theme=gr.themes.Soft
610
  u1 = gr.Textbox(label="", placeholder="example.com", scale=4, container=False)
611
  b1 = gr.Button("진단 / Scan", variant="primary", scale=1)
612
  o1 = gr.HTML()
613
- b1.click(_spin1, lang_state, o1).then(free_scan, [u1, lang_state], o1)
614
- u1.submit(_spin1, lang_state, o1).then(free_scan, [u1, lang_state], o1)
615
  with gr.Tab("② 심화 · DEEP"):
616
  g_d2 = gr.HTML(desc2("ko"))
617
  gr.LoginButton()
@@ -619,7 +624,7 @@ with gr.Blocks(title="CHITOS 인스턴트 스캔", css=CSS, theme=gr.themes.Soft
619
  b2 = gr.Button("인증 후 심화 진단 / Verify & deep scan", variant="primary")
620
  f2 = gr.File(label="리포트 다운로드 / Download report (HTML)", visible=False)
621
  o2 = gr.HTML()
622
- b2.click(_loading, lang_state, [o2, f2]).then(deep_scan, inputs=[u2, lang_state], outputs=[o2, f2])
623
  with gr.Tab("③ 상담 · CONSULT"):
624
  g_d3 = gr.HTML(desc3("ko"))
625
  g_ah = gr.HTML(area_help("ko"))
@@ -635,10 +640,11 @@ with gr.Blocks(title="CHITOS 인스턴트 스캔", css=CSS, theme=gr.themes.Soft
635
  g_gl = gr.HTML(guideline_html("ko"))
636
  g_ft = gr.HTML(footer_html("ko"))
637
 
638
- _outs = [lang_state, lang_radio, hdr, g_d1, g_d2, g_d3, g_ah, g_gl, g_ft]
639
- lang_radio.change(set_lang, lang_radio, _outs)
640
- demo.load(set_lang, lang_radio, _outs,
641
- js="() => [ (navigator.language||'en').toLowerCase().startsWith('ko') ? '한국어' : 'English' ]")
 
642
 
643
  if __name__ == "__main__":
644
  demo.launch()
 
350
 
351
  def free_scan(url, lang):
352
  if not (url or "").strip():
353
+ return _note(_L("진단할 주소를 입력하세요. 예: example.com", "Enter a URL to scan. e.g. example.com", lang), "mid"), None
354
+ d = _api("/scan", {"url": url})
355
+ return build_teaser(d, lang), d
356
 
357
 
358
  def deep_scan(url, lang, profile: gr.OAuthProfile | None = None, oauth_token: gr.OAuthToken | None = None):
359
  if profile is None or oauth_token is None:
360
  return _note(_L('먼저 위의 <b>"Sign in with Hugging Face"</b> 버튼으로 로그인하세요. (회사/조직 도메인 메일로 가입한 계정)',
361
+ 'First sign in with the <b>"Sign in with Hugging Face"</b> button above. (an account registered on your company/org domain email)', lang), "mid"), gr.update(visible=False), None
362
  if not (url or "").strip():
363
+ return _note(_L("진단할 주소를 입력하세요.", "Enter a URL to scan.", lang), "mid"), gr.update(visible=False), None
364
  try:
365
  email = (whoami(token=oauth_token.token).get("email") or "").lower()
366
  except Exception:
367
  email = ""
368
  if not email:
369
  return _note(_L("로그인 계정의 인증된 이메일을 읽지 못했습니다. HF 계정 이메일 인증 후 다시 시도하세요.",
370
+ "Could not read a verified email from your account. Verify your HF account email and try again.", lang), "hi"), gr.update(visible=False), None
371
  edom = email.split("@")[-1]
372
  tdom = _domain(url)
373
  if edom != tdom and not tdom.endswith("." + edom) and not edom.endswith("." + tdom):
374
  return _note(_L("도메인 소유 확인 실패. 진단 대상(<b>%s</b>)의 도메인 이메일로 가입한 HF 계정으로 로그인해야 합니다. 현재 계정 도메인: <b>%s</b>" % (html.escape(tdom), html.escape(edom)),
375
+ "Ownership check failed. Sign in with an HF account on the target domain (<b>%s</b>). Your account domain: <b>%s</b>" % (html.escape(tdom), html.escape(edom)), lang), "hi"), gr.update(visible=False), None
376
  d = _api("/deep", {"url": url})
377
  body = build_report(d, deep=True, verified_email=email, lang=lang)
378
  if d.get("error") or d.get("blocked"):
379
+ return body, gr.update(visible=False), None
380
+ return body, gr.update(value=_save_html(body, d.get("host", "site"), True), visible=True), {"d": d, "email": email}
381
 
382
 
383
  def consult(area, name, company, email, phone, msg, lang):
 
591
  "Non-intrusive & non-destructive · no unauthorized deep scans of others' sites · active testing only after domain verification/authorization · Service: QuantID · Engine: CHITOS", lang) + '</div>'
592
 
593
 
594
+ def relang(label, last_scan, last_deep):
595
  lang = "en" if str(label).startswith("Eng") else "ko"
596
+ o1u = gr.update(value=build_teaser(last_scan, lang)) if last_scan else gr.update()
597
+ o2u = gr.update(value=build_report(last_deep["d"], deep=True, verified_email=last_deep.get("email"), lang=lang)) if last_deep else gr.update()
598
  return (lang, gr.update(value=label), header_html(lang), desc1(lang), desc2(lang), desc3(lang),
599
+ area_help(lang), guideline_html(lang), footer_html(lang), o1u, o2u)
600
 
601
 
602
  CSS = "#title{text-align:center}.gradio-container{max-width:980px!important;margin:0 auto}"
603
  with gr.Blocks(title="CHITOS 인스턴트 스캔", css=CSS, theme=gr.themes.Soft(primary_hue="purple")) as demo:
604
  lang_state = gr.State("ko")
605
+ last_scan = gr.State(None)
606
+ last_deep = gr.State(None)
607
  with gr.Row():
608
  gr.HTML("")
609
  lang_radio = gr.Radio(["한국어", "English"], value="한국어", label="", container=False, scale=0, min_width=220)
 
615
  u1 = gr.Textbox(label="", placeholder="example.com", scale=4, container=False)
616
  b1 = gr.Button("진단 / Scan", variant="primary", scale=1)
617
  o1 = gr.HTML()
618
+ b1.click(_spin1, lang_state, o1).then(free_scan, [u1, lang_state], [o1, last_scan])
619
+ u1.submit(_spin1, lang_state, o1).then(free_scan, [u1, lang_state], [o1, last_scan])
620
  with gr.Tab("② 심화 · DEEP"):
621
  g_d2 = gr.HTML(desc2("ko"))
622
  gr.LoginButton()
 
624
  b2 = gr.Button("인증 후 심화 진단 / Verify & deep scan", variant="primary")
625
  f2 = gr.File(label="리포트 다운로드 / Download report (HTML)", visible=False)
626
  o2 = gr.HTML()
627
+ b2.click(_loading, lang_state, [o2, f2]).then(deep_scan, inputs=[u2, lang_state], outputs=[o2, f2, last_deep])
628
  with gr.Tab("③ 상담 · CONSULT"):
629
  g_d3 = gr.HTML(desc3("ko"))
630
  g_ah = gr.HTML(area_help("ko"))
 
640
  g_gl = gr.HTML(guideline_html("ko"))
641
  g_ft = gr.HTML(footer_html("ko"))
642
 
643
+ _ins = [lang_radio, last_scan, last_deep]
644
+ _outs = [lang_state, lang_radio, hdr, g_d1, g_d2, g_d3, g_ah, g_gl, g_ft, o1, o2]
645
+ lang_radio.change(relang, _ins, _outs)
646
+ demo.load(relang, _ins, _outs,
647
+ js="() => [ (navigator.language||'en').toLowerCase().startsWith('ko') ? '한국어' : 'English', null, null ]")
648
 
649
  if __name__ == "__main__":
650
  demo.launch()