Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
gr.HTML("<h2>π hCaptcha Verification</h2>")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|