Update app.py
Browse files
app.py
CHANGED
|
@@ -3,35 +3,41 @@ import os, requests, gradio as gr
|
|
| 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 "
|
| 9 |
url = "https://hcaptcha.com/siteverify"
|
| 10 |
data = {"secret": HCAPTCHA_SECRET, "response": token}
|
| 11 |
result = requests.post(url, data=data).json()
|
| 12 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
with gr.Blocks() as demo:
|
| 15 |
-
|
| 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="
|
| 21 |
<script>
|
| 22 |
-
function
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 3 |
HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
|
| 4 |
HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
|
| 5 |
|
| 6 |
+
def verify_hcaptcha(token: str):
|
| 7 |
if not token:
|
| 8 |
+
return "<b style='color:red'>β No token received.</b>"
|
| 9 |
url = "https://hcaptcha.com/siteverify"
|
| 10 |
data = {"secret": HCAPTCHA_SECRET, "response": token}
|
| 11 |
result = requests.post(url, data=data).json()
|
| 12 |
+
return f"""
|
| 13 |
+
<div style='font-family:monospace; padding:10px; background:#f7f7f7; border-radius:8px;'>
|
| 14 |
+
<b>π Token:</b> {token}<br><br>
|
| 15 |
+
<b>β
Verification Result:</b><br>{result}
|
| 16 |
+
</div>
|
| 17 |
+
"""
|
| 18 |
|
| 19 |
with gr.Blocks() as demo:
|
| 20 |
+
output = gr.HTML("<i>β‘ Solve captcha to continue...</i>", elem_id="verify-output")
|
|
|
|
| 21 |
|
| 22 |
gr.HTML(f"""
|
| 23 |
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
| 24 |
+
<div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="sendToken"></div>
|
| 25 |
<script>
|
| 26 |
+
async function sendToken(token) {{
|
| 27 |
+
document.querySelector('#verify-output').innerHTML = "β³ Verifying token...";
|
| 28 |
+
try {{
|
| 29 |
+
const response = await fetch("/run/predict", {{
|
| 30 |
+
method: "POST",
|
| 31 |
+
headers: {{ "Content-Type": "application/json" }},
|
| 32 |
+
body: JSON.stringify({{ "data": [token], "fn_index": 0 }})
|
| 33 |
+
}});
|
| 34 |
+
const result = await response.json();
|
| 35 |
+
document.querySelector('#verify-output').innerHTML = result.data[0];
|
| 36 |
+
}} catch (e) {{
|
| 37 |
+
document.querySelector('#verify-output').innerHTML = "β Error: " + e;
|
| 38 |
}}
|
| 39 |
}}
|
| 40 |
</script>
|
| 41 |
""")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
demo.launch()
|