french directory Gradio机器学习App生产部署方案
在生产环境进行模型部署过程中,构建应用程序编程接口(API)的方式已普遍应用;本文将详细阐述这一操作的各个步骤;同时,还将对相关工具进行详细介绍;旨在协助读者全面了解并掌握这一流程。
投入生产常见法
在生产环境中,构建API已成为一种普遍的实践方式。这种方式使得模型能够更加顺畅地融入实际应用场景中。众多企业和开发者普遍采用了这一策略pg娱乐电子游戏官网官方网站入口,旨在将他们训练的模型推广至市场。以数据分析、图像识别等众多领域为例,API为更广泛的用户群体提供了高效的服务。
在线工具推荐
pip install fastapi uvicorn gradio sentence-transformers
网络中蕴藏着多种实用高效的工具,例如Three.js AI纹理开发包以及YOLO合成数据生成器等。这些工具在3D模型的设计与开发、数据生成等多个领域均得到了广泛的应用。Three.js AI纹理开发工具辅助开发者高效制作纹理,此外,YOLO合成数据生成器能够生成适合模型训练的合成数据,这两项技术共同为模型的训练及实际应用提供了重要的支撑。
# main.py
from sentence_transformers import SentenceTransformer, util
bi_encoder = SentenceTransformer('nq-distilbert-base-v1')
### Create corpus embeddings containing the wikipedia passages
### To keep things summaraized, we are not going to show the code for this part
def predict(question):
# Encode the query using the bi-encoder and find potentially relevant passages
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k)
hits = hits[0] # Get the hits for the first query
# transform hits into a list of dictionaries, and obtain passages with corpus_id
results = [
{
"score": hit["score"],
"title": passages[hit["corpus_id"]][0],
"text": passages[hit["corpus_id"]][1],
}
for hit in hits
]
return results
FastAPI框架优势
# main.py
import gradio as gr
def gradio_predict(question: str):
results = predict(question) # results is a list of dictionaries
best_result = results[0]
# return a tuple of the title and text as a string, and the score as a number
return f"{best_result['title']}\n\n{best_result['text']}", best_result["score"]
# main.py
demo = gr.Interface(
fn=gradio_predict,
inputs=gr.Textbox(
label="Ask a question", placeholder="What is the capital of France?"
),
outputs=[gr.Textbox(label="Answer"), gr.Number(label="Score")],
allow_flagging="never",
)
demo.launch()
FastAPI是一款当代的Web开发框架,以其卓越的性能而闻名。该框架通过Python的规范类型提示技术来构建API,并提供了便捷的快速部署方案。同时,FastAPI能够自动生成API文档,这一功能依托于OpenAPI UI,严格遵守OpenAPI规范。这一特性显著降低了开发者编写文档的工作量,提升了开发工作的效率,同时,在众多实际项目中,这一特性已被广泛应用。
Gradio库作用
Gradio是一个专注于快速构建Web界面的库,尤其适合用于机器学习模型的开发。该库可作为虚拟机的轻量级替代品,将应用程序及其相关依赖项封装于一个独立的单元之中。对于小型创业公司而言,Gradio能够迅速搭建展示界面,极大地简化了模型的测试与展示流程。
# main.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Request(BaseModel):
question: str
class Result(BaseModel):
score: float
title: str
text: str
class Response(BaseModel):
results: typing.List[Result] # list of Result objects
@app.post("/predict", response_model=Response)
async def predict_api(request: Request):
results = predict(request.question)
return Response(
results=[
Result(score=r["score"], title=r["title"], text=r["text"])
for r in results
]
)
预测函数编写
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
在启动Gradio之前pg下载388.pm麻将胡了,必须先编写一个用于预测的函数;该函数的任务是接收模型输入的问题,并输出相应的答案;为了便于测试预测功能,可以利用Gradio构建一个Web界面。要启动Gradio演示,需要运行main.py文件;尽管模型下载和初始化可能需要一定时间,但用户仍能直接观察到模型的预测效果。
构建与部署
class Request(BaseModel):
question: str
class Result(BaseModel):
score: float
title: str
text: str
class Response(BaseModel):
results: typing.List[Result] # list of Result objects
在FastAPI应用的根目录下,必须建立一个POST请求的接口,该接口的主要功能是接收特定格式的JSON数据,并输出相应的预测结果。通过执行特定的指令,可以对FastAPI应用进行测试,并向其发送POST请求。此外,还需要构建一个统一的服务器架构,并将Gradio的演示功能整合到FastAPI应用中。在完成 Docker 镜像的构建之后,必须编写一个 .sh 脚本来启动应用程序,同时利用 PORT 环境变量来设定端口号。采用 Docker 进行部署,可以迅速实现一个符合生产标准的 API 端点的部署。
@app.post("/predict", response_model=Response)
async def predict_api(request: Request):
results = predict(request.question)
return Response(
results=[
Result(score=r["score"], title=r["title"], text=r["text"])
for r in results
]
)
在部署模型及构建API接口环节,您是否遭遇了挑战?请于评论区分享您的具体遭遇,并且,敬请记得为本文点赞并予以传播。