CoolFace
Apppublic

aschen/push-model-from-web

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.ts98 linesDownload Raw Back to root
1import { createRepo, commit, CommitFile, whoAmI } from "@huggingface/hub";2 3const c = console;4 5const FILES_TO_UPLOAD = [6	"./mobilenet/model.json",7	"./mobilenet/group1-shard1of2",8	"./mobilenet/group1-shard2of2",9	"./mobilenet/coffee.jpg",10	"./mobilenet/README.md",11];12 13function filenameFromURL(url: string): string {14	return url.substring(url.lastIndexOf("/") + 1);15}16 17window.document.addEventListener("DOMContentLoaded", () => {18	const tokenEl = document.querySelector<HTMLInputElement>("#token")!;19	const repoNameEl = document.querySelector<HTMLInputElement>("#repo_name")!;20	const button = document.querySelector("#submit")!;21	const output = document.querySelector("#logs")!;22	const form = document.getElementsByTagName("form")[0];23 24	const storedToken = window.localStorage.getItem("hf_token");25	if (storedToken) {26		tokenEl.value = storedToken;27		/// ^to help in dev.28	}29 30	repoNameEl.value = `hello-world-${Date.now() % 1_000}`;31	/// "random" repo name32 33	form.addEventListener("submit", async (event) => {34		event.preventDefault();35 36		const token = tokenEl.value;37		const repoName = repoNameEl.value;38		if (!token || !repoName) {39			alert("You need a token and a repo name");40			return;41		}42 43		button.setAttribute("disabled", "disabled");44 45		const credentials = {46			accessToken: token,47		};48 49		const result = await fetch("./mobilenet/model.json", {50			headers: {51				Range: "bytes=0-100",52			},53		});54		const text = await result.text();55		c.log(text);56		output.append("\n" + text);57		// try {58		// 	const { name: username } = await whoAmI({ credentials });59		// 	const name = `${username}/${repoName}`;60		// 	const { repoUrl } = await createRepo({61		// 		repo: {62		// 			type: "model",63		// 			name,64		// 		},65		// 		credentials,66		// 	});67 68		// 	const operations: CommitFile[] = await Promise.all(69		// 		FILES_TO_UPLOAD.map(async (file) => {70		// 			return {71		// 				operation: "addOrUpdate",72		// 				path: filenameFromURL(file),73		// 				content: await (await fetch(file)).blob(),74		// 			};75		// 		})76		// 	);77		// 	const commitOutput = await commit({78		// 		repo: {79		// 			type: "model",80		// 			name,81		// 		},82		// 		credentials,83		// 		title: "upload model",84		// 		operations,85		// 	});86		// 	c.log(commitOutput);87 88		// 	button.insertAdjacentHTML(89		// 		"afterend",90		// 		`<div class="text-green-500 mb-6">๐ŸŽ‰ Upload complete! Model page is <a target="_blank" class="text-bold underline" href="${repoUrl}">${repoUrl}</a></div>`91		// 	);92		// } catch (err) {93		// 	output.append("\n" + err);94		// }95		button.removeAttribute("disabled");96	});97});98