-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainapi.py
105 lines (85 loc) · 2.52 KB
/
mainapi.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#Imports
from fastapi.responses import RedirectResponse
from ML.wrapper import customwrapper
from fastapi import FastAPI
from gtts import gTTS
import uvicorn
import time
import os
import re
from models.savedtext import (
get_text
)
from models.summ import (
insert,
getq
)
# from sendemail import (
# send_mail_summ,
# send_mail_ques
# )
transformer = customwrapper()
app = FastAPI()
#SUMMARY
@app.get('/summ')
async def S(user : str):
text = get_text(user, "app.db")[-1][0]
max_len = int(len(text.split(" "))*0.8)
min_len = int(len(text.split(" "))*0.2)
summary = transformer.summarize(text, min_length=min_len, max_lenght=max_len)
#Sends email with summary
#send_mail_summ(user,summary[0]["summary_text"])
insert(user, summary[0]["summary_text"])
response = RedirectResponse(url='http://localhost:5000/display')
return response
#Questions
@app.get('/ques')
async def Q(user : str):
quest = get_text(user, "app.db")[-1][0]
templist = []
quest = quest.split(" ")
qlist = []
fqlist = []
li = []
q = None
for i in range(len(quest)):
templist.append(quest[i])
if i % 20 == 0 and i != 0:
try:
print(' '.join(templist))
q = transformer.question(' '.join(templist))
qlist.append(q)
except Exception as e:
fqlist=qlist.pop()
for di in fqlist:
li.append(di["question"])
insert(user, "\n".join(li))
templist = []
qlist = []
break
qu = getq(user)
#send_mail_ques(user, qu[0])
response = RedirectResponse(url='http://localhost:5000/display')
return response
#Converts para to speech
def convertpts(para : list):
for p in para:
language = 'en'
myobj = gTTS(text=p, lang=language, slow=False)
myobj.save("testaudio.mp3")
os.system("mplayer testaudio.mp3")
np = "Next Paragraph... Should I continue?"
myobj1 = gTTS(text=np, lang=language, slow=False)
myobj1.save("next.mp3")
os.system("mplayer next.mp3")
time.sleep(2.5)
#Voice
@app.get('/voic')
async def V(user : str):
text = str(get_text(user, "app.db")[-1][0])
para = list(filter(lambda x: x != "" and len(re.sub(r" ", "", x)) != 0, text.split('\n')))
convertpts(para)
response = RedirectResponse(url='http://localhost:5000/dashboard')
return response
if __name__ == '__main__':
uvicorn.run(app, port=2222)