river1235854/algorithm
0
1import sqlite32import random3from fastapi import FastAPI4from fastapi.middleware.cors import CORSMiddleware5 6app = FastAPI()7app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])8 9DB_PATH = "neural_memory.db"10 11def get_db():12 conn = sqlite3.connect(DB_PATH)13 conn.row_factory = sqlite3.Row14 return conn15 16@app.on_event("startup")17def startup():18 conn = get_db()19 # Table for 10M image URLs20 conn.execute("CREATE TABLE IF NOT EXISTS images (id INTEGER PRIMARY KEY, url TEXT)")21 # Table for the User's AI-discovered interests22 conn.execute("CREATE TABLE IF NOT EXISTS brain (tag TEXT PRIMARY KEY, score REAL)")23 24 # Mock seeding 10M (Real world: loop your CSV here)25 if conn.execute("SELECT count(*) FROM images").fetchone()[0] == 0:26 # Just a sample of real-world "monster" looking things27 sample_urls = [28 "https://upload.wikimedia.org/wikipedia/commons/1/1b/Deep_sea_fish_6.jpg",29 "https://upload.wikimedia.org/wikipedia/commons/d/d3/Giant_Isopod.jpg"30 ]31 for url in sample_urls:32 conn.execute("INSERT INTO images (url) VALUES (?)", (url,))33 conn.commit()34 35@app.get("/get_feed")36def get_feed():37 conn = get_db()38 # Pull 50 random candidates39 candidates = conn.execute("SELECT * FROM images ORDER BY RANDOM() LIMIT 50").fetchall()40 # Return them all; the HTML AI will help rank them or process them41 return [{"id": c['id'], "url": c['url']} for c in candidates]42 43@app.post("/report_tags")44def report_tags(img_id: int, tags: list, action: str):45 """46 This receives the AI tags from your HTML.47 If action is 'like', these tags get +score.48 """49 conn = get_db()50 weight = 1.0 if action == "like" else -0.551 for t in tags:52 conn.execute("""INSERT INTO brain (tag, score) VALUES (?, ?) 53 ON CONFLICT(tag) DO UPDATE SET score = score + ?""", (t, weight, weight))54 conn.commit()55 return {"status": "brain_updated"}