Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,65 @@
|
|
| 1 |
import os
|
| 2 |
-
import time
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
|
| 8 |
-
HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
if not token:
|
| 13 |
return "β Captcha not solved."
|
| 14 |
|
| 15 |
url = "https://hcaptcha.com/siteverify"
|
| 16 |
-
data = {
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
if result.get("success"):
|
| 23 |
-
|
| 24 |
-
return f"β
Success! Welcome, {username or 'user'}"
|
| 25 |
else:
|
| 26 |
-
return
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with gr.Blocks(css="""
|
| 29 |
-
body { font-family: 'Segoe UI', sans-serif; background: #
|
| 30 |
-
h2 { text-align:center; color:#
|
| 31 |
.card {
|
| 32 |
-
background: white; border-radius:
|
| 33 |
-
box-shadow: 0
|
| 34 |
-
|
| 35 |
-
button { border-radius: 12px !important; font-weight: bold; }
|
| 36 |
-
.loading {
|
| 37 |
-
display:none; text-align:center; margin-top:10px;
|
| 38 |
}
|
|
|
|
| 39 |
""") as demo:
|
| 40 |
|
| 41 |
-
gr.HTML("<h2>π
|
| 42 |
|
| 43 |
with gr.Column(elem_classes="card"):
|
| 44 |
-
|
| 45 |
-
password = gr.Textbox(label="Password", type="password")
|
| 46 |
|
| 47 |
-
# Hidden
|
| 48 |
token_box = gr.Textbox(label="hCaptcha Token", visible=False)
|
| 49 |
|
| 50 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
gr.HTML(f"""
|
| 52 |
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
| 53 |
-
<div
|
|
|
|
|
|
|
| 54 |
<script>
|
| 55 |
function setToken(token) {{
|
|
|
|
| 56 |
let textbox = document.querySelector('textarea[aria-label="hCaptcha Token"]');
|
| 57 |
if (textbox) {{
|
| 58 |
textbox.value = token;
|
|
@@ -62,22 +69,19 @@ button { border-radius: 12px !important; font-weight: bold; }
|
|
| 62 |
</script>
|
| 63 |
""")
|
| 64 |
|
| 65 |
-
with gr.Row():
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
loading = gr.HTML('<div id="loading" class="loading">β³ Verifying...</div>')
|
| 69 |
-
output = gr.Textbox(label="Result", interactive=False)
|
| 70 |
|
| 71 |
-
# Functions
|
| 72 |
def show_loading():
|
| 73 |
-
return gr.
|
| 74 |
|
| 75 |
def hide_loading():
|
| 76 |
-
return gr.
|
| 77 |
|
| 78 |
-
# Chain
|
| 79 |
-
|
| 80 |
-
fn=verify_hcaptcha, inputs=
|
| 81 |
).then(
|
| 82 |
fn=hide_loading, inputs=None, outputs=loading
|
| 83 |
)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load keys from HF Secrets (Settings β Repository secrets)
|
| 6 |
+
HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET")
|
| 7 |
+
HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY")
|
| 8 |
|
| 9 |
+
def verify_hcaptcha(token):
|
| 10 |
+
"""Verify hCaptcha token with hcaptcha.com/siteverify"""
|
| 11 |
if not token:
|
| 12 |
return "β Captcha not solved."
|
| 13 |
|
| 14 |
url = "https://hcaptcha.com/siteverify"
|
| 15 |
+
data = {"secret": HCAPTCHA_SECRET, "response": token}
|
| 16 |
+
try:
|
| 17 |
+
r = requests.post(url, data=data, timeout=8)
|
| 18 |
+
result = r.json()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"β Verification error: {e}"
|
| 21 |
|
| 22 |
if result.get("success"):
|
| 23 |
+
return "β
Verification passed β you are human!"
|
|
|
|
| 24 |
else:
|
| 25 |
+
# return short, useful message for debugging
|
| 26 |
+
msg = result.get("error-codes") or result
|
| 27 |
+
return f"β Verification failed: {msg}"
|
| 28 |
|
| 29 |
with gr.Blocks(css="""
|
| 30 |
+
body { font-family: 'Segoe UI', sans-serif; background: #f4f6f8; }
|
| 31 |
+
h2 { text-align:center; color:#222; margin-top:18px; }
|
| 32 |
.card {
|
| 33 |
+
background: white; border-radius: 12px; padding: 20px;
|
| 34 |
+
box-shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
|
| 35 |
+
max-width: 520px; margin: 20px auto;
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
+
.button-row { display:flex; justify-content:center; gap:12px; margin-top:12px; }
|
| 38 |
""") as demo:
|
| 39 |
|
| 40 |
+
gr.HTML("<h2>π Let's verify!</h2>")
|
| 41 |
|
| 42 |
with gr.Column(elem_classes="card"):
|
| 43 |
+
gr.Markdown("Complete the hCaptcha widget below, then click **Verify**. Loading indicator will show while verifying.")
|
|
|
|
| 44 |
|
| 45 |
+
# Hidden token holder (will be filled by hCaptcha JS)
|
| 46 |
token_box = gr.Textbox(label="hCaptcha Token", visible=False)
|
| 47 |
|
| 48 |
+
# Loading textbox (visible) - we'll update this with .update()
|
| 49 |
+
loading = gr.Textbox(value="", interactive=False, label="", visible=True)
|
| 50 |
+
|
| 51 |
+
# Result/output textbox
|
| 52 |
+
output = gr.Textbox(label="Result", interactive=False)
|
| 53 |
+
|
| 54 |
+
# Inject hCaptcha widget + JS that writes token into the hidden textbox
|
| 55 |
gr.HTML(f"""
|
| 56 |
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
| 57 |
+
<div style="display:flex; justify-content:center; margin-top:10px;">
|
| 58 |
+
<div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
|
| 59 |
+
</div>
|
| 60 |
<script>
|
| 61 |
function setToken(token) {{
|
| 62 |
+
// Find the hidden Gradio textarea by aria-label
|
| 63 |
let textbox = document.querySelector('textarea[aria-label="hCaptcha Token"]');
|
| 64 |
if (textbox) {{
|
| 65 |
textbox.value = token;
|
|
|
|
| 69 |
</script>
|
| 70 |
""")
|
| 71 |
|
| 72 |
+
with gr.Row(elem_classes="button-row"):
|
| 73 |
+
verify_btn = gr.Button("Verify", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Functions to control loading (must return Textbox.update)
|
| 76 |
def show_loading():
|
| 77 |
+
return gr.Textbox.update(value="β³ Verifying...", interactive=False)
|
| 78 |
|
| 79 |
def hide_loading():
|
| 80 |
+
return gr.Textbox.update(value="", interactive=False)
|
| 81 |
|
| 82 |
+
# Chain: show loading -> verify -> hide loading
|
| 83 |
+
verify_btn.click(fn=show_loading, inputs=None, outputs=loading).then(
|
| 84 |
+
fn=verify_hcaptcha, inputs=token_box, outputs=output
|
| 85 |
).then(
|
| 86 |
fn=hide_loading, inputs=None, outputs=loading
|
| 87 |
)
|