update api.py's function and fix bug

pull/226/head
LemonQu-GIT 2023-03-24 22:52:41 +08:00
parent 697ee00263
commit 437693b8be
3 changed files with 37 additions and 6 deletions

Binary file not shown.

Binary file not shown.

41
api.py
View File

@ -1,11 +1,10 @@
from fastapi import FastAPI, Request
from transformers import AutoTokenizer, AutoModel
import uvicorn, json, datetime
import uvicorn, json, datetime, os
app = FastAPI()
api = FastAPI()
@app.post("/")
@api.post("/")
async def create_item(request: Request):
global model, tokenizer
json_post_raw = await request.json()
@ -26,10 +25,42 @@ async def create_item(request: Request):
print(log)
return answer
@api.post("/clear")
async def clear_history():
global history
history = []
now = datetime.datetime.now()
time = now.strftime("%Y-%m-%d %H:%M:%S")
answer = {
"response": "history cleared",
"status": 200,
"time": time
}
log = "[" + time + "] " + 'History Cleared'
print(log)
return answer
@api.post("/history")
async def get_history():
global history
now = datetime.datetime.now()
time = now.strftime("%Y-%m-%d %H:%M:%S")
answer = {
"response": history,
"status": 200,
"time": time
}
log = "[" + time + "] " + 'Get History'
print(log)
return answer
if __name__ == '__main__':
uvicorn.run('api:app', host='0.0.0.0', port=8000, workers=1)
try:
uvicorn.run('api:api', host='0.0.0.0', port=8000, workers=1)
except KeyboardInterrupt:
os._exit(0)
history = []
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
model.eval()