feat(image-to-text): clean json export to image_file and prompt only, sort queue sequentially by filename, and auto-resize images >720px on upload

This commit is contained in:
2026-10-29 13:27:57 +05:30
parent eb7b549785
commit 47c35da17e
4 changed files with 280 additions and 54 deletions
+25 -19
View File
@@ -7,6 +7,7 @@ from __future__ import annotations
import csv
import json
import re
import time
from pathlib import Path
from typing import Any, Callable, Generator, List, Optional, Union
@@ -40,7 +41,7 @@ def find_image_files(
if p.is_file() and p.suffix.lower() in valid_exts:
image_files.append(p)
image_files.sort()
image_files.sort(key=lambda p: [int(c) if c.isdigit() else c.lower() for c in re.split(r'(\d+)', p.name)])
return image_files
@@ -86,19 +87,29 @@ class ImageProcessor:
"prompt": prompt_text,
}
# Check image dimensions
# Check image dimensions & resize to 720 if greater
img_input = image_path
if isinstance(image_path, (str, Path)):
p = Path(image_path)
if not p.is_file():
raise FileNotFoundError(f"Image not found: {p}")
with Image.open(p) as img:
image_info["width"], image_info["height"] = img.size
if max(img.size) > 720:
img_copy = img.convert("RGB")
img_copy.thumbnail((720, 720), Image.Resampling.LANCZOS)
img_input = img_copy
image_info["width"], image_info["height"] = img_copy.size
elif isinstance(image_path, Image.Image):
image_info["width"], image_info["height"] = image_path.size
if max(image_path.size) > 720:
image_path.thumbnail((720, 720), Image.Resampling.LANCZOS)
image_info["width"], image_info["height"] = image_path.size
img_input = image_path
# Run inference
generated_text = self.model.generate(
image_input=image_path,
image_input=img_input,
prompt=prompt_text,
max_new_tokens=max_new_tokens,
temperature=temperature,
@@ -248,26 +259,21 @@ class ImageProcessor:
total_elapsed = time.time() - start_batch_time
# Save aggregated JSON
# Save aggregated JSON (strictly image_file and prompt only)
if save_json:
json_file = Path(save_json)
json_file.parent.mkdir(parents=True, exist_ok=True)
clean_items = [
{
"image_file": Path(item.get("file", "")).name if item.get("file") else "",
"prompt": item.get("result", "")
}
for item in results
if item.get("result")
]
with open(json_file, "w", encoding="utf-8") as f:
json.dump(
{
"total": total_images,
"processed": processed_count,
"skipped": skipped_count,
"failed": failed_count,
"mode": mode,
"elapsed_seconds": round(total_elapsed, 2),
"items": results,
},
f,
indent=2,
ensure_ascii=False,
)
print(f"[+] Aggregated JSON saved to: {json_file}")
json.dump(clean_items, f, indent=2, ensure_ascii=False)
print(f"[+] Aggregated clean JSON saved to: {json_file}")
# Save aggregated CSV
if save_csv:
+66 -5
View File
@@ -16,6 +16,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
@@ -664,8 +665,28 @@ func handleExportZip(w http.ResponseWriter, r *http.Request) {
}
}
// 2. Write dataset summary JSON
summaryBytes, err := json.MarshalIndent(jobData, "", " ")
// 2. Write dataset summary JSON (strictly image_file and prompt only)
type CleanPromptJSON struct {
ImageFile string `json:"image_file"`
Prompt string `json:"prompt"`
}
var cleanSummary []CleanPromptJSON
for _, item := range jobData.Results {
filename, _ := item["filename"].(string)
if filename == "" {
file, _ := item["file"].(string)
filename = filepath.Base(file)
}
resultText, _ := item["result"].(string)
if resultText != "" {
cleanSummary = append(cleanSummary, CleanPromptJSON{
ImageFile: filename,
Prompt: resultText,
})
}
}
summaryBytes, err := json.MarshalIndent(cleanSummary, "", " ")
if err == nil {
f, err := zipWriter.Create("dataset_summary.json")
if err == nil {
@@ -716,7 +737,22 @@ func handleExportZipDirect(w http.ResponseWriter, r *http.Request) {
}
}
summaryBytes, _ := json.MarshalIndent(req, "", " ")
// Write dataset summary JSON (strictly image_file and prompt only)
type CleanPromptJSON struct {
ImageFile string `json:"image_file"`
Prompt string `json:"prompt"`
}
var cleanSummary []CleanPromptJSON
for _, it := range req.Items {
if it.Text != "" {
cleanSummary = append(cleanSummary, CleanPromptJSON{
ImageFile: it.Filename,
Prompt: it.Text,
})
}
}
summaryBytes, _ := json.MarshalIndent(cleanSummary, "", " ")
if f, err := zipWriter.Create("dataset_summary.json"); err == nil {
_, _ = f.Write(summaryBytes)
}
@@ -785,6 +821,11 @@ func handleScanFolder(w http.ResponseWriter, r *http.Request) {
}
}
// Sort files sequentially by filename
sort.Slice(files, func(i, j int) bool {
return files[i].Name < files[j].Name
})
writeJSON(w, http.StatusOK, map[string]any{
"folder": folderPath,
"total": len(files),
@@ -800,9 +841,29 @@ func handleExportJSON(w http.ResponseWriter, r *http.Request) {
return
}
type CleanPromptJSON struct {
ImageFile string `json:"image_file"`
Prompt string `json:"prompt"`
}
var cleanResults []CleanPromptJSON
for _, item := range jobData.Results {
filename, _ := item["filename"].(string)
if filename == "" {
file, _ := item["file"].(string)
filename = filepath.Base(file)
}
resultText, _ := item["result"].(string)
if resultText != "" {
cleanResults = append(cleanResults, CleanPromptJSON{
ImageFile: filename,
Prompt: resultText,
})
}
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"batch_results_%s.json\"", jobID[:8]))
_ = json.NewEncoder(w).Encode(jobData)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"dataset_prompts_%s.json\"", jobID[:8]))
_ = json.NewEncoder(w).Encode(cleanResults)
}
func handleExportCSV(w http.ResponseWriter, r *http.Request) {
+6
View File
@@ -178,6 +178,8 @@ def generate(req: GenerateRequest):
raise HTTPException(status_code=404, detail=f"Image file not found: {req.image_path}")
try:
image = Image.open(img_path).convert("RGB")
if max(image.width, image.height) > 720:
image.thumbnail((720, 720), Image.Resampling.LANCZOS)
except Exception as exc:
raise HTTPException(status_code=400, detail=f"Failed to read image: {exc}")
elif req.image_base64:
@@ -187,6 +189,8 @@ def generate(req: GenerateRequest):
raw_data = raw_data.split(",", 1)[1]
img_bytes = base64.b64decode(raw_data)
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
if max(image.width, image.height) > 720:
image.thumbnail((720, 720), Image.Resampling.LANCZOS)
except Exception as exc:
raise HTTPException(status_code=400, detail=f"Failed to decode base64 image: {exc}")
else:
@@ -289,6 +293,8 @@ def _execute_batch_job(job_id: str, req: BatchStartRequest):
t0 = time.time()
try:
img = Image.open(img_path).convert("RGB")
if max(img.width, img.height) > 720:
img.thumbnail((720, 720), Image.Resampling.LANCZOS)
text = model.generate(
image_input=img,
prompt=effective_prompt,
+183 -30
View File
@@ -237,38 +237,172 @@
// ── Unified Intake & Queue Management ──
function addFilesToQueue(files) {
const validExts = [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tiff", ".tif", ".avif"];
let addedCount = 0;
// Helper: Naturally sort queue items sequentially by file name
function sortQueueByFilename() {
state.queue.sort((a, b) =>
a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: "base" })
);
}
for (const file of files) {
// Helper: Resize image to max 720px right when uploaded if greater than 720px
function prepareAndResizeImage(file, maxDimension = 720) {
return new Promise((resolve) => {
const validExts = [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tiff", ".tif", ".avif"];
const ext = "." + file.name.split(".").pop().toLowerCase();
if (!validExts.includes(ext) && !file.type.startsWith("image/")) continue;
if (!validExts.includes(ext) && !file.type.startsWith("image/")) {
resolve(null);
return;
}
const previewUrl = URL.createObjectURL(file);
state.queue.push({
id: "q_" + Date.now() + "_" + Math.random().toString(36).slice(2, 7),
file: file,
serverPath: null,
name: file.name,
size: file.size,
previewUrl: previewUrl,
status: "pending", // "pending" | "processing" | "done" | "failed"
result: "",
elapsed: 0,
width: 0,
height: 0,
error: ""
});
addedCount++;
const img = new Image();
const objectUrl = URL.createObjectURL(file);
img.onload = () => {
const origW = img.naturalWidth || img.width;
const origH = img.naturalHeight || img.height;
// If within 720 on both sides, keep original file untouched
if (origW <= maxDimension && origH <= maxDimension) {
resolve({
file: file,
previewUrl: objectUrl,
width: origW,
height: origH,
name: file.name,
size: file.size,
resized: false
});
return;
}
// If greater than 720, downscale preserving aspect ratio
let targetW = origW;
let targetH = origH;
if (origW >= origH) {
targetW = maxDimension;
targetH = Math.max(1, Math.round((origH * maxDimension) / origW));
} else {
targetH = maxDimension;
targetW = Math.max(1, Math.round((origW * maxDimension) / origH));
}
const canvas = document.createElement("canvas");
canvas.width = targetW;
canvas.height = targetH;
const ctx = canvas.getContext("2d", { alpha: false });
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
ctx.drawImage(img, 0, 0, targetW, targetH);
let mimeType = file.type;
if (!mimeType || mimeType === "" || mimeType.includes("bmp") || mimeType.includes("tiff")) {
mimeType = "image/jpeg";
}
canvas.toBlob(
(blob) => {
URL.revokeObjectURL(objectUrl);
if (!blob) {
resolve({
file: file,
previewUrl: URL.createObjectURL(file),
width: origW,
height: origH,
name: file.name,
size: file.size,
resized: false
});
return;
}
const resizedFile = new File([blob], file.name, {
type: blob.type || mimeType,
lastModified: Date.now()
});
const previewUrl = URL.createObjectURL(resizedFile);
resolve({
file: resizedFile,
previewUrl: previewUrl,
width: targetW,
height: targetH,
name: file.name,
size: resizedFile.size,
resized: true,
origW: origW,
origH: origH
});
},
mimeType,
0.92
);
};
img.onerror = () => {
URL.revokeObjectURL(objectUrl);
resolve(null);
};
img.src = objectUrl;
});
}
async function addFilesToQueue(files) {
if (!files || files.length === 0) return;
const totalFiles = files.length;
if (totalFiles > 12) {
showToast(`Uploading & optimizing ${totalFiles} images (auto-resizing >720px)...`, "info");
}
let addedCount = 0;
let resizedCount = 0;
// Process in concurrent batches of 6 for high performance & responsive UI
const batchSize = 6;
for (let i = 0; i < files.length; i += batchSize) {
const batch = files.slice(i, i + batchSize);
const preparedBatch = await Promise.all(batch.map((f) => prepareAndResizeImage(f, 720)));
for (const prep of preparedBatch) {
if (!prep) continue;
if (prep.resized) resizedCount++;
state.queue.push({
id: "q_" + Date.now() + "_" + Math.random().toString(36).slice(2, 7),
file: prep.file,
serverPath: null,
name: prep.name,
size: prep.size,
previewUrl: prep.previewUrl,
status: "pending", // "pending" | "processing" | "done" | "failed"
result: "",
elapsed: 0,
width: prep.width,
height: prep.height,
resized: prep.resized,
error: ""
});
addedCount++;
}
}
if (addedCount > 0) {
showToast(`Loaded ${addedCount} image(s)`, "info");
// Sort queue sequentially by filename
sortQueueByFilename();
if (resizedCount > 0) {
showToast(`Loaded ${addedCount} images (${resizedCount} resized to max 720px)`, "info");
} else {
showToast(`Loaded ${addedCount} image(s)`, "info");
}
updateQueueUI();
}
}
window.addFilesToStudio = addFilesToQueue;
window.getStudioQueue = () => state.queue;
function addServerFilesToQueue(filesList) {
let addedCount = 0;
for (const f of filesList) {
@@ -284,11 +418,14 @@
elapsed: 0,
width: 0,
height: 0,
resized: false,
error: ""
});
addedCount++;
}
if (addedCount > 0) {
// Sort queue sequentially by filename
sortQueueByFilename();
showToast(`Loaded ${addedCount} image(s) from server path`, "info");
updateQueueUI();
}
@@ -797,6 +934,10 @@
const saveSidecar = saveSidecarCb.checked;
const skipExisting = skipExistingCb.checked;
// Ensure queue is strictly sorted sequentially by filename before processing
sortQueueByFilename();
updateQueueUI();
const total = state.queue.length;
// Process queue items sequentially so user sees immediate results per item
@@ -946,12 +1087,18 @@
};
downloadTxtBtn.addEventListener("click", () => downloadSingleTxt());
// 3. Single JSON
downloadJsonBtn.addEventListener("click", () => {
const item = state.queue[state.selectedIndex];
// 3. Single JSON (Strictly image_file and prompt only)
window.downloadSingleJson = function (itemId) {
const item = state.queue.find((x) => x.id === itemId) || state.queue[state.selectedIndex];
if (!item || !item.result) return;
downloadBlob(JSON.stringify(item, null, 2), `${item.name.replace(/\.[^/.]+$/, "")}_output.json`, "application/json");
});
const stem = item.name.replace(/\.[^/.]+$/, "");
const cleanJson = {
image_file: item.name,
prompt: item.result
};
downloadBlob(JSON.stringify(cleanJson, null, 2), `${stem}.json`, "application/json");
};
downloadJsonBtn.addEventListener("click", () => downloadSingleJson());
// 4. Batch ZIP of All Sidecars (Direct Rock-solid Server Zip Generation)
downloadAllZipBtn.addEventListener("click", async () => {
@@ -1008,14 +1155,20 @@
downloadBlob(csv, `batch_results_${Date.now()}.csv`, "text/csv;charset=utf-8");
});
// 6. Batch JSON Export
// 6. Batch JSON Export (Strictly image_file and prompt only)
exportJsonBtn.addEventListener("click", () => {
const doneItems = state.queue.filter((x) => x.status === "done" || x.status === "failed");
const doneItems = state.queue.filter((x) => x.status === "done" && x.result);
if (doneItems.length === 0) {
showToast("No processed results to export", "error");
return;
}
downloadBlob(JSON.stringify(doneItems, null, 2), `batch_results_${Date.now()}.json`, "application/json");
const cleanJsonList = doneItems.map((it) => ({
image_file: it.name,
prompt: it.result
}));
downloadBlob(JSON.stringify(cleanJsonList, null, 2), `dataset_prompts_${Date.now()}.json`, "application/json");
});
// 7. Copy All Prompts