fix(route/huggingface): add huggingface group models detail (#20646)
* Adding Huggingface Model details * more robust * fix deepscan's problem, shall not be a problem !!!!! 404 should be a proper state !!!!! * md2html and image url fix
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import { load } from 'cheerio';
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import { FetchError } from 'ofetch';
|
||||
|
||||
import type { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
const md = MarkdownIt({
|
||||
html: true,
|
||||
});
|
||||
|
||||
export const route: Route = {
|
||||
path: '/models/:group',
|
||||
categories: ['programming'],
|
||||
@@ -44,7 +51,7 @@ async function handler(ctx) {
|
||||
const { body: response } = await got(url);
|
||||
const $ = load(response);
|
||||
|
||||
const items = $('article')
|
||||
let items = $('article')
|
||||
.toArray()
|
||||
.map((article) => {
|
||||
const $article = $(article);
|
||||
@@ -63,6 +70,68 @@ async function handler(ctx) {
|
||||
})
|
||||
.filter((item) => item.title);
|
||||
|
||||
items = await Promise.all(
|
||||
items.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
try {
|
||||
// 一般有readme, 且没设置权限的模型
|
||||
const { body: detailResp } = await got(item.link + '/raw/main/README.md');
|
||||
|
||||
item.description += md.render(detailResp);
|
||||
// Qwen一般是外链,主要针对DeepSeek
|
||||
const $Out = load(item.description);
|
||||
$Out('img').each((_, e) => {
|
||||
const $e = $Out(e);
|
||||
|
||||
const src = $e.attr('src');
|
||||
if (!src) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果不是绝对 URL(没有 http:// 或 https://),添加 base URL
|
||||
if (/^https?:\/\//i.test(src)) {
|
||||
// 已经是完整 URL
|
||||
$e.attr('src', src);
|
||||
} else {
|
||||
// 处理以 / 开头的绝对路径
|
||||
if (src.startsWith('/')) {
|
||||
$e.attr('src', `${item.link}/resolve/main/` + src);
|
||||
} else {
|
||||
// 处理相对路径(如 ./images/pic.png 或 images/pic.png)
|
||||
const baseUrl = item.link + '/resolve/main/';
|
||||
$e.attr('src', baseUrl + src.replace(/^\.\//, ''));
|
||||
}
|
||||
}
|
||||
});
|
||||
item.description = $Out.html();
|
||||
return item;
|
||||
} catch (error) {
|
||||
if (error instanceof FetchError && (error.statusCode === 403 || error.statusCode === 401)) {
|
||||
// 要权限的情况
|
||||
// Example: https://huggingface.co/facebook/sam-3d-objects/raw/main/README.md
|
||||
try {
|
||||
// 以免再次出错
|
||||
// 这里可以不管image相对绝对路径,一般要认证的模型都是外链、或者索性图也是403
|
||||
const { body: respHtml } = await got(item.link + '/blob/main/README.md?code=true');
|
||||
const $ = load(respHtml);
|
||||
const detailHtml = $('body').find('div > main > div > section > div > div > div > div > div > table > tbody').text().trim();
|
||||
item.description += md.render(detailHtml);
|
||||
return item;
|
||||
} catch {
|
||||
return item;
|
||||
}
|
||||
} else {
|
||||
// 没有介绍页面的情况 error.statusCode === 404
|
||||
// Example: https://huggingface.co/ianyang02/aita_qwen3-30b/raw/main/README.md
|
||||
// return item;
|
||||
// 其他错误的情况
|
||||
return item;
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: `Huggingface ${group} Models`,
|
||||
link: url,
|
||||
|
||||
Reference in New Issue
Block a user