This repository was archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi.py
More file actions
142 lines (116 loc) · 4.11 KB
/
api.py
File metadata and controls
142 lines (116 loc) · 4.11 KB
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
'''
This is an API to call the logic of segmenting an input text.
Author: Binod Gyawali
Date: November, 2019
'''
import os
import tempfile
from concurrent.futures import ThreadPoolExecutor
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from os.path import join
from smtplib import SMTP
from flask import Flask, jsonify, request
import segment
import utils
import requests
import json
import html
# load the embeddings and vocabulary
embeddings, vocabulary = utils.load_models()
app = Flask(__name__)
executor = ThreadPoolExecutor(1)
def send_email(body, receipient):
"""
A function that uses the SMTP library to send an
email with the given body and receipients.
Parameters
----------
body : str
Body of the email to be sent.
Raises
------
Exception
If the email could not be sent.
"""
# set up the email S/MIME and other fields
# depending on whether we are sending the email
# as plain text or not
msg = MIMEMultipart('alternative')
text_body = body
content = MIMEText(text_body)
msg.attach(content)
msg['Subject'] = "segments"
msg['From'] = "bgyawali@ets.org"
msg['To'] = receipient
# try sending the email; if it fails, raise an exception
try:
# send the message via our own SMTP server running on localhost
s = SMTP('127.0.0.1')
s.send_message(msg)
print('Email sent successfully')
s.quit()
except Exception as e:
raise Exception(str(e))
def submit_segments(segments, request_id, return_url):
segments_str = '====='.join(segments)
#segments_str_encoded = html.escape(segments_str)
segments_str_encoded = json.dumps(segments_str)
logfname = os.path.join('logs', str(request_id) + '_log.txt')
logfile = open(logfname, 'w')
logfile.write(segments_str_encoded)
print(segments_str)
print(request_id)
print(return_url)
#post_url = 'http://c3dev.research.ets.org/TextSegmentation/handlers/SegmentationResponse.ashx'
try:
to_submit = json.dumps({'requestId': request_id, 'segmentedText': segments_str_encoded, 'statusCode': 1, 'statusMessage': 'completed'})
except Exception as e:
print(e)
r = requests.post(return_url, data={'message': to_submit})
print(r.status_code, r.reason)
logfile.write('\n' + str(r.status_code) + ' ' + r.reason)
logfile.close()
def segment_text(input_text, request_id, return_url):
"""
A function to segment the input text and send an email with the segments.
Paramters
---------
input_text: str
The text to segment
send_to: str
The email address to send back the segments
"""
with tempfile.TemporaryDirectory() as temp_dir:
input_dir = join(temp_dir, 'input')
output_dir = join(temp_dir, 'output')
for dirname in [input_dir, output_dir]:
os.makedirs(dirname)
with open(join(input_dir, 'input.txt'), 'w') as wf:
wf.write(input_text)
with open(os.path.join('input-logs', str(request_id) + '_input.txt'), 'w') as input_log:
input_log.write(input_text)
segment.run_segmentation(input_dir, output_dir, embeddings, vocabulary)
segmented_text = open(join(output_dir, 'input.txt.seg')).read()
segments = segmented_text.split('=====')
#print('==='.join(segments))
submit_segments(segments, request_id, return_url)
# send email
#send_email(segmented_text, send_to)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/post_segments', methods=["POST"])
def post_segments():
request_json = request.get_json()
input_text = html.unescape(request_json.get('passageText'))
request_id = request_json.get('requestId')
passageId = request_json.get('passageId')
return_url = request_json.get('returnURL')
#send_to = request_json.get('send_to_email')
print(input_text)
executor.submit(segment_text, input_text, request_id, return_url)
print(request_json)
return (jsonify({"status" : "ok"}), 201)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)