动手制作个人电脑对话机器人transformers+DialoGPT

简介:DialoGPT是一个对话模型,由微软基于GPT-2训练。由于DialoGPT在对话数据上进行了预训练,所以它比原始的GPT-2更擅长生成类似对话的文本。DialoGPT的主要目标是生成自然且连贯的对话,而不是在所有情况下都提供事实上的正确答案。此外,由于模型的预训练数据主要是英文,因此它可能无法很好地处理中文输入。在运行代码之前,请确保已经安装了Hugging Face的Transformers库。

历史攻略:

OpenCV合成全景图

Python+opencv:图像修复

flask+opencv+实时滤镜(原图、黑白、怀旧、素描)

flask+opencv:实时视频直播推流平台Demo

安装:

python 复制代码
pip install transformers

案例源码:

python 复制代码
# -*- coding: utf-8 -*-
# time: 2023/6/9 14:00
# file: test.py
# 公众号: 玩转测试开发

from transformers import GPT2LMHeadModel, GPT2Tokenizer


def chatbot_response(prompt):
    tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-small')
    model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-small')

    inputs = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
    outputs = model.generate(inputs, max_length=1000, temperature=0.7, pad_token_id=tokenizer.eos_token_id)

    response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)

    return response


print(chatbot_response("Hi, what is your name?"))
print(chatbot_response("Nice to meet you."))

运行效果:

相关推荐
databook17 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室18 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三19 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python