Skip to content

Instantly share code, notes, and snippets.

@danesherbs
Last active January 30, 2024 03:10
Show Gist options
  • Save danesherbs/6beb19a0cca5693940ea8f75d51d9cde to your computer and use it in GitHub Desktop.
Save danesherbs/6beb19a0cca5693940ea8f75d51d9cde to your computer and use it in GitHub Desktop.
A tiny web app to render OpenAI eval log files nicely :)
from flask import Flask, render_template, request, redirect
import jsonlines
import os
app = Flask(__name__)
@app.route("/")
def home():
default_fname = "tmp/evallogs/230807123401PG6IQKIY_gpt-3.5-turbo-16k,gpt-3.5-turbo-16k_make-me-say.jsonl"
return redirect(default_fname)
@app.route("/<path:path_to_jsonl>")
def view_sampling(path_to_jsonl: str):
path_to_jsonl = "/" + path_to_jsonl
page_idx = int(request.args.get("page", 1)) - 1
lines = get_lines(path_to_jsonl)
line = lines[page_idx] if 0 <= page_idx < len(lines) else None
return render_template(
"index.html",
line=line,
page=page_idx,
total_pages=len(lines),
path_to_jsonl=path_to_jsonl,
)
def get_lines(path: str) -> list[dict]:
# pre-conditions
assert os.path.exists(path)
with jsonlines.open(path) as reader:
lines = [obj for obj in reader if obj.get("type") == "sampling"]
for obj in lines:
assert isinstance(obj, dict)
assert "data" in obj
assert "prompt" in obj["data"]
assert isinstance(obj["data"]["prompt"], list)
# body
sorted_lines = sorted(
lines,
key=lambda obj: len(obj["data"]["prompt"]),
reverse=True,
)
# post-conditions
assert all("data" in obj for obj in sorted_lines)
assert all("prompt" in obj["data"] for obj in sorted_lines)
assert all("sampled" in obj["data"] for obj in sorted_lines)
return sorted_lines
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001, debug=False)
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.tailwindcss.com"></script>
<title>conversation viewer</title>
</head>
<body class="bg-gray-100 p-8">
<div class="mb-4 flex justify-between">
{% if page > 0 %}
<a href="{{ url_for('view_sampling', path_to_jsonl=path_to_jsonl[1:], page=page) }}"
class="px-4 py-2 bg-blue-500 text-white rounded">Previous</a>
{% else %}
<span class="px-4 py-2 bg-gray-300 text-white rounded">Previous</span>
{% endif %}
{% if page + 1 < total_pages %} <a
href="{{ url_for('view_sampling', path_to_jsonl=path_to_jsonl[1:], page=page+2) }}"
class="px-4 py-2 bg-blue-500 text-white rounded">Next</a>
{% else %}
<span class="px-4 py-2 bg-gray-300 text-white rounded">Next</span>
{% endif %}
</div>
{% if line %}
<div class="bg-white p-4 rounded shadow">
<div class="mb-4">Event ID: {{ line["event_id"] }}</div>
<div class="mb-4">Sample ID: {{ line["sample_id"] }}</div>
<div class="mb-4">
<table class="min-w-full bg-white border rounded">
<thead>
<tr>
<th class="py-2 px-4 border-b">Role</th>
<th class="py-2 px-4 border-b">Content</th>
</tr>
</thead>
<tbody>
{% for item in line["data"]["prompt"] %}
<tr class="{% if loop.index0 % 2 == 0 %}bg-gray-100{% else %}bg-white{% endif %}">
<td class="py-2 px-4 border-b">{{ item["role"] }}</td>
<td class="py-2 px-4 border-b">{{ item["content"] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% else %}
<div class="text-red-500">No sampling lines found in the provided path.</div>
{% endif %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment