Skip to content

部署上线

概述

将财报分析 Agent 部署到生产环境,让用户可以通过 Web 或 API 访问。

部署方案

方案优点缺点适用场景
Streamlit快速、简单性能一般原型演示
FastAPI高性能、标准需前端API 服务
Gradio易用、美观定制性差AI 应用

Streamlit 部署

基础应用

python
import streamlit as st
import yfinance as yf

st.title("财报分析 Agent")

# 输入
symbol = st.text_input("输入股票代码", "AAPL")

if st.button("开始分析"):
    with st.spinner("正在分析..."):
        ticker = yf.Ticker(symbol)
        info = ticker.info

        # 显示基本信息
        st.subheader(f"{info.get('longName')}")

        col1, col2, col3 = st.columns(3)
        col1.metric("市盈率", f"{info.get('trailingPE', 'N/A'):.2f}")
        col2.metric("市净率", f"{info.get('priceToBook', 'N/A'):.2f}")
        col3.metric("ROE", f"{info.get('returnOnEquity', 0)*100:.1f}%")

运行命令

bash
streamlit run app.py --server.port 8501

FastAPI 部署

API 服务

python
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="财报分析 API")

class AnalysisRequest(BaseModel):
    symbol: str
    analysis_type: str = "full"

@app.post("/analyze")
async def analyze(request: AnalysisRequest):
    # 执行分析
    result = run_analysis(request.symbol)
    return {"status": "success", "data": result}

@app.get("/health")
async def health():
    return {"status": "healthy"}

启动服务

bash
uvicorn main:app --host 0.0.0.0 --port 8000

Docker 部署

Dockerfile

dockerfile
FROM python:3.10-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

构建运行

bash
docker build -t financial-agent .
docker run -p 8000:8000 financial-agent

云平台部署

Vercel (Streamlit)

bash
# 安装 Vercel CLI
npm i -g vercel

# 部署
vercel --prod

Railway

bash
# 连接 GitHub 仓库后自动部署
# 设置环境变量
OPENAI_API_KEY=your-key

监控运维

日志配置

python
import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler("app.log"),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger(__name__)

健康检查

python
@app.get("/health")
async def health_check():
    return {
        "status": "healthy",
        "version": "1.0.0",
        "timestamp": datetime.now().isoformat()
    }

安全配置

python
# API 密钥验证
from fastapi import Header, HTTPException

async def verify_api_key(x_api_key: str = Header(...)):
    if x_api_key != os.getenv("API_KEY"):
        raise HTTPException(status_code=401, detail="Invalid API key")
    return x_api_key

项目完成

恭喜!你已完成财报分析 Agent 的全部开发。

回顾:

  1. 需求分析 - 明确功能和技术选型
  2. 数据获取 - 接入财报 API
  3. 数据分析 - 计算财务指标
  4. AI 分析 - LLM 生成洞察
  5. 可视化 - 图表和报告
  6. 部署上线 - 生产环境部署

← 返回可视化 | 返回项目三

最近更新

基于 Apache 2.0 许可发布