-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (40 loc) · 1.45 KB
/
main.py
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
from fastapi import FastAPI,Request
import shutil
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import io
import spacy
from summerizer import calculate_sentences_score,calculate_word_frequency,summerize,normalize,readingTime,reduced_by
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
class Message(BaseModel):
text: str
app = FastAPI()
nlp = spacy.load("en_core_web_sm")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
templates=Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def read_root(request:Request):
return templates.TemplateResponse("home.html",{"request":request})
@app.post("/")
async def root(message:Message):
docx=nlp(message.text)
words_freaquency=calculate_word_frequency(docx)
normalize(words_freaquency)
sentences_score=calculate_sentences_score(docx,words_freaquency)
#print("reading time befor sum",readingTime(docx))
summary=summerize(sentences_score)
docy=nlp(summary)
#print("reading time befor sum",readingTime(docy))
reduce=reduced_by(message.text,summary)
return({"result":summary,"before":readingTime(docx),"after":readingTime(docy),"reduced":reduce})