Files

1020 lines
28 KiB
Go

package main
import (
"archive/zip"
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unified-dashboard/internal/common"
mymiddleware "unified-dashboard/internal/middleware"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/google/uuid"
)
var (
baseDir string
uploadDir string
historyDir string
exportDir string
historyFile string
workerPort string
workerCmd *exec.Cmd
historyMu sync.RWMutex
httpClient = &http.Client{Timeout: 300 * time.Second}
)
type HistoryItem struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Mode string `json:"mode"`
PromptUsed string `json:"prompt_used"`
Result string `json:"result"`
ElapsedSeconds float64 `json:"elapsed_seconds"`
Width int `json:"width"`
Height int `json:"height"`
ImageFilename string `json:"image_filename"`
ImageURL string `json:"image_url"`
}
func initPaths() {
cwd, _ := os.Getwd()
baseDir = filepath.Join(cwd, "data", "image-to-text")
uploadDir = filepath.Join(baseDir, "uploads")
historyDir = filepath.Join(baseDir, "history")
exportDir = filepath.Join(baseDir, "exports")
historyFile = filepath.Join(historyDir, "history.json")
_ = os.MkdirAll(uploadDir, 0755)
_ = os.MkdirAll(historyDir, 0755)
_ = os.MkdirAll(exportDir, 0755)
}
func getFreePort() (string, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return "", err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return "", err
}
defer l.Close()
return strconv.Itoa(l.Addr().(*net.TCPAddr).Port), nil
}
func startPythonWorker() {
var err error
workerPort, err = getFreePort()
if err != nil {
log.Fatalf("Failed to allocate free port for Python worker: %v", err)
}
workDir, _ := os.Getwd()
workerScript := filepath.Join(workDir, "cmd", "image-to-text", "worker.py")
pythonBin := "/home/creserver/extras/local_venv/bin/python3"
if _, err := os.Stat(pythonBin); os.IsNotExist(err) {
pythonBin = "python3"
}
workerCmd = exec.Command(pythonBin, workerScript, "--port", workerPort)
workerCmd.Dir = workDir
workerCmd.Stdout = os.Stdout
workerCmd.Stderr = os.Stderr
// Put in independent process group so we can terminate it cleanly
workerCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
go func() {
slog.Info("Starting Python Vision worker", "port", workerPort, "script", workerScript)
if err := workerCmd.Run(); err != nil {
slog.Warn("Python worker process exited", "error", err)
}
}()
// Wait for worker readiness
workerURL := fmt.Sprintf("http://127.0.0.1:%s/health", workerPort)
ready := false
for i := 0; i < 40; i++ {
resp, err := httpClient.Get(workerURL)
if err == nil && resp.StatusCode == 200 {
resp.Body.Close()
ready = true
slog.Info("Python Vision worker is ready", "port", workerPort)
break
}
if resp != nil {
resp.Body.Close()
}
time.Sleep(500 * time.Millisecond)
}
if !ready {
slog.Error("Python Vision worker failed to start in time", "port", workerPort)
}
}
func stopPythonWorker() {
if workerCmd != nil && workerCmd.Process != nil {
slog.Info("Terminating Python Vision worker...")
pgid, err := syscall.Getpgid(workerCmd.Process.Pid)
if err == nil {
_ = syscall.Kill(-pgid, syscall.SIGTERM)
} else {
_ = workerCmd.Process.Kill()
}
}
}
func writeJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(data)
}
func readHistory() []HistoryItem {
historyMu.RLock()
defer historyMu.RUnlock()
if _, err := os.Stat(historyFile); os.IsNotExist(err) {
return []HistoryItem{}
}
data, err := os.ReadFile(historyFile)
if err != nil {
return []HistoryItem{}
}
var items []HistoryItem
if err := json.Unmarshal(data, &items); err != nil {
return []HistoryItem{}
}
return items
}
func saveHistoryItem(item HistoryItem) {
historyMu.Lock()
defer historyMu.Unlock()
var items []HistoryItem
if data, err := os.ReadFile(historyFile); err == nil {
_ = json.Unmarshal(data, &items)
}
// Prepend new item (newest first), keep max 100 items
items = append([]HistoryItem{item}, items...)
if len(items) > 100 {
items = items[:100]
}
if data, err := json.MarshalIndent(items, "", " "); err == nil {
_ = os.WriteFile(historyFile, data, 0644)
}
}
// Proxy helper for worker requests
func forwardWorkerRequest(method, path string, body io.Reader, contentType string) (*http.Response, error) {
url := fmt.Sprintf("http://127.0.0.1:%s%s", workerPort, path)
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return httpClient.Do(req)
}
func handleHealth(w http.ResponseWriter, r *http.Request) {
resp, err := forwardWorkerRequest("GET", "/health", nil, "")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
"status": "error",
"worker": "offline",
"error": err.Error(),
})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handleModelStatus(w http.ResponseWriter, r *http.Request) {
resp, err := forwardWorkerRequest("GET", "/model/status", nil, "")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "Worker unavailable: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handleModelLoad(w http.ResponseWriter, r *http.Request) {
resp, err := forwardWorkerRequest("POST", "/model/load", r.Body, "application/json")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "Worker unavailable: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handleModelUnload(w http.ResponseWriter, r *http.Request) {
resp, err := forwardWorkerRequest("POST", "/model/unload", nil, "")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "Worker unavailable: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handlePresets(w http.ResponseWriter, r *http.Request) {
resp, err := forwardWorkerRequest("GET", "/presets", nil, "")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "Worker unavailable: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handleGenerate(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var imagePath string
var imageFilename string
var imageURL string
var mode = "prompt"
var customPrompt = ""
var maxNewTokens = 512
var temperature = 0.2
var topP = 0.9
if strings.HasPrefix(contentType, "multipart/form-data") {
// Limit to 50MB
if err := r.ParseMultipartForm(50 << 20); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to parse form: " + err.Error()})
return
}
if m := r.FormValue("mode"); m != "" {
mode = m
}
if cp := r.FormValue("custom_prompt"); cp != "" {
customPrompt = cp
}
if mt := r.FormValue("max_new_tokens"); mt != "" {
if v, err := strconv.Atoi(mt); err == nil {
maxNewTokens = v
}
}
if t := r.FormValue("temperature"); t != "" {
if v, err := strconv.ParseFloat(t, 64); err == nil {
temperature = v
}
}
if tp := r.FormValue("top_p"); tp != "" {
if v, err := strconv.ParseFloat(tp, 64); err == nil {
topP = v
}
}
file, header, err := r.FormFile("image")
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Image file is required: " + err.Error()})
return
}
defer file.Close()
ext := filepath.Ext(header.Filename)
if ext == "" {
ext = ".jpg"
}
savedName := fmt.Sprintf("%s_%s%s", time.Now().Format("20060102_150405"), uuid.New().String()[:8], ext)
savedPath := filepath.Join(uploadDir, savedName)
out, err := os.Create(savedPath)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to save file: " + err.Error()})
return
}
defer out.Close()
if _, err := io.Copy(out, file); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to write file: " + err.Error()})
return
}
imagePath = savedPath
imageFilename = header.Filename
imageURL = "uploads/" + savedName
} else {
// JSON payload
var reqPayload struct {
ImagePath string `json:"image_path"`
ImageBase64 string `json:"image_base64"`
Mode string `json:"mode"`
CustomPrompt string `json:"custom_prompt"`
MaxNewTokens int `json:"max_new_tokens"`
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
}
if err := json.NewDecoder(r.Body).Decode(&reqPayload); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
return
}
imagePath = reqPayload.ImagePath
if reqPayload.Mode != "" {
mode = reqPayload.Mode
}
customPrompt = reqPayload.CustomPrompt
if reqPayload.MaxNewTokens > 0 {
maxNewTokens = reqPayload.MaxNewTokens
}
if reqPayload.Temperature >= 0 {
temperature = reqPayload.Temperature
}
if reqPayload.TopP > 0 {
topP = reqPayload.TopP
}
// If base64 provided without imagePath, save to uploadDir
if imagePath == "" && reqPayload.ImageBase64 != "" {
b64Data := reqPayload.ImageBase64
if comma := strings.Index(b64Data, ","); comma != -1 {
b64Data = b64Data[comma+1:]
}
rawBytes, err := io.ReadAll(strings.NewReader(b64Data))
if err == nil {
savedName := fmt.Sprintf("%s_%s.png", time.Now().Format("20060102_150405"), uuid.New().String()[:8])
savedPath := filepath.Join(uploadDir, savedName)
_ = os.WriteFile(savedPath, rawBytes, 0644)
imagePath = savedPath
imageFilename = savedName
imageURL = "uploads/" + savedName
}
}
}
// Prepare payload for worker
workerPayload := map[string]any{
"image_path": imagePath,
"mode": mode,
"custom_prompt": customPrompt,
"max_new_tokens": maxNewTokens,
"temperature": temperature,
"top_p": topP,
"save_sidecar": true,
}
payloadBytes, _ := json.Marshal(workerPayload)
resp, err := forwardWorkerRequest("POST", "/generate", bytes.NewReader(payloadBytes), "application/json")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Inference worker error: " + err.Error()})
return
}
defer resp.Body.Close()
var workerResp struct {
Success bool `json:"success"`
Result string `json:"result"`
ElapsedSeconds float64 `json:"elapsed_seconds"`
Mode string `json:"mode"`
PromptUsed string `json:"prompt_used"`
Width int `json:"width"`
Height int `json:"height"`
SidecarPath string `json:"sidecar_path"`
Detail string `json:"detail"`
}
if err := json.NewDecoder(resp.Body).Decode(&workerResp); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to decode worker response: " + err.Error()})
return
}
if !workerResp.Success && workerResp.Detail != "" {
writeJSON(w, resp.StatusCode, map[string]string{"error": workerResp.Detail})
return
}
// Save to history
histItem := HistoryItem{
ID: uuid.New().String(),
Timestamp: time.Now(),
Mode: workerResp.Mode,
PromptUsed: workerResp.PromptUsed,
Result: workerResp.Result,
ElapsedSeconds: workerResp.ElapsedSeconds,
Width: workerResp.Width,
Height: workerResp.Height,
ImageFilename: imageFilename,
ImageURL: imageURL,
}
saveHistoryItem(histItem)
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"result": workerResp.Result,
"elapsed_seconds": workerResp.ElapsedSeconds,
"mode": workerResp.Mode,
"prompt_used": workerResp.PromptUsed,
"width": workerResp.Width,
"height": workerResp.Height,
"image_url": imageURL,
"image_filename": imageFilename,
"sidecar_path": workerResp.SidecarPath,
})
}
func handleBatch(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var folderPath string
var imagePaths []string
var mode = "prompt"
var customPrompt = ""
var recursive = false
var sidecar = true
var skipExisting = true
var maxNewTokens = 512
var temperature = 0.2
if strings.HasPrefix(contentType, "multipart/form-data") {
// Multipart: user uploaded multiple files directly
if err := r.ParseMultipartForm(500 << 20); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to parse form: " + err.Error()})
return
}
if m := r.FormValue("mode"); m != "" {
mode = m
}
if cp := r.FormValue("custom_prompt"); cp != "" {
customPrompt = cp
}
if r.FormValue("sidecar") == "false" {
sidecar = false
}
if r.FormValue("skip_existing") == "false" {
skipExisting = false
}
if mt := r.FormValue("max_new_tokens"); mt != "" {
if v, err := strconv.Atoi(mt); err == nil {
maxNewTokens = v
}
}
if t := r.FormValue("temperature"); t != "" {
if v, err := strconv.ParseFloat(t, 64); err == nil {
temperature = v
}
}
batchID := uuid.New().String()[:8]
batchUploadDir := filepath.Join(uploadDir, "batch_"+batchID)
_ = os.MkdirAll(batchUploadDir, 0755)
files := r.MultipartForm.File["images"]
if len(files) == 0 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "No images provided"})
return
}
for _, fileHeader := range files {
f, err := fileHeader.Open()
if err != nil {
continue
}
destPath := filepath.Join(batchUploadDir, fileHeader.Filename)
out, err := os.Create(destPath)
if err == nil {
_, _ = io.Copy(out, f)
out.Close()
imagePaths = append(imagePaths, destPath)
}
f.Close()
}
} else {
// JSON request
var req struct {
FolderPath string `json:"folder_path"`
ImagePaths []string `json:"image_paths"`
Mode string `json:"mode"`
CustomPrompt string `json:"custom_prompt"`
Recursive bool `json:"recursive"`
Sidecar bool `json:"sidecar"`
SkipExisting bool `json:"skip_existing"`
MaxNewTokens int `json:"max_new_tokens"`
Temperature float64 `json:"temperature"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
return
}
folderPath = req.FolderPath
imagePaths = req.ImagePaths
if req.Mode != "" {
mode = req.Mode
}
customPrompt = req.CustomPrompt
recursive = req.Recursive
sidecar = req.Sidecar
skipExisting = req.SkipExisting
if req.MaxNewTokens > 0 {
maxNewTokens = req.MaxNewTokens
}
if req.Temperature >= 0 {
temperature = req.Temperature
}
}
workerReq := map[string]any{
"folder_path": folderPath,
"image_paths": imagePaths,
"mode": mode,
"custom_prompt": customPrompt,
"recursive": recursive,
"sidecar": sidecar,
"skip_existing": skipExisting,
"max_new_tokens": maxNewTokens,
"temperature": temperature,
}
payloadBytes, _ := json.Marshal(workerReq)
resp, err := forwardWorkerRequest("POST", "/batch/start", bytes.NewReader(payloadBytes), "application/json")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Worker error: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func handleBatchStatus(w http.ResponseWriter, r *http.Request) {
jobID := chi.URLParam(r, "job_id")
if jobID == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing job_id"})
return
}
resp, err := forwardWorkerRequest("GET", "/batch/"+jobID+"/status", nil, "")
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Worker error: " + err.Error()})
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
type BatchJobStatusResponse struct {
JobID string `json:"job_id"`
Status string `json:"status"`
Total int `json:"total"`
Processed int `json:"processed"`
Skipped int `json:"skipped"`
Failed int `json:"failed"`
ElapsedSeconds float64 `json:"elapsed_seconds"`
Results []map[string]interface{} `json:"results"`
}
func fetchBatchJobData(jobID string) (*BatchJobStatusResponse, error) {
resp, err := forwardWorkerRequest("GET", "/batch/"+jobID+"/status", nil, "")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("worker returned status %d", resp.StatusCode)
}
var data BatchJobStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}
func handleExportZip(w http.ResponseWriter, r *http.Request) {
jobID := chi.URLParam(r, "job_id")
jobData, err := fetchBatchJobData(jobID)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "Job not found or inaccessible: " + err.Error()})
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"dataset_sidecars_%s.zip\"", jobID[:8]))
zipWriter := zip.NewWriter(w)
defer zipWriter.Close()
// 1. Write individual .txt sidecars
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 == "" {
continue
}
baseName := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".txt"
f, err := zipWriter.Create(baseName)
if err == nil {
_, _ = f.Write([]byte(resultText))
}
}
// 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 {
_, _ = f.Write(summaryBytes)
}
}
}
type ExportZipDirectRequest struct {
JobName string `json:"job_name"`
Items []struct {
Filename string `json:"filename"`
Text string `json:"text"`
Status string `json:"status"`
Elapsed float64 `json:"elapsed"`
} `json:"items"`
}
func handleExportZipDirect(w http.ResponseWriter, r *http.Request) {
var req ExportZipDirectRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
return
}
jobName := req.JobName
if jobName == "" {
jobName = "vision_dataset"
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s_sidecars.zip\"", jobName))
zipWriter := zip.NewWriter(w)
defer zipWriter.Close()
for _, it := range req.Items {
if it.Text == "" {
continue
}
baseName := strings.TrimSuffix(it.Filename, filepath.Ext(it.Filename)) + ".txt"
if baseName == ".txt" {
baseName = "prompt.txt"
}
f, err := zipWriter.Create(baseName)
if err == nil {
_, _ = f.Write([]byte(it.Text))
}
}
// 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)
}
}
func handleScanFolder(w http.ResponseWriter, r *http.Request) {
folderPath := r.URL.Query().Get("path")
if folderPath == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "path query parameter is required"})
return
}
recursive := r.URL.Query().Get("recursive") == "true"
fi, err := os.Stat(folderPath)
if err != nil || !fi.IsDir() {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("Directory not found or invalid: %s", folderPath)})
return
}
validExts := map[string]bool{
".jpg": true, ".jpeg": true, ".png": true, ".webp": true,
".bmp": true, ".tiff": true, ".tif": true, ".avif": true,
}
type ScannedFile struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
}
var files []ScannedFile
if recursive {
_ = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
ext := strings.ToLower(filepath.Ext(path))
if validExts[ext] {
files = append(files, ScannedFile{
Name: filepath.Base(path),
Path: path,
Size: info.Size(),
})
}
}
return nil
})
} else {
entries, err := os.ReadDir(folderPath)
if err == nil {
for _, entry := range entries {
if !entry.IsDir() {
ext := strings.ToLower(filepath.Ext(entry.Name()))
if validExts[ext] {
info, err := entry.Info()
var size int64
if err == nil {
size = info.Size()
}
files = append(files, ScannedFile{
Name: entry.Name(),
Path: filepath.Join(folderPath, entry.Name()),
Size: size,
})
}
}
}
}
}
// 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),
"files": files,
})
}
func handleExportJSON(w http.ResponseWriter, r *http.Request) {
jobID := chi.URLParam(r, "job_id")
jobData, err := fetchBatchJobData(jobID)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "Job not found or inaccessible: " + err.Error()})
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=\"dataset_prompts_%s.json\"", jobID[:8]))
_ = json.NewEncoder(w).Encode(cleanResults)
}
func handleExportCSV(w http.ResponseWriter, r *http.Request) {
jobID := chi.URLParam(r, "job_id")
jobData, err := fetchBatchJobData(jobID)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "Job not found or inaccessible: " + err.Error()})
return
}
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"batch_results_%s.csv\"", jobID[:8]))
csvWriter := csv.NewWriter(w)
defer csvWriter.Flush()
_ = csvWriter.Write([]string{"Filename", "Status", "Prompt/Caption/OCR", "Elapsed (s)", "Width", "Height"})
for _, item := range jobData.Results {
fn, _ := item["filename"].(string)
st, _ := item["status"].(string)
res, _ := item["result"].(string)
if res == "" {
res, _ = item["error"].(string)
}
var elStr string
if el, ok := item["elapsed_seconds"].(float64); ok {
elStr = fmt.Sprintf("%.2f", el)
}
var wStr, hStr string
if width, ok := item["width"].(float64); ok {
wStr = strconv.Itoa(int(width))
}
if height, ok := item["height"].(float64); ok {
hStr = strconv.Itoa(int(height))
}
_ = csvWriter.Write([]string{fn, st, res, elStr, wStr, hStr})
}
}
func handleGetHistory(w http.ResponseWriter, r *http.Request) {
items := readHistory()
writeJSON(w, http.StatusOK, map[string]any{
"total": len(items),
"items": items,
})
}
func handleDeleteHistory(w http.ResponseWriter, r *http.Request) {
historyMu.Lock()
defer historyMu.Unlock()
_ = os.WriteFile(historyFile, []byte("[]"), 0644)
writeJSON(w, http.StatusOK, map[string]string{"message": "History cleared"})
}
func main() {
common.InitLogger("image-to-text")
initPaths()
startPythonWorker()
port := os.Getenv("PORT")
if port == "" {
port = "5011"
}
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
r.Use(mymiddleware.RequestLogger)
r.Use(middleware.Recoverer)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
}))
if scriptName := os.Getenv("SCRIPT_NAME"); scriptName != "" {
r.Use(common.PrefixMiddleware(scriptName))
}
// Static web assets
workDir, _ := os.Getwd()
webDir := filepath.Join(workDir, "web", "image-to-text")
r.Handle("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir(filepath.Join(webDir, "assets")))))
r.Get("/styles.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(webDir, "styles.css"))
})
r.Get("/app.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(webDir, "app.js"))
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(webDir, "index.html"))
})
// Uploaded media directory
r.Handle("/uploads/*", http.StripPrefix("/uploads/", http.FileServer(http.Dir(uploadDir))))
// Health and Telemetry API
r.Get("/health", handleHealth)
r.Get("/api/model/status", handleModelStatus)
r.Post("/api/model/load", handleModelLoad)
r.Post("/api/model/unload", handleModelUnload)
r.Get("/api/presets", handlePresets)
// Inference API
r.Post("/api/generate", handleGenerate)
r.Post("/api/batch", handleBatch)
r.Get("/api/batch/{job_id}/status", handleBatchStatus)
r.Get("/api/batch/{job_id}/export-zip", handleExportZip)
r.Head("/api/batch/{job_id}/export-zip", handleExportZip)
r.Get("/api/batch/{job_id}/export-json", handleExportJSON)
r.Head("/api/batch/{job_id}/export-json", handleExportJSON)
r.Get("/api/batch/{job_id}/export-csv", handleExportCSV)
r.Head("/api/batch/{job_id}/export-csv", handleExportCSV)
r.Post("/api/export-zip", handleExportZipDirect)
r.Get("/api/scan-folder", handleScanFolder)
// History API
r.Get("/api/history", handleGetHistory)
r.Delete("/api/history", handleDeleteHistory)
server := &http.Server{
Addr: ":" + port,
Handler: r,
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 300 * time.Second,
WriteTimeout: 0,
IdleTimeout: 120 * time.Second,
}
// Graceful shutdown handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
s := <-sigChan
slog.Info("Shutdown signal caught", "signal", s)
stopPythonWorker()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
os.Exit(0)
}()
slog.Info("Image-to-Text Studio starting", "addr", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("Server error", "error", err)
}
}