Hugging_Face实战

系列博客目录


文章目录


hugging face在线使用 对国内用户不友好,所以我们使用hugging face的三个核心组件(Transformers、datasets、Tokenizer)来实现本地调用。

1.使用API

python 复制代码
import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key="hf_XXX",
)

result = client.text_classification(
    "I hate you.",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)

print(result)

输出:

powershell 复制代码
D:\anaconda3\envs\Hugging_Face\python.exe D:\Hugging_Face\Hugging_Face_practise\API_test\self.py 
[TextClassificationOutputElement(label='NEGATIVE', score=0.9992952346801758), TextClassificationOutputElement(label='POSITIVE', score=0.0007047692197375)]

Process finished with exit code 0

2.下载模型

python 复制代码
# 将模型下载到本地调用
from transformers import AutoModelForCausalLM,AutoTokenizer
# 将模型和分词工具下载到本地,并指定保存路径
model_name ="uer/gpt2-chinese-cluecorpussmall"
cache_dir ="model/uer/gpt2-chinese-cluecorpussmall"
# 下载模型
AutoModelForCausalLM.from_pretrained(model_name, cache_dir=cache_dir)

#下载分词工具
AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)

print(f"模型分词器已下载到:{cache_dir}")

下载后效果:

其中config.json 是配置文件,里面的"vocab_size": 21128就是最多有多少种token。

3.使用本地模型,并且使用generator中多个参数

python 复制代码
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
#设置具体包含 config.json 的目录
model_dir =r"D:\Hugging_Face\Hugging_Face_practise\API_test\model\uer\gpt2-chinese-cluecorpussmall\models--uer--gpt2-chinese-cluecorpussmall\snapshots\c2c0249d8a2731f269414cc3b22dff021f8e07a3"
# 加载模型和分词器
model = AutoModelForCausalLM.from_pretrained(model_dir)
tokenizer =AutoTokenizer.from_pretrained(model_dir)

#使用加载的模型和分词器创建生成文本的 pipeline
generator =pipeline("text-generation", model=model, device="cuda", tokenizer=tokenizer)

# 生成文本
# output = generator("你好,我是一款语言模型,", max_length = 50, num_return_sequences=1)
output = generator(
"你好,我是一款语言模型",
    max_length=50,  # 指定生成文本的最大长度。这里的50表示生成的文本最多包含50个标记(tokens)
    num_return_sequences=1,  # 参数指定返回多少个独立生成的文本序列。值为1表示只生成并返回一段文本。
    truncation=True,  # 该参数决定是否截断输入文本以适应模型的最大输入长度。如果True,超过模型最大输入长度的部分会被截断
    temperature=0.7,  # 该参数控制生成文本的随机性。值越低,生成的文本越保守((倾向于选择概率较高的词),值越高,生成的文本越多样(倾向于选择更多不同的词)
    top_k=50,  # 该参数限制模型在每一步生成时仅从概率最高的k个词中选择下一个词。这里 top_k=50 表示模型在生成每个词时只考虑概率最高的前50个候选词。如果设置为1,则模型每次产生的结果相同
    top_p=0.9,  # 该参数(又称为核采样)进一步限制模型生成时的间汇选择范围。它会选择一组累积概率达到 p 的词汇,模型只会从这个概率集合中采样。
    clean_up_tokenization_spaces=False  # 该参数控制生成的文本中是否清理分词时引入的空格
)
print(output[0])

4.

5.

6.

7.

8.

9.

10.

相关推荐
曲径海盗1 小时前
【Python 学习第 41 天】for 循环
python·学习笔记·编程入门
摇滚侠1 小时前
《On Java 中文版 基础卷》阅读笔记 实现隐藏 11
java·笔记·python
沧海一笑-dj1 小时前
【Python】Python学习笔记-模块与包
python·pip·模块·包·python包·python模块·包与模块
echome8881 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
云和数据.ChenGuang1 小时前
Python 单继承与多继承深度剖析:从 MRO 到实战取舍
开发语言·人工智能·python·langchain
威哥爱编程1 小时前
Python 智能体开发实战 02:你的 AI 助教说话像换了个模型?九成的 system prompt 都写错了
python·agent
威哥爱编程1 小时前
Python 智能体开发实战 01:装了三遍环境还在报错?虚拟环境和 DeepSeek 一次配到位
python·agent
小咕聊编程2 小时前
基于Python的豆瓣电影数据可视化系统构建分析
开发语言·python·信息可视化
小小张说故事3 小时前
Python 的 dict 为什么查询是 O(1)?拆开哈希表、扰动函数与扩容阈值
python
像风一样的男人@4 小时前
python --基于scp的文件传输助手
开发语言·python