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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -62
app.py CHANGED
@@ -1,74 +1,37 @@
1
- import os
2
- import time
3
- import requests
4
- import gradio as gr
5
 
6
  HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
7
  HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
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}
18
- try:
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()
 
1
+ import os, requests, gradio as gr
 
 
 
2
 
3
  HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
4
  HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
5
 
6
  def verify_hcaptcha(token):
7
  if not token:
8
+ return "❌ No token received."
 
 
 
 
9
  url = "https://hcaptcha.com/siteverify"
10
  data = {"secret": HCAPTCHA_SECRET, "response": token}
11
+ result = requests.post(url, data=data).json()
12
+ return f"πŸ” Token = {token}\nβœ… Result: {result}"
13
+
14
+ with gr.Blocks() as demo:
15
+ # token box kelihatan biar gampang debug
16
+ token_box = gr.Textbox(label="hCaptcha Token (Debug Visible)", visible=True, elem_id="hcaptcha-token")
17
+
18
+ gr.HTML(f"""
19
+ <script src="https://hcaptcha.com/1/api.js" async defer></script>
20
+ <div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
21
+ <script>
22
+ function setToken(token) {{
23
+ // debug visible
24
+ let textbox = document.getElementById("hcaptcha-token");
25
+ if (textbox) {{
26
+ textbox.value = token;
27
+ textbox.dispatchEvent(new Event("input", {{ bubbles: true }}));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }}
29
+ }}
30
+ </script>
31
+ """)
 
 
 
 
 
 
32
 
33
+ btn = gr.Button("Verify Captcha")
34
+ output = gr.Textbox(label="Result", lines=4)
35
+ btn.click(fn=verify_hcaptcha, inputs=[token_box], outputs=[output])
36
 
37
  demo.launch()