Spaces:
Running
Running
Download text2scryfall.js from vedalken/text2scryfall: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/vedalken/text2scryfall/resolve/main/text2scryfall.js
- Command line
-
hf download hf://spaces/vedalken/text2scryfall/text2scryfall.js
-
curl -L -o text2scryfall.js https://huggingface.co/spaces/vedalken/text2scryfall/resolve/main/text2scryfall.js
1.26 kB
| // Shared by the browser page (index.html) and the Node scorer (eval.mjs). | |
| // Must match SYSTEM_PROMPT in src/text2scryfall/predict.py: the model was trained with it. | |
| export const SYSTEM_PROMPT = "Convert the user's request into a Scryfall search query. Reply with the query only."; | |
| export async function translate(generator, text, options = {}) { | |
| const messages = [ | |
| { role: "system", content: SYSTEM_PROMPT }, | |
| { role: "user", content: text }, | |
| ]; | |
| const out = await generator(messages, { max_new_tokens: 64, do_sample: false, ...options }); | |
| return out[0].generated_text.at(-1).content.trim(); | |
| } | |
| // vocab.json (written by `uv run python -m text2scryfall.rules`) as {field: Set of values}. | |
| export const loadVocab = (json) => Object.fromEntries(Object.entries(json).map(([k, v]) => [k, new Set(v)])); | |
| // Moves a term to the only field its value belongs to: is:artifact -> t:artifact, t:promo -> is:promo. | |
| export function repair(query, vocab) { | |
| return query.replace(/(^|[\s(])(-?)(is|t|kw):([a-z]+)(?=$|[\s)])/g, (term, pre, neg, key, value) => { | |
| if (vocab[key].has(value)) return term; | |
| const fits = ["t", "kw", "is"].filter((k) => vocab[k].has(value)); | |
| return fits.length === 1 ? `${pre}${neg}${fits[0]}:${value}` : term; | |
| }); | |
| } | |