-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (83 loc) · 3.13 KB
/
app.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
'''
######################################
Issue #1000- Auto Tagging of files
######################################
'''
import RAKE
from rake_nltk import Rake, Metric
from tika import parser
import requests
from requests import Session
from urllib.parse import urlparse
from os.path import splitext
from flask import Flask, request, jsonify, render_template,session,jsonify
app = Flask(__name__)
@app.route('/auto-tagger' ,methods=['POST','GET'])
def auto():
# email = request.args['email']
# password = request.args['password']
# payload = {'email':email,'password':password}
token = request.args['token']
url = request.args['url']
# appid = request.args['appid']
with Session():
session = requests.Session()
# response = session.post('https://gateway.eela.tech/api/login',data=payload)
# x = response.text
# y = x.strip('{}')
# z = y.strip('"success":{"token":')
# token = z.strip('","app":"1234560')
token_final = '"'+"Bearer "+token+'"'
headers = {"Authorization": token_final}
path = urlparse(url).path
ext = splitext(path)[1]
r = requests.get(url, headers=headers,verify=False,stream=True)
r.raw.decode_content = True
#print("--------test1--------")
if ext == ".pdf":
with open("/tmp/1.pdf","wb") as pdf:
for chunk in r.iter_content(chunk_size=1024):
# writing one chunk at a time to pdf file
if chunk:
pdf.write(chunk)
raw = parser.from_file('/tmp/1.pdf')
r = Rake(ranking_metric=Metric.WORD_FREQUENCY, max_length=1)
text = raw['content']
#print("--------test2---------")
r.extract_keywords_from_text(text)
key = r.get_ranked_phrases()
tags = []
length = len(key)
if length<5:
for i in range(length):
print("Keywords:", key[i])
tags.append(key[i])
elif length>=5:
for i in range(5):
print("Keywords:", key[i])
tags.append(key[i])
return jsonify(tags)
if ext == ".docx":
with open("/tmp/1.docx","wb") as doc:
for chunk in r.iter_content(chunk_size=1024):
# writing one chunk at a time to doc file
if chunk:
doc.write(chunk)
raw = parser.from_file('/tmp/1.docx')
r = Rake(ranking_metric=Metric.WORD_FREQUENCY, max_length=1)
text = raw['content']
r.extract_keywords_from_text(text)
key = r.get_ranked_phrases()
tags = []
length = len(key)
if length<5:
for i in range(length):
print("Keywords:", key[i])
tags.append(key[i])
elif length>=5:
for i in range(8):
print("Keywords:", key[i])
tags.append(key[i])
return jsonify(tags)
if __name__ == "__main__":
app.run(debug=True)