-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
93 lines (83 loc) · 3.74 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
92
93
from flask import Flask, request, render_template, flash, redirect
from flask.ext.runner import Runner
from forms import InputForm
import parse_sent
import AMRICA.disagree
import hashlib
import time
import os
import base64
import logging
from logging.handlers import RotatingFileHandler
import json
from flask_jsonpify import jsonify
hash = hashlib.sha1()
app = Flask(__name__)
app.config.from_object('config')
runner = Runner(app)
@app.route('/amreager', methods=['GET', 'POST'])
def main():
form = InputForm()
sent = request.args.get('sent')
lang = request.args.get('lang')
graph = None
if form.validate_on_submit():
if hasattr(form.input_sent, 'lang'):
graph = parse_sent.run(form.input_sent.data, form.input_sent.lang)
else:
#compatability with older site
graph = parse_sent.run(form.input_sent.data)
elif sent is not None:
if lang is None:
lang = 'en'
graph = parse_sent.run(sent, lang)
if graph is not None:
app.logger.info('Input: ' + sent)
hash.update(str(time.time()))
hash.update(sent)
last = hash.hexdigest()
AMRICA.disagree.run("# ::id 1\n" + graph, "tmp" + last + ".png")
binpng = open("tmp" + last + ".png", "rb").read()
os.remove("tmp" + last + ".png")
link = "http://bollin.inf.ed.ac.uk:9010/amreager?lang=" + lang + "&sent=" + "+".join([t for t in sent.split()])
return jsonify({'graph': graph.replace("\n","<br/>").replace(" "," "), 'png': base64.b64encode(binpng), 'link': link}, sort_keys=True, indent=4, separators=(',', ': '))
else:
return jsonify({'graph': 'ERROR', 'png': '', 'link': ''}, sort_keys=True, indent=4, separators=(',', ': '))
return render_template('sentence.html',form=form)
@app.route('/', methods=['GET', 'POST'])
def main2():
form = InputForm()
sent = request.args.get('sent')
lang = request.args.get('lang')
graph = None
if form.validate_on_submit():
if hasattr(form.input_sent, 'lang'):
graph = parse_sent.run(form.input_sent.data, form.input_sent.lang)
else:
#compatability with older site
graph = parse_sent.run(form.input_sent.data)
elif sent is not None:
if lang is None:
lang = 'en'
graph = parse_sent.run(sent, lang)
if graph is not None:
app.logger.info('Input: ' + sent)
hash.update(str(time.time()))
hash.update(sent)
last = hash.hexdigest()
AMRICA.disagree.run("# ::id 1\n" + graph, "tmp" + last + ".png")
binpng = open("tmp" + last + ".png", "rb").read()
os.remove("tmp" + last + ".png")
link = "<span id ='link' style='height:150;width:162;background-color:pink'> http://bollin.inf.ed.ac.uk:9010?lang" + lang + "&sent=" + "+".join([t for t in sent.split()]) + "</span>"
return render_template('sentence.html', form=form, graph=graph.replace("\n","<br/>").replace(" "," "),png="<img src=\"data:image/png;base64," + base64.b64encode(binpng) + "\">", link="<b>Link:</b> " + link + " <button onClick=\"ClipBoard();\">Copy to Clipboard</button>")
else:
return jsonify({'graph': 'ERROR', 'png': '', 'link': ''}, sort_keys=True, indent=4, separators=(',', ': '))
return render_template('sentence.html',form=form)
if __name__ == '__main__':
handler = RotatingFileHandler('sentences.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(message)s")
handler.setFormatter(formatter)
app.logger.addHandler(handler)
app.logger.setLevel(logging.DEBUG)
runner.run()