当前位置: 首页 > news >正文

专题网站怎么做wordpress全站网易云音乐播放

专题网站怎么做,wordpress全站网易云音乐播放,微营销网站建设,品牌营销的重要性如果机器学习模型没有投入生产供人们使用#xff0c;就无法充分发挥其潜力。 根据我们的经验#xff0c;将模型投入生产的最常见方法是为其创建 API。 然而#xff0c;我们发现这个过程对于 ML 开发人员来说可能相当令人畏惧#xff0c;特别是如果他们不熟悉 Web 开发的话。…如果机器学习模型没有投入生产供人们使用就无法充分发挥其潜力。 根据我们的经验将模型投入生产的最常见方法是为其创建 API。 然而我们发现这个过程对于 ML 开发人员来说可能相当令人畏惧特别是如果他们不熟悉 Web 开发的话。 在这篇文章中我们将向你展示如何使用 FastAPI 和 Gradio 将模型公开为 API然后创建一个 Docker 映像该映像已准备好部署到容器实例中。 在线工具推荐 Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 1、技术栈概述 我们使用 nq-distilbert-base-v1这是一个在 Natural Questions 数据集上训练过的sentence-transformer模型。 该数据集包含来自 Google 搜索的真实问题以及来自维基百科的注释数据提供了答案。 对于段落我们将维基百科文章标题与各个文本段落一起编码。 我们使用的是简单英语维基百科它仅包含 17 万篇文章因此它要小得多并且可以装入内存。 该模型还支持 GPU因此可以在 Nvidia CUDA 设备上运行。 1.1 FastAPI FastAPI 是一个现代、快速的 Web 框架用于基于标准 Python 类型提示在 Python 中构建 API。 它开箱即用支持通过 Swagger UI 使用 OpenAPI 标准自动生成 API 文档。 1.2 Gradio Gradio 是一个 Python 库用于为 ML 模型快速创建 Web 界面。 它构建在 Streamlit 之上旨在易于使用和部署。 这对于向其他人展示你的模型并获得反馈非常有用且有趣。 1.3 Docker Docker 是一个用于以容器形式构建、运行和运输应用程序的平台。 它是虚拟机的轻量级替代方案是将应用程序及其依赖项打包到单个单元中的好方法。 可以说它已经成为将应用程序投入生产的事实上的标准。 2、构建 API 我们将使用 FastAPI 来构建我们的 API并使用 uvicorn 来运行它。 Gradio 将用于创建演示 Web 界面并将其安装在 FastAPI 应用程序上。 理想情况下你应该使用 python 3.7 及更高版本。 首先安装这些依赖项 pip install fastapi uvicorn gradio sentence-transformers 在使用 FastAPI 之前我们需要编写一个预测函数该函数接受问题模型的输入并返回答案模型的输出 # main.pyfrom sentence_transformers import SentenceTransformer, utilbi_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 partdef predict(question):# Encode the query using the bi-encoder and find potentially relevant passagesquestion_embedding bi_encoder.encode(query, convert_to_tensorTrue)hits util.semantic_search(question_embedding, corpus_embeddings, top_ktop_k)hits hits[0] # Get the hits for the first query# transform hits into a list of dictionaries, and obtain passages with corpus_idresults [{score: hit[score],title: passages[hit[corpus_id]][0],text: passages[hit[corpus_id]][1],}for hit in hits]return results 在 Github 上查看此文件的完整代码。 3、使用 Gradio 测试预测函数 为了快速测试预测功能我们可以使用 Gradio 为其创建一个 Web 界面。 但首先创建一个函数从 Gradio 读取输入调用预测函数并以 Gradio 可以理解的格式返回输出。 # main.pyimport gradio as grdef gradio_predict(question: str):results predict(question) # results is a list of dictionariesbest_result results[0]# return a tuple of the title and text as a string, and the score as a numberreturn f{best_result[title]}\n\n{best_result[text]}, best_result[score] 我们现在可以为 gradio_predict 函数创建一个 Gradio 接口 # main.pydemo gr.Interface(fngradio_predict,inputsgr.Textbox(labelAsk a question, placeholderWhat is the capital of France?),outputs[gr.Textbox(labelAnswer), gr.Number(labelScore)],allow_flaggingnever, )demo.launch() 现在运行 python main.py 来启动 Gradio 演示它现在可以在 localhost:7860 上使用。 由于下载和加载模型因此初始化可能需要一段时间 尝试发送问题你应该会看到答案和分数。 4、创建 FastAPI 应用 现在我们使用 FastAPI 在 /predict 创建一个 POST 请求端点该端点接收带有问题键的 JSON 对象并返回预测函数的结果。 # main.pyfrom fastapi import FastAPI from pydantic import BaseModelapp FastAPI()class Request(BaseModel):question: strclass Result(BaseModel):score: floattitle: strtext: strclass Response(BaseModel):results: typing.List[Result] # list of Result objectsapp.post(/predict, response_modelResponse) async def predict_api(request: Request):results predict(request.question)return Response(results[Result(scorer[score], titler[title], textr[text])for r in results]) 让我们来分解一下。 from fastapi import FastAPI from pydantic import BaseModelapp FastAPI() 这将创建一个 FastAPI 应用程序我们现在可以向其添加端点。 class Request(BaseModel):question: strclass Result(BaseModel):score: floattitle: strtext: strclass Response(BaseModel):results: typing.List[Result] # list of Result objects 这些是我们用来定义 API 预期输入和输出的 pydantic 模型。 定义这些模型是可选的但这样做是一个很好的做法因为它允许 FastAPI 自动生成 API 文档。 app.post(/predict, response_modelResponse) async def predict_api(request: Request):results predict(request.question)return Response(results[Result(scorer[score], titler[title], textr[text])for r in results]) 这是我们添加到 FastAPI 应用程序的 POST 请求端点。 它接受一个请求模型并返回一个我们之前定义的响应模型。 本质上它运行我们编写的预测函数并返回结果。 5、测试 FastAPI 应用程序 现在我们可以通过运行 uvicorn main:app --reload 来测试 FastAPI 应用程序然后使用包含问题键的 JSON 正文向 localhost:8000/predict 发送 POST 请求 curl -X POST http://localhost:8000/predict \ -H accept: application/json \ -H Content-Type: application/json \ -d {\question\:\What is the capital of France?\} 应该收到一个结果列表作为问题的答案 {results: [{score: 0.8317935466766357,title: Capital of France,text: The capital of France is Paris. In the course of history, the national capital has been in many locations other than Paris.},{score: 0.7517948150634766,title: Versailles, Yvelines,text: Versailles is a French city. It is in the western suburbs of Paris, 17.1 km. (10.6 miles) from the center of Paris. It is the \capital\ of the Yvelines département. This city is very important for the History of France because it was formerly the capital of the kingdom of France.},{score: 0.7379363775253296,title: Arrondissement of Sarlat-la-Canéda,text: The arrondissement of Sarlat-la-Canéda is an arrondissement of France. It is part of the Dordogne \département\ in the Nouvelle-Aquitaine region. Its capital is the city of Sarlat-la-Canéda.},{score: 0.7338694334030151,title: Arrondissement of Figeac,text: The arrondissement of Figeac is an arrondissement of France. It is part of the Lot \département\ in the Occitanie region. Its capital is the city of Figeac.},{score: 0.7309824228286743,title: Arrondissement of Confolens,text: The arrondissement of Confolens is an arrondissement of France, in the Charente department, Nouvelle-Aquitaine region. Its capital is the city of Confolens.}] } 6、浏览 FastAPI 文档 现在在浏览器中打开 localhost:8000/docs你应该会看到 FastAPI 文档。 这对于其他开发人员了解如何使用你的 API 非常有用。 7、将 Gradio 演示安装为应用程序 要使用 FastAPI 应用程序和我们创建的 Gradio 演示构建统一的服务器我们只需将 Gradio 演示安装到 FastAPI 应用程序中即可 # main.py# mounting at the root path app gr.mount_gradio_app(app, demo, path/) 就这样我们可以使用 uvicorn main:app --reload 重新启动应用程序Gradio 演示现在应该可以在 localhost:8000 上使用。 8、构建 Docker 镜像 现在我们有了一个可以运行的应用程序我们可以为其构建一个 Docker 镜像。 我们将使用 Docker Hub 上提供的 pytorch 基础镜像。 具体来说我们将使用 pytorch/pytorch:1.13.1-cuda11.6-cudnn8-runtime 镜像它是 PyTorch 1.13.1 启用 CUDA 的映像。 在构建图像之前我们需要一个 Entrypoint.sh 脚本来使用 uvicorn 运行应用程序。 它还需要一个 PORT 环境变量以便我们可以指定运行应用程序的端口 # entrypoint.sh#!/bin/bashuvicorn --host 0.0.0.0 --port $PORT app:app 之后我们在 Dockerfile 中编写一组用于构建映像的指令 # Dockerfile# Base image FROM pytorch/pytorch:1.13.1-cuda11.6-cudnn8-runtime# Set environment variables ENV PORT8000# Install python dependencies RUN pip install sentence-transformers fastapi uvicorn gradio# Set working directory WORKDIR /model# Copy files to /model COPY . .# Make entrypoint.sh executable RUN chmod x ./entrypoint.sh# Run entrypoint.sh when container starts ENTRYPOINT [ /model/entrypoint.sh ] 为了构建镜像我们运行 docker build -t wiki-qa . 现在我们完成了 我们可以运行镜像 docker run -it --rm -p 8000:8000 wiki-qa 该应用程序应该再次在 localhost:8000 上可用。 该映像现在已准备好部署到任何容器服务。 它可以在 CPU 或 GPU 上运行具体取决于你希望预测的速度。 例如你可以使用 Deploifai 部署此映像并在几分钟内获得生产就绪的 API 端点 10、生产环境性能 我们在 Deploifai 上创建了 2 个测试部署一个在 CPU 上一个在 GPU (K80) 上以比较模型在 CPU 与 GPU 上的性能 部署vCPURAMAPI响应时间CPU28 GB2 - 4 秒GPU (K80)28 GB0.5 - 1 秒 原文链接Gradio App生产部署 - BimAnt
http://www.zqtcl.cn/news/428961/

