前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LLM】构建一个可以图像生成的聊天机器人

【LLM】构建一个可以图像生成的聊天机器人

原创
作者头像
Freedom123
发布2024-04-29 16:09:05
730
发布2024-04-29 16:09:05
举报
文章被收录于专栏:AIGCAIGC

toc


今天,我从头开始创建图像生成聊天机器人。按照已经构建的聊天机器人 awesome-tiny-sd 的脚本来执行

awesome-tiny-sd项目地址:https://astrabert.github.io/awesome-tiny-sd

环境准备

安装依赖库

代码语言:shell
复制
python3 -m pip install gradio==4.25.0 diffusers==0.27.2 torch==2.1.2 pydantic==2.6.4 accelerate transformers trl peft

安装完毕之后,在当前文件夹中创建文件如下:

代码语言:shell
复制
./
|__ app.py
|__ imgen.py

第一步:导入stable-diffusion

导入stable-diffusion模型到imgen.py文件中

  • 导入依赖选项
代码语言:shell
复制
  from diffusers import DiffusionPipeline
  import torch
  • 定义图像生成管道(这将自动下载您指定的稳定扩散模型及其所有相关组件
代码语言:shell
复制
  pipeline = DiffusionPipeline.from_pretrained("segmind/small-sd", torch_dtype=torch.float32)

我们选择的是segmind/small-sd,这个模型不是很大,而且对CPU支持友好。

segmind/small-sd下载地址:https://huggingface.co/segmind/small-sd

第二步:定义核心函数

  • 导入依赖选项
代码语言:shell
复制
  import gradio as gr
  import time
  from imgen import *
  • 定义一个简单功能,打印用户喜欢的和不喜欢的信息
代码语言:shell
复制
  def print_like_dislike(x: gr.LikeData):
      print(x.index, x.value, x.liked)
  • 将新消息和/或上传的文件附加到聊天机器人历史记录的功能:
代码语言:shell
复制
  def add_message(history, message):
      if len(message["files"]) > 0:
          history.append((message["files"], None))
      if message["text"] is not None and message["text"] != "":
          history.append((message["text"], None))
      return history, gr.MultimodalTextbox(value=None, interactive=False)
  • 从文本提示符开始生成图像的函数:
代码语言:python
代码运行次数:0
复制
  def bot(history):
      if type(history[-1][0]) != tuple: ## text prompt
          try:
              prompt = history[-1][0]
              image = pipeline(prompt).images[0] ## call the model
              image.save("generated_image.png")
              response = ("generated_image.png",)
              history[-1][1] = response
              yield history ## return the image
          except Exception as e:
              response = f"Sorry, the error '{e}' occured while generating the response; check [troubleshooting documentation](https://astrabert.github.io/awesome-tiny-sd/#troubleshooting) for more"
              history[-1][1] = ""
              for character in response:
                  history[-1][1] += character
                  time.sleep(0.05)
                  yield history
      if type(history[-1][0]) == tuple: ## input are files
          response = f"Sorry, this version still does not support uploaded files :(" ## We will see how to add this functionality in the future
          history[-1][1] = ""
          for character in response:
              history[-1][1] += character
              time.sleep(0.05)
              yield history

第三步:构建聊天机器人

  • 使用 Gradio 定义聊天机器人块:
代码语言:python
代码运行次数:0
复制
  with gr.Blocks() as demo:
      chatbot = gr.Chatbot(
          [[None, ("Hi, I am awesome-tiny-sd, a little stable diffusion model that lets you generate images:blush:\nJust write me a prompt, I'll generate what you ask for:heart:",)]], ## the first argument is the chat history
          label="awesome-tiny-sd",
          elem_id="chatbot",
          bubble_full_width=False,
      ) ## this is the base chatbot architecture
  
      chat_input = gr.MultimodalTextbox(interactive=True, file_types=["png","jpg","jpeg"], placeholder="Enter your image-generating prompt...", show_label=False) ## types of supported input
  
      chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input]) ## receive a message
      bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response") ## send a message
      bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
  
      chatbot.like(print_like_dislike, None, None)
      clear = gr.ClearButton(chatbot) ## show clear button
  • 启动机器人
代码语言:python
代码运行次数:0
复制
  demo.queue()
  if __name__ == "__main__":
      demo.launch(server_name="0.0.0.0", share=False)
  • 执行脚本
代码语言:python
代码运行次数:0
复制
  python3 app.py

现在等着机器人启动成功,一单 stable diffusion 通道加载成功,聊天机器人应该在 localhost:7860(或类似 Linux 的操作系统为 0.0.0.0:7860)上运行。

小结

今天我们学习了通过stable diffusion模型构建对话机器人来生成图像信息,你可以通过 awesome-tiny-sd直接运行你的对话机器人来生成图像,您将使用 awesome-tiny-sd 生成的第一张图像是什么?请在下面的评论中告诉我

其他

线上体验地址:Hugging Face:https://huggingface.co/spaces/as-cle-bert/awesome-tiny-sd

您可以下载awesome-tiny-sd Docker镜像并通过容器运行它:

代码语言:shell
复制
docker pull ghcr.io/astrabert/awesome-tiny-sd:latest
docker run -p 7860:7860 ghcr.io/astrabert/awesome-tiny-sd:latest

我正在参与2024腾讯技术创作特训营最新征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境准备
  • 第一步:导入stable-diffusion
  • 第二步:定义核心函数
  • 第三步:构建聊天机器人
  • 小结
  • 其他
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档