首页 > 数据库 >昇思AI框架实践2:基于T5的SQL语句生成模型推理

昇思AI框架实践2:基于T5的SQL语句生成模型推理

时间:2024-08-30 20:24:45浏览次数:8  
标签:t5 AI 模型 T5 SQL small import pretrained

 MindSpore 基于T5的SQL语句生成项目实施

基于T5的SQL语句生成项目介绍

  • 本项目旨在开发一个基于T5-small模型的自然语言转SQL语句生成器。该生成器能够将用户以自然语言形式提出的查询请求转换为对应的SQL查询语句,从而使得即使是不熟悉SQL语言的用户也能够轻松地从数据库中检索所需信息。
  • 本项目使用HuggingFace中经过大量英文语料预训练的T5-small模型并对其模型迁移,将其变为MindSpore可用的模型。

项目地址:昇思大模型平台

 

项目mindspore环境安装,参见:昇思AI框架实践1:安装MindSpoe和MindFormers-CSDN博客

 

 下载基于T5的SQL语句生成模型

项目地址:昇思大模型平台

模型文件下载地址:昇思大模型平台 

前面走了弯路,使用git clone下载了模型。其实mindformers支持自动下载模型,所以只要在代码里设定好模型的名字为t5_small即可

import mindspore
from mindformers import T5ForConditionalGeneration, T5Tokenizer


t5 = T5ForConditionalGeneration.from_pretrained(”t5_small“)

使用git下载模型(不必须)

模型位置:昇思大模型平台

使用git下载

git clone https://source-xihe-mindspore.osinfra.cn/zhanglingyun2023/Text2SQL_model.git

MindSpoe模型推理lenet模型例子

加载lenet模型例子

from mindspore import load_checkpoint, Tensor
from mindspore.common import set_seed
from mindvision.classification.models import lenet
from mindspore.train import Model

# 定义模型
net = lenet(num_classes=10, pretrained=False)

# 加载参数
param_dict = load_checkpoint("./lenet/lenet-1_1875.ckpt")

# 将参数加载到模型中
load_param_into_net(net, param_dict)

 推理lenet模型

# 假设data是一个包含输入数据的字典,labels是实际标签
output = model.predict(Tensor(data['image']))
predicted = np.argmax(output.asnumpy(), axis=1)
print(f'Predicted: "{predicted}", Actual: "{labels}"')

后来发现可以直接用MindFormers推理,非常简单方便。

使用MindFormers推理 

MindFormers里面给的例子:

python run_mindformer.py --config {CONFIG_PATH} --run_mode {train/finetune/eval/predict}

根据这个例子,改写的命令应该是:

python mindformers/run_mindformer.py --config Text2SQL_model/text2sql.yaml --run_mode Text2SQL_model/text2sql.ckpt

后来发现了该项目里面gradio app的例子代码,参考该代码,MindFormers在python里面使用起来更简单方便。

项目中的gradio app例子代码

import gradio as gr
import mindspore
from mindformers import T5ForConditionalGeneration, T5Tokenizer

model_path = './'
t5 = T5ForConditionalGeneration.from_pretrained(model_path)
tokenizer = T5Tokenizer.from_pretrained("t5_small")
t5.set_train(False)
mindspore.set_context(mode=0, device_id=0)

def generate_SQL(text):
    text = "translate English to SQL: %s </s>" % text
    inputs = tokenizer(text)
    outputs = t5.generate(inputs["input_ids"],do_sample=False)
    response = tokenizer.decode(outputs,skip_special_tokens=True)[0]
    return response

# 创建 Gradio 界面
iface = gr.Interface(
    fn=generate_SQL,
    inputs=[
        gr.Textbox(lines=2, placeholder="请输入需求"),
    ],
    outputs=gr.Textbox(),
    title="SQL语句生成器",
    description="请输入英文需求,自动生成SQL语句。\n 例如:Search for the names of all employees over the age of 30。"
)

# 运行应用程序
iface.launch()

经过测试,发现可以直接在t5 = T5ForConditionalGeneration.from_pretrained(model_path) 这句话里写模型名字,如:t5 = T5ForConditionalGeneration.from_pretrained("t5_small")系统会自动下载模型。当然也可以像例子那样写,手工下载模型文件到相应的目录。

根据例子代码改写的推理代码

如果没有手工下载模型,那就在设置里写上模型名字"t5_small"即可。

t5 = T5ForConditionalGeneration.from_pretrained(”t5_small“)

tokenizer = T5Tokenizer.from_pretrained("t5_small")

import mindspore
from mindformers import T5ForConditionalGeneration, T5Tokenizer

# model_path = './'
# t5 = T5ForConditionalGeneration.from_pretrained(model_path)
t5 = T5ForConditionalGeneration.from_pretrained("t5_small")
tokenizer = T5Tokenizer.from_pretrained("t5_small")
t5.set_train(False)
mindspore.set_context(mode=1, device_id=0)