相关文章:

  • 新网站做内链智能网站推广软件
  • 西宁市住房和城乡建设局网站广州站是指哪个站
  • 帮建网站网页设计师考试内容
  • seo网站开发txt 发布 wordpress
  • 资讯门户类网站模板定制系统开发公司
  • 让网站快速收录初中毕业如何提升学历
  • 石家庄做网站价格seo优化效果
  • 为什么浏览器打开是2345网址导航seo免费资源大全
  • 网站工程是干啥的动态个人网页制作html教程
  • 阿里云多网站建设wordpress 统计分析
  • 长沙网站定制公司科技特长生
  • 查公司的口碑和评价的网站中学生怎么做网站
  • 做网站买空间多少钱深圳seo优化公司
  • 中国建设银行北京市互联网网站wordpress商城购物表单
  • 万网网站备案管理查询工程建设项目的网站
  • 网站建设国内外研究现状模板ppt设计大赛
  • 专业网站优化方案网站设计过程怎么写
  • 福州定制网站建设网站ip过万
  • wordpress网站评论插件厦门软件网站建设
  • 网站黄金比例wordpress转typecho
  • 重庆有哪些网络公司百度系优化
  • 无锡网站制作方案企业三合一建站公司怎么找
  • 钉钉crm客户管理系统免费seo网站推荐一下软件
  • wordpress公司网站模版怎么显示wordpress里元素的源代码
  • 泉州网站制作运营商专业wordpress评论软件
  • 网站开发是什么意思啊有没有帮人做简历的网站
  • 企业网站模板包含什么维度 网站建设
  • 个人备案做运营网站宁波建网站模板
  • 做网站需要懂什么软件合肥网站制作套餐
  • 中国建设银行官方网站纪念钞预约网上买手表哪个网站可以买到正品