flashdata-dev's picture
Publish youtube_metadata tool and request guide
c0bd27b verified
Raw History Blame Contribute Delete
4.59 kB
const byId = id => document.getElementById(id);
const pythonLiteral = value => {
if (value === null) return 'None';
if (typeof value === 'boolean') return value ? 'True' : 'False';
if (Array.isArray(value)) return `[${value.map(pythonLiteral).join(', ')}]`;
if (typeof value === 'object') return `{${Object.entries(value).map(([k,v]) => `${JSON.stringify(k)}: ${pythonLiteral(v)}`).join(', ')}}`;
return JSON.stringify(value);
};
const [task, example, schema] = await Promise.all(['task.json','example.json','input-schema.json'].map(async file => {
const response = await fetch(file);
if (!response.ok) throw new Error(`Unable to load ${file}`);
return response.json();
}));
document.title = task.title;
for (const key of ['title','scenario','task','next','limit']) byId(key).textContent = task[key];
byId('docs').href = `https://flashdata.dev/docs/sources/${task.source}?utm_source=hugging-face&utm_medium=integration&utm_campaign=intent_gtm_2026q4&utm_content=${task.slug}`;
byId('source').href = `https://huggingface.co/spaces/${task.repo_id}/blob/main/tool.py`;
byId('readme').href = `https://huggingface.co/spaces/${task.repo_id}/blob/main/README.md`;
byId('parameters').value = JSON.stringify(task.input,null,2);
byId('response').value = JSON.stringify(example.response,null,2);
byId('sample-request').textContent = JSON.stringify(example.request,null,2);
function generate() {
try {
const args = JSON.parse(byId('parameters').value);
if (!args || Array.isArray(args) || typeof args !== 'object') throw new Error('Enter a JSON object.');
const allowed = task.source === 'youtube_download' ? ['operation','parameters','job_id'] : Object.keys(schema.properties);
if (Object.keys(args).some(key => !allowed.includes(key))) throw new Error('Use only the documented inputs; keep credentials in your local environment.');
const code = [
'# Install: python -m pip install smolagents==1.26.0 langchain-flashdata==0.2.1',
'import getpass', 'import os', 'from smolagents import load_tool', '',
'os.environ["FLASHDATA_API_KEY"] = getpass.getpass("FlashData API key: ")',
...(task.source === 'youtube_download' && args.operation === 'submit' ? ['os.environ["FLASHDATA_DOWNLOAD_STORAGE"] = getpass.getpass("Customer storage JSON: ")'] : []),
`tool = load_tool(${JSON.stringify(task.repo_id)}, trust_remote_code=True)`,
`result = tool(**${pythonLiteral(args)})`, 'print(result)'
];
byId('python').textContent = code.join('\n');
byId('input-status').textContent = 'Python call generated. The tool validates full parameter constraints before a request.';
} catch (error) {
byId('python').textContent = '';
byId('input-status').textContent = error.message;
}
}
function inspect() {
byId('results').replaceChildren();
try {
const response = JSON.parse(byId('response').value);
const selected = task.path.split('.').reduce((value, key) => value?.[key], response);
if (selected === undefined || selected === null) throw new Error(`No value at ${task.path}. Check the response format; failed or pending requests may have different fields.`);
const rows = Array.isArray(selected) ? selected : [selected];
byId('response-status').textContent = `${rows.length} item(s) at ${task.path}. Showing up to 10.`;
for (const row of rows.slice(0,10)) {
const card = document.createElement('div'); card.className = 'result';
const fields = typeof row === 'object' && row !== null ? task.fields.filter(key => Object.hasOwn(row,key)) : [];
if (!fields.length) { const p=document.createElement('p'); p.className='field'; p.textContent=typeof row === 'string' ? row : JSON.stringify(row); card.append(p); }
for (const key of fields) {
const line=document.createElement('p'); line.className='field';
const label=document.createElement('b'); label.textContent=key+': '; line.append(label);
const value=row[key];
if (typeof value === 'string' && /^https?:\/\//.test(value)) {
const link=document.createElement('a'); link.href=value; link.textContent=value; link.target='_blank'; link.rel='noopener noreferrer'; line.append(link);
} else line.append(document.createTextNode(typeof value === 'object' ? JSON.stringify(value) : String(value)));
card.append(line);
}
byId('results').append(card);
}
} catch (error) { byId('response-status').textContent=error.message; }
}
byId('generate').addEventListener('click',generate);
byId('inspect').addEventListener('click',inspect);
generate(); inspect();