Update app.py
Browse files
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 "β
|
| 12 |
-
|
| 13 |
-
# simulate loading
|
| 14 |
-
time.sleep(1.5)
|
| 15 |
-
|
| 16 |
url = "https://hcaptcha.com/siteverify"
|
| 17 |
data = {"secret": HCAPTCHA_SECRET, "response": token}
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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 |
-
|
| 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 |
-
|
| 71 |
-
|
| 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()
|