Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# π Load keys from Hugging Face Secrets (Settings β Repository secrets)
|
| 7 |
+
HCAPTCHA_SECRET = os.environ.get("HCAPTCHA_SECRET") # your secret key
|
| 8 |
+
HCAPTCHA_SITEKEY = os.environ.get("HCAPTCHA_SITEKEY") # your site key
|
| 9 |
+
|
| 10 |
+
# π Verify function
|
| 11 |
+
def verify_hcaptcha(token, username, password):
|
| 12 |
+
if not token:
|
| 13 |
+
return "β Captcha not solved."
|
| 14 |
+
|
| 15 |
+
url = "https://hcaptcha.com/siteverify"
|
| 16 |
+
data = {
|
| 17 |
+
"secret": HCAPTCHA_SECRET,
|
| 18 |
+
"response": token
|
| 19 |
+
}
|
| 20 |
+
result = requests.post(url, data=data).json()
|
| 21 |
+
|
| 22 |
+
if result.get("success"):
|
| 23 |
+
# You can extend this part to check username/password in a real app
|
| 24 |
+
return f"β
Success! Welcome, {username or 'user'}"
|
| 25 |
+
else:
|
| 26 |
+
return f"β Verification failed: {result}"
|
| 27 |
+
|
| 28 |
+
with gr.Blocks(css="""
|
| 29 |
+
body { font-family: 'Segoe UI', sans-serif; background: #f8f9fa; }
|
| 30 |
+
h2 { text-align:center; color:#333; }
|
| 31 |
+
.card {
|
| 32 |
+
background: white; border-radius: 16px; padding: 24px;
|
| 33 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.1); max-width: 400px; margin: auto;
|
| 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>π Login Form with hCaptcha</h2>")
|
| 42 |
+
|
| 43 |
+
with gr.Column(elem_classes="card"):
|
| 44 |
+
username = gr.Textbox(label="Username")
|
| 45 |
+
password = gr.Textbox(label="Password", type="password")
|
| 46 |
+
|
| 47 |
+
# Hidden box for hCaptcha token
|
| 48 |
+
token_box = gr.Textbox(label="hCaptcha Token", visible=False)
|
| 49 |
+
|
| 50 |
+
# Inject hCaptcha widget
|
| 51 |
+
gr.HTML(f"""
|
| 52 |
+
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
| 53 |
+
<div class="h-captcha" data-sitekey="{HCAPTCHA_SITEKEY}" data-callback="setToken"></div>
|
| 54 |
+
<script>
|
| 55 |
+
function setToken(token) {{
|
| 56 |
+
let textbox = document.querySelector('textarea[aria-label="hCaptcha Token"]');
|
| 57 |
+
if (textbox) {{
|
| 58 |
+
textbox.value = token;
|
| 59 |
+
textbox.dispatchEvent(new Event("input", {{ bubbles: true }}));
|
| 60 |
+
}}
|
| 61 |
+
}}
|
| 62 |
+
</script>
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
submit = gr.Button("Login", variant="primary")
|
| 67 |
+
|
| 68 |
+
loading = gr.HTML('<div id="loading" class="loading">β³ Verifying...</div>')
|
| 69 |
+
output = gr.Textbox(label="Result", interactive=False)
|
| 70 |
+
|
| 71 |
+
# Functions for loading state
|
| 72 |
+
def show_loading():
|
| 73 |
+
return gr.HTML.update(value='<div id="loading" class="loading" style="display:block;">β³ Verifying...</div>')
|
| 74 |
+
|
| 75 |
+
def hide_loading():
|
| 76 |
+
return gr.HTML.update(value='<div id="loading" class="loading" style="display:none;">β³ Verifying...</div>')
|
| 77 |
+
|
| 78 |
+
# Chain events: show loader β verify β hide loader
|
| 79 |
+
submit.click(fn=show_loading, inputs=None, outputs=loading).then(
|
| 80 |
+
fn=verify_hcaptcha, inputs=[token_box, username, password], outputs=output
|
| 81 |
+
).then(
|
| 82 |
+
fn=hide_loading, inputs=None, outputs=loading
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
demo.launch()
|