Spaces:
Running
Running
Download schema.sql from quantilimited/explication: direct link, hf CLI and curl.
- Browser
- Download file 15.1 kB
-
https://huggingface.co/spaces/quantilimited/explication/resolve/main/schema.sql
- Command line
-
hf download hf://spaces/quantilimited/explication/schema.sql
-
curl -L -o schema.sql https://huggingface.co/spaces/quantilimited/explication/resolve/main/schema.sql
15.1 kB
| -- Explication — accounts, plans, models, shared wording and spend. | |
| -- Paste this into Supabase → SQL Editor → New query → Run. | |
| -- | |
| -- One address is the developer: lucas.owen13@icloud.com. Every write to plans, | |
| -- models, API keys and page wording is restricted to that account here, in the | |
| -- database, so the rule holds even if someone opens the developer page. | |
| create extension if not exists pgcrypto; | |
| -- --------------------------------------------------------------------------- | |
| -- Who is the developer | |
| -- --------------------------------------------------------------------------- | |
| create or replace function public.explication_is_dev() | |
| returns boolean | |
| language sql | |
| stable | |
| as $$ | |
| select coalesce(lower(auth.jwt() ->> 'email'), '') = 'lucas.owen13@icloud.com' | |
| $$; | |
| -- --------------------------------------------------------------------------- | |
| -- Tables | |
| -- --------------------------------------------------------------------------- | |
| create table if not exists public.explication_settings ( | |
| id integer primary key default 1 check (id = 1), | |
| paypal_client_id text not null default '', | |
| paypal_mode text not null default 'live' check (paypal_mode in ('live', 'sandbox')), | |
| checkout_mode text not null default 'function' check (checkout_mode in ('payment_links', 'function')), | |
| checkout_function text not null default 'paypal-checkout', | |
| currency text not null default 'GBP', | |
| support_email text not null default 'lucas.owen13@icloud.com', | |
| updated_at timestamptz not null default now() | |
| ); | |
| create table if not exists public.explication_plans ( | |
| id text primary key, | |
| name text not null, | |
| kind text not null check (kind in ('one_time', 'subscription')), | |
| price_cents integer not null default 0, | |
| currency text not null default 'GBP', | |
| interval text check (interval is null or interval in ('month', 'year')), | |
| blurb text not null default '', | |
| features jsonb not null default '[]'::jsonb, | |
| cost_limit_cents integer not null default 0, | |
| download_tier integer not null default 0, | |
| byok boolean not null default true, | |
| paypal_plan_id text not null default '', | |
| paypal_payment_link text not null default '', | |
| active boolean not null default true, | |
| sort integer not null default 0, | |
| updated_at timestamptz not null default now() | |
| ); | |
| create table if not exists public.explication_models ( | |
| id text primary key, | |
| name text not null, | |
| host text not null default '', | |
| endpoint_url text not null default '', | |
| model_name text not null default '', | |
| cost_in_per_million numeric not null default 0, | |
| cost_out_per_million numeric not null default 0, | |
| min_tier integer not null default 0, | |
| download_size text not null default '', | |
| note text not null default '', | |
| enabled boolean not null default true, | |
| sort integer not null default 0, | |
| updated_at timestamptz not null default now() | |
| ); | |
| -- Hosted model keys. Read and write by the developer only; the AI function | |
| -- reads them with the service role, so they never reach a browser. | |
| create table if not exists public.explication_model_secrets ( | |
| model_id text primary key references public.explication_models(id) on delete cascade, | |
| api_key text not null, | |
| updated_at timestamptz not null default now() | |
| ); | |
| -- Page wording overridden by the developer, by key. | |
| create table if not exists public.explication_content ( | |
| key text primary key, | |
| value text not null, | |
| updated_at timestamptz not null default now(), | |
| updated_by text | |
| ); | |
| create table if not exists public.explication_entitlements ( | |
| id uuid primary key default gen_random_uuid(), | |
| user_id uuid references auth.users(id) on delete cascade, | |
| email text, | |
| plan_id text references public.explication_plans(id) on delete set null, | |
| source text not null default 'grant', | |
| granted_at timestamptz not null default now(), | |
| expires_at timestamptz, | |
| revoked boolean not null default false, | |
| paypal_payer_id text, | |
| paypal_subscription_id text, | |
| paypal_order_id text | |
| ); | |
| create index if not exists explication_entitlements_user on public.explication_entitlements (user_id); | |
| create index if not exists explication_entitlements_email on public.explication_entitlements (lower(email)); | |
| create unique index if not exists explication_entitlements_paypal_order | |
| on public.explication_entitlements (paypal_order_id) where paypal_order_id is not null; | |
| -- PayPal checkout state. Only the service role (the Edge Functions) can read or | |
| -- write these; they never reach a browser. | |
| create table if not exists public.explication_paypal_orders ( | |
| order_id text primary key, | |
| user_id uuid references auth.users(id) on delete cascade, | |
| plan_id text references public.explication_plans(id) on delete set null, | |
| origin text not null default '', | |
| kind text not null default 'one_time', | |
| subscription_id text, | |
| status text not null default 'created', | |
| created_at timestamptz not null default now() | |
| ); | |
| create table if not exists public.explication_paypal_billing_plans ( | |
| plan_id text not null references public.explication_plans(id) on delete cascade, | |
| mode text not null, | |
| paypal_product_id text, | |
| paypal_plan_id text not null, | |
| updated_at timestamptz not null default now(), | |
| primary key (plan_id, mode) | |
| ); | |
| create table if not exists public.explication_paypal_payments ( | |
| sale_id text primary key, | |
| subscription_id text, | |
| plan_id text, | |
| amount_cents integer not null default 0, | |
| created_at timestamptz not null default now() | |
| ); | |
| create table if not exists public.explication_usage ( | |
| id uuid primary key default gen_random_uuid(), | |
| user_id uuid references auth.users(id) on delete cascade, | |
| model_id text, | |
| cost_cents numeric not null default 0, | |
| tokens_in integer not null default 0, | |
| tokens_out integer not null default 0, | |
| created_at timestamptz not null default now() | |
| ); | |
| create index if not exists explication_usage_user on public.explication_usage (user_id, created_at desc); | |
| -- --------------------------------------------------------------------------- | |
| -- Row-level security | |
| -- --------------------------------------------------------------------------- | |
| alter table public.explication_settings enable row level security; | |
| alter table public.explication_plans enable row level security; | |
| alter table public.explication_models enable row level security; | |
| alter table public.explication_model_secrets enable row level security; | |
| alter table public.explication_content enable row level security; | |
| alter table public.explication_entitlements enable row level security; | |
| alter table public.explication_usage enable row level security; | |
| alter table public.explication_paypal_orders enable row level security; | |
| alter table public.explication_paypal_billing_plans enable row level security; | |
| alter table public.explication_paypal_payments enable row level security; | |
| drop policy if exists settings_read on public.explication_settings; | |
| create policy settings_read on public.explication_settings for select using (true); | |
| drop policy if exists settings_write on public.explication_settings; | |
| create policy settings_write on public.explication_settings for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists plans_read on public.explication_plans; | |
| create policy plans_read on public.explication_plans for select using (active or public.explication_is_dev()); | |
| drop policy if exists plans_write on public.explication_plans; | |
| create policy plans_write on public.explication_plans for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists models_read on public.explication_models; | |
| create policy models_read on public.explication_models for select using (enabled or public.explication_is_dev()); | |
| drop policy if exists models_write on public.explication_models; | |
| create policy models_write on public.explication_models for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists secrets_dev on public.explication_model_secrets; | |
| create policy secrets_dev on public.explication_model_secrets for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists content_read on public.explication_content; | |
| create policy content_read on public.explication_content for select using (true); | |
| drop policy if exists content_write on public.explication_content; | |
| create policy content_write on public.explication_content for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists entitlements_read on public.explication_entitlements; | |
| create policy entitlements_read on public.explication_entitlements for select using ( | |
| public.explication_is_dev() | |
| or user_id = auth.uid() | |
| or lower(coalesce(email, '')) = lower(coalesce(auth.jwt() ->> 'email', '')) | |
| ); | |
| drop policy if exists entitlements_write on public.explication_entitlements; | |
| create policy entitlements_write on public.explication_entitlements for all | |
| using (public.explication_is_dev()) with check (public.explication_is_dev()); | |
| drop policy if exists usage_read on public.explication_usage; | |
| create policy usage_read on public.explication_usage for select using ( | |
| public.explication_is_dev() or user_id = auth.uid() | |
| ); | |
| -- Usage rows are written only by the AI function with the service role, | |
| -- which bypasses row-level security. No insert policy on purpose. | |
| -- --------------------------------------------------------------------------- | |
| -- Seed: settings, plans and models | |
| -- --------------------------------------------------------------------------- | |
| insert into public.explication_settings (id) values (1) on conflict (id) do nothing; | |
| insert into public.explication_plans | |
| (id, name, kind, price_cents, currency, interval, blurb, features, cost_limit_cents, download_tier, byok, sort) | |
| values | |
| ('paragraph-lite', 'Paragraph Lite', 'one_time', 400, 'GBP', null, | |
| 'Buy the smallest model once and keep it. Nothing you do here costs the site another penny.', | |
| '["Paragraph Lite model, yours to keep","Download it and run it offline","All rule-based marking and the process log","Bring your own API key for any hosted model"]'::jsonb, | |
| 0, 1, true, 1), | |
| ('student', 'Student', 'subscription', 400, 'GBP', 'month', | |
| 'Every model hosted here, with £1.20 of model usage included each month.', | |
| '["Every hosted model, including 3B and 7B","£1.20 of model usage per month","Download Lite and 1.5B for offline use","Bring your own key and pay your provider directly"]'::jsonb, | |
| 120, 2, true, 2), | |
| ('scholar', 'Scholar', 'subscription', 900, 'GBP', 'month', | |
| 'More room for long essays, with £3.50 of model usage each month.', | |
| '["Everything in Student","£3.50 of model usage per month","Download the 3B model as well","Priority when the hosted models are busy"]'::jsonb, | |
| 350, 3, true, 3), | |
| ('examiner', 'Examiner', 'subscription', 1900, 'GBP', 'month', | |
| 'The whole catalogue, £9 of model usage a month, and the best offline model.', | |
| '["Everything in Scholar","£9.00 of model usage per month","Download all four models, including 7B","Early access to new models and marking prompts"]'::jsonb, | |
| 900, 4, true, 4) | |
| on conflict (id) do nothing; | |
| insert into public.explication_models | |
| (id, name, host, endpoint_url, model_name, cost_in_per_million, cost_out_per_million, min_tier, download_size, note, sort) | |
| values | |
| ('Qwen2.5-0.5B-Instruct-q4f16_1-MLC', 'Paragraph Lite', 'Browser (WebGPU)', '', 'Qwen2.5 0.5B Instruct', 0, 0, 1, '~0.4 GB', 'Quickest to load. Enough for short feedback.', 1), | |
| ('Qwen2.5-1.5B-Instruct-q4f16_1-MLC', 'Qwen2.5 1.5B', 'Browser (WebGPU)', '', 'Qwen2.5 1.5B Instruct', 0, 0, 2, '~1.1 GB', 'The default when WebGPU is available.', 2), | |
| ('Qwen2.5-3B-Instruct-q4f16_1-MLC', 'Qwen2.5 3B', 'Browser (WebGPU)', '', 'Qwen2.5 3B Instruct', 0, 0, 3, '~2.0 GB', 'Better analysis; needs a stronger GPU.', 3), | |
| ('Qwen2.5-7B-Instruct-q4f16_1-MLC', 'Qwen2.5 7B', 'Browser (WebGPU)', '', 'Qwen2.5 7B Instruct', 0, 0, 4, '~4.5 GB', 'Best feedback here, but only on a capable GPU.', 4) | |
| on conflict (id) do nothing; | |
| -- --------------------------------------------------------------------------- | |
| -- The current offer. Safe to run again: it updates the shop front in place. | |
| -- --------------------------------------------------------------------------- | |
| insert into public.explication_plans | |
| (id, name, kind, price_cents, currency, interval, blurb, features, cost_limit_cents, download_tier, byok, sort, active) | |
| values | |
| ('paragraph-lite', 'Paragraph Lite', 'one_time', 400, 'GBP', null, | |
| 'The little model that started it all, yours to keep. Download it once and mark paragraphs on the bus, on the train, anywhere.', | |
| '["Paragraph Lite model, yours for good","Runs fully offline after one download","Every marking tool, forever","Use your own AI key if you have one"]'::jsonb, | |
| 0, 1, true, 1, true), | |
| ('model-library', 'Model Library', 'one_time', 1400, 'GBP', null, | |
| 'All four models in one go: the quick one, the strong one, and the two in between. Nothing else to buy.', | |
| '["All four offline models unlocked","Download on every device you own","Works on a plane, offline, forever","Free updates when new models arrive"]'::jsonb, | |
| 0, 4, true, 2, true), | |
| ('exam-season', 'Exam Season', 'one_time', 2400, 'GBP', null, | |
| 'Everything unlocked for the run-up to the exams, including a pot of hosted AI for the nights you want the big models.', | |
| '["All four offline models","Priority access to the hosted models","One payment, nothing renews"]'::jsonb, | |
| 600, 4, true, 3, true), | |
| ('ai-booster', 'AI Booster', 'one_time', 600, 'GBP', null, | |
| 'Already have a membership? Drop another pot of hosted AI into your account and keep the big models coming.', | |
| '["Stacks with any plan you already have","Never expires","No subscription, no renewal"]'::jsonb, | |
| 500, 0, true, 4, true), | |
| ('student', 'Student', 'subscription', 400, 'GBP', 'month', | |
| 'The whole shelf of hosted models, plus two offline downloads. About the price of a coffee a month.', | |
| '["Every hosted model, including the big ones","Download Lite and 1.5B for offline use","Use your own key and keep it free of limits"]'::jsonb, | |
| 120, 2, true, 5, true), | |
| ('scholar', 'Scholar', 'subscription', 900, 'GBP', 'month', | |
| 'For long essays and heavy revision weeks: more AI, more downloads, and the 3B model offline.', | |
| '["Everything in Student","Download the 3B model as well","Priority when the hosted models are busy"]'::jsonb, | |
| 400, 3, true, 6, true), | |
| ('examiner', 'Examiner', 'subscription', 1900, 'GBP', 'month', | |
| 'The full shelf, the biggest budget and every model offline. For the year it really matters.', | |
| '["Everything in Scholar","Download all four models, including 7B","Early access to new models and marking prompts"]'::jsonb, | |
| 1000, 4, true, 7, true) | |
| on conflict (id) do update set | |
| name = excluded.name, | |
| kind = excluded.kind, | |
| price_cents = excluded.price_cents, | |
| interval = excluded.interval, | |
| blurb = excluded.blurb, | |
| features = excluded.features, | |
| cost_limit_cents = excluded.cost_limit_cents, | |
| download_tier = excluded.download_tier, | |
| byok = excluded.byok, | |
| sort = excluded.sort, | |
| active = excluded.active, | |
| updated_at = now(); | |