Spaces:
Sleeping
Sleeping
添加 CORS 和信任主机中间件,优化预测路由的异步处理,调整 Gunicorn 配置以提高性能
Browse files- app.py +26 -6
- blkeras.py +3 -0
- gunicorn.conf.py +18 -11
app.py
CHANGED
|
@@ -2,7 +2,8 @@ import os
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
from transformers import pipeline
|
| 8 |
|
|
@@ -11,6 +12,21 @@ from us_stock import fetch_symbols
|
|
| 11 |
|
| 12 |
app = FastAPI() # 创建 FastAPI 应用
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# 定义请求模型
|
| 15 |
class TextRequest(BaseModel):
|
| 16 |
text: str
|
|
@@ -45,16 +61,20 @@ async def initialize_symbols():
|
|
| 45 |
# 在 FastAPI 启动时初始化变量
|
| 46 |
await fetch_symbols()
|
| 47 |
|
|
|
|
| 48 |
@app.post("/api/predict")
|
| 49 |
async def predict(request: PredictRequest):
|
| 50 |
from blkeras import predict
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return {"error": str(e)}
|
| 60 |
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
| 7 |
|
| 8 |
from transformers import pipeline
|
| 9 |
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI() # 创建 FastAPI 应用
|
| 14 |
|
| 15 |
+
# 添加 CORS 中间件和限流配置
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"],
|
| 19 |
+
allow_credentials=True,
|
| 20 |
+
allow_methods=["*"],
|
| 21 |
+
allow_headers=["*"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# 添加信任主机中间件
|
| 25 |
+
app.add_middleware(
|
| 26 |
+
TrustedHostMiddleware,
|
| 27 |
+
allowed_hosts=["*"]
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
# 定义请求模型
|
| 31 |
class TextRequest(BaseModel):
|
| 32 |
text: str
|
|
|
|
| 61 |
# 在 FastAPI 启动时初始化变量
|
| 62 |
await fetch_symbols()
|
| 63 |
|
| 64 |
+
# 优化预测路由
|
| 65 |
@app.post("/api/predict")
|
| 66 |
async def predict(request: PredictRequest):
|
| 67 |
from blkeras import predict
|
| 68 |
|
| 69 |
try:
|
| 70 |
+
# 使用 asyncio.to_thread 将同步操作转换为异步
|
| 71 |
+
import asyncio
|
| 72 |
+
result = await asyncio.to_thread(
|
| 73 |
+
predict,
|
| 74 |
+
request.text,
|
| 75 |
+
request.stock_codes
|
| 76 |
+
)
|
| 77 |
+
return result
|
| 78 |
except Exception as e:
|
| 79 |
return {"error": str(e)}
|
| 80 |
|
blkeras.py
CHANGED
|
@@ -100,6 +100,9 @@ def predict(text: str, stock_codes: list):
|
|
| 100 |
|
| 101 |
try:
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
start_time = datetime.now()
|
| 104 |
input_text = text
|
| 105 |
affected_stock_codes = stock_codes
|
|
|
|
| 100 |
|
| 101 |
try:
|
| 102 |
|
| 103 |
+
print(f"Input Text Length: {len(text)}, Start with: {text[:200] if len(text) > 200 else text}")
|
| 104 |
+
print("Input stock codes:", stock_codes)
|
| 105 |
+
|
| 106 |
start_time = datetime.now()
|
| 107 |
input_text = text
|
| 108 |
affected_stock_codes = stock_codes
|
gunicorn.conf.py
CHANGED
|
@@ -3,24 +3,31 @@ import multiprocessing
|
|
| 3 |
# 监听地址和端口
|
| 4 |
bind = "0.0.0.0:7860"
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# 工作方式
|
| 10 |
-
# 方案1: 使用 Uvicorn 的 worker (推荐)
|
| 11 |
worker_class = "uvicorn.workers.UvicornWorker"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
#
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
# 进程名称前缀
|
| 26 |
proc_name = 'gunicorn_fastapi'
|
|
|
|
| 3 |
# 监听地址和端口
|
| 4 |
bind = "0.0.0.0:7860"
|
| 5 |
|
| 6 |
+
# 修改工作进程数配置策略
|
| 7 |
+
# 对于CPU密集型应用,建议设置为 CPU核心数 + 1
|
| 8 |
+
workers = multiprocessing.cpu_count() + 1
|
| 9 |
+
|
| 10 |
+
# 每个工作进程的线程数
|
| 11 |
+
# 设置为2,增加并发处理能力
|
| 12 |
+
threads = 2
|
| 13 |
|
| 14 |
# 工作方式
|
|
|
|
| 15 |
worker_class = "uvicorn.workers.UvicornWorker"
|
| 16 |
|
| 17 |
+
# 提高每个工作进程的并发连接数
|
| 18 |
+
worker_connections = 2000
|
| 19 |
|
| 20 |
+
# 优化工作进程配置
|
| 21 |
+
#max_requests = 1000 # 工作进程处理多少个请求后自动重启
|
| 22 |
+
#max_requests_jitter = 50 # 添加随机重启偏差,避免同时重启
|
| 23 |
+
#graceful_timeout = 120 # 优雅重启超时时间
|
| 24 |
|
| 25 |
+
# keepalive超时设置
|
| 26 |
+
#keepalive = 5 # 保持连接超时时间
|
| 27 |
|
| 28 |
+
# 工作模式
|
| 29 |
+
worker_tmp_dir = "/dev/shm" # 使用内存文件系统提高性能
|
| 30 |
+
preload_app = True # 预加载应用,减少启动时间
|
| 31 |
|
| 32 |
# 进程名称前缀
|
| 33 |
proc_name = 'gunicorn_fastapi'
|