def generate_SQL(text):
    text = "translate English to SQL: %s </s>" % text
    inputs = tokenizer(text)
    outputs = t5.generate(inputs["input_ids"],do_sample=False)
    response = tokenizer.decode(outputs,skip_special_tokens=True)[0]
    return response

description="请输入英文需求,自动生成SQL语句。\n 例如:Search for the names of all employees over the age of 30。"
inputs = input(description)
output = generate_SQL(inputs)
print (output)
while True:
    inputs = input(description)
    if inputs=="q" or inputs=="0" :
        break
    output = generate_SQL(inputs)
    print(output)

推理结果

2024-08-29 13:30:48,564 - mindformers[mindformers/generation/text_generator.py:478] - INFO - total time: 19.149714946746826 s; generated tokens: 13 tokens; generate speed: 0.678861279979964 tokens/s

SELECT User FROM table WHERE Name = hello

共计用时19秒,在cpu下速度算是可以了。

问题:Search for the names of all employees over the age of 30。"

回答:SELECT Name FROM table WHERE Label = "Stu_Db" AND Age > 30

标签:t5,AI,模型,T5,SQL,small,import,pretrained
From: https://blog.csdn.net/skywalk8163/article/details/141690517

相关文章

  • [kubernetes]使用kubeadm和containerd部署kubernetes
    前言因宿主机内核版本限制和垂直伸缩特性的需要,安装的k8s版本为1.25,runtime为containerd,cni为calico。containerd、kubeadm、kubelet也可以用包管理器来安装,因为不想配repo,也不想校验repo版有哪些区别,所以这几个都是用原生二进制方式安装的。环境信息IPHostnameOSVersio......
  • The American University in Cairo CSEA End of Winter Break Contest 2023
    链接:https://codeforces.com/gym/104168\(\\\)ADivisorDifference签到,输出\(n-1\)即可,复杂度\(O(1)\)。点击查看代码#pragmaGCCoptimize("unroll-loops,Ofast")#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;#defineendl&......
  • dailydictation.com
    [  "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",  "window",  "self",  "document",  &......
  • 怎么清除mysql磁盘mysql删除的数据
    在MySQL中,被删除的数据默认情况下是被放置在一个空间上被标记为可重用但实际并未立即释放的状态。这允许快速重用该空间,但如果需要彻底从磁盘上清除这些数据,可以使用OPTIMIZETABLE命令。请注意,OPTIMIZETABLE并不能保证彻底删除数据,因为它的目的是重新组织表并释放未使用的空间......
  • DynamiCrafter:Animating open-domain images with video diffusion priors
    1.Method图像条件视频生成,1.1ImageDynamicsfromVideoDiffusionPriors1.1.1文本对齐的上下文表征文本嵌入通过clip构建,图像通过clip编码,主要代表语义层面的视觉内容,未能捕获图像的完整信息,为了提取更完整的信息,使用来自clip图像vit最后一层的全视觉标记,该token在条......
  • tail: inotify resources exhausted
    "tail:inotifyresourcesexhausted"这个错误表明系统的inotify资源已经耗尽。inotify是Linux内核的一项功能,用于监视文件系统中的事件,例如文件的创建、删除、修改等。tail-f命令使用inotify来实时监视文件的变化,如果系统中的inotify资源耗尽,tail会退回到使用轮询(pol......
  • 设置 Nginx、MySQL 日志轮询
    title:设置Nginx、MySQL日志轮询tags:author:ChingeYangdate:2024-8-301.Nginx设置日志轮询机器直接安装的:/etc/logrotate.d/nginx/var/log/nginx/*.log{dailymissingokrotate30compressdelaycompressno......
  • 云计算:LNMP网站架构,前期准备,安装php,安装MySQL
    准备工作(初始化)1.关闭防火墙systemctl disablefirewalld --now    //直接永久关闭防火墙2.关闭SELINUX 查看SELINUX:getenforce永久关闭:[root@localhost~]#vim/etc/selinux/configSELINUX=enforcing|disabled或者[root@localhost~]#sed-i's/^S......
  • C#之中SqlConnection的Close和Dispose的区别和在使用using语句管理SqlConnection对象
    SqlConnection的Close和Dispose的区别在C#中,SqlConnection对象的Close和Dispose方法都可以用来释放数据库连接资源,但它们的作用和使用场景有所不同。Close方法SqlConnection.Close方法用于关闭与数据库的连接。当你调用这个方法时,它会关闭连接,但不会释放与连接关联的所有......
  • AI学会“视听”新语言,人大北邮上海AI Lab引领多模态理解革命 | ECCV2024亮点
    你是否想过,AI是如何“理解”我们这个多彩世界的呢?最近,一项由中国人民大学高瓴GeWu-Lab、北京邮电大学、上海AILab等机构联合研究的成果,为AI的“感官”升级提供了一种新思路。这项研究被收录于即将召开的计算机视觉顶级会议ECCV2024。AI的“视听盛宴”想象一下,你正在观......