-
Notifications
You must be signed in to change notification settings - Fork 3
/
teem_tag.py
executable file
·145 lines (105 loc) · 4.5 KB
/
teem_tag.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
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from core import tagger
from textteaser import TextTeaser
from pymongo import *
from flask import Flask, request
from urlparse import urljoin
import os, sys, pickle, pymongo, json, requests, logging
from flask.views import View
from flask import Flask
from flask import render_template, redirect
from werkzeug import secure_filename
from subprocess import Popen,PIPE
# Initialising Flask. Webserver to handle POST from SwellRT.
app = Flask(__name__)
# SwellRT IP. Works only with docker-compose setup.
swellrt_host = os.environ.get('SWELLRT_HOST')
if swellrt_host:
swellrt = 'http://' + swellrt_host +':9898/swell/'
else:
swellrt = 'http://swellrt:9898/swell/'
session = False
# For authentication. Defaults to: username = [email protected], password = teemtag
tag_user = os.environ.get('TEEMTAG_USERNAME')
tag_pwd = os.environ.get('TEEMTAG_PASSWORD')
@app.route("/", methods=['GET', 'POST'])
def tags():
if request.method == 'POST':
global session
if not session:
session = authfromSwellRT()
data = request.get_json()
app.logger.info(data)
#Initialisation for context
wave_id = data['waveid']
description = data['data']['text']
name = data['data']['name']
#Generating tags
tags = json.dumps(mytagger(data['data']['text'],10), default=lambda x: str(x).strip('"\''))
#Generating summary of 4 lines
tt = TextTeaser()
sentences = tt.summarize(name, description)
summary = json.dumps(sentences[:4])
#For logs
app.logger.info(tags)
app.logger.info(summary);
post2swellRT(session,wave_id,tags,summary)
return json.dumps(True)
else:
tags = json.dumps("Hello from Teem Tag",10, default=lambda x: str(x).strip('"\''))
return tags
@app.route("/image_classify", methods=['GET', 'POST'])
def classify_image():
return render_template('image.html')
@app.route('/imageupload/', methods=['POST'])
def imageupload():
image=request.form['path']
#image = "/home/fenil/Pictures/img1.jpg"
sys.path.append("/usr/local/lib/python2.7/site-packages/tensorflow/models/image/imagenet")
import classify_image
proc = Popen(['python','/usr/local/lib/python2.7/site-packages/tensorflow/models/image/imagenet/classify_image.py','--image_file',image],stdout=PIPE, stderr=PIPE)
image_classification, err = proc.communicate()
#app.logger.info(image_classification)
return render_template('image.html', image_classification=image_classification,image=image)
def authfromSwellRT():
session = requests.session()
swellrt_auth_link = urljoin(swellrt,'auth')
try:
if tag_user and tag_pwd:
session.post(swellrt_auth_link, json={"id": tag_user,"password": tag_pwd})
else:
session.post(swellrt_auth_link, json={"id":"[email protected]","password":"teemtag"})
except requests.exceptions.RequestException as e:
app.logger.error('Authentication failed from SwellRT')
try:
auth_test = session.get(swellrt_auth_link)
except requests.exceptions.RequestException as e:
app.logger.error('Authentication checking failed')
if auth_test.status_code == 200:
app.logger.info('Succesful Authentication')
return session
else:
app.logger.error('Cannot authenticate from SwellRT. Exiting!')
def post2swellRT(session,wave_id,tags,summary):
#Making the Update Link for tags
update_link = swellrt + 'object/' + wave_id + '/tags'
try:
update = session.post(update_link, json=tags)
except requests.exceptions.RequestException as e:
app.logger.info('Updating tags to SwellRT failed')
#Making the Update Link for summary
summary_update_link = swellrt + 'object/' + wave_id + '/summary'
try:
update = session.post(summary_update_link, json=summary)
except requests.exceptions.RequestException as e:
app.logger.info('Updating summary to SwellRT failed')
if __name__ == "__main__":
#Building dictionary from NLTK Corpus
if not(os.path.isfile('data/nltkdict.pkl')):
build_dict_from_nltk('data/nltkdict.pkl')
weights = pickle.load(open('data/nltkdict.pkl'))
#Initialising mytagger object which tags the string
mytagger = tagger.Tagger(tagger.Reader(), tagger.Stemmer(), tagger.Rater(weights))
#Webserver running in debug mode to show logs
app.run(debug=True, host='0.0.0.0')