mirror of https://github.com/THUDM/ChatGLM-6B
update api.py's function and fix bug
parent
697ee00263
commit
437693b8be
Binary file not shown.
Binary file not shown.
43
api.py
43
api.py
|
@ -1,11 +1,10 @@
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from transformers import AutoTokenizer, AutoModel
|
from transformers import AutoTokenizer, AutoModel
|
||||||
import uvicorn, json, datetime
|
import uvicorn, json, datetime, os
|
||||||
|
|
||||||
app = FastAPI()
|
api = FastAPI()
|
||||||
|
|
||||||
|
@api.post("/")
|
||||||
@app.post("/")
|
|
||||||
async def create_item(request: Request):
|
async def create_item(request: Request):
|
||||||
global model, tokenizer
|
global model, tokenizer
|
||||||
json_post_raw = await request.json()
|
json_post_raw = await request.json()
|
||||||
|
@ -26,10 +25,42 @@ async def create_item(request: Request):
|
||||||
print(log)
|
print(log)
|
||||||
return answer
|
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__':
|
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)
|
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 = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
|
||||||
model.eval()
|
model.eval()
|
Loading…
Reference in New Issue