-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
executable file
·135 lines (117 loc) · 4.3 KB
/
common.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import Flask, url_for, redirect, render_template, request, Blueprint
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from get_audio_transcript import get_audio_transcript
from get_summary import get_summary
from get_img_text import get_img_text
from werkzeug import secure_filename
from twilio.rest import Client
from send_message import send_message
import time
import json
import requests
#import db
import sys
import db
import nltk
import os
import re
import io
import importlib
importlib.reload(sys)
from rake_nltk import Rake
totalSize = 0
r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.
# sys.setdefaultencoding('utf-8')
# model = gensim.models.KeyedVectors.load_word2vec_format('./GoogleNews-vectors-negative300.bin', binary=True)
commonPages = Blueprint('commonPages', __name__)
#-----------------Routing--------------------------------
@commonPages.route("/", methods = ['POST', 'GET'])
def index():
return redirect(url_for('commonPages.main'))
@commonPages.route("/main", methods = ['POST', 'GET'])
def main():
if request.method == 'GET':
return render_template("displayresults.html")
if request.method == 'POST':
searchcontent = request.form["search"]
return redirect(url_for('commonPages.displayresults', question = searchcontent))
@commonPages.route('/upload')
def upload_file():
return render_template('my-form.html')
@commonPages.route('/uploader', methods = ['GET', 'POST'])
def upload_file_post():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
ext = getExtension(f.filename)
text = ""
if ext == '.mp4' or ext == '.wav':
text = get_audio_transcript(secure_filename(f.filename))
else:
text = get_img_text(secure_filename(f.filename))
print(text)
send_message(f.filename, file_size(secure_filename(f.filename)), convert_bytes(totalSize))
return render_template("displayresults.html", summary_sentences = get_summary(text), source = text)
@commonPages.route("/displayresults", methods = ['POST', 'GET'])
def displayresults():
question = request.args.get('question')
if request.method == 'GET':
if valid_or_not(question):
r.extract_keywords_from_text(question)
temp, mainwords, judgement = ["abc", ["sadasd","asdas"],["adsa"]], "csa", "adsa"
# db.writequestion(question, mainwords)
return render_template("displayresults.html", keynote = r.get_ranked_phrases()[0:6])
else:
return redirect(url_for('commonPages.notfound'))
if request.method == 'POST':
searchcontent = request.form["search"]
return redirect(url_for('commonPages.displayresults', question = searchcontent))
@commonPages.route("/notfound", methods = ['POST', 'GET'])
def notfound():
if request.method == 'GET':
return render_template("notfound.html")
if request.method == 'POST':
searchcontent = request.form["search"]
return redirect(url_for('commonPages.displayresults', question = searchcontent))
def search_for_result(sentence):
return ["abc","c"], "bcs", "adsada"
def valid_or_not(searchcontent):
return True
def process_content():
try:
result = []
for i in tokenized[:5]:
words = nltk.word_tokenize(i)
return nltk.pos_tag(words)
# print(tagged)
return result
except Exception as e:
print("error: "+str(e))
def getExtension(path):
"""
Gets the file extension from path
:param str path: Path of the file
:returns: File extension
:rtype: str
"""
filename, file_extension = os.path.splitext(path)
return file_extension
def convert_bytes(num):
"""
this function will convert bytes to MB.... GB... etc
"""
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
def file_size(file_path):
"""
this function will return the file size
"""
if os.path.isfile(file_path):
file_info = os.stat(file_path)
global totalSize
totalSize = totalSize + file_info.st_size
return convert_bytes(file_info.st_size)