Spaces:
Runtime error
Runtime error
quyip
commited on
Commit
·
cdc5783
1
Parent(s):
f23d40a
Add application file
Browse files- .gitignore +3 -0
- Dockerfile +14 -0
- main.py +16 -0
- requirement.txt +9 -0
- summary.py +85 -0
- utils/tag_utils.py +14 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__/
|
2 |
+
.venv
|
3 |
+
.idea
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
|
3 |
+
from summary import summarize
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
|
8 |
+
@app.get("/")
|
9 |
+
async def root():
|
10 |
+
return {"message": "Hello World"}
|
11 |
+
|
12 |
+
|
13 |
+
@app.post("/summary/")
|
14 |
+
async def summary(text_request):
|
15 |
+
text = text_request.text
|
16 |
+
return summarize(text)
|
requirement.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
langdetect
|
4 |
+
protobuf
|
5 |
+
sentencepiece
|
6 |
+
transformers
|
7 |
+
torch
|
8 |
+
tensorflow
|
9 |
+
tf-keras
|
summary.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
from langdetect import detect
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
from utils.tag_utils import filter_tags
|
7 |
+
|
8 |
+
AiSummaryVersion = 1
|
9 |
+
summarization_pipeline = pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum")
|
10 |
+
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
|
19 |
+
}
|
20 |
+
summary = get_summarization(text) if len(text) > 100 else text
|
21 |
+
translated = get_en_translation(summary)
|
22 |
+
tags1 = get_classification(translated)
|
23 |
+
tags2 = get_tags(translated)
|
24 |
+
tags = filter_tags(list(set(tags1 + tags2)))
|
25 |
+
|
26 |
+
return {
|
27 |
+
"ver": AiSummaryVersion,
|
28 |
+
"summary": summary,
|
29 |
+
"tags": tags,
|
30 |
+
}
|
31 |
+
|
32 |
+
|
33 |
+
def get_summarization(text: str):
|
34 |
+
try:
|
35 |
+
result = summarization_pipeline(text)
|
36 |
+
return result[0]['summary_text'] if isinstance(result, list) else result['summary_text']
|
37 |
+
except:
|
38 |
+
return None
|
39 |
+
|
40 |
+
|
41 |
+
def get_en_translation(text: str):
|
42 |
+
if text is None:
|
43 |
+
return None
|
44 |
+
try:
|
45 |
+
if is_english(text):
|
46 |
+
return text
|
47 |
+
result = en_translation_pipe(text)
|
48 |
+
return result[0]['translation_text'] if isinstance(result, list) else result['translation_text']
|
49 |
+
except:
|
50 |
+
return None
|
51 |
+
|
52 |
+
|
53 |
+
def is_english(text):
|
54 |
+
try:
|
55 |
+
lang = detect(text)
|
56 |
+
return lang == 'en'
|
57 |
+
except:
|
58 |
+
return False
|
59 |
+
|
60 |
+
|
61 |
+
def get_tags(text: str):
|
62 |
+
if text is None:
|
63 |
+
return []
|
64 |
+
try:
|
65 |
+
result = tag_gen_pipe(text)
|
66 |
+
tag_str = result[0]['generated_text'] if isinstance(result, list) else result['generated_text']
|
67 |
+
tags = re.split(r'[&,]', tag_str)
|
68 |
+
tags = [tag.strip() for tag in tags]
|
69 |
+
tags = [tag for tag in tags if len(tag) > 2 and len(tag.split(' ')) == 1]
|
70 |
+
return tags
|
71 |
+
except:
|
72 |
+
return []
|
73 |
+
|
74 |
+
|
75 |
+
def get_classification(text: str):
|
76 |
+
if text is None:
|
77 |
+
return []
|
78 |
+
try:
|
79 |
+
result = classification_pipe(text)
|
80 |
+
if isinstance(result, list):
|
81 |
+
return [tag['label'].strip() for tag in result if tag['score'] > 0.75]
|
82 |
+
else:
|
83 |
+
return [result['label'].strip()] if result['score'] > 0.75 else []
|
84 |
+
except:
|
85 |
+
return []
|
utils/tag_utils.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
BlockTagNames = {'fifty'}
|
4 |
+
BlockTagContents = ['voice']
|
5 |
+
|
6 |
+
|
7 |
+
def filter_tags(tags: List[str]) -> List[str]:
|
8 |
+
tags = [tag.strip().lower() for tag in tags]
|
9 |
+
tags = [tag for tag in tags
|
10 |
+
if len(tag) > 2
|
11 |
+
and tag not in BlockTagNames
|
12 |
+
and not any(content in tag for content in BlockTagContents)]
|
13 |
+
return sorted(tags)
|
14 |
+
|