Spaces:
Sleeping
Sleeping
Commit ·
062e60a
1
Parent(s): 09a75d5
routes bug
Browse files- app/app.py +202 -199
app/app.py
CHANGED
|
@@ -13,211 +13,214 @@ class FlaskApp:
|
|
| 13 |
self.register_routes()
|
| 14 |
|
| 15 |
def register_routes(self):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
self.messages.append(user_message_dict)
|
| 30 |
-
|
| 31 |
-
# Iterate through agents, differentiate between admin and LLM agents
|
| 32 |
-
for i, agent_index in enumerate(self.agent_order):
|
| 33 |
-
agent = self.agents[agent_index]
|
| 34 |
-
|
| 35 |
-
if agent_index == 0:
|
| 36 |
-
# Admin agent, skip LLM request
|
| 37 |
-
continue
|
| 38 |
-
|
| 39 |
-
# LLM agent, request response
|
| 40 |
-
llm_response = agent.request_llm_response(user_message)
|
| 41 |
-
pending_llm_message = {
|
| 42 |
-
'content': llm_response,
|
| 43 |
-
'author': f'llm{agent_index + 1}', # Dynamically assign author label
|
| 44 |
-
'approved': False
|
| 45 |
}
|
| 46 |
-
self.
|
| 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 |
else:
|
| 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 |
-
all_messages=self.messages,
|
| 146 |
-
pending_messages=self.pending_llm_responses,
|
| 147 |
-
agent_order=self.agent_order,
|
| 148 |
-
llm_1_system_content=self.agents[1].system_content,
|
| 149 |
-
llm_2_system_content=self.agents[2].system_content)
|
| 150 |
-
|
| 151 |
-
@self.flask_app.route('/reinitialize', methods=['POST'])
|
| 152 |
-
def reinitialize_dashboards():
|
| 153 |
-
# Reset session data or other necessary variables
|
| 154 |
-
session.clear() # Clears the session, you can also reset other custom variables here if needed
|
| 155 |
-
|
| 156 |
-
# Reinitialize your variables here (like message history, agent order, etc.)
|
| 157 |
-
# Assuming self.agent_order and messages are variables to reset
|
| 158 |
-
self.messages = [] # Reset message history
|
| 159 |
-
self.pending_llm_responses = [] # Reset pending LLM messages
|
| 160 |
-
self.agent_order = [0, 1, 2] # Reset agent order to default
|
| 161 |
|
| 162 |
-
|
| 163 |
-
return redirect(url_for('admin_dashboard')) # You can redirect to 'user_dashboard' or another page if necessary
|
| 164 |
-
|
| 165 |
-
@self.flask_app.route('/user_dashboard_reset', methods=['POST'])
|
| 166 |
-
def user_dashboard_reset():
|
| 167 |
-
# After reinitializing, update the user dashboard
|
| 168 |
-
return render_template('partials/_user_message_list.html', messages=self.messages)
|
| 169 |
-
|
| 170 |
-
@self.flask_app.route('/login', methods=['GET', 'POST'])
|
| 171 |
-
def login():
|
| 172 |
-
if request.method == 'POST':
|
| 173 |
-
username = request.form.get('username')
|
| 174 |
-
|
| 175 |
-
if username == 'admin':
|
| 176 |
-
session['username'] = 'admin'
|
| 177 |
-
return redirect(url_for('admin_dashboard'))
|
| 178 |
-
elif username == 'user':
|
| 179 |
-
session['username'] = 'user'
|
| 180 |
-
return redirect(url_for('user_dashboard')) # Use the correct route name
|
| 181 |
-
else:
|
| 182 |
-
# If the username is incorrect, show an error message
|
| 183 |
-
return "Invalid username. Please use 'user' or 'admin'."
|
| 184 |
-
return render_template('login.html')
|
| 185 |
-
|
| 186 |
-
@self.flask_app.route('/logout')
|
| 187 |
-
def logout():
|
| 188 |
-
session.clear()
|
| 189 |
-
return redirect(url_for('login'))
|
| 190 |
-
|
| 191 |
-
# Route for index.html
|
| 192 |
-
@self.flask_app.route('/')
|
| 193 |
-
def index():
|
| 194 |
-
return render_template('index.html')
|
| 195 |
-
|
| 196 |
-
# Endpoint to check whether a refresh is needed
|
| 197 |
-
@self.flask_app.route('/check_refresh')
|
| 198 |
-
def check_refresh():
|
| 199 |
-
global refresh_needed
|
| 200 |
-
return jsonify({"refresh_needed": refresh_needed})
|
| 201 |
-
|
| 202 |
-
# Reset the refresh flag
|
| 203 |
-
@self.flask_app.route('/reset_refresh', methods=['POST'])
|
| 204 |
-
def reset_refresh():
|
| 205 |
-
global refresh_needed
|
| 206 |
-
refresh_needed = False
|
| 207 |
-
return '', 204
|
| 208 |
-
|
| 209 |
-
@self.flask_app.route('/get_message_history', methods=['GET'])
|
| 210 |
-
def get_message_history():
|
| 211 |
-
return jsonify(self.messages)
|
| 212 |
-
|
| 213 |
-
@self.flask_app.route('/get_pending_messages', methods=['GET'])
|
| 214 |
-
def get_pending_messages():
|
| 215 |
-
return jsonify(self.pending_llm_responses)
|
| 216 |
-
|
| 217 |
-
@self.flask_app.route('/get_agent_order', methods=['GET'])
|
| 218 |
-
def get_agent_order():
|
| 219 |
-
global agent_order
|
| 220 |
-
return jsonify({"agent_order": agent_order})
|
| 221 |
|
| 222 |
if __name__ == '__main__':
|
| 223 |
# Initialize the LLM agents with default system content
|
|
|
|
| 13 |
self.register_routes()
|
| 14 |
|
| 15 |
def register_routes(self):
|
| 16 |
+
if not self.routes_registered:
|
| 17 |
+
@self.flask_app.route('/dashboard', methods=['GET', 'POST'])
|
| 18 |
+
def user_dashboard():
|
| 19 |
+
global refresh_needed
|
| 20 |
+
|
| 21 |
+
if request.method == 'POST':
|
| 22 |
+
user_message = request.form.get('message')
|
| 23 |
+
if user_message:
|
| 24 |
+
# User message submission
|
| 25 |
+
user_message_dict = {
|
| 26 |
+
'content': user_message,
|
| 27 |
+
'author': session.get('user_id', 'User'),
|
| 28 |
+
'approved': True # Show immediately but still needs approval in the background
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
self.messages.append(user_message_dict)
|
| 31 |
+
|
| 32 |
+
# Iterate through agents, differentiate between admin and LLM agents
|
| 33 |
+
for i, agent_index in enumerate(self.agent_order):
|
| 34 |
+
agent = self.agents[agent_index]
|
| 35 |
+
|
| 36 |
+
if agent_index == 0:
|
| 37 |
+
# Admin agent, skip LLM request
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
# LLM agent, request response
|
| 41 |
+
llm_response = agent.request_llm_response(user_message)
|
| 42 |
+
pending_llm_message = {
|
| 43 |
+
'content': llm_response,
|
| 44 |
+
'author': f'llm{agent_index + 1}', # Dynamically assign author label
|
| 45 |
+
'approved': False
|
| 46 |
+
}
|
| 47 |
+
self.pending_llm_responses.append(pending_llm_message)
|
| 48 |
+
|
| 49 |
+
flash('Message submitted and LLM responses are awaiting approval!')
|
| 50 |
+
return redirect(url_for('user_dashboard'))
|
| 51 |
+
|
| 52 |
+
refresh_needed = True
|
| 53 |
+
return render_template('dashboard.html', messages=self.messages, agent_order=self.agent_order)
|
| 54 |
+
|
| 55 |
+
@self.flask_app.route('/change_agent_order', methods=['POST'])
|
| 56 |
+
def change_agent_order():
|
| 57 |
+
global agent_order
|
| 58 |
+
# Get the new agent order from the admin form
|
| 59 |
+
agent_1 = request.form.get('agent_1')
|
| 60 |
+
agent_2 = request.form.get('agent_2')
|
| 61 |
+
agent_3 = request.form.get('agent_3')
|
| 62 |
+
|
| 63 |
+
# Update the agent order
|
| 64 |
+
agent_order = [agent_1, agent_2, agent_3]
|
| 65 |
+
|
| 66 |
+
# Redirect back to the admin dashboard
|
| 67 |
+
return redirect(url_for('admin_dashboard'))
|
| 68 |
+
|
| 69 |
+
@self.flask_app.route('/admin_dashboard', methods=['GET', 'POST'])
|
| 70 |
+
def admin_dashboard():
|
| 71 |
+
global refresh_needed
|
| 72 |
+
|
| 73 |
+
if request.method == 'POST':
|
| 74 |
+
action = request.form.get('action')
|
| 75 |
+
|
| 76 |
+
if action == 'update_system_content':
|
| 77 |
+
# Get system content for LLM agents from form input
|
| 78 |
+
new_llm_1_content = request.form.get('llm_1_system_content')
|
| 79 |
+
new_llm_2_content = request.form.get('llm_2_system_content')
|
| 80 |
+
|
| 81 |
+
# Ensure that the content is updated in the agents
|
| 82 |
+
if new_llm_1_content is not None:
|
| 83 |
+
self.agents[1].system_content = new_llm_1_content # Directly update the system_content attribute
|
| 84 |
+
if new_llm_2_content is not None:
|
| 85 |
+
self.agents[2].system_content = new_llm_2_content # Directly update the system_content attribute
|
| 86 |
+
|
| 87 |
+
return redirect(url_for('admin_dashboard'))
|
| 88 |
+
|
| 89 |
+
if action == 'change_order':
|
| 90 |
+
# Capture the selected agent order from the dropdown
|
| 91 |
+
self.agent_order = request.form.getlist('agent_order[]')
|
| 92 |
+
|
| 93 |
+
# Refresh the page to see the updated agent order
|
| 94 |
+
return redirect("/admin_dashboard")
|
| 95 |
+
|
| 96 |
+
# Admin sending a message to the user
|
| 97 |
+
if action == 'send':
|
| 98 |
+
admin_message = request.form.get('admin_message')
|
| 99 |
+
if admin_message:
|
| 100 |
+
new_message = {
|
| 101 |
+
'content': admin_message,
|
| 102 |
+
'author': 'Admin',
|
| 103 |
+
'approved': True
|
| 104 |
+
}
|
| 105 |
+
self.messages.append(new_message)
|
| 106 |
+
flash('Message sent to the user!')
|
| 107 |
+
else:
|
| 108 |
+
flash('Error: Message content is empty.')
|
| 109 |
+
return redirect(url_for('admin_dashboard'))
|
| 110 |
|
| 111 |
+
message_id = request.form.get('message_id')
|
| 112 |
+
|
| 113 |
+
if message_id is not None:
|
| 114 |
+
message_id = int(message_id)
|
| 115 |
+
|
| 116 |
+
# Approve action
|
| 117 |
+
if action == 'approve':
|
| 118 |
+
if message_id is not None and message_id < len(self.pending_llm_responses):
|
| 119 |
+
self.pending_llm_responses[message_id]['approved'] = True
|
| 120 |
+
self.messages.append(self.pending_llm_responses.pop(message_id))
|
| 121 |
+
|
| 122 |
+
return redirect(url_for('admin_dashboard'))
|
| 123 |
+
|
| 124 |
+
# Delete action
|
| 125 |
+
if action == 'delete':
|
| 126 |
+
if message_id is not None and message_id < len(self.pending_llm_responses):
|
| 127 |
+
del self.pending_llm_responses[message_id]
|
| 128 |
+
return redirect(url_for('admin_dashboard'))
|
| 129 |
+
|
| 130 |
+
# Regenerate action
|
| 131 |
+
if action == 'regenerate':
|
| 132 |
+
if message_id is not None and message_id < len(self.pending_llm_responses):
|
| 133 |
+
message_content = self.pending_llm_responses[message_id]['content']
|
| 134 |
+
|
| 135 |
+
for agent in self.agents:
|
| 136 |
+
if agent is not None:
|
| 137 |
+
regenerated_response = agent.request_llm_response(message_content)
|
| 138 |
+
self.pending_llm_responses[message_id]['content'] = regenerated_response
|
| 139 |
+
break
|
| 140 |
+
return redirect(url_for('admin_dashboard'))
|
| 141 |
|
| 142 |
+
# Set the refresh_needed flag to True
|
| 143 |
+
refresh_needed = True
|
| 144 |
+
|
| 145 |
+
return render_template('admin_dashboard.html',
|
| 146 |
+
all_messages=self.messages,
|
| 147 |
+
pending_messages=self.pending_llm_responses,
|
| 148 |
+
agent_order=self.agent_order,
|
| 149 |
+
llm_1_system_content=self.agents[1].system_content,
|
| 150 |
+
llm_2_system_content=self.agents[2].system_content)
|
| 151 |
+
|
| 152 |
+
@self.flask_app.route('/reinitialize', methods=['POST'])
|
| 153 |
+
def reinitialize_dashboards():
|
| 154 |
+
# Reset session data or other necessary variables
|
| 155 |
+
session.clear() # Clears the session, you can also reset other custom variables here if needed
|
| 156 |
+
|
| 157 |
+
# Reinitialize your variables here (like message history, agent order, etc.)
|
| 158 |
+
# Assuming self.agent_order and messages are variables to reset
|
| 159 |
+
self.messages = [] # Reset message history
|
| 160 |
+
self.pending_llm_responses = [] # Reset pending LLM messages
|
| 161 |
+
self.agent_order = [0, 1, 2] # Reset agent order to default
|
| 162 |
+
|
| 163 |
+
# Redirect to the dashboard (or admin dashboard)
|
| 164 |
+
return redirect(url_for('admin_dashboard')) # You can redirect to 'user_dashboard' or another page if necessary
|
| 165 |
+
|
| 166 |
+
@self.flask_app.route('/user_dashboard_reset', methods=['POST'])
|
| 167 |
+
def user_dashboard_reset():
|
| 168 |
+
# After reinitializing, update the user dashboard
|
| 169 |
+
return render_template('partials/_user_message_list.html', messages=self.messages)
|
| 170 |
+
|
| 171 |
+
@self.flask_app.route('/login', methods=['GET', 'POST'])
|
| 172 |
+
def login():
|
| 173 |
+
if request.method == 'POST':
|
| 174 |
+
username = request.form.get('username')
|
| 175 |
+
|
| 176 |
+
if username == 'admin':
|
| 177 |
+
session['username'] = 'admin'
|
| 178 |
+
return redirect(url_for('admin_dashboard'))
|
| 179 |
+
elif username == 'user':
|
| 180 |
+
session['username'] = 'user'
|
| 181 |
+
return redirect(url_for('user_dashboard')) # Use the correct route name
|
| 182 |
else:
|
| 183 |
+
# If the username is incorrect, show an error message
|
| 184 |
+
return "Invalid username. Please use 'user' or 'admin'."
|
| 185 |
+
return render_template('login.html')
|
| 186 |
+
|
| 187 |
+
@self.flask_app.route('/logout')
|
| 188 |
+
def logout():
|
| 189 |
+
session.clear()
|
| 190 |
+
return redirect(url_for('login'))
|
| 191 |
+
|
| 192 |
+
# Route for index.html
|
| 193 |
+
@self.flask_app.route('/')
|
| 194 |
+
def index():
|
| 195 |
+
return render_template('index.html')
|
| 196 |
+
|
| 197 |
+
# Endpoint to check whether a refresh is needed
|
| 198 |
+
@self.flask_app.route('/check_refresh')
|
| 199 |
+
def check_refresh():
|
| 200 |
+
global refresh_needed
|
| 201 |
+
return jsonify({"refresh_needed": refresh_needed})
|
| 202 |
+
|
| 203 |
+
# Reset the refresh flag
|
| 204 |
+
@self.flask_app.route('/reset_refresh', methods=['POST'])
|
| 205 |
+
def reset_refresh():
|
| 206 |
+
global refresh_needed
|
| 207 |
+
refresh_needed = False
|
| 208 |
+
return '', 204
|
| 209 |
+
|
| 210 |
+
@self.flask_app.route('/get_message_history', methods=['GET'])
|
| 211 |
+
def get_message_history():
|
| 212 |
+
return jsonify(self.messages)
|
| 213 |
+
|
| 214 |
+
@self.flask_app.route('/get_pending_messages', methods=['GET'])
|
| 215 |
+
def get_pending_messages():
|
| 216 |
+
return jsonify(self.pending_llm_responses)
|
| 217 |
|
| 218 |
+
@self.flask_app.route('/get_agent_order', methods=['GET'])
|
| 219 |
+
def get_agent_order():
|
| 220 |
+
global agent_order
|
| 221 |
+
return jsonify({"agent_order": agent_order})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
self.routes_registered = True # Mark routes as registered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
if __name__ == '__main__':
|
| 226 |
# Initialize the LLM agents with default system content
|