import { useState } from "react"; import chartXkcd from "chart.xkcd"; const projectTypes = ["model", "dataset", "space"]; function transformLikesData(likesData) { // Step 1 likesData.sort((a, b) => new Date(a.likedAt) - new Date(b.likedAt)); // Step 2 const cumulativeLikes = {}; let cumulativeCount = 0; // Step 3 likesData.forEach(like => { const date = like.likedAt cumulativeCount++; cumulativeLikes[date] = cumulativeCount; }); // Step 4 const transformedData = Object.keys(cumulativeLikes).map(date => ({ x: date, y: cumulativeLikes[date].toString() })); return transformedData; } const datasets = []; function App() { const [projectType, setProjectType] = useState("models"); const [projectName, setProjectName] = useState(""); const [hasGraph, setHasGraph] = useState(false); const [isLoading, setIsLoading] = useState(false); // const [datasets, setDatasets] = useState([]); const onSubmit = async () => { setIsLoading(true) const svg = document.querySelector('.line-chart') const res = await fetch(`https://huggingface.co/api/${projectType}/${projectName}/likers?expand[]=likeAt`) /** * Format: * [{"user": "timqian", "likedAt": "2021-07-01T00:00:00.000Z"}, {"user": "yy", "likedAt": "2021-07-02T00:00:00.000Z"}] */ const likers = await res.json() let likeHistory = transformLikesData(likers) if (likeHistory.length > 40) { // sample 20 points const sampledLikeHistory = [] const step = Math.floor(likeHistory.length / 20) for (let i = 0; i < likeHistory.length; i += step) { sampledLikeHistory.push(likeHistory[i]) } // Add the last point if it's not included if (sampledLikeHistory[sampledLikeHistory.length - 1].x !== likeHistory[likeHistory.length - 1].x) { sampledLikeHistory.push(likeHistory[likeHistory.length - 1]) } likeHistory = sampledLikeHistory } // if likeHistory is empty, show error message if (likeHistory.length === 0) { setIsLoading(false) alert("No like history found") return } datasets.push({ label: projectName, data: likeHistory, }) // draw chart in next tick // setTimeout(() => { new chartXkcd.XY(svg, { title: 'Like History', xLabel: 'Time', yLabel: 'Likes', data: { datasets, }, options: { // unxkcdify: true, xTickCount: 3, yTickCount: 4, legendPosition: chartXkcd.config.positionType.upLeft, showLine: true, timeFormat: 'MM/DD/YYYY', dotSize: 0.5, dataColors: [ "#FBBF24", // Warm Yellow "#60A5FA", // Light Blue "#14B8A6", // Teal "#A78BFA", // Soft Purple "#FF8C00", // Orange "#64748B", // Slate Gray "#FB7185", // Coral Pink "#6EE7B7", // Mint Green "#2563EB", // Deep Blue "#374151" // Charcoal ] }, }); setHasGraph(true) setIsLoading(false) setProjectName("") // }, 0.2) } return (

View the like history of a project on huggingface 🤗

setProjectName(e.target.value)} onFocus={(e) => e.target.select()} onKeyDown={async (e) => { if (e.key === "Enter") { try { await onSubmit(); } catch (err) { setIsLoading(false); alert(`No like history found for ${projectName}, please check the name and try again`); } } }} disabled={isLoading} /> { isLoading &&
}
{ hasGraph && 🤗 like-history.ai }
Install the chrome extension
); } export default App;