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

 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

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值