Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving CAPTCHA Accessibility in DeepSeek v3 #232

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed DeepSeek_V3.pdf
Binary file not shown.
40 changes: 40 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Flask, render_template, request, redirect, url_for, flash
import hcaptcha

app = Flask(__name__)
app.secret_key = 'your_secret_key'

# Configure hCaptcha
hcaptcha_site_key = 'your_hcaptcha_site_key'
hcaptcha_secret_key = 'your_hcaptcha_secret_key'

@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
hcaptcha_response = request.form['h-captcha-response']
if hcaptcha.verify(hcaptcha_secret_key, hcaptcha_response):
# Process the sign-up form
flash('Sign-up successful!', 'success')
return redirect(url_for('login'))
else:
flash('hCaptcha verification failed. Please try again.', 'danger')
return render_template('signup.html', hcaptcha_site_key=hcaptcha_site_key)

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
hcaptcha_response = request.form['h-captcha-response']
if hcaptcha.verify(hcaptcha_secret_key, hcaptcha_response):
# Process the login form
flash('Login successful!', 'success')
return redirect(url_for('dashboard'))
else:
flash('hCaptcha verification failed. Please try again.', 'danger')
return render_template('login.html', hcaptcha_site_key=hcaptcha_site_key)

@app.route('/dashboard')
def dashboard():
return 'Welcome to the dashboard!'

if __name__ == '__main__':
app.run(debug=True)
Binary file removed figures/benchmark.png
Binary file not shown.
Binary file removed figures/niah.png
Binary file not shown.
15 changes: 15 additions & 0 deletions inference/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .config import ModelArgs
from .attention import MLA
from .moe import Gate, Expert, MoE
from .linear import Linear, ColumnParallelLinear, RowParallelLinear

__all__ = [
'ModelArgs',
'MLA',
'Gate',
'Expert',
'MoE',
'Linear',
'ColumnParallelLinear',
'RowParallelLinear'
]
25 changes: 25 additions & 0 deletions inference/models/attention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from .config import ModelArgs
from ..kernel import act_quant, weight_dequant, fp8_gemm

class MLA(nn.Module):
def __init__(self, args: ModelArgs):
super().__init__()
self.dim = args.dim
self.n_heads = args.n_heads
self.n_local_heads = args.n_heads // dist.get_world_size() if dist.is_initialized() else args.n_heads
self.q_lora_rank = args.q_lora_rank
self.kv_lora_rank = args.kv_lora_rank
self.qk_nope_head_dim = args.qk_nope_head_dim
self.qk_rope_head_dim = args.qk_rope_head_dim
self.qk_head_dim = args.qk_nope_head_dim + args.qk_rope_head_dim
self.v_head_dim = args.v_head_dim

# Initialize components (implementation from original MLA class)
# ... (rest of the MLA implementation)

def forward(self, x: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]):
# ... (MLA forward implementation)
36 changes: 36 additions & 0 deletions inference/models/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from dataclasses import dataclass
from typing import Literal

@dataclass
class ModelArgs:
max_batch_size: int = 8
max_seq_len: int = 4096 * 4
dtype: Literal["bf16", "fp8"] = "bf16"
vocab_size: int = 102400
dim: int = 2048
inter_dim: int = 10944
moe_inter_dim: int = 1408
n_layers: int = 27
n_dense_layers: int = 1
n_heads: int = 16
# moe
n_routed_experts: int = 64
n_shared_experts: int = 2
n_activated_experts: int = 6
n_expert_groups: int = 1
n_limited_groups: int = 1
score_func: Literal["softmax", "sigmoid"] = "softmax"
route_scale: float = 1.
# mla
q_lora_rank: int = 0
kv_lora_rank: int = 512
qk_nope_head_dim: int = 128
qk_rope_head_dim: int = 64
v_head_dim: int = 128
# yarn
original_seq_len: int = 4096
rope_theta: float = 10000.0
rope_factor: float = 40
beta_fast: int = 32
beta_slow: int = 1
mscale: float = 1.
28 changes: 28 additions & 0 deletions inference/models/linear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from ..kernel import act_quant, weight_dequant, fp8_gemm

class Linear(nn.Module):
dtype = torch.bfloat16

def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype = None):
# ... (Linear implementation)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# ... (Linear forward implementation)

class ColumnParallelLinear(Linear):
def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype = None):
# ... (ColumnParallelLinear implementation)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# ... (ColumnParallelLinear forward implementation)

class RowParallelLinear(Linear):
def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype = None):
# ... (RowParallelLinear implementation)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# ... (RowParallelLinear forward implementation)
30 changes: 30 additions & 0 deletions inference/models/moe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from .config import ModelArgs
from .linear import Linear, ColumnParallelLinear, RowParallelLinear

class Gate(nn.Module):
def __init__(self, args: ModelArgs):
super().__init__()
# ... (Gate implementation)

def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
# ... (Gate forward implementation)

class Expert(nn.Module):
def __init__(self, dim: int, inter_dim: int):
super().__init__()
# ... (Expert implementation)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# ... (Expert forward implementation)

class MoE(nn.Module):
def __init__(self, args: ModelArgs):
super().__init__()
# ... (MoE implementation)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# ... (MoE forward implementation)
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hcaptcha
flask
85 changes: 85 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* General styles for the form elements */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 0;
}

.container {
max-width: 500px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}

h1 {
text-align: center;
color: #333;
}

.form-group {
margin-bottom: 15px;
}

label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

input[type="text"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}

button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #0056b3;
}

/* Styles for hCaptcha widget */
.h-captcha {
margin: 20px 0;
}

/* High contrast and readability for visually impaired users */
body {
background-color: #000;
color: #fff;
}

input[type="text"],
input[type="email"],
input[type="password"] {
background-color: #333;
color: #fff;
border: 1px solid #555;
}

button {
background-color: #ffcc00;
color: #000;
}

button:hover {
background-color: #e6b800;
}
29 changes: 29 additions & 0 deletions templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
</head>
<body>
<div class="container">
<h1>Login</h1>
<form action="{{ url_for('login') }}" method="POST">
<div class="form-group">
<label for="username" aria-label="Username">Username</label>
<input type="text" id="username" name="username" required aria-required="true">
</div>
<div class="form-group">
<label for="password" aria-label="Password">Password</label>
<input type="password" id="password" name="password" required aria-required="true">
</div>
<div class="form-group">
<div class="h-captcha" data-sitekey="{{ hcaptcha_site_key }}" role="presentation" aria-hidden="true"></div>
</div>
<button type="submit" aria-label="Login">Login</button>
</form>
</div>
<script src="https://hcaptcha.com/1/api.js" async defer></script>
</body>
</html>
33 changes: 33 additions & 0 deletions templates/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Up</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
</head>
<body>
<div class="container">
<h1>Sign Up</h1>
<form action="{{ url_for('signup') }}" method="POST">
<div class="form-group">
<label for="username" aria-label="Username">Username</label>
<input type="text" id="username" name="username" required aria-required="true">
</div>
<div class="form-group">
<label for="email" aria-label="Email">Email</label>
<input type="email" id="email" name="email" required aria-required="true">
</div>
<div class="form-group">
<label for="password" aria-label="Password">Password</label>
<input type="password" id="password" name="password" required aria-required="true">
</div>
<div class="form-group">
<div class="h-captcha" data-sitekey="{{ hcaptcha_site_key }}" role="presentation" aria-hidden="true"></div>
</div>
<button type="submit" aria-label="Sign Up">Sign Up</button>
</form>
</div>
<script src="https://hcaptcha.com/1/api.js" async defer></script>
</body>
</html>
Loading