Production state
This commit is contained in:
@@ -47,6 +47,9 @@ func New(cfg Config, log *slog.Logger, approval ApprovalVerifier) (*Tool, error)
|
||||
return nil, err
|
||||
}
|
||||
t := &Tool{cfg: cfg, log: log, approval: approval, store: newStore(), queue: make(chan string, cfg.QueueCapacity)}
|
||||
if _, err := t.systemPrompt(); err != nil {
|
||||
return nil, fmt.Errorf("load Comic Animator system prompt: %w", err)
|
||||
}
|
||||
t.approvalLimiter = auth.NewLimiter(5, 5*time.Minute)
|
||||
t.client = &openrouter.Client{BaseURL: cfg.OpenRouterBaseURL, APIKey: cfg.OpenRouterAPIKey, SiteURL: cfg.OpenRouterSiteURL, AppName: cfg.OpenRouterAppName, HTTP: &http.Client{Timeout: cfg.HTTPTimeout}}
|
||||
sm := http.NewServeMux()
|
||||
@@ -84,10 +87,25 @@ func id(prefix string) string {
|
||||
}
|
||||
func claims(r *http.Request) auth.Claims { c, _ := auth.ClaimsFrom(r); return c }
|
||||
|
||||
const systemPrompt = `You prepare image-to-video prompts for complete comic-book pages. Treat the student's description as the source of truth. Create one concise, provider-ready image-to-video prompt. Preserve the full page composition, fixed panel borders, captions, lettering, speech bubbles, dialogue, character identity, clothing, colors, art style, and backgrounds. Do not crop, zoom, pan, rotate, reframe, or let anything cross panels unless explicitly requested. Animate only described actions with restrained secondary movement. Return JSON only: {"video_prompt":"..."}`
|
||||
const maxSystemPromptBytes = 64 << 10
|
||||
|
||||
func (t *Tool) systemPrompt() (string, error) {
|
||||
b, err := os.ReadFile(t.cfg.PromptFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(b) == 0 || len(b) > maxSystemPromptBytes {
|
||||
return "", fmt.Errorf("system prompt must contain between 1 and %d bytes", maxSystemPromptBytes)
|
||||
}
|
||||
prompt := strings.TrimSpace(string(b))
|
||||
if prompt == "" {
|
||||
return "", fmt.Errorf("system prompt is empty")
|
||||
}
|
||||
return prompt, nil
|
||||
}
|
||||
|
||||
func (t *Tool) page(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, toolPage, map[string]any{"CSRF": claims(r).CSRFToken, "Duration": t.cfg.VideoDuration, "AspectRatio": t.cfg.VideoAspectRatio})
|
||||
render(w, toolPage, map[string]any{"CSRF": claims(r).CSRFToken, "Duration": t.cfg.VideoDuration})
|
||||
}
|
||||
func (t *Tool) upload(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, t.cfg.MaxUploadBytes+1<<20)
|
||||
@@ -152,7 +170,7 @@ func (t *Tool) upload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
u := Upload{ID: uid, SessionID: claims(r).SessionID, Path: path, Name: filepath.Base(h.Filename), MIMEType: mt, Size: n, CreatedAt: time.Now().UTC()}
|
||||
t.store.putUpload(u)
|
||||
fmt.Fprintf(w, `<div class="preview"><img src="uploads/%s/preview" alt="Uploaded comic preview"><input type="hidden" name="upload_id" value="%s"><p>%s</p></div>`, template.HTMLEscapeString(uid), template.HTMLEscapeString(uid), template.HTMLEscapeString(u.Name))
|
||||
fmt.Fprintf(w, `<div id="upload-preview" class="preview"><img src="uploads/%s/preview" alt="Uploaded comic preview"><input type="hidden" name="upload_id" value="%s"><p title="%s">%s</p></div>`, template.HTMLEscapeString(uid), template.HTMLEscapeString(uid), template.HTMLEscapeString(u.Name), template.HTMLEscapeString(u.Name))
|
||||
}
|
||||
|
||||
func validateWebP(head []byte) error {
|
||||
@@ -201,6 +219,12 @@ func (t *Tool) prompt(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
dataURL := "data:" + u.MIMEType + ";base64," + base64.StdEncoding.EncodeToString(b)
|
||||
systemPrompt, err := t.systemPrompt()
|
||||
if err != nil {
|
||||
t.log.Error("could not load system prompt", "path", t.cfg.PromptFile, "error", err)
|
||||
fragmentError(w, 500, "The prompt configuration could not be loaded.")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), t.cfg.HTTPTimeout)
|
||||
defer cancel()
|
||||
result, err := t.client.Prompt(ctx, t.cfg.PromptModel, desc, dataURL, systemPrompt)
|
||||
@@ -213,7 +237,7 @@ func (t *Tool) prompt(w http.ResponseWriter, r *http.Request) {
|
||||
fragmentError(w, 502, "Generated prompt was too long.")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, `<label for="reviewed_prompt">Editable video prompt</label><textarea id="reviewed_prompt" name="reviewed_prompt" rows="10" maxlength="12000" required>%s</textarea><small>%d characters</small>`, template.HTMLEscapeString(result), len([]rune(result)))
|
||||
fmt.Fprintf(w, `<label for="reviewed_prompt">Video prompt</label><textarea class="textarea textarea-bordered" id="reviewed_prompt" name="reviewed_prompt" form="comic-form" rows="10" maxlength="12000" required>%s</textarea><small>%d characters</small>`, template.HTMLEscapeString(result), len([]rune(result)))
|
||||
}
|
||||
func (t *Tool) submit(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 128<<10)
|
||||
@@ -229,13 +253,12 @@ func (t *Tool) submit(w http.ResponseWriter, r *http.Request) {
|
||||
u, err := t.ownedUpload(r, r.FormValue("upload_id"))
|
||||
prompt := strings.TrimSpace(r.FormValue("reviewed_prompt"))
|
||||
duration, errDuration := strconv.Atoi(r.FormValue("duration"))
|
||||
aspect := r.FormValue("aspect_ratio")
|
||||
if err != nil || prompt == "" || len(prompt) > 12000 || errDuration != nil || duration != t.cfg.VideoDuration || aspect != t.cfg.VideoAspectRatio {
|
||||
if err != nil || prompt == "" || len(prompt) > 12000 || errDuration != nil || duration != t.cfg.VideoDuration {
|
||||
fragmentError(w, 400, "Check the image, prompt, and settings.")
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
g := Generation{ID: id("gen_"), SessionID: claims(r).SessionID, UploadID: u.ID, OriginalName: u.Name, ReviewedPrompt: prompt, Status: Queued, Duration: duration, Resolution: t.cfg.VideoResolution, AspectRatio: aspect, CreatedAt: now, UpdatedAt: now}
|
||||
g := Generation{ID: id("gen_"), SessionID: claims(r).SessionID, UploadID: u.ID, OriginalName: u.Name, ReviewedPrompt: prompt, Status: Queued, Duration: duration, Resolution: t.cfg.VideoResolution, CreatedAt: now, UpdatedAt: now}
|
||||
t.store.putGeneration(g)
|
||||
select {
|
||||
case t.queue <- g.ID:
|
||||
|
||||
Reference in New Issue
Block a user