简介

Hugging Face 的核心使命是 “民主化优秀的机器学习”。它通过构建一个开源的、协作式的平台,极大地降低了获取、训练和部署最先进机器学习模型的门槛,让研究人员、开发者和公司都能从中受益。一个基于 PyTorch、TensorFlow 和 JAX 的开源 Python 库。
你可以将它理解为机器学习领域的 “GitHub”。就像 GitHub 托管代码、促进协作一样,Hugging Face 托管模型、数据集和机器学习应用。
Hugging Face Inference Providers 将 15 个以上的推理合作伙伴整合到一个兼容 OpenAI 的端点下。 使用相同的统一 API 从原型转移到生产,无需管理任何基础设施。

LLM调用案例

使用moonshotai/Kimi-K2-Instruct-0905向LLM发出你的首次推理请求。

案例1

提示词:Summarize the plot of ‘Matrix’.

代码1

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.environ["HF_TOKEN"],
)

completion = client.chat.completions.create(
    model="moonshotai/Kimi-K2-Instruct-0905",
    messages=[
        {
            "role": "user",
            "content": "Summarize the plot of 'Matrix'."
        }
    ],
)

print(completion.choices[0].message)

{
“id”: “chatcmpl-528737c3-9ec1-4715-8917-65117a20908d”,
“object”: “chat.completion”,
“created”: 1765958650,
“model”: “moonshotai/kimi-k2-instruct-0905”,
“choices”: [
{
“index”: 0,
“message”: {
“role”: “assistant”,
“content”: “In The Matrix (1999), computer hacker Neo discovers that the world he knows is a simulated reality created by sentient machines to subdue humanity while their bodies are used as an energy source. Freed by a rebel crew led by Morpheus and the warrior Trinity, Neo learns to manipulate the Matrix’s rules and accept his role as “The One,” a prophesied savior. After rescuing Morpheus from the machine enforcers (Agents) and being killed inside the program, Neo reawakens with full control over the simulation, defeating the Agents and announcing to the machines that he will free humanity.”
},
“logprobs”: null,
“finish_reason”: “stop”
}
],
“usage”: {
“queue_time”: 0.001922328,
“prompt_tokens”: 34,
“prompt_time”: 0.012118245,
“completion_tokens”: 129,
“completion_time”: 0.506438183,
“total_tokens”: 163,
“total_time”: 0.518556428
},
“usage_breakdown”: null,
“system_fingerprint”: “fp_5fe129dff6”,
“x_groq”: {
“id”: “req_01kcnnd25efskbaggy672xa9z0”,
“seed”: 636195086
},
“service_tier”: “on_demand”
}

结果

In The Matrix (1999), computer hacker Neo discovers that the world he knows is a simulated reality created by sentient machines to subdue humanity while their bodies are used as an energy source. Freed by a rebel crew led by Morpheus and the warrior Trinity, Neo learns to manipulate the Matrix’s rules and accept his role as “The One,” a prophesied savior. After rescuing Morpheus from the machine enforcers (Agents) and being killed inside the program, Neo reawakens with full control over the simulation, defeating the Agents and announcing to the machines that he will free humanity.

图像生成

此处用的模型是 black-forest-labs/FLUX.1-dev.

提示词:A cute alien creature in a spaceship

代码

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="together",
    api_key=os.environ["HF_TOKEN"],
)

# output is a PIL.Image object
image = client.text_to_image(
    "A cute alien creature in a spaceship",
    model="black-forest-labs/FLUX.1-dev",
)

{
“id”: “oPYiVjF-2kFHot-9af4e7962b023b42-SEA”,
“model”: “black-forest-labs/FLUX.1-dev”,
“object”: “list”,
“data”: [
{
“timings”: {
“inference”: 2.1059570303186774
},
“index”: 0,
“b64_json”: “/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEB…rA/MDkYGe1O10K6R/9k=”
}
]
}

可视化结果

在这里插入图片描述
在这里插入图片描述

Hugging Face 本质上是一个以开源库为引擎、以托管平台为基地、以全球社区为动力的AI基础设施层。 它不仅仅是工具的集合,更是一个活跃的、不断成长的生态系统,深刻地改变了人们创建、分享和部署机器学习模型的方式,是当今AI民主化运动中最关键的推动者之一。

提示

这里当我们第一次进行注册时,官方给我的案例可以如上文所示,当我们从我们注册时候的账户实现后,点击进行验证后才能实现再次调用官网首页的内容后就可以实现了
可视化结果
注册后的案例:
调用大模型

中英文皆可的影像生成
在主页上,这里有超过51000的模型,可以通过左上角选择主要的研究方向来选择不同的模型。
在这里插入图片描述
每一个模型都有详细的介绍,大家可以通过进入主要的模型来进行详细的分析。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