From 6f9c0aa2281f87e003b08e6bcbb1b63376cf5d3b Mon Sep 17 00:00:00 2001 From: Lucien Date: Sun, 26 Mar 2023 00:42:28 +0800 Subject: [PATCH] add cors support, update .ignore, update README --- .gitignore | 2 ++ README.md | 2 +- README_en.md | 2 +- websocket_api.py | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 083d732..ee8c5db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ __pycache__ .idea +THUDM +*.tar.gz diff --git a/README.md b/README.md index 8471fd8..f97b3f3 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ curl -X POST "http://127.0.0.1:8000" \ 由于上述 API 不支持流式返回,故在 fastapi 的基础上增加了对 websocket 的支持。 -首先安装额外的依赖 `pip install 'fastapi~=0.95.0' 'websockets~=10.4'`,然后运行 [websocket_api.py](./websocket_api.py) 即可。 +首先安装额外的依赖 `pip install 'fastapi~=0.95.0' 'websockets~=10.4' 'uvicorn~=0.21.1'`,然后运行 [websocket_api.py](./websocket_api.py) 即可。 ```shell python websocket_api.py diff --git a/README_en.md b/README_en.md index 4c43de5..3fc3ede 100644 --- a/README_en.md +++ b/README_en.md @@ -113,7 +113,7 @@ The returned value is This DEMO showed that you can embed ChatGLM-6B to your own website through websocket. HTML file: [websocket_demo.html](./websocket_demo.html). -First install the additional dependency `pip install 'fastapi~=0.95.0' 'websockets~=10.4'`. Then run [websocket_api.py](./websocket_api.py) in the repo. +First install the additional dependency `pip install 'fastapi~=0.95.0' 'websockets~=10.4' 'uvicorn~=0.21.1'`. Then run [websocket_api.py](./websocket_api.py) in the repo. ```shell python websocket_api.py diff --git a/websocket_api.py b/websocket_api.py index 8a33fcd..06aefbe 100644 --- a/websocket_api.py +++ b/websocket_api.py @@ -1,5 +1,6 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect from fastapi.responses import HTMLResponse +from fastapi.middleware.cors import CORSMiddleware from transformers import AutoTokenizer, AutoModel import uvicorn @@ -10,6 +11,10 @@ model = AutoModel.from_pretrained(pretrained, trust_remote_code=True).half().cud model = model.eval() app = FastAPI() +app.add_middleware( + CORSMiddleware +) + with open('websocket_demo.html') as f: html = f.read()