Spaces:
Sleeping
Sleeping
Create api.py
Browse files
api.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Union
|
2 |
+
|
3 |
+
from fastapi import FastAPI
|
4 |
+
from epub2txt import epub2txt
|
5 |
+
import config
|
6 |
+
|
7 |
+
model = llm.model(config.CKP_PATH)
|
8 |
+
prompt = os.getenv("prompt")
|
9 |
+
|
10 |
+
from fastllm_pytools import llm
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
|
15 |
+
@app.websockets("/{f_name}/ws")
|
16 |
+
async def read_root(*,
|
17 |
+
websocket: WebSocket,
|
18 |
+
f_name: str):
|
19 |
+
await websocket.accept()
|
20 |
+
ch_list = epub2txt(f_name, outputlist=True)
|
21 |
+
chapter_titles = epub2txt.content_titles
|
22 |
+
title = epub2txt.title
|
23 |
+
|
24 |
+
idx = 0
|
25 |
+
sm_list = []
|
26 |
+
for text in ch_list[2:]:
|
27 |
+
idx += 1
|
28 |
+
docs = []
|
29 |
+
for i in range(0, len(text)//2000+1, 2000):
|
30 |
+
t = text[i:i+2048]
|
31 |
+
if len(t) > 0:
|
32 |
+
docs.append(model.response(prompt+t))
|
33 |
+
await websocket.send_text(f"chsum: {docs[-1]}")
|
34 |
+
hist = docs[0]
|
35 |
+
for doc in docs[1:]:
|
36 |
+
hist = model.response(prompt+"\n"+hist+"\n"+doc)
|
37 |
+
await websocket.send_text(f"draft_sum: {hist}")
|
38 |
+
sm_list.append(hist)
|
39 |
+
mdobj_str = f"# {title}\n\n"
|
40 |
+
for ct, sm in zip(chapter_titles[2:], sm_list):
|
41 |
+
mdobj_str += f"## {ct}\n\n{sm}\n\n\n"
|
42 |
+
await websocket.send_text(f"output: {mdobj_str}")
|
43 |
+
|
44 |
+
# uvicorn api:app --reload
|