assets / app.py
MaoDing's picture
初始化资产中心门户
766d088 verified
Raw History Blame Contribute Delete
7.04 kB
"""
现象人工智能 (phenom-ai) — 内部模型与数据资产管理门户
Space: https://huggingface.co/spaces/phenom-ai/assets
"""
import gradio as gr
from huggingface_hub import HfApi, list_models, list_datasets
ORG = "phenom-ai"
api = HfApi()
# ── Colors ──────────────────────────────────────────────────────────────
CSS = """
.main-title { text-align: center; font-size: 2em; margin-bottom: 0.5em; color: #1f2937; }
.subtitle { text-align: center; color: #6b7280; margin-bottom: 2em; }
.card { border-radius: 12px; padding: 20px; background: #f9fafb; border: 1px solid #e5e7eb; }
.section-header { font-size: 1.3em; font-weight: 600; color: #374151; margin-bottom: 1em; }
.tag { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 0.85em; margin: 2px; }
.tag-model { background: #dbeafe; color: #1e40af; }
.tag-dataset { background: #dcfce7; color: #166534; }
.stat-card { text-align: center; padding: 16px; border-radius: 10px; background: white; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.stat-number { font-size: 2em; font-weight: 700; color: #111827; }
.stat-label { font-size: 0.85em; color: #6b7280; margin-top: 4px; }
"""
def fetch_models():
"""获取组织下所有模型"""
try:
models = list_models(author=ORG, token=None)
items = []
for m in models:
tags = m.tags or []
pipeline = next((t for t in tags if t in [
"text-generation", "text-classification", "image-to-text",
"image-classification", "zero-shot-image-classification",
"object-detection", "translation", "summarization",
"automatic-speech-recognition", "text-to-image",
"image-to-image", "feature-extraction",
]), "Other")
items.append({
"名称": f"[{m.id}]({m.url})",
"类型": pipeline,
"下载": f"{m.downloads or 0:,}",
"点赞": f"{m.likes or 0}",
"更新": str(m.lastModified)[:10] if m.lastModified else "-",
"私有": "是" if m.private else "否",
})
return items if else [["暂无模型,等待上传..."]]
except Exception as e:
return [[f"加载失败: {e}"]]
def fetch_datasets():
"""获取组织下所有数据集"""
try:
datasets = list_datasets(author=ORG, token=None)
items = []
for d in datasets:
items.append({
"名称": f"[{d.id}]({d.url})",
"下载": f"{d.downloads or 0:,}",
"点赞": f"{d.likes or 0}",
"更新": str(d.lastModified)[:10] if d.lastModified else "-",
"私有": "是" if d.private else "否",
})
return items if else [["暂无数据集,等待上传..."]]
except Exception as e:
return [[f"加载失败: {e}"]]
def get_stats():
"""统计概览"""
try:
models = list_models(author=ORG, token=None)
datasets = list_datasets(author=ORG, token=None)
model_count = sum(1 for _ in models)
dataset_count = sum(1 for _ in datasets)
total_downloads = sum(m.downloads or 0 for m in models) + sum(d.downloads or 0 for d in datasets)
total_likes = sum(m.likes or 0 for m in models) + sum(d.likes or 0 for d in datasets)
return model_count, dataset_count, total_downloads, total_likes
except Exception as e:
return 0, 0, 0, 0
with gr.Blocks(title="现象人工智能 — 资产中心", css=CSS, theme=gr.themes.Soft()) as demo:
# ── Header ───────────────────────────────────────────────────────
gr.HTML("""
<div class="main-title">🏢 现象人工智能</div>
<div class="subtitle">内部模型与数据资产管理门户 | phenom-ai on HuggingFace</div>
""")
# ── Stats Row ─────────────────────────────────────────────────────
m_count, d_count, dl_count, lk_count = get_stats()
with gr.Row():
with gr.Column(scale=1):
gr.HTML(f'<div class="stat-card"><div class="stat-number">{m_count}</div><div class="stat-label">模型数</div></div>')
with gr.Column(scale=1):
gr.HTML(f'<div class="stat-card"><div class="stat-number">{d_count}</div><div class="stat-label">数据集</div></div>')
with gr.Column(scale=1):
gr.HTML(f'<div class="stat-card"><div class="stat-number">{dl_count:,}</div><div class="stat-label">总下载量</div></div>')
with gr.Column(scale=1):
gr.HTML(f'<div class="stat-card"><div class="stat-number">{lk_count}</div><div class="stat-label">总点赞</div></div>')
# ── Models Section ───────────────────────────────────────────────
gr.Markdown("## 🤖 模型库")
model_table = gr.Dataframe(
headers=["名称", "类型", "下载", "点赞", "更新", "私有"],
value=fetch_models(),
col_types=["markdown", "str", "number", "number", "str", "str"],
interactive=False,
wrap=True,
)
btn_refresh_models = gr.Button("🔄 刷新模型列表", variant="secondary")
# ── Datasets Section ─────────────────────────────────────────────
gr.Markdown("## 📊 数据集")
dataset_table = gr.Dataframe(
headers=["名称", "下载", "点赞", "更新", "私有"],
value=fetch_datasets(),
col_types=["markdown", "number", "number", "str", "str"],
interactive=False,
wrap=True,
)
btn_refresh_datasets = gr.Button("🔄 刷新数据集列表", variant="secondary")
# ── Footer ───────────────────────────────────────────────────────
gr.Markdown("""
---
*本页面由 [HuggingFace Spaces](https://huggingface.co/spaces) 托管*
*需要上传新模型或数据集?联系管理员或使用 `hf upload` 命令*
""")
gr.HTML('<div style="text-align:center;color:#9ca3af;font-size:0.85em;margin-top:1em;">© 2026 现象人工智能 (phenom-ai). All rights reserved.</div>')
# ── Events ────────────────────────────────────────────────────────
btn_refresh_models.click(fn=fetch_models, outputs=model_table)
btn_refresh_datasets.click(fn=fetch_datasets, outputs=dataset_table)
if __name__ == "__main__":
demo.launch()