-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkps.py
69 lines (54 loc) · 2.07 KB
/
kps.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
""" Aby uruchomić produkcyjną wersję należy skorzystać z gunicorna
Uruchamiamy poleceniem:
gunicorn -w THREADS -b HOST:PORT kps:app
- THREADS - liczba niezależnych workerów (wątków)
- HOST - host, dla localhosta ustawić 127.0.0.1
- PORT - port, można wybrać dowolny, polecam 5010
"""
from flask import Flask, render_template, request, abort, jsonify
from algorithm.QuestionTypeRecognition import QuestionTypeRecognition
from algorithm.AnswerExtraction import AnswerExtraction
app = Flask(__name__)
app_name = "KPS - moduł odpowiadania na pytania"
engines = ["combined", "duckduckgo", "bing", "google", "yahoo"]
strategies = ["singlequery", "stopwords", "chunks"]
def find_answer(query, engine, strategy):
recognizer = QuestionTypeRecognition()
recognizer.set_question(query)
domain = recognizer.find_domain()
url = "<brak>"
answer = "<brak>"
if domain != "Domain not found":
pos = recognizer.get_position()
extract_answer = AnswerExtraction(query, pos, domain)
extract_answer.find_summaries(engine, strategy)
[answer,url] = extract_answer.find_answer()
return {
"query": query,
"engine": engine,
"strategy": strategy,
"answer": answer,
"url": url
}
@app.route("/")
def index():
data = {
"engines": engines,
"strategies": strategies
}
return render_template('kps/index.html', app_name=app_name, data=data)
@app.route("/fe/ask")
def show_result():
engine = request.args.get("engine", "combined")
strategy = request.args.get("strategy", "singlequery")
query = request.args.get("query")
answer = find_answer(query, engine, strategy)
return render_template("kps/result.html", app_name=app_name, data=answer)
@app.route("/ask/<query>")
def ask(query):
engine = request.args.get("engine", "combined")
strategy = request.args.get("strategy", "singlequery")
answer = find_answer(query, engine, strategy)
return jsonify(answer)
if __name__ == "__main__":
app.run(host="localhost",port=5010)