Hugging Face 的应用

简介: Hugging Face 是一家专注于开发机器学习应用工具的公司,以其用于自然语言处理的 Transformers 库而闻名,同时提供了一个平台让用户分享机器学习模型和数据集。Transformers 库支持多种任务,如文本分类、生成、总结等,并兼容 PyTorch、TensorFlow 等框架。Hugging Face 还推出了 Text Generation Inference 工具包,用于高效部署大规模语言模型。在国内,百度千帆和魔搭社区等平台也在提供类似的服务和支持。

大纲

Hugging-Face 介绍

  • Hugging-Face 大语言模型 LLM 管理
  • Transformers 机器学习框架
  • 文本生成推理 (TGI)

Hugging Face

Hugging-Face -- 大语言模型界的 Github

Hugging Face 专门开发用于构建机器学习应用的工具。该公司的代表产品是其为自然语言处理应用构建的 transformers 库,以及允许用户共享机器学习模型和数据集的平台

image.png

大模型平台 hugging face

image.png

image.png

image.png

国内对标 -- 百度千帆

百度智能云千帆大模型平台(以下简称千帆或千帆大模型平台)是面向企业开发者的一站式大模型开发及服务运行平台。千帆不仅提供了包括文心一言底层模型和第三方开源大模型,还提供了各种 AI 开发工具和整套开发环境,方便客户轻松使用和开发大模型应用。支持数据管理、自动化模型 SFT 以及推理服务云端部署的一站式大模型定制服务,助力各行业的生成式 AI 应用需求落地。

image.png

百度千帆

国内对标 -- 魔搭社区

image.png

ModelScope 社区成立于 2022 年 6 月,是一个模型开源社区及创新平台,由阿里巴巴通义实验室(Institute for Intelligent Computing),联合 CCF 开源发展委员会,共同作为项目发起方。社区联合国内 AI 领域合作伙伴与高校机构, 致力于通过开放的社区合作,构建深度学习相关的模型开源社区,并开放相关模型创新技术,推动基于“模型即服务”(Model-as-a-Service)理念的模型应用生态的繁荣发展。

Hugging Face 是行业风向标

  • 开发者仓库 Hub
  • 机器学习模型 Hub
  • 数据集 Hub
  • 机器学习算法库与封装工具
  • 解决方案
  • 文档

大语言模型管理

通过 Git 下载大模型

git lfs install
#模型会下载到本地目录,并不在hugging face的标准管理目录里,可能会导致后续Transformers库重新下载
git clone https://huggingface.co/google/owlv2-base-patch16-ensemble

image.png

客户端工具 huggingface_hub


python -m pip install huggingface_hub
# 需要开启代理
huggingface-cli login

高速下载


python -m pip install huggingface_hub[hf_transfer]

#缺点:目前还不支持代理,导致下载大文件可能有时候更慢。
export HF_HUB_ENABLE_HF_TRANSFER=1

huggingface-cli download gpt2 config.json

huggingface-cli 用法


(base) hogwarts: llama seveniruby$ huggingface-cli -h
usage: huggingface-cli <command> [<args>]

positional arguments:
  {
   
   env,login,whoami,logout,repo,upload,download,lfs-enable-largefiles,lfs-multipart-upload,scan-cache,delete-cache}
                        huggingface-cli command helpers
    env                 Print information about the environment.
    login               Log in using a token from huggingface.co/settings/tokens
    whoami              Find out which huggingface.co account you are logged in as.
    logout              Log out
    repo                {
   
   create, ls-files} Commands to interact with your huggingface.co repos.
    upload              Upload a file or a folder to a repo on the Hub
    download            Download files from the Hub
    lfs-enable-largefiles
                        Configure your repository to enable upload of files > 5GB.
    lfs-multipart-upload
                        Command will get called by git-lfs, do not call it directly.
    scan-cache          Scan cache directory.
    delete-cache        Delete revisions from the cache directory.

options:
  -h, --help            show this help message and exit

使用示例

