Vibow commited on
Commit
2c13efa
Β·
verified Β·
1 Parent(s): 282dfcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -25
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import requests
3
  import gradio as gr
4
 
@@ -7,7 +8,10 @@ HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
7
 
8
  def verify_hcaptcha(token):
9
  if not token:
10
- return "❌ Captcha not solved (no token)."
 
 
 
11
 
12
  url = "https://hcaptcha.com/siteverify"
13
  data = {"secret": HCAPTCHA_SECRET, "response": token}
@@ -15,36 +19,56 @@ def verify_hcaptcha(token):
15
  r = requests.post(url, data=data, timeout=8)
16
  result = r.json()
17
  except Exception as e:
18
- return f"❌ Verification error: {e}"
19
 
20
  if result.get("success"):
21
- return "βœ… Verification passed β€” you are human!"
22
  else:
23
- return f"❌ Verification failed: {result}"
24
-
25
- with gr.Blocks() as demo:
26
 
27
- token_state = gr.State(value="") # store token here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  gr.HTML("<h2>πŸ”’ hCaptcha Verification</h2>")
30
 
31
- # Inject hCaptcha widget + JS that writes token to gr.State
32
- gr.HTML(f"""
33
- <script src="https://hcaptcha.com/1/api.js" async defer></script>
34
- <div style="display:flex; justify-content:center; margin:10px;">
35
- <div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
36
- </div>
37
- <script>
38
- function setToken(token) {{
39
- // trigger Gradio state update
40
- window.gradioApp().getComponentById('{token_state.elem_id}').setEventValue(token)
41
- }}
42
- </script>
43
- """)
44
-
45
- verify_btn = gr.Button("Verify", variant="primary")
46
- status = gr.Markdown("")
47
-
48
- verify_btn.click(fn=verify_hcaptcha, inputs=token_state, outputs=status)
 
 
 
 
 
 
 
 
49
 
50
  demo.launch()
 
1
  import os
2
+ import time
3
  import requests
4
  import gradio as gr
5
 
 
8
 
9
  def verify_hcaptcha(token):
10
  if not token:
11
+ return "❌ Captcha not solved.", "⚠️ No token received."
12
+
13
+ # simulate loading
14
+ time.sleep(1.5)
15
 
16
  url = "https://hcaptcha.com/siteverify"
17
  data = {"secret": HCAPTCHA_SECRET, "response": token}
 
19
  r = requests.post(url, data=data, timeout=8)
20
  result = r.json()
21
  except Exception as e:
22
+ return f"❌ Verification error: {e}", f"Token: {token}"
23
 
24
  if result.get("success"):
25
+ return "βœ… Verification passed β€” you are human!", f"πŸ”‘ Token:\n{token}"
26
  else:
27
+ return f"❌ Verification failed: {result.get('error-codes')}", f"πŸ”‘ Token:\n{token}"
 
 
28
 
29
+ with gr.Blocks(css="""
30
+ body { font-family: 'Segoe UI', sans-serif; background:#f5f7fa; }
31
+ h2 { text-align:center; margin:20px; color:#222; }
32
+ .card {
33
+ background:white; border-radius:14px; padding:20px;
34
+ box-shadow:0 4px 12px rgba(0,0,0,0.1);
35
+ max-width:500px; margin:20px auto;
36
+ }
37
+ button { border-radius:10px !important; font-weight:bold; }
38
+ .debug-box {
39
+ background:#f1f1f1; border-radius:8px; padding:10px;
40
+ font-family: monospace; font-size: 0.85em;
41
+ white-space: break-all;
42
+ }
43
+ """) as demo:
44
 
45
  gr.HTML("<h2>πŸ”’ hCaptcha Verification</h2>")
46
 
47
+ with gr.Column(elem_classes="card"):
48
+ token_state = gr.State(value="") # token storage
49
+
50
+ # hCaptcha widget
51
+ gr.HTML(f"""
52
+ <script src="https://hcaptcha.com/1/api.js" async defer></script>
53
+ <div style="display:flex; justify-content:center; margin:10px;">
54
+ <div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
55
+ </div>
56
+ <script>
57
+ function setToken(token) {{
58
+ window.gradioApp().getComponentById('{token_state.elem_id}').setEventValue(token)
59
+ }}
60
+ </script>
61
+ """)
62
+
63
+ verify_btn = gr.Button("Verify", variant="primary")
64
+ status = gr.Markdown("")
65
+ debug = gr.Markdown("", elem_classes="debug-box")
66
+
67
+ def show_loading():
68
+ return "⏳ Verifying...", "βŒ› Waiting for token..."
69
+
70
+ verify_btn.click(fn=show_loading, inputs=None, outputs=[status, debug]).then(
71
+ fn=verify_hcaptcha, inputs=token_state, outputs=[status, debug]
72
+ )
73
 
74
  demo.launch()