刘悦 commited on
Commit
a8b46cf
1 Parent(s): c2f9254

Update server.py

Browse files

接口服务允许跨域请求,接入其他服务,比如ollama或者其他大模型服务

Files changed (1) hide show
  1. runtime/python/fastapi/server.py +10 -0
runtime/python/fastapi/server.py CHANGED
@@ -10,6 +10,7 @@ import sys
10
  import io,time
11
  from fastapi import FastAPI, Response, File, UploadFile, Form
12
  from fastapi.responses import HTMLResponse
 
13
  from contextlib import asynccontextmanager
14
  ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
15
  sys.path.append('{}/../../..'.format(ROOT_DIR))
@@ -39,6 +40,15 @@ async def lifespan(app: FastAPI):
39
 
40
  app = FastAPI(lifespan=lifespan)
41
 
 
 
 
 
 
 
 
 
 
42
  def buildResponse(output):
43
  buffer = io.BytesIO()
44
  torchaudio.save(buffer, output, 22050, format="wav")
 
10
  import io,time
11
  from fastapi import FastAPI, Response, File, UploadFile, Form
12
  from fastapi.responses import HTMLResponse
13
+ from fastapi.middleware.cors import CORSMiddleware #引入 CORS中间件模块
14
  from contextlib import asynccontextmanager
15
  ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
16
  sys.path.append('{}/../../..'.format(ROOT_DIR))
 
40
 
41
  app = FastAPI(lifespan=lifespan)
42
 
43
+ #设置允许访问的域名
44
+ origins = ["*"] #"*",即为所有,也可以改为允许的特定ip。
45
+ app.add_middleware(
46
+ CORSMiddleware,
47
+ allow_origins=origins, #设置允许的origins来源
48
+ allow_credentials=True,
49
+ allow_methods=["*"], # 设置允许跨域的http方法,比如 get、post、put等。
50
+ allow_headers=["*"]) #允许跨域的headers,可以用来鉴别来源等作用。
51
+
52
  def buildResponse(output):
53
  buffer = io.BytesIO()
54
  torchaudio.save(buffer, output, 22050, format="wav")