Spaces:
Runtime error
Runtime error
quyip
commited on
Commit
·
4adaa35
1
Parent(s):
b3b4ef3
fix
Browse files- main.py +10 -27
- requirements.txt +1 -0
- utils/cache_layer.py +30 -0
- utils/data_proto.py +20 -0
- summary.py → utils/summary_utils.py +0 -10
main.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
|
4 |
from fastapi import FastAPI
|
5 |
-
from pydantic import BaseModel
|
6 |
|
7 |
-
from
|
|
|
|
|
8 |
|
9 |
KEY = 'J9l#K4wP5h@2'
|
10 |
app = FastAPI()
|
|
|
11 |
|
12 |
|
13 |
@app.get("/")
|
@@ -15,12 +17,6 @@ async def root():
|
|
15 |
return {"message": "Hello World"}
|
16 |
|
17 |
|
18 |
-
class SummaryReq(BaseModel):
|
19 |
-
key: str
|
20 |
-
id: str
|
21 |
-
text: str
|
22 |
-
|
23 |
-
|
24 |
@app.post("/summary/")
|
25 |
async def summary(request: SummaryReq):
|
26 |
if request.key != KEY:
|
@@ -28,25 +24,12 @@ async def summary(request: SummaryReq):
|
|
28 |
return summarize(request.id, request.text)
|
29 |
|
30 |
|
31 |
-
class Page(TypedDict):
|
32 |
-
id: str
|
33 |
-
text: str
|
34 |
-
|
35 |
-
|
36 |
-
class SummariesReq(BaseModel):
|
37 |
-
key: str
|
38 |
-
pages: List[Page]
|
39 |
-
|
40 |
-
|
41 |
@app.post("/summaries/")
|
42 |
async def summaries(request: SummariesReq):
|
43 |
if request.key != KEY:
|
44 |
return 'Unauthorized'
|
45 |
-
result = []
|
46 |
print(f'process pages length: {len(request.pages)}')
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
print(e)
|
52 |
-
return result
|
|
|
1 |
+
import asyncio
|
2 |
+
from concurrent.futures import ThreadPoolExecutor
|
3 |
|
4 |
from fastapi import FastAPI
|
|
|
5 |
|
6 |
+
from utils.cache_layer import get_summarize_from_cache, summarize_un_cache_page
|
7 |
+
from utils.data_proto import SummaryReq, SummariesReq
|
8 |
+
from utils.summary_utils import summarize
|
9 |
|
10 |
KEY = 'J9l#K4wP5h@2'
|
11 |
app = FastAPI()
|
12 |
+
executor = ThreadPoolExecutor()
|
13 |
|
14 |
|
15 |
@app.get("/")
|
|
|
17 |
return {"message": "Hello World"}
|
18 |
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
@app.post("/summary/")
|
21 |
async def summary(request: SummaryReq):
|
22 |
if request.key != KEY:
|
|
|
24 |
return summarize(request.id, request.text)
|
25 |
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
@app.post("/summaries/")
|
28 |
async def summaries(request: SummariesReq):
|
29 |
if request.key != KEY:
|
30 |
return 'Unauthorized'
|
|
|
31 |
print(f'process pages length: {len(request.pages)}')
|
32 |
+
pages_summaries, uncached_pages = get_summarize_from_cache(request.pages)
|
33 |
+
loop = asyncio.get_event_loop()
|
34 |
+
await loop.run_in_executor(executor, summarize_un_cache_page, uncached_pages)
|
35 |
+
return pages_summaries
|
|
|
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
fastapi
|
2 |
uvicorn
|
|
|
3 |
diskcache
|
4 |
langdetect
|
5 |
protobuf
|
|
|
1 |
fastapi
|
2 |
uvicorn
|
3 |
+
asyncio
|
4 |
diskcache
|
5 |
langdetect
|
6 |
protobuf
|
utils/cache_layer.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
from diskcache import Cache
|
4 |
+
|
5 |
+
from utils.data_proto import Page
|
6 |
+
from utils.summary_utils import summarize
|
7 |
+
|
8 |
+
cache = Cache(directory='/.cache/tmp/summary', size_limit=int(1e9)) # 1GB
|
9 |
+
|
10 |
+
|
11 |
+
def get_summarize_from_cache(pages: List[Page]):
|
12 |
+
pages_summaries = []
|
13 |
+
uncached_pages = []
|
14 |
+
for page in pages:
|
15 |
+
id = page['id']
|
16 |
+
summary = cache.get(id)
|
17 |
+
if summary is not None:
|
18 |
+
pages_summaries.append(summary)
|
19 |
+
else:
|
20 |
+
uncached_pages.append(page)
|
21 |
+
return pages_summaries, uncached_pages
|
22 |
+
|
23 |
+
|
24 |
+
def summarize_un_cache_page(pages: List[Page]):
|
25 |
+
print(f'summarize pages length: {len(pages)}')
|
26 |
+
for page in pages:
|
27 |
+
id = page['id']
|
28 |
+
summary = summarize(id, page['text'])
|
29 |
+
cache.set(id, summary)
|
30 |
+
print(f'processed page: {id}')
|
utils/data_proto.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing_extensions import TypedDict
|
5 |
+
|
6 |
+
|
7 |
+
class SummaryReq(BaseModel):
|
8 |
+
key: str
|
9 |
+
id: str
|
10 |
+
text: str
|
11 |
+
|
12 |
+
|
13 |
+
class Page(TypedDict):
|
14 |
+
id: str
|
15 |
+
text: str
|
16 |
+
|
17 |
+
|
18 |
+
class SummariesReq(BaseModel):
|
19 |
+
key: str
|
20 |
+
pages: List[Page]
|
summary.py → utils/summary_utils.py
RENAMED
@@ -1,6 +1,5 @@
|
|
1 |
import re
|
2 |
|
3 |
-
from diskcache import Cache
|
4 |
from langdetect import detect
|
5 |
from transformers import pipeline
|
6 |
|
@@ -12,15 +11,8 @@ en_translation_pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en
|
|
12 |
classification_pipe = pipeline("text-classification", model="Yueh-Huan/news-category-classification-distilbert")
|
13 |
tag_gen_pipe = pipeline("text2text-generation", model="fabiochiu/t5-base-tag-generation")
|
14 |
|
15 |
-
cache = Cache(directory='/.cache/tmp/summary', size_limit=int(1e9)) # 1GB
|
16 |
-
|
17 |
|
18 |
def summarize(id: str, text: str):
|
19 |
-
value = cache.get(id)
|
20 |
-
if value is not None:
|
21 |
-
print(f'cached page: {id}')
|
22 |
-
return value
|
23 |
-
|
24 |
if text is None or len(text) < 10:
|
25 |
return {
|
26 |
"ver": AiSummaryVersion
|
@@ -38,8 +30,6 @@ def summarize(id: str, text: str):
|
|
38 |
"summary": summary,
|
39 |
"tags": tags,
|
40 |
}
|
41 |
-
cache.set(id, value)
|
42 |
-
print(f'processed page: {id}')
|
43 |
return value
|
44 |
|
45 |
|
|
|
1 |
import re
|
2 |
|
|
|
3 |
from langdetect import detect
|
4 |
from transformers import pipeline
|
5 |
|
|
|
11 |
classification_pipe = pipeline("text-classification", model="Yueh-Huan/news-category-classification-distilbert")
|
12 |
tag_gen_pipe = pipeline("text2text-generation", model="fabiochiu/t5-base-tag-generation")
|
13 |
|
|
|
|
|
14 |
|
15 |
def summarize(id: str, text: str):
|
|
|
|
|
|
|
|
|
|
|
16 |
if text is None or len(text) < 10:
|
17 |
return {
|
18 |
"ver": AiSummaryVersion
|
|
|
30 |
"summary": summary,
|
31 |
"tags": tags,
|
32 |
}
|
|
|
|
|
33 |
return value
|
34 |
|
35 |
|