| """ |
| One-File Algorithms - HF Project #5 |
| Complex AI concepts in single Python files with zero dependencies |
| """ |
|
|
| import gradio as gr |
| import sys |
| import os |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
|
|
| from shared.components import create_premium_hero, create_footer |
|
|
| |
| |
| |
|
|
| custom_css = """ |
| .gradio-container { |
| font-family: 'Inter', sans-serif; |
| background: |
| radial-gradient(circle at top left, rgba(78, 205, 196, 0.12), transparent 28%), |
| radial-gradient(circle at top right, rgba(255, 107, 107, 0.10), transparent 30%); |
| } |
| |
| .algorithm-shell { |
| background: rgba(255,255,255,0.05); |
| border: 1px solid rgba(255,255,255,0.10); |
| border-radius: 20px; |
| padding: 1rem; |
| box-shadow: 0 18px 36px rgba(0,0,0,0.14); |
| } |
| """ |
|
|
| ALGORITHMS = { |
| "attention": { |
| "name": "Attention Mechanism", |
| "description": "Self-attention from 'Attention Is All You Need'", |
| "code": '''""" |
| Self-Attention Mechanism |
| From "Attention Is All You Need" (Vaswani et al., 2017) |
| |
| Zero dependencies - Pure Python + Basic Math |
| """ |
| |
| def softmax(x): |
| """Numerically stable softmax""" |
| exp_x = [math.exp(i - max(x)) for i in x] |
| sum_exp = sum(exp_x) |
| return [i / sum_exp for i in exp_x] |
| |
| def dot_product(a, b): |
| """Dot product of two vectors""" |
| return sum(x * y for x, y in zip(a, b)) |
| |
| def matrix_multiply(A, B): |
| """Matrix multiplication""" |
| return [[sum(a * b for a, b in zip(row, col)) |
| for col in zip(*B)] |
| for row in A] |
| |
| def self_attention(queries, keys, values, d_k): |
| """ |
| Compute self-attention |
| |
| Args: |
| queries: List of query vectors |
| keys: List of key vectors |
| values: List of value vectors |
| d_k: Dimension of keys (for scaling) |
| |
| Returns: |
| Attention output vectors |
| """ |
| # Step 1: Compute attention scores (Q @ K^T) |
| scores = [] |
| for q in queries: |
| score_row = [] |
| for k in keys: |
| score = dot_product(q, k) / (d_k ** 0.5) # Scaled dot-product |
| score_row.append(score) |
| scores.append(score_row) |
| |
| # Step 2: Apply softmax to get attention weights |
| attention_weights = [softmax(row) for row in scores] |
| |
| # Step 3: Weighted sum of values |
| output = [] |
| for weights in attention_weights: |
| out_vec = [0] * len(values[0]) |
| for w, v in zip(weights, values): |
| out_vec = [o + w * val for o, val in zip(out_vec, v)] |
| output.append(out_vec) |
| |
| return output, attention_weights |
| |
| # Example Usage |
| if __name__ == "__main__": |
| import math |
| |
| # Input vectors (embedding_dim = 4) |
| queries = [[1, 0, 1, 0], [0, 1, 0, 1], [1, 1, 0, 0]] |
| keys = [[1, 0, 1, 0], [0, 1, 0, 1], [1, 1, 0, 0]] |
| values = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] |
| |
| d_k = 4 # Key dimension |
| |
| # Compute attention |
| output, weights = self_attention(queries, keys, values, d_k) |
| |
| print("Input Queries:", queries) |
| print("\\nAttention Weights:") |
| for i, w in enumerate(weights): |
| print(f" Query {i}: {[f'{x:.3f}' for x in w]}") |
| print("\\nOutput:") |
| for i, o in enumerate(output): |
| print(f" Position {i}: {[f'{x:.2f}' for x in o]}") |
| ''' |
| }, |
| "transformer_block": { |
| "name": "Transformer Block", |
| "description": "Complete transformer encoder block", |
| "code": '''""" |
| Transformer Encoder Block |
| Minimal implementation with multi-head attention |
| """ |
| |
| import math |
| |
| class TransformerBlock: |
| """Single transformer encoder block""" |
| |
| def __init__(self, d_model, num_heads, d_ff): |
| self.d_model = d_model |
| self.num_heads = num_heads |
| self.d_ff = d_ff |
| self.head_dim = d_model // num_heads |
| |
| def layer_norm(self, x, eps=1e-6): |
| """Layer normalization""" |
| mean = sum(x) / len(x) |
| variance = sum((xi - mean) ** 2 for xi in x) / len(x) |
| return [(xi - mean) / math.sqrt(variance + eps) for xi in x] |
| |
| def feedforward(self, x): |
| """Two-layer feedforward network with ReLU""" |
| # FFN(x) = max(0, xW1 + b1)W2 + b2 |
| # Simplified: just apply ReLU and scale |
| hidden = [max(0, xi * 2) for xi in x] |
| output = [hi * 0.5 for hi in hidden] |
| return output |
| |
| def forward(self, x): |
| """Forward pass through transformer block""" |
| # 1. Multi-head self-attention + residual |
| attn_out = self.self_attention(x) |
| x = [a + b for a, b in zip(x, attn_out)] |
| |
| # 2. Layer norm |
| x = self.layer_norm(x) |
| |
| # 3. Feedforward + residual |
| ff_out = self.feedforward(x) |
| x = [a + b for a, b in zip(x, ff_out)] |
| |
| # 4. Layer norm |
| x = self.layer_norm(x) |
| |
| return x |
| |
| # Minimal but complete! |
| ''' |
| }, |
| "diffusion": { |
| "name": "Diffusion Model", |
| "description": "Simple 1D diffusion process", |
| "code": '''""" |
| Diffusion Model - Simplified 1D |
| Based on DDPM (Denoising Diffusion Probabilistic Models) |
| """ |
| |
| import math |
| import random |
| |
| class SimpleDiffusion: |
| """1D diffusion model for learning""" |
| |
| def __init__(self, timesteps=100): |
| self.timesteps = timesteps |
| |
| # Define beta schedule (variance schedule) |
| self.betas = [0.0001 + (0.02 - 0.0001) * t / timesteps |
| for t in range(timesteps)] |
| |
| # Pre-compute alphas |
| self.alphas = [1 - b for b in self.betas] |
| self.alpha_bars = [] |
| alpha_bar = 1 |
| for alpha in self.alphas: |
| alpha_bar *= alpha |
| self.alpha_bars.append(alpha_bar) |
| |
| def add_noise(self, x, t): |
| """Add noise at timestep t (forward process)""" |
| alpha_bar = self.alpha_bars[t] |
| noise = random.gauss(0, 1) # Sample from N(0,1) |
| |
| # x_t = sqrt(alpha_bar_t) * x_0 + sqrt(1 - alpha_bar_t) * noise |
| noisy_x = math.sqrt(alpha_bar) * x + math.sqrt(1 - alpha_bar) * noise |
| |
| return noisy_x, noise |
| |
| def denoise_step(self, x_t, t, predicted_noise): |
| """Single denoising step (reverse process)""" |
| alpha = self.alphas[t] |
| alpha_bar = self.alpha_bars[t] |
| beta = self.betas[t] |
| |
| # Compute mean |
| mean = (x_t - beta * predicted_noise / math.sqrt(1 - alpha_bar)) / math.sqrt(alpha) |
| |
| # Add noise if not final step |
| if t > 0: |
| noise = random.gauss(0, 1) |
| sigma = math.sqrt(beta) |
| x_t_minus_1 = mean + sigma * noise |
| else: |
| x_t_minus_1 = mean |
| |
| return x_t_minus_1 |
| |
| # Example: Generate samples |
| model = SimpleDiffusion(timesteps=50) |
| x_clean = 5.0 # Target value |
| |
| # Forward: Add noise |
| x_noisy, noise = model.add_noise(x_clean, t=25) |
| print(f"Clean: {x_clean:.2f} -> Noisy: {x_noisy:.2f}") |
| |
| # Reverse: Denoise (would use learned model in practice) |
| x_denoised = model.denoise_step(x_noisy, t=25, predicted_noise=noise) |
| print(f"Denoised: {x_denoised:.2f}") |
| ''' |
| }, |
| "rag": { |
| "name": "RAG (Retrieval-Augmented Generation)", |
| "description": "Simple RAG pipeline", |
| "code": '''""" |
| RAG - Retrieval-Augmented Generation |
| Minimal implementation with cosine similarity |
| """ |
| |
| import math |
| |
| class SimpleRAG: |
| """Minimalist RAG system""" |
| |
| def __init__(self): |
| self.documents = [] |
| self.embeddings = [] |
| |
| def simple_hash_embedding(self, text, dim=8): |
| """Super simple embedding: character frequency""" |
| embedding = [0] * dim |
| for char in text.lower(): |
| idx = hash(char) % dim |
| embedding[idx] += 1 |
| |
| # Normalize |
| norm = math.sqrt(sum(x**2 for x in embedding)) |
| return [x / (norm + 1e-8) for x in embedding] |
| |
| def cosine_similarity(self, a, b): |
| """Cosine similarity between vectors""" |
| dot = sum(x * y for x, y in zip(a, b)) |
| norm_a = math.sqrt(sum(x**2 for x in a)) |
| norm_b = math.sqrt(sum(x**2 for x in b)) |
| return dot / (norm_a * norm_b + 1e-8) |
| |
| def add_document(self, text): |
| """Add document to knowledge base""" |
| self.documents.append(text) |
| self.embeddings.append(self.simple_hash_embedding(text)) |
| |
| def retrieve(self, query, top_k=2): |
| """Retrieve most relevant documents""" |
| query_emb = self.simple_hash_embedding(query) |
| |
| # Compute similarities |
| similarities = [] |
| for i, doc_emb in enumerate(self.embeddings): |
| sim = self.cosine_similarity(query_emb, doc_emb) |
| similarities.append((sim, i)) |
| |
| # Sort and get top-k |
| similarities.sort(reverse=True) |
| top_docs = [self.documents[i] for _, i in similarities[:top_k]] |
| |
| return top_docs |
| |
| def generate(self, query, retrieved_docs): |
| """Generate answer (simplified - just concatenate context)""" |
| context = " ".join(retrieved_docs) |
| return f"Based on: {context}\\n\\nAnswer to '{query}': [LLM would generate here]" |
| |
| # Usage Example |
| rag = SimpleRAG() |
| |
| # Add knowledge |
| rag.add_document("The Eiffel Tower is in Paris, France.") |
| rag.add_document("Paris is the capital of France.") |
| rag.add_document("The Louvre Museum is in Paris.") |
| |
| # Query |
| query = "Where is the Eiffel Tower?" |
| docs = rag.retrieve(query, top_k=2) |
| answer = rag.generate(query, docs) |
| |
| print(answer) |
| ''' |
| } |
| } |
|
|
| def build_interface(): |
| """Build Gradio interface""" |
|
|
| with gr.Blocks(css=custom_css, title="One-File Algorithms", theme=gr.themes.Monochrome()) as app: |
| create_premium_hero( |
| "One-File Algorithms", |
| "Big AI ideas, reduced to beautifully readable single-file examples you can copy, skim, and study in seconds.", |
| "π", |
| badge="Code Museum", |
| highlights=["Self-contained", "Zero dependencies", "Study-friendly"], |
| ) |
|
|
| gr.Markdown("## π¨ Algorithm Gallery") |
| gr.Markdown("*Beautifully commented, self-contained, production-ready*") |
|
|
| with gr.Row(): |
| algo_selector = gr.Dropdown( |
| choices=list(ALGORITHMS.keys()), |
| value="attention", |
| label="Select Algorithm" |
| ) |
|
|
| algo_title = gr.Markdown() |
| algo_desc = gr.Markdown() |
| code_display = gr.Code(language="python", lines=30) |
| copy_btn = gr.Button("π Copy to Clipboard", variant="primary") |
|
|
| gr.Markdown("## π Full Collection") |
| for key, algo in ALGORITHMS.items(): |
| with gr.Accordion(f"π {algo['name']}", open=False): |
| gr.Markdown(f"*{algo['description']}*") |
| gr.Code(value=algo['code'], language="python", lines=15) |
|
|
| create_footer("One-File Algorithms") |
|
|
| def update_display(algo_key): |
| algo = ALGORITHMS[algo_key] |
| return ( |
| f"# {algo['name']}", |
| f"*{algo['description']}*", |
| algo['code'] |
| ) |
|
|
| algo_selector.change( |
| fn=update_display, |
| inputs=[algo_selector], |
| outputs=[algo_title, algo_desc, code_display] |
| ) |
|
|
| app.load( |
| fn=update_display, |
| inputs=[algo_selector], |
| outputs=[algo_title, algo_desc, code_display] |
| ) |
|
|
| return app |
|
|
| if __name__ == "__main__": |
| app = build_interface() |
| app.launch(server_name="0.0.0.0", server_port=7860) |
|
|