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

算法/机理模型演示平台搭建(二)——算法接口部署(FastApi)

算法/机理模型演示平台搭建(二)—— 算法接口部署(FastApi)

    • 1. 项目结构
    • 2. 构建 Docker 镜像
    • 3. 运行 Docker 容器
    • 4. 访问 API 文档
    • 5. 调用 API

1. 项目结构

在这里插入图片描述
app
在这里插入图片描述
app/algorithms
在这里插入图片描述
app/models
在这里插入图片描述

Dockerfile

FROM python:3.9-slimWORKDIR /codeCOPY ./requirements.txt /code/requirements.txtRUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir -r requirements.txtCOPY ./app /code/appEXPOSE 8000CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

源码地址:https://download.csdn.net/download/qq_37128840/90949457

2. 构建 Docker 镜像

在包含 Dockerfile 的项目根目录下,运行以下命令构建镜像:

docker build -t industrial-algorithms-api .

3. 运行 Docker 容器

构建成功后,运行以下命令启动容器:

docker run -d --name industrial-algo-container -p 8000:8000 industrial-algorithms-api
  • -d: 在后台运行容器。
  • --name industrial-algo-container: 为容器指定一个名称。
  • -p 8000:8000: 将主机的 8000 端口映射到容器的 8000 端口。

4. 访问 API 文档

容器运行后,在浏览器中访问 http://localhost:8000/docs (或 http://<你的Docker主机IP>:8000/docs,如果 Docker 不在本地运行)。

你将看到由 FastAPI 自动生成的交互式 API 文档 (Swagger UI),其中列出了所有可用的算法端点、所需的输入参数(请求体)和预期的输出格式(响应体)。

在这里插入图片描述

5. 调用 API

你可以使用 API 文档页面直接发送请求来测试各个算法,或者使用 curlPostman 或其他编程语言(如 Python 的 requests 库)向以下端点发送 POST 或 GET 请求:

  • /algorithms/linear_regression (POST)
  • /algorithms/logistic_regression (POST)
  • /algorithms/decision_tree (POST)
  • /algorithms/svm (POST)
  • /algorithms/knn (POST)
  • /algorithms/kmeans (POST)
  • /algorithms/apriori (POST)
  • /algorithms/cnn_summary (POST) - 获取 CNN 模型摘要
  • /algorithms/fea_explanation (GET) - 获取 FEA 说明
  • /algorithms/meshing_explanation (GET) - 获取 Meshing 说明
  • /algorithms/genetic_algorithm (POST)
  • /algorithms/aco_tsp (POST)
  • /algorithms/pso (POST)
  • /algorithms/interpolation (POST)
  • /algorithms/pid_control (POST)
  • /algorithms/kalman_filter (POST)
  • /algorithms/fft_analysis (POST)
  • /algorithms/naive_bayes (POST) - 新增
  • /algorithms/random_forest (POST) - 新增
  • /algorithms/gradient_boosting (POST) - 新增
  • /algorithms/pca (POST) - 新增
  • /algorithms/svd (POST) - 新增
  • /algorithms/autoencoder (POST) - 新增
  • /algorithms/rnn_lstm (POST) - 新增
  • /algorithms/reinforcement_learning_concept (GET) - 新增
  • /algorithms/anomaly_detection (POST) - 新增
  • /algorithms/fuzzy_logic_concept (GET) - 新增
  • /algorithms/monte_carlo (POST) - 新增
  • /algorithms/simulated_annealing (POST) - 新增
  • /algorithms/bayesian_optimization_concept (GET) - 新增
  • /algorithms/gmm (POST) - 新增
  • /algorithms/arima (POST) - 新增
  • /algorithms/discrete_event_simulation (POST) - 新增
  • /algorithms/agent_based_modeling (POST) - 新增
  • /algorithms/system_dynamics (POST) - 新增

请求体示例:

下面是每个 POST 端点所需请求体的示例 JSON 数据。
请注意,对于 genetic_algorithmpso,API 目前使用固定的示例函数进行优化,请求体主要用于调整算法参数。
对于新增的模拟算法 (DES, ABM, SD),API 实现也是概念性的简化模拟。

  1. /algorithms/linear_regression
    {"x_train": [1, 2, 3, 4, 5],"y_train": [2, 4, 5, 4, 5],"x_predict": [6, 7, 8],"learning_rate": 0.01,"epochs": 1000
    }
    

在这里插入图片描述

  1. /algorithms/logistic_regression

    {"x_train": [1, 2, 3, 6, 7, 8],"y_train": [0, 0, 0, 1, 1, 1],"x_predict": [4, 5],"learning_rate": 0.05,"epochs": 2000,"threshold": 0.5
    }
    
  2. /algorithms/decision_tree

    {"X_train": [[1, 1],[1, 0],[0, 1],[0, 0],[1, 1],[0, 1]],"y_train": [1, 1, 0, 0, 1, 0],"X_predict": [[1, 0],[0, 0]],"max_depth": 2
    }
    
  3. /algorithms/svm

    {"X_train": [[1, 2], [2, 3], [3, 3],[6, 5], [7, 8], [8, 6]],"y_train": [1, 1, 1, -1, -1, -1],"X_predict": [[2, 2],[7, 7]],"learning_rate": 0.001,"epochs": 5000<
http://www.xdnf.cn/news/10944.html

相关文章:

  • 算法训练第七天
  • Web后端快速入门(Maven)
  • TDengine 的 AI 应用实战——运维异常检测
  • Ubuntu22.04安装MinkowskiEngine
  • 灵活运用 NextJS 服务端组件与客户端组件
  • vue-14(使用 ‘router.push‘ 和 ‘router.replace‘ 进行编程导航)
  • Walle-Web:打造轻量级高效的DevOps自动化部署平台
  • Vue混入
  • 种草平台:重新定义购物的乐趣革命
  • 北京大学肖臻老师《区块链技术与应用》公开课:07-BTC-挖矿难度
  • 基于LEAP模型在能源环境发展、碳排放建模预测及分析中实践应用
  • 论文分类打榜赛Baseline:ms-swift微调InternLM实践
  • 常用工具推荐---QQ截图功能、iLovePDF与Pandoc
  • 云服务器部署Gin+gorm 项目 demo
  • python调用硅基流动的视觉语言模型
  • 自然语言处理(NLP)的系统学习路径规划
  • HarmonyOS运动开发:精准估算室内运动的距离、速度与步幅
  • docker中组合这几个命令来排查 import 模块失败 的问题
  • 数字商城小程序源码,开启便捷电商新体验
  • 【论文笔记】High-Resolution Representations for Labeling Pixels and Regions
  • RAG入门 - Reader(2)
  • 定时器:中央对齐模式剖析
  • Neovim - 打造一款属于自己的编辑器(一)
  • 第二章支线六 ·CSS幻纹术:背景、遮罩与视觉层级
  • 实验设计与分析(第6版,Montgomery著,傅珏生译) 第10章拟合回归模型10.9节思考题10.12 R语言解题
  • 大模型分布式训练笔记(基于accelerate+deepspeed分布式训练解决方案)
  • 互联网大厂Java求职面试:AI大模型与云原生技术的深度融合
  • Java面试八股--06-Linux篇
  • Linux或者Windows下PHP版本查看方法总结
  • 【C++项目】负载均衡在线OJ系统-1