-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
124 lines (85 loc) · 3.94 KB
/
Copy pathapp.py
File metadata and controls
124 lines (85 loc) · 3.94 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
import os
import sys
import time
from xai_sdk import Client
from xai_sdk.search import SearchParameters, web_source
from xai_sdk.chat import system, user
from llm_files.clients import get_openai_client, get_xai_client
from flask import Flask, jsonify, render_template, request
from functions import load_votes, load_vote, count_votes, classify_vote, parse_votes, initialize_data
from llm_files.llm_functions import chat_with_llm, explain_quote
app = Flask(__name__)
# for llm call at run time
@app.route('/explain/<int:voteId>', methods=['POST'])
def llm_actions(voteId):
language = request.args.get('language', 'de')
model = request.args.get('model', 'grok-4')
# Access fields
data = request.get_json(force=True) # force=True ensures it parses even without correct header
highlighted_text = data.get("highlighted_text", None)
explanations = explain_quote(highlighted_text, voteId, language, model)
return jsonify(explanations), 200
@app.route('/chat', methods=['GET', 'POST'])
def chat_interaction():
"""
GET /chat: given a user's session and a voting topic, provides a list of unique chatIds
(which can used to further extend conversation)
POST /chat:
:return:
"""
if request.method == 'GET':
return jsonify({"text": "es giht nünt"}), 200
elif request.method == 'POST':
data = request.get_json(force=True) # force=True ensures it parses even without correct header
# Access fields
input = data.get("input", None)
markdown_path = data.get("markdown_path", None)
response = chat_with_llm(input, markdown_path)
return jsonify(response), 200
else:
return jsonify({"text": "Bad Request"}), 400
# This is currently not used, but prepared if eventually a UI based on Next.JS with SSR will be developed
@app.route('/api/get-votes')
def api_get_votes():
return load_votes('de')
# This is currently not used, but prepared if eventually a UI based on Next.JS with SSR will be developed
@app.route('/api/get-votes/<int:voteId>')
def api_get_vote(voteId):
language = request.args.get('language', 'de')
vote = load_vote(voteId, language)
contents = next((e for e in vote["erlaeuterungen"] if e["langKey"] == language), None)
# print(contents['erlaeuterung'])
return contents['erlaeuterung']
@app.route('/')
def home():
language = request.args.get('language', 'de')
model = request.args.get('model', 'grok-4')
a, b = count_votes()
return render_template('index.html', tracked_votes=a, total_votes=b, language=language, model=model)
@app.route('/votes')
def votes():
language = request.args.get('language', 'de')
model = request.args.get('model', 'grok-4')
abstimmungstage = load_votes(language)
return render_template("votes.html", abstimmungstage=abstimmungstage, language=language, model=model)
@app.route('/votes/<int:voteId>')
def vote(voteId):
language = request.args.get('language', 'de')
model = request.args.get('model', 'grok-4')
date, vote = load_vote(voteId, language)
contents = next((e for e in vote["erlaeuterungen"] if e["langKey"] == language), None)
return render_template('vote.html', vote_date=date, vote_id=voteId,
erlaeuterung=contents['erlaeuterung'], vote_type=classify_vote(voteId), model=model, newsArticles=vote['voteNewsArticles'], summary=vote['voteSummary'])
@app.route('/vorlage.html')
def vorlage_html():
return render_template('vorlage_old.html')
if __name__ == '__main__':
s = time.time()
success, return_string = initialize_data() # TESTMODE=True
if not success:
print(f"Failed to initialize data: {return_string}")
sys.exit(0)
print("Time of Initialization: ", (time.time() - s) / 60, "minutes")
# app.run(debug=True, use_reloader=False) # for later: debug=False or at least: use_reloader=False (better performance at start up)
# Docker version
app.run(host='0.0.0.0', port=10002, debug=False)