huggingface-cli login
huggingface-cli download gpt2
huggingface-cli download gpt2 config.json
huggingface-cli download gpt2 --revision main

使用 Hub client library 管理


from huggingface_hub import hf_hub_download
hf_hub_download(repo_id="google/pegasus-xsum", filename="config.json")

from huggingface_hub import hf_hub_download
hf_hub_download(
    repo_id="google/pegasus-xsum",
    filename="config.json",
    revision="4d33b01d79672f27f001f6abade33f22d993b151"
)

hugging face 的本地缓存文件结构


#文件快照
./snapshots
./snapshots/187d69aae616814bc1386bdfe38e256518d17637
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/added_tokens.json
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/tokenizer_config.json
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/special_tokens_map.json
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/config.json
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/README.md
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/merges.txt
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/.gitattributes
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/pytorch_model.bin
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/vocab.json
./snapshots/187d69aae616814bc1386bdfe38e256518d17637/preprocessor_config.json
#真正的文件缓存
./blobs
./blobs/76e821f1b6f0a9709293c3b6b51ed90980b3166b
./blobs/69feda8b53b1c9e2a85ae756bf58c120c3c1b4b4a4d97d4876578c1809a63d76
./blobs/39808ac074d69141c0b2f065d16f56dfa5194cac
./blobs/1c4c373d81adc0c63bb3368a89f4a92574395c2c
./blobs/b24bfd579e8682df31c7794a3e1c3ef6a9a22a7a
./blobs/5ff4ac542d5e800dd3aadbdcda354868b5f24ddd
./blobs/a6344aac8c09253b3b630fb776ae94478aa0275b
./blobs/dfc1049705d082052a81e7d18bde6d11fc137a0d
./blobs/ee790e862e5a47398da080893272d3c2b76cf0e5
./blobs/469be27c5c010538f845f518c4f5e8574c78f7c8

#分支,指定分支有助于解决重复下载问题
./refs
./refs/main

hugging face transformers 机器学习框架

Transformers 库

Transformers 是一个预训练的最先进模型库,用于自然语言处理 (NLP)、计算机视觉以及音频和语音处理任务。该库不仅包含 Transformer 模型,还包含非 Transformer 模型,例如用于计算机视觉任务的现代卷积网络。Transformers 提供 API 和工具,可轻松下载和训练最先进的预训练模型,它支持 PyTorch、TensorFlow 和 JAX 之间的框架互操作性。

image.png

Transformers 能力

  • 自然语言处理:文本分类、命名实体识别、问答、语言建模、摘要、翻译、多项选择和文本生成。
  • 计算机视觉:图像分类、对象检测和分割。
  • 音频:自动语音识别和音频分类。
  • 多模态:表格问答、光学字符识别、扫描文档信息提取、视频分类和视觉问答。

image.png

Task Description Modality Pipeline identifier
Text classification assign a label to a given sequence of text NLP pipeline(task=“sentiment-analysis”)
Text generation generate text given a prompt NLP pipeline(task=“text-generation”)
Summarization generate a summary of a sequence of text or document NLP pipeline(task=“summarization”)
Task Description Modality Pipeline identifier
Image classification assign a label to an image Computer vision pipeline(task=“image-classification”)
Image segmentation assign a label to each individual pixel of an image (supports semantic, panoptic, and instance segmentation) Computer vision pipeline(task=“image-segmentation”)
Object detection predict the bounding boxes and classes of objects in an image Computer vision pipeline(task=“object-detection”)
Task Description Modality Pipeline identifier
Audio classification assign a label to some audio data Audio pipeline(task=“audio-classification”)
Automatic speech recognition transcribe speech into text Audio pipeline(task=“automatic-speech-recognition”)
Task Description Modality Pipeline identifier
Visual question answering answer a question about the image, given an image and a question Multimodal pipeline(task=“vqa”)
Document question answering answer a question about the document, given a document and a question Multimodal pipeline(task=“document-question-answering”)
Image captioning generate a caption for a given image Multimodal pipeline(task=“image-to-text”)

快速开始

pip install transformers datasets
pip install torch
pip install tensorflow

#测试
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"

pipeline api 使用
pipe = pipeline("text-classification")
pipe("This restaurant is awesome")

#输出
[{
   
   'label': 'POSITIVE', 'score': 0.9998743534088135}]

使用第三方库

pipe = pipeline(model="FacebookAI/roberta-large-mnli")
pipe("This restaurant is awesome")

#输出
[{
   
   'label': 'NEUTRAL', 'score': 0.7313136458396912}]

图像识别


def test_zero_shot_object_detector():
    with open('coco_sample.png', mode='rb') as f:
        image = Image.open(f)
        object_detector = pipeline('object-detection', revision='main')

        predictions = object_detector(image)
        print(json.dumps(predictions, indent=2, ensure_ascii=False))

#输出
[
  {
   
   
    "score": 0.9982201457023621,
    "label": "remote",
    "box": {
   
   
      "xmin": 40,
      "ymin": 70,
      "xmax": 175,
      "ymax": 117
    }
  },
  {
   
   
    "score": 0.9960021376609802,
    "label": "remote",
    "box": {
   
   
      "xmin": 333,
      "ymin": 72,
      "xmax": 368,
      "ymax": 187
    }
  },
  {
   
   
    "score": 0.9954745173454285,
    "label": "couch",
    "box": {
   
   
      "xmin": 0,
      "ymin": 1,
      "xmax": 639,
      "ymax": 473
    }
  },
  {
   
   
    "score": 0.9988006353378296,
    "label": "cat",
    "box": {
   
   
      "xmin": 13,
      "ymin": 52,
      "xmax": 314,
      "ymax": 470
    }
  },
  {
   
   
    "score": 0.9986783862113953,
    "label": "cat",
    "box": {
   
   
      "xmin": 345,
      "ymin": 23,
      "xmax": 640,
      "ymax": 368
    }
  }
]

image.png

文本生成推理 Text Generation Inference

Text Generation Inference

文本生成推理 (TGI) 是一个用于部署和服务大型语言模型 (LLM) 的工具包。TGI 为最流行的开源 LLM 提供高性能文本生成,包括 Llama、Falcon、StarCoder、BLOOM、GPT-NeoX 和 T5。

image.png


model=tiiuae/falcon-7b-instruct
volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run

docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:1.4 --model-id $model

model=teknium/OpenHermes-2.5-Mistral-7B
volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run

docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:1.4 --model-id $model

TGI 接口

curl 127.0.0.1:8080/generate \
    -X POST \
    -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":20}}' \
    -H 'Content-Type: application/json'

编程使用 TGI


import requests

headers = {
   
   
    "Content-Type": "application/json",
}

data = {
   
   
    'inputs': 'What is Deep Learning?',
    'parameters': {
   
   
        'max_new_tokens': 20,
    },
}

response = requests.post('http://127.0.0.1:8080/generate', headers=headers, json=data)
print(response.json())
# {'generated_text': '\n\nDeep Learning is a subset of Machine Learning that is concern
相关文章
|
存储 人工智能 大数据
Huggingface又上不去了?这里有个新的解决方案!
AI开发者都知道,HuggingFace是一个高速发展的社区,包括Meta、Google、Microsoft、Amazon在内的超过5000家组织机构在为HuggingFace开源社区贡献代码、数据集和模型。
|
XML 存储 SQL
独立部署Xray反练平台——详细说明加举例xxe漏洞
xray 是一款功能强大的安全评估工具,由多名经验丰富的一线安全从业者呕心打造而成,主要特性有:检测速度快。发包速度快; 漏洞检测算法高效。支持范围广。大至 OWASP Top 10 通用漏洞检测,小至各种 CMS 框架 POC,均可以支持。代码质量高。编写代码的人员素质高, 通过 Code Review、单元测试、集成测试等多层验证来提高代码可靠性。高级可定制。通过配置文件暴露了引擎的各种参数,通过修改配置文件可以极大的客制化功能。安全无威胁。xray 定位为一款安全辅助评估工具,而不是攻击工具,内置的所有 payload 和 poc 均为无害化检查。
2697 0
独立部署Xray反练平台——详细说明加举例xxe漏洞
|
9月前
|
存储 人工智能 运维
刚用AI Agent指挥官处理服务器告警,3分钟搞定之前熬2小时的活,我直接提前下班撸串去了
这是一篇生动的技术叙事:运维工程师亲述从“凌晨2小时盲查告警”到“3分钟AI自动修复”的真实跃迁。AI Agent指挥官可自动聚类告警、登录服务器诊断、定位代码Bug、执行备份/清理/回滚,并生成复盘报告——不止提效4000%,更防误操作、避背锅、助预防。运维价值,本该是稳而非熬。
685 1
|
10月前
|
SQL 人工智能 自然语言处理
RDSAI-CLI - 用AI重塑数据库终端CLI体验
阿里云RDS团队推出AI原生命令行工具RDSAI-CLI,融合大语言模型与传统CLI优势,支持自然语言交互、智能SQL生成、性能诊断与优化建议,实现“意图式操作”,让数据库真正“听懂人话”。现已开源,助力开发者高效管理数据库。
|
9月前
|
人工智能 API 数据安全/隐私保护
如何使用 LiteLLM 网关代理统一管理你的大模型
大模型使用混乱?费用失控、切换成本高、权限难管?LiteLLM 是轻量实用的大模型网关,统一 OpenAI 兼容接口,纳管 OpenAI、Qwen、本地 vLLM/Ollama 等多源模型。支持 Docker Compose 一键部署、虚拟 Key 权限控制、预算限额与全链路可观测性,业务代码零改造即可接入。
4774 8
|
机器学习/深度学习 人工智能 算法
小米7B参数推理大模型首次开源!Xiaomi MiMo:数学代码双杀,超越32B巨头
小米开源的MiMo推理大模型通过联动预训练与强化学习算法,在7B参数规模下实现数学推理与代码生成能力的突破性提升,技术报告显示其性能超越部分32B级模型。
2469 74
小米7B参数推理大模型首次开源!Xiaomi MiMo:数学代码双杀,超越32B巨头
|
人工智能 Kubernetes API
Dify+DeepSeek实战教程!企业级 AI 文档库本地化部署,数据安全与智能检索我都要
接下来这篇文章,就打算用最接地气的方式,手把手带你从 0 到 1 搭建一套专属的本地知识库系统。无论你是想优化企业内部文档检索(不用担心敏感数据上传云端的风险),还是像我一样想为用户打造更智能的文档服务,都能跟着步骤一步步实现。咱们不卖关子,直接上干货
4873 14
Dify+DeepSeek实战教程!企业级 AI 文档库本地化部署,数据安全与智能检索我都要
|
存储 人工智能 自然语言处理
DeepSeek R1+Open WebUI实现本地知识库的搭建和局域网访问
本文介绍了使用 DeepSeek R1 和 Open WebUI 搭建本地知识库的详细步骤与注意事项,涵盖核心组件介绍、硬件与软件准备、模型部署、知识库构建及问答功能实现等内容,适用于本地文档存储、向量化与检索增强生成(RAG)场景的应用开发。
4947 0
|
机器学习/深度学习 自然语言处理 PyTorch
精通 Transformers(一)(2)
精通 Transformers(一)
824 4
|
11月前
|
Linux Docker 容器
docker下部署 vLLM 启动Qwen3-VL-32B-Instruct模型
本文介绍在CentOS系统、A10 6×24G显卡环境下,通过Docker部署vLLM并启动Qwen3-VL-32B-Instruct大模型的完整流程,涵盖镜像拉取、容器配置、多卡并行与显存优化设置,支持32K上下文,附带启动脚本及调用验证示例。
9880 2