quyip commited on
Commit
a680719
·
1 Parent(s): 2a78aa3
Files changed (3) hide show
  1. main.py +3 -2
  2. requirements.txt +1 -0
  3. summary.py +12 -2
main.py CHANGED
@@ -12,12 +12,13 @@ async def root():
12
 
13
 
14
  class SummaryReq(BaseModel):
15
- text: str
16
  key: str
 
 
17
 
18
 
19
  @app.post("/summary/")
20
  async def summary(request: SummaryReq):
21
  if request.key != KEY:
22
  return 'Unauthorized'
23
- return summarize(request.text)
 
12
 
13
 
14
  class SummaryReq(BaseModel):
 
15
  key: str
16
+ id: str
17
+ text: str
18
 
19
 
20
  @app.post("/summary/")
21
  async def summary(request: SummaryReq):
22
  if request.key != KEY:
23
  return 'Unauthorized'
24
+ return summarize(request.id, request.text)
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  fastapi
2
  uvicorn
 
3
  langdetect
4
  protobuf
5
  sentencepiece
 
1
  fastapi
2
  uvicorn
3
+ diskcache
4
  langdetect
5
  protobuf
6
  sentencepiece
summary.py CHANGED
@@ -1,5 +1,6 @@
1
  import re
2
 
 
3
  from langdetect import detect
4
  from transformers import pipeline
5
 
@@ -11,8 +12,14 @@ en_translation_pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en
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(text: str):
16
  if text is None or len(text) < 10:
17
  return {
18
  "ver": AiSummaryVersion
@@ -24,11 +31,14 @@ def summarize(text: str):
24
  tags = filter_tags(tags1 + tags2)
25
  tags = sorted(list(set(tags)))
26
 
27
- return {
 
28
  "ver": AiSummaryVersion,
29
  "summary": summary,
30
  "tags": tags,
31
  }
 
 
32
 
33
 
34
  def get_summarization(text: str):
 
1
  import re
2
 
3
+ from diskcache import Cache
4
  from langdetect import detect
5
  from transformers import pipeline
6
 
 
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
+ return value
22
 
 
23
  if text is None or len(text) < 10:
24
  return {
25
  "ver": AiSummaryVersion
 
31
  tags = filter_tags(tags1 + tags2)
32
  tags = sorted(list(set(tags)))
33
 
34
+ value = {
35
+ "id": id,
36
  "ver": AiSummaryVersion,
37
  "summary": summary,
38
  "tags": tags,
39
  }
40
+ cache.set(id, value)
41
+ return value
42
 
43
 
44
  def get_summarization(text: str):