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

算法/机理模型演示平台搭建(二)——算法接口部署(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.lryc.cn/news/2398154.html

相关文章:

  • 动态规划-647.回文子串-力扣(LeetCode)
  • es 的字段类型(text和keyword)
  • Kotlin 中companion object {} 什么时候触发
  • 仿真每日一练 | Workbench中接触种类及选择方法简介
  • Go语言中的rune和byte类型详解
  • superior哥AI系列第6期:Transformer注意力机制:AI界的“注意力革命“
  • 【java面试】redis篇
  • 高效易用的 MAC 版 SVN 客户端:macSvn 使用体验
  • 【搭建 Transformer】
  • 自然图像数据集
  • Linux下使用nmcli连接网络
  • HCIP(BGP综合实验)
  • Attention Is All You Need (Transformer) 以及Transformer pytorch实现
  • uniapp+vue2+uView项目学习知识点记录
  • 精美的软件下载页面HTML源码:现代UI与动画效果的完美结合
  • 车载诊断架构 --- DTC消抖参数(Trip Counter DTCConfirmLimit )
  • javaEE->IO:
  • Oracle 用户/权限/角色管理
  • 使用免费wordpress成品网站模板需要注意点什么
  • 深入理解 JSX:React 的核心语法
  • 工厂方法模式深度解析:从原理到应用实战
  • TS 星际通信指南:从 TCP 到 UDP 的宇宙漫游
  • python可视化:端午假期旅游火爆原因分析
  • Missashe考研日记—Day51-Day57
  • electron-vite_18桌面共享
  • SOC-ESP32S3部分:28-BLE低功耗蓝牙
  • Git-flow流
  • VirtualBox给Rock Linux9.x配置网络
  • 知识图谱增强的大型语言模型编辑
  • .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想