File size: 1,232 Bytes
4f75c30
d433389
308c6df
d433389
 
e4c416d
 
d26d155
d433389
 
 
 
 
b643921
 
d433389
b643921
d433389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5ea3e0
d433389
 
a11ecec
d433389
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os

from fastapi import FastAPI, WebSocket
from epub2txt import epub2txt

from fastllm_pytools import llm

model = llm.model(os.getenv("checkpoint_path"))
prompt = os.getenv("prompt")

app = FastAPI()


@app.websocket("/ws")
async def read_root(websocket: WebSocket):
    await websocket.accept()
    f_name = await websocket.receive_text()
    ch_list = epub2txt(f_name, outputlist=True)
    chapter_titles = epub2txt.content_titles
    title = epub2txt.title

    idx = 0
    sm_list = []
    for text in ch_list[2:]:
        idx += 1
        docs = []
        for i in range(0, len(text)//2000+1, 2000):
            t = text[i:i+2048]
            if len(t) > 0:
                docs.append(model.response(prompt+t))
                await websocket.send_text(f"chsum: {docs[-1]}")
        hist = docs[0]
        for doc in docs[1:]:
            hist = model.response(prompt+"\n"+hist+"\n"+doc)
            await websocket.send_text(f"draft_sum: {hist}")
        sm_list.append(hist)
        mdobj_str = f"# {title}\n\n{hist}\n\n\n"
        for ct, sm in zip(chapter_titles[2:], sm_list):
            mdobj_str += f"## {ct}\n\n{sm}\n\n\n"
        await websocket.send_text(f"output: {mdobj_str}")

# uvicorn api:app --reload