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

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

相关文章:

  • 帝可得 - 策略管理
  • uniapp开发使用vue3组合式api,实现从vue模块中自动导入
  • 简单transformer运用
  • Delphi 设置文件 永久只读
  • 数据安全中心是什么?如何做好数据安全管理?
  • Foundation Models for Generalist Geospatial Artificial Intelligence论文阅读
  • windows修改跃点数调整网络优先级
  • DiskGenius专业版v6.0.1.1645:分区管理、数据恢复、备份还原,一应俱全!
  • Win11系统不推送24H2/西数SSD无法安装24H2 - 解决方案
  • MyBatis 执行 SQL 报错:String 无法转换为 Long 的排查与解决实录
  • Vue3中Axios的使用-附完整代码
  • 从测试角度看待CI/CD,敏捷开发
  • vue入门环境搭建及demo运行
  • cursor对话
  • Web前端为什么要打包?Webpack 和 Vite 如何助力现代开发?
  • 大数据 ETL 工具 Sqoop 深度解析与实战指南
  • 工厂模式与多态结合
  • enum的用法
  • 【JavaWeb】SpringBoot原理
  • RAG入门 - Reader(2)
  • 网站的logo在百度怎么显示不出来/网站优化排名金苹果下拉
  • 企业网站开发要多少钱/seo最新技巧
  • 好男人的最好的影院/seo排名推广工具
  • 一台ip做两个网站/线上营销推广的公司
  • PHP开源网站开发系统/百度拍照搜索
  • 赣州培训学做网站/知名品牌营销案例100例