Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

update evals page to take a folder

Abi Raja2512d081333b
diff --git a/backend/routes/evals.py b/backend/routes/evals.py
index 3a9073ed..a4cd347e 100644
--- a/backend/routes/evals.py
+++ b/backend/routes/evals.py
@@ -1,5 +1,5 @@
import os
-from fastapi import APIRouter, Query, Request
+from fastapi import APIRouter, Query, Request, HTTPException
from pydantic import BaseModel
from evals.utils import image_to_data_url
from evals.config import EVALS_DIR
@@ -8,6 +8,8 @@ from evals.runner import run_image_evals
from typing import List, Dict
from llm import Llm
from prompts.types import Stack
+from pathlib import Path
+import base64
router = APIRouter()
@@ -20,43 +22,56 @@ class Eval(BaseModel):
outputs: list[str]
-async def get_evals():
- # Get all evals from EVALS_DIR
- input_dir = EVALS_DIR + "/inputs"
- output_dir = EVALS_DIR + "/outputs"
[email protected]("/evals", response_model=list[Eval])
+async def get_evals(folder: str):
+ if not folder:
+ raise HTTPException(status_code=400, detail="Folder path is required")
- evals: list[Eval] = []
- for file in os.listdir(input_dir):
- if file.endswith(".png"):
- input_file_path = os.path.join(input_dir, file)
- input_file = await image_to_data_url(input_file_path)
-
- # Construct the corresponding output file names
- output_file_names = [
- file.replace(".png", f"_{i}.html") for i in range(0, N)
- ] # Assuming 3 outputs for each input
-
- output_files_data: list[str] = []
- for output_file_name in output_file_names:
- output_file_path = os.path.join(output_dir, output_file_name)
- # Check if the output file exists
- if os.path.exists(output_file_path):
- with open(output_file_path, "r") as f:
- output_files_data.append(f.read())
- else:
- output_files_data.append(
- "<html><h1>Output file not found.</h1></html>"
- )
-
- evals.append(
- Eval(
- input=input_file,
- outputs=output_files_data,
- )
+ folder_path = Path(folder)
+ if not folder_path.exists():
+ raise HTTPException(status_code=404, detail=f"Folder not found: {folder}")
+
+ try:
+ evals: list[Eval] = []
+ # Get all HTML files from folder
+ files = {
+ f: os.path.join(folder, f)
+ for f in os.listdir(folder)
+ if f.endswith(".html")
+ }
+
+ # Extract base names
+ base_names: Set[str] = set()
+ for filename in files.keys():
+ base_name = (
+ filename.rsplit("_", 1)[0]
+ if "_" in filename
+ else filename.replace(".html", "")
)
+ base_names.add(base_name)
+
+ for base_name in base_names:
+ input_path = os.path.join(EVALS_DIR, "inputs", f"{base_name}.png")
+ if not os.path.exists(input_path):
+ continue
- return evals
+ # Find matching output file
+ output_file = None
+ for filename, filepath in files.items():
+ if filename.startswith(base_name):
+ output_file = filepath
+ break
+
+ if output_file:
+ input_data = await image_to_data_url(input_path)
+ with open(output_file, "r", encoding="utf-8") as f:
+ output_html = f.read()
+ evals.append(Eval(input=input_data, outputs=[output_html]))
+
+ return evals
+
+ except Exception as e:
+ raise HTTPException(status_code=500, detail=f"Error processing evals: {str(e)}")
class PairwiseEvalResponse(BaseModel):
@@ -181,7 +196,7 @@ class BestOfNEvalsResponse(BaseModel):
async def get_best_of_n_evals(request: Request):
# Get all query parameters
query_params = dict(request.query_params)
-
+
# Extract all folder paths (folder1, folder2, folder3, etc.)
folders = []
i = 1
@@ -204,8 +219,8 @@ async def get_best_of_n_evals(request: Request):
files_by_folder = []
for folder in folders:
files = {
- f: os.path.join(folder, f)
- for f in os.listdir(folder)
+ f: os.path.join(folder, f)
+ for f in os.listdir(folder)
if f.endswith(".html")
}
files_by_folder.append(files)
@@ -253,7 +268,4 @@ async def get_best_of_n_evals(request: Request):
if len(outputs) == len(folders): # Only add if we have outputs from all folders
evals.append(Eval(input=input_image, outputs=outputs))
- return BestOfNEvalsResponse(
- evals=evals,
- folder_names=folder_names
- )
+ return BestOfNEvalsResponse(evals=evals, folder_names=folder_names)
diff --git a/frontend/src/components/evals/EvalsPage.tsx b/frontend/src/components/evals/EvalsPage.tsx
index c66bc9c3..3aef32b9 100644
--- a/frontend/src/components/evals/EvalsPage.tsx
+++ b/frontend/src/components/evals/EvalsPage.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect } from "react";
+import React, { useState } from "react";
import { HTTP_BACKEND_URL } from "../../config";
import RatingPicker from "./RatingPicker";
@@ -10,28 +10,65 @@ interface Eval {
function EvalsPage() {
const [evals, setEvals] = React.useState<Eval[]>([]);
const [ratings, setRatings] = React.useState<number[]>([]);
+ const [folderPath, setFolderPath] = useState("");
+ const [isLoading, setIsLoading] = useState(false);
const total = ratings.reduce((a, b) => a + b, 0);
const max = ratings.length * 4;
const score = ((total / max) * 100 || 0).toFixed(2);
- useEffect(() => {
- if (evals.length > 0) return;
+ const loadEvals = async () => {
+ if (!folderPath) {
+ alert("Please enter a folder path");
+ return;
+ }
- fetch(`${HTTP_BACKEND_URL}/evals`)
- .then((res) => res.json())
- .then((data) => {
- setEvals(data);
- setRatings(new Array(data.length).fill(0));
+ setIsLoading(true);
+ try {
+ const queryParams = new URLSearchParams({
+ folder: `/Users/abi/Downloads/${folderPath}`,
});
- }, [evals]);
+
+ const response = await fetch(`${HTTP_BACKEND_URL}/evals?${queryParams}`);
+ const data = await response.json();
+
+ console.log(data);
+
+ setEvals(data);
+ setRatings(new Array(data.length).fill(0));
+ } catch (error) {
+ console.error("Error loading evals:", error);
+ alert("Error loading evals. Please check the folder path and try again.");
+ } finally {
+ setIsLoading(false);
+ }
+ };
return (
<div className="mx-auto">
- <div className="flex items-center justify-between w-full h-12 bg-zinc-950">
- <span className="text-2xl font-semibold text-white ml-4">
- Total: {total} out of {max} ({score}%)
- </span>
+ <div className="flex flex-col items-center justify-center w-full py-4 bg-zinc-950 text-white">
+ <div className="flex flex-col gap-4 mb-4 w-full max-w-2xl px-4">
+ <input
+ type="text"
+ value={folderPath}
+ onChange={(e) => setFolderPath(e.target.value)}
+ placeholder="Enter folder name in Downloads"
+ className="w-full px-4 py-2 rounded text-black"
+ />
+ <button
+ onClick={loadEvals}
+ disabled={isLoading}
+ className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded disabled:bg-blue-300"
+ >
+ {isLoading ? "Loading..." : "Load Evals"}
+ </button>
+ </div>
+
+ {evals.length > 0 && (
+ <span className="text-2xl font-semibold">
+ Total: {total} out of {max} ({score}%)
+ </span>
+ )}
</div>
<div className="flex flex-col gap-y-4 mt-4 mx-auto justify-center">