Vibow commited on
Commit
36a49c2
Β·
verified Β·
1 Parent(s): 320bf72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -39
app.py CHANGED
@@ -1,58 +1,65 @@
1
  import os
2
- import time
3
  import requests
4
  import gradio as gr
5
 
6
- # πŸ”‘ Load keys from Hugging Face Secrets (Settings β†’ Repository secrets)
7
- HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET") # your secret key
8
- HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY") # your site key
9
 
10
- # πŸš€ Verify function
11
- def verify_hcaptcha(token, username, password):
12
  if not token:
13
  return "❌ Captcha not solved."
14
 
15
  url = "https://hcaptcha.com/siteverify"
16
- data = {
17
- "secret": HCAPTCHA_SECRET,
18
- "response": token
19
- }
20
- result = requests.post(url, data=data).json()
 
21
 
22
  if result.get("success"):
23
- # You can extend this part to check username/password in a real app
24
- return f"βœ… Success! Welcome, {username or 'user'}"
25
  else:
26
- return f"❌ Verification failed: {result}"
 
 
27
 
28
  with gr.Blocks(css="""
29
- body { font-family: 'Segoe UI', sans-serif; background: #f8f9fa; }
30
- h2 { text-align:center; color:#333; }
31
  .card {
32
- background: white; border-radius: 16px; padding: 24px;
33
- box-shadow: 0 4px 10px rgba(0,0,0,0.1); max-width: 400px; margin: auto;
34
- }
35
- button { border-radius: 12px !important; font-weight: bold; }
36
- .loading {
37
- display:none; text-align:center; margin-top:10px;
38
  }
 
39
  """) as demo:
40
 
41
- gr.HTML("<h2>πŸ”’ Login Form with hCaptcha</h2>")
42
 
43
  with gr.Column(elem_classes="card"):
44
- username = gr.Textbox(label="Username")
45
- password = gr.Textbox(label="Password", type="password")
46
 
47
- # Hidden box for hCaptcha token
48
  token_box = gr.Textbox(label="hCaptcha Token", visible=False)
49
 
50
- # Inject hCaptcha widget
 
 
 
 
 
 
51
  gr.HTML(f"""
52
  <script src="https://hcaptcha.com/1/api.js" async defer></script>
53
- <div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
 
 
54
  <script>
55
  function setToken(token) {{
 
56
  let textbox = document.querySelector('textarea[aria-label="hCaptcha Token"]');
57
  if (textbox) {{
58
  textbox.value = token;
@@ -62,22 +69,19 @@ button { border-radius: 12px !important; font-weight: bold; }
62
  </script>
63
  """)
64
 
65
- with gr.Row():
66
- submit = gr.Button("Login", variant="primary")
67
-
68
- loading = gr.HTML('<div id="loading" class="loading">⏳ Verifying...</div>')
69
- output = gr.Textbox(label="Result", interactive=False)
70
 
71
- # Functions for loading state
72
  def show_loading():
73
- return gr.HTML.update(value='<div id="loading" class="loading" style="display:block;">⏳ Verifying...</div>')
74
 
75
  def hide_loading():
76
- return gr.HTML.update(value='<div id="loading" class="loading" style="display:none;">⏳ Verifying...</div>')
77
 
78
- # Chain events: show loader β†’ verify β†’ hide loader
79
- submit.click(fn=show_loading, inputs=None, outputs=loading).then(
80
- fn=verify_hcaptcha, inputs=[token_box, username, password], outputs=output
81
  ).then(
82
  fn=hide_loading, inputs=None, outputs=loading
83
  )
 
1
  import os
 
2
  import requests
3
  import gradio as gr
4
 
5
+ # Load keys from HF Secrets (Settings β†’ Repository secrets)
6
+ HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
7
+ HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
8
 
9
+ def verify_hcaptcha(token):
10
+ """Verify hCaptcha token with hcaptcha.com/siteverify"""
11
  if not token:
12
  return "❌ Captcha not solved."
13
 
14
  url = "https://hcaptcha.com/siteverify"
15
+ data = {"secret": HCAPTCHA_SECRET, "response": token}
16
+ try:
17
+ r = requests.post(url, data=data, timeout=8)
18
+ result = r.json()
19
+ except Exception as e:
20
+ return f"❌ Verification error: {e}"
21
 
22
  if result.get("success"):
23
+ return "βœ… Verification passed β€” you are human!"
 
24
  else:
25
+ # return short, useful message for debugging
26
+ msg = result.get("error-codes") or result
27
+ return f"❌ Verification failed: {msg}"
28
 
29
  with gr.Blocks(css="""
30
+ body { font-family: 'Segoe UI', sans-serif; background: #f4f6f8; }
31
+ h2 { text-align:center; color:#222; margin-top:18px; }
32
  .card {
33
+ background: white; border-radius: 12px; padding: 20px;
34
+ box-shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
35
+ max-width: 520px; margin: 20px auto;
 
 
 
36
  }
37
+ .button-row { display:flex; justify-content:center; gap:12px; margin-top:12px; }
38
  """) as demo:
39
 
40
+ gr.HTML("<h2>πŸ”’ Let's verify!</h2>")
41
 
42
  with gr.Column(elem_classes="card"):
43
+ gr.Markdown("Complete the hCaptcha widget below, then click **Verify**. Loading indicator will show while verifying.")
 
44
 
45
+ # Hidden token holder (will be filled by hCaptcha JS)
46
  token_box = gr.Textbox(label="hCaptcha Token", visible=False)
47
 
48
+ # Loading textbox (visible) - we'll update this with .update()
49
+ loading = gr.Textbox(value="", interactive=False, label="", visible=True)
50
+
51
+ # Result/output textbox
52
+ output = gr.Textbox(label="Result", interactive=False)
53
+
54
+ # Inject hCaptcha widget + JS that writes token into the hidden textbox
55
  gr.HTML(f"""
56
  <script src="https://hcaptcha.com/1/api.js" async defer></script>
57
+ <div style="display:flex; justify-content:center; margin-top:10px;">
58
+ <div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
59
+ </div>
60
  <script>
61
  function setToken(token) {{
62
+ // Find the hidden Gradio textarea by aria-label
63
  let textbox = document.querySelector('textarea[aria-label="hCaptcha Token"]');
64
  if (textbox) {{
65
  textbox.value = token;
 
69
  </script>
70
  """)
71
 
72
+ with gr.Row(elem_classes="button-row"):
73
+ verify_btn = gr.Button("Verify", variant="primary")
 
 
 
74
 
75
+ # Functions to control loading (must return Textbox.update)
76
  def show_loading():
77
+ return gr.Textbox.update(value="⏳ Verifying...", interactive=False)
78
 
79
  def hide_loading():
80
+ return gr.Textbox.update(value="", interactive=False)
81
 
82
+ # Chain: show loading -> verify -> hide loading
83
+ verify_btn.click(fn=show_loading, inputs=None, outputs=loading).then(
84
+ fn=verify_hcaptcha, inputs=token_box, outputs=output
85
  ).then(
86
  fn=hide_loading, inputs=None, outputs=loading
87
  )