-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (46 loc) · 1.48 KB
/
main.py
File metadata and controls
58 lines (46 loc) · 1.48 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fastapi import FastAPI, HTTPException
from kiwipiepy import Kiwi
from krwordrank.sentence import summarize_with_sentences
from pydantic import BaseModel
import body_request
import markdown_to_text
class Item(BaseModel):
body_string: str
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/keyword")
async def keyword(bodyRequest: body_request.BodyRequest):
result = markdown_to_text.markdown_to_text(bodyRequest)
lines = result.strip().split('\n')
kiwi = Kiwi()
preprocessingResult = []
for line in lines:
newString = ""
result = kiwi.analyze(line)
for token, pos, _, _ in result[0][0]:
if len(token) != 1 and pos.startswith('N') or pos.startswith('SL'):
token += " "
newString += token
print(newString)
preprocessingResult.append(newString)
try:
keywords, _ = summarize_with_sentences(
preprocessingResult,
penalty=None,
stopwords=None,
diversity=0.5,
num_keywords=100,
num_keysents=10,
verbose=False
)
except ValueError as e:
# 분석을 할 수 없는 경우 value error 전송
print("An error occurred:", e)
return 'value error'
sorted_keywords = [
{"keyword": word, "score": r}
for word, r in sorted(keywords.items(), key=lambda x: x[1], reverse=True)[:10]
]
return sorted_keywords