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,