Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a history page (static) #967

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ package-lock.json
logs/
*.orig
*.log
server_log.txt
server_log.txt

# db files
*.db
41 changes: 41 additions & 0 deletions backend/server/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sqlite3
from datetime import datetime

DB_NAME = 'research_history.db'

def init_db():
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS research_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()

def save_research(title: str, content: str):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('INSERT INTO research_history (title, content) VALUES (?, ?)', (title, content))
conn.commit()
conn.close()

def get_all_research():
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('SELECT * FROM research_history ORDER BY created_at DESC')
results = c.fetchall()
conn.close()
return results

def get_research_by_id(research_id: int):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('SELECT * FROM research_history WHERE id = ?', (research_id,))
result = c.fetchone()
conn.close()
return result
17 changes: 16 additions & 1 deletion backend/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
update_environment_variables, handle_file_upload, handle_file_deletion,
execute_multi_agents, handle_websocket_communication
)
from backend.server.database import init_db, save_research, get_all_research, get_research_by_id


from gpt_researcher.utils.logging_config import setup_research_logging
Expand Down Expand Up @@ -93,7 +94,7 @@ def startup_event():
os.makedirs("outputs", exist_ok=True)
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
os.makedirs(DOC_PATH, exist_ok=True)

init_db() # Initialize the database

# Routes

Expand Down Expand Up @@ -125,6 +126,20 @@ async def delete_file(filename: str):
return await handle_file_deletion(filename, DOC_PATH)


@app.get("/history")
async def history_page(request: Request):
research_history = get_all_research()
return templates.TemplateResponse("history.html", {"request": request, "history": research_history})


@app.get("/history/{research_id}")
async def get_research(research_id: int):
research = get_research_by_id(research_id)
if research:
return {"id": research[0], "title": research[1], "content": research[2], "created_at": research[3]}
return {"error": "Research not found"}, 404


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
Expand Down
5 changes: 5 additions & 0 deletions backend/server/server_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi.responses import JSONResponse, FileResponse
from gpt_researcher.document.document import DocumentLoader
from backend.utils import write_md_to_pdf, write_md_to_word, write_text_to_md
from backend.server.database import save_research
from pathlib import Path
from datetime import datetime
from fastapi import HTTPException
Expand Down Expand Up @@ -147,6 +148,10 @@ async def handle_start_command(websocket, data: str, manager):
headers
)
report = str(report)

# Save to SQLite database
save_research(title=task, content=report)

file_paths = await generate_report_files(report, sanitized_filename)
# Add JSON log path to file_paths
file_paths["json"] = os.path.relpath(logs_handler.log_file)
Expand Down
111 changes: 111 additions & 0 deletions frontend/history.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Research History - GPT Researcher</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="./static/favicon.ico">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="/site/styles.css" />
<style>
.history-item {
border: 1px solid #ddd;
padding: 15px;
margin: 10px 0;
border-radius: 5px;
transition: background-color 0.3s ease;
}
.history-item:hover {
background-color: #f5f5f5;
}
.history-meta {
color: #666;
font-size: 0.9em;
}
.history-list {
margin: 2rem 0;
}
.modal-content {
background-color: #f8f9fa;
color: #212529;
}
.modal-body {
color: #212529;
}
#modalContent {
color: #212529;
}
</style>
</head>
<body>
<h1 style="font-size: 32px; font-weight: bold; text-align: center; margin: 20px 0;">Research History</h1>
<main class="container">

<div class="text-center mb-4">
<a href="/" class="btn btn-primary">New Research</a>
</div>
<div class="history-list">
{% for item in history %}
<div class="history-item">
<h3>{{ item[1] }}</h3>
<div class="history-meta">
ID: {{ item[0] }} | Date: {{ item[3] }}
</div>
<a href="#" class="btn btn-sm btn-secondary mt-2"
onclick="showContent('{{ item[0] }}')">View Report</a>
</div>
{% endfor %}
</div>




</main>

<!-- Add required scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<!-- Update the modal structure -->
<div class="modal fade" id="contentModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Research Report</h5>
<button type="button" class="close" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<div class="modal-body">
<div id="modalContent" class="margin-div"></div>
</div>
</div>
</div>
</div>

<script>
async function showContent(id) {
const response = await fetch(`/history/${id}`);
const data = await response.json();

const converter = new showdown.Converter();
const html = converter.makeHtml(data.content);

document.getElementById('modalContent').innerHTML = html;
$('#contentModal').modal('show');
}
</script>

<footer>
<p>
<a target="_blank" href="https://gptr.dev">Homepage</a> |
<a target="_blank" href="https://github.com/assafelovic/gpt-researcher">GitHub</a> |
<a target="_blank" href="https://discord.gg/spBgZmm3Xe">Discord</a>
</p>
<p>GPT Researcher &copy; 2024</p>
</footer>
</body>
</html>
5 changes: 4 additions & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ <h1 class="text-4xl font-extrabold mx-auto lg:text-7xl">
Say Hello to <b>GPT Researcher</b>, your AI mate for rapid insights and comprehensive research. <br>
GPT Researcher takes care of everything from accurate source gathering and organization of research results to generation of customized reports with citations.
</p>
<a href="#form" class="btn btn-primary">Get Started</a>
<div>
<a href="#form" class="btn btn-primary">Get Started</a>
<a href="/history" class="btn btn-secondary" target="_blank">View History</a>
</div>
</div>
</section>

Expand Down