diff --git a/app.py b/app.py index b6db24e..265a5dd 100644 --- a/app.py +++ b/app.py @@ -16,7 +16,14 @@ import random import boto3 import tiktoken import together -from flask import Flask, render_template, request, send_from_directory +from flask import ( + Flask, + render_template, + request, + send_from_directory, + jsonify, + Response, +) from flask_socketio import SocketIO, emit, join_room @@ -96,7 +103,7 @@ HELP_MESSAGE = """ - `gpt-4`: For GPT-4, send a message with `gpt-4` and include your prompt. - `gpt-4o-2024-08-06`: For the cheapest version of GPT-4o, send a message with `gpt-4o-2024-08-06` and include your prompt. - `gpt-mini`: For GPT-4o-mini, send a message with `gpt-mini` and include your prompt. -- `gpt-o1-mini`: For GPT-4o-mini, send a message with `gpt-mini` and include your prompt. +- `gpt-o1-mini`: For GPT-o1-mini, send a message with `gpt-o1-mini` and include your prompt. - `claude-haiku`: For Claude-haiku, send a message with `claude-haiku` and include your prompt. - `claude-sonnet`: For Claude-sonnet, send a message with `claude-sonnet` and include your prompt. - `claude-opus`: For Claude-opus, send a message with `claude-opus` and include your prompt. @@ -272,6 +279,40 @@ def chat(room_name): ) +@app.route("/download_chat_history", methods=["GET"]) +def download_chat_history(): + room_name = request.args.get("room_name") + room = get_room(room_name) + + if not room: + return jsonify({"error": "Room not found"}), 404 + + messages = Message.query.filter_by(room_id=room.id).all() + + if not messages: + return jsonify({"error": "No messages found"}), 404 + + chat_history = [ + { + "role": "system" if message.username in system_users else "user", + "content": message.content, + } + for message in messages + if not message.is_base64_image() + ] + + if not chat_history: + return jsonify({"error": "No valid messages found"}), 404 + + response = Response( + response=json.dumps(chat_history, indent=2), + status=200, + mimetype="application/json", + ) + response.headers["Content-Disposition"] = f"attachment; filename={room.name}.json" + return response + + @app.route("/search") def search_page(): # Query all rooms so that newest is first. diff --git a/templates/base.html b/templates/base.html index 854d5b8..408f45b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -163,6 +163,12 @@ display: block; padding-right: 0.8em; /* Adjust the padding as needed */ } + #downloadButton { + position: fixed; + top: 10px; + right: 10px; + z-index: 1000; + } @@ -175,6 +181,7 @@ +
@@ -190,30 +197,31 @@ {% endfor %}
+ {% block content %}{% endblock %}
- + // Add event listener for keyword search the "Enter" key + document.getElementById("search-keywords").addEventListener("keydown", function(event) { + if (event.key === "Enter") { + event.preventDefault(); + performSearch(); + } + }); + diff --git a/templates/chat.html b/templates/chat.html index 76e38a5..6909ffb 100644 --- a/templates/chat.html +++ b/templates/chat.html @@ -3,6 +3,8 @@ {% block title %}Chatroom{% endblock %} {% block content %} + Download Chat History +