A page that makes pictures, beside the chat
/images offers the models that make pictures, and the chat no longer offers them. Like the chat, the browser holds a session and never a key; the page's request is rebuilt from its own fields and sent down the path every /v1/images/generations request takes. Size, how many, seed, steps and a negative prompt; each picture shown with its seed, a download link and a way to make it again. Nothing is kept but the usage. The menu offers the page while some model makes pictures. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4a4fe8fa16
commit
62db7c5586
14 files changed
+456
-6
No files matched your search
@@ -56,6 +56,7 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) {
|
||||
func(w http.ResponseWriter, req *http.Request, _ store.User) {
|
||||
web.ReplyRenderer(r.cfg.MaxBodyBytes)(w, req)
|
||||
}))
|
||||
r.registerImagesPage(mux, pages)
|
||||
}
|
||||
|
||||
// chatSend dispatches one turn on behalf of a signed-in person.
|
||||
@@ -256,6 +257,12 @@ func (r *Router) principalFor(req *http.Request, user store.User, asked string)
|
||||
// by name, "big-model" came first and every visitor who pressed Send without
|
||||
// touching the picker got placeholder text.
|
||||
func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel {
|
||||
return r.modelsOf(req, user, false)
|
||||
}
|
||||
|
||||
// modelsOf is the models a person may use from a page: those that make
|
||||
// pictures, or those that write text.
|
||||
func (r *Router) modelsOf(req *http.Request, user store.User, pictures bool) []web.ChatModel {
|
||||
principal, err := r.principalFor(req, user, req.URL.Query().Get("context"))
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -283,7 +290,7 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel {
|
||||
continue
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
if r.permits(principal, name) {
|
||||
if entry.MakesImages() == pictures && r.permits(principal, name) {
|
||||
out = append(out, web.ChatModel{Name: name, Simulated: entry.Simulated(), Hardware: hardware[name]})
|
||||
}
|
||||
}
|
||||
@@ -299,7 +306,7 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel {
|
||||
continue
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
if r.permits(principal, name) {
|
||||
if entry.MakesImages() == pictures && r.permits(principal, name) {
|
||||
out = append(out, web.ChatModel{
|
||||
Name: name, Simulated: entry.Simulated(), OnDemand: true,
|
||||
})
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/comfy"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/proxy"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/version"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/web"
|
||||
)
|
||||
|
||||
// /v1/images/generations, OpenAI's shape, made by ComfyUI: the request goes
|
||||
@@ -199,3 +205,82 @@ func (r *Router) generateImages(ctx context.Context, w http.ResponseWriter, req
|
||||
func ctxCauseIsClient(ctx context.Context) bool {
|
||||
return !errors.Is(context.Cause(ctx), context.DeadlineExceeded)
|
||||
}
|
||||
|
||||
// registerImagesPage mounts the page that makes pictures, beside the chat
|
||||
// and for the same reasons: a model tried without a client, with the
|
||||
// browser holding a session and never a key, and the request sent down the
|
||||
// one path every API request takes.
|
||||
func (r *Router) registerImagesPage(mux *http.ServeMux, pages *web.AccountPages) {
|
||||
pages.SetImages(func() bool {
|
||||
for _, m := range r.modelMap() {
|
||||
if m.MakesImages() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
mux.HandleFunc("GET /images", pages.Guard(func(w http.ResponseWriter, req *http.Request, user store.User) {
|
||||
contexts, _ := r.chatContexts(req.Context(), user)
|
||||
current, err := r.pickContext(req.Context(), user, req.URL.Query().Get("context"))
|
||||
if err != nil {
|
||||
current = ""
|
||||
}
|
||||
if current == "" && !r.personalOff {
|
||||
current = personalContext
|
||||
}
|
||||
web.Images(user, r.modelsOf(req, user, true), contexts, current, version.Version)(w, req)
|
||||
}))
|
||||
mux.HandleFunc("POST /images/generate", pages.Guard(r.imagesSend))
|
||||
}
|
||||
|
||||
// imagesSend makes pictures on behalf of a signed-in person.
|
||||
func (r *Router) imagesSend(w http.ResponseWriter, req *http.Request, user store.User) {
|
||||
body, err := io.ReadAll(io.LimitReader(req.Body, r.cfg.MaxBodyBytes))
|
||||
if err != nil {
|
||||
http.Error(w, "could not read the request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
Size string `json:"size"`
|
||||
N int `json:"n"`
|
||||
Seed *int64 `json:"seed"`
|
||||
Steps int `json:"steps"`
|
||||
NegativePrompt string `json:"negative_prompt"`
|
||||
// Context is what the pictures are charged to, checked as the
|
||||
// chat's is.
|
||||
Context string `json:"context"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &in); err != nil || in.Model == "" {
|
||||
http.Error(w, "a model and a prompt are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
principal, err := r.principalFor(req, user, in.Context)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
// Rebuilt from the fields the page has, so it can set nothing else.
|
||||
out := map[string]any{"model": in.Model, "prompt": in.Prompt, "size": in.Size, "n": in.N}
|
||||
if in.Seed != nil {
|
||||
out["seed"] = *in.Seed
|
||||
}
|
||||
if in.Steps > 0 {
|
||||
out["steps"] = in.Steps
|
||||
}
|
||||
if s := strings.TrimSpace(in.NegativePrompt); s != "" {
|
||||
out["negative_prompt"] = s
|
||||
}
|
||||
upstream, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
http.Error(w, "could not build the request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
next := req.Clone(auth.WithPrincipal(req.Context(), principal))
|
||||
next.Body = io.NopCloser(bytes.NewReader(upstream))
|
||||
next.ContentLength = int64(len(upstream))
|
||||
next.Header.Set("Content-Type", "application/json")
|
||||
next.Header.Del(knowledgeHeader)
|
||||
r.handleInference(imagesPath)(w, next)
|
||||
}
|
||||
@@ -227,3 +227,44 @@ func TestPicturesAreChargedByThePictureAndRecorded(t *testing.T) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestThePicturesPageOffersPictureModelsAndTheChatTheOthers(t *testing.T) {
|
||||
e := newFakeEngine(t, "chat")
|
||||
rt, _, _ := imageRouterWith(t, func(cfg *config.Server) {
|
||||
cfg.Auth.Enabled = true
|
||||
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
|
||||
})
|
||||
rt.SetEndpoints(t.Context(), append(rt.reg.Infos(), endpoint.Info{ID: "c", URL: e.srv.URL, Models: []string{"chat"}}))
|
||||
org, _ := rt.store.CreateOrg(t.Context(), "acme")
|
||||
team, _ := rt.store.CreateTeam(t.Context(), org, "design")
|
||||
project, err := rt.store.CreateProject(t.Context(), team, "posters")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := store.User{ID: "usr-1", ProjectID: project}
|
||||
names := func(pictures bool) []string {
|
||||
var out []string
|
||||
for _, m := range rt.modelsOf(httptest.NewRequest("GET", "/", nil), user, pictures) {
|
||||
out = append(out, m.Name)
|
||||
}
|
||||
return out
|
||||
}
|
||||
if got := names(false); len(got) != 1 || got[0] != "chat" {
|
||||
t.Errorf("the chat offers %v", got)
|
||||
}
|
||||
if got := names(true); len(got) != 1 || got[0] != "flux" {
|
||||
t.Errorf("the pictures page offers %v", got)
|
||||
}
|
||||
|
||||
// The page's request is rebuilt: a field it has no business sending
|
||||
// does not reach the model.
|
||||
body := `{"model":"flux","prompt":"a kite","size":"512x512","n":1,"seed":3,"response_format":"url"}`
|
||||
rec := httptest.NewRecorder()
|
||||
rt.imagesSend(rec, httptest.NewRequest("POST", "/images/generate", strings.NewReader(body)), user)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "b64_json") {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec.Header().Get("x-llmgw-seed") != "3" {
|
||||
t.Errorf("seed %q", rec.Header().Get("x-llmgw-seed"))
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,10 @@ type AccountPages struct {
|
||||
// with it: the policies in force, whose judges the mirror page serves.
|
||||
hasGuard bool
|
||||
// hasSettings is set when /admin/settings is registered.
|
||||
hasSettings bool
|
||||
hasSettings bool
|
||||
// images says whether some model makes pictures; nil when the
|
||||
// pictures page is not mounted.
|
||||
images func() bool
|
||||
guardLibrary func() guard.Config
|
||||
// guardFor names the guardrail policies in force for someone, and
|
||||
// whether they keep nothing; nil when there are none.
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
type imagesData struct {
|
||||
Nav Nav
|
||||
Contexts []ChatContext
|
||||
Context string
|
||||
User store.User
|
||||
Models []ChatModel
|
||||
CSRF string
|
||||
Version string
|
||||
Now string
|
||||
}
|
||||
|
||||
// Images renders the page that makes pictures, for one signed-in person.
|
||||
// Its models are those that make them, as the chat's are those that write.
|
||||
func Images(user store.User, models []ChatModel, contexts []ChatContext, current, version string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
render(w, r, "images", imagesData{
|
||||
Nav: navFrom(r, "images"),
|
||||
Contexts: contexts, Context: current,
|
||||
User: user, Models: models, CSRF: csrfFor(r),
|
||||
Version: version, Now: time.Now().UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// SetImages has the menu offer the pictures page while some model makes
|
||||
// them. Asked on every page, so it must be cheap: the catalog, not a request.
|
||||
func (p *AccountPages) SetImages(available func() bool) { p.images = available }
|
||||
@@ -0,0 +1,36 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
func TestThePicturesPageDrawsInEveryLanguage(t *testing.T) {
|
||||
for _, lang := range []string{"en", "tr"} {
|
||||
req := httptest.NewRequest("GET", "/images", nil)
|
||||
req.Header.Set("Accept-Language", lang)
|
||||
rec := httptest.NewRecorder()
|
||||
Images(store.User{Email: "a@b.c"}, []ChatModel{{Name: "flux-schnell", Hardware: []string{"6000-24g"}}},
|
||||
[]ChatContext{{Value: "personal", Label: "Personal"}}, "personal", "v0")(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%s: %d %s", lang, rec.Code, rec.Body.String())
|
||||
}
|
||||
page := rec.Body.String()
|
||||
for _, want := range []string{`<option value="flux-schnell">`, `id="prompt"`, `/images/generate`} {
|
||||
if !strings.Contains(page, want) {
|
||||
t.Errorf("%s: %s missing", lang, want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(page, "images.") {
|
||||
t.Errorf("%s: an untranslated key is on the page", lang)
|
||||
}
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
Images(store.User{}, nil, nil, "", "v0")(rec, httptest.NewRequest("GET", "/images", nil))
|
||||
if !strings.Contains(rec.Body.String(), `class="empty"`) {
|
||||
t.Error("no models, and the page does not say so")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"images.title": "Pictures",
|
||||
"images.size": "Size",
|
||||
"images.count": "How many",
|
||||
"images.negative": "Negative prompt",
|
||||
"images.negative_hint": "what the picture should not have; FLUX schnell takes little notice of it",
|
||||
"images.seed": "Seed",
|
||||
"images.seed_hint": "the same seed and prompt make the same picture; empty is a new one each time",
|
||||
"images.steps": "Steps",
|
||||
"images.steps_hint": "more is slower and finer; empty leaves it to the model (4 for schnell, 20 for dev)",
|
||||
"images.prompt_placeholder": "Describe the picture…",
|
||||
"images.make": "Make",
|
||||
"images.kept": "Pictures stay on this page until you leave it; nothing of them is kept on the gateway. What is recorded is the usage: how many pictures, and what they cost.",
|
||||
"images.empty": "No model you may use makes pictures right now.",
|
||||
"images.empty_why": "Either the cluster has no picture model, or your project's allowlist leaves it out. A model that makes pictures is a ComfyUI server in the catalog (<code>api: comfyui</code>).",
|
||||
"images.js.making": "Making… {s} s",
|
||||
"images.js.made": "{n} made in {s} s.",
|
||||
"images.js.failed": "Not made: {why}",
|
||||
"images.js.download": "download",
|
||||
"images.js.reuse_seed": "use this seed",
|
||||
"images.js.seed": "seed {seed}"
|
||||
}
|
||||
@@ -11,5 +11,6 @@
|
||||
"nav.account": "Account",
|
||||
"nav.sign_out": "Sign out",
|
||||
"nav.docs": "Docs",
|
||||
"nav.settings": "Settings"
|
||||
"nav.settings": "Settings",
|
||||
"nav.images": "Pictures"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"images.title": "Görseller",
|
||||
"images.size": "Boyut",
|
||||
"images.count": "Adet",
|
||||
"images.negative": "Negatif istem",
|
||||
"images.negative_hint": "görselde olmaması gerekenler; FLUX schnell buna pek kulak asmaz",
|
||||
"images.seed": "Tohum",
|
||||
"images.seed_hint": "aynı tohum ve istem aynı görseli üretir; boş bırakılırsa her seferinde yenisi",
|
||||
"images.steps": "Adım",
|
||||
"images.steps_hint": "fazlası daha yavaş ve daha ince; boşsa modele bırakılır (schnell için 4, dev için 20)",
|
||||
"images.prompt_placeholder": "Görseli tarif edin…",
|
||||
"images.make": "Üret",
|
||||
"images.kept": "Görseller siz sayfadan ayrılana kadar burada kalır; ağ geçidinde onlardan hiçbir şey saklanmaz. Kaydedilen kullanımdır: kaç görsel üretildiği ve maliyeti.",
|
||||
"images.empty": "Şu anda kullanabileceğiniz, görsel üreten bir model yok.",
|
||||
"images.empty_why": "Ya kümede görsel modeli yok ya da projenizin izin listesi onu içermiyor. Görsel üreten model, katalogda bir ComfyUI sunucusudur (<code>api: comfyui</code>).",
|
||||
"images.js.making": "Üretiliyor… {s} sn",
|
||||
"images.js.made": "{n} görsel {s} sn'de üretildi.",
|
||||
"images.js.failed": "Üretilemedi: {why}",
|
||||
"images.js.download": "indir",
|
||||
"images.js.reuse_seed": "bu tohumu kullan",
|
||||
"images.js.seed": "tohum {seed}"
|
||||
}
|
||||
@@ -11,5 +11,6 @@
|
||||
"nav.account": "Hesap",
|
||||
"nav.sign_out": "Çıkış yap",
|
||||
"nav.docs": "Belgeler",
|
||||
"nav.settings": "Ayarlar"
|
||||
"nav.settings": "Ayarlar",
|
||||
"nav.images": "Görseller"
|
||||
}
|
||||
+5
-1
@@ -15,7 +15,8 @@ import (
|
||||
// between different programs.
|
||||
type Nav struct {
|
||||
// Current is the page being shown, so its link is marked rather than
|
||||
// offered: "cluster", "people", "quotas", "usage", "mirror", "chat", "keys".
|
||||
// offered: "cluster", "people", "quotas", "usage", "mirror", "chat", "images",
|
||||
// "keys".
|
||||
Current string
|
||||
// Admin shows the administration links, and Role which of them: a
|
||||
// billing role sees usage and quotas only.
|
||||
@@ -34,6 +35,8 @@ type Nav struct {
|
||||
// Org offers the organization page: to an administrator, and to anyone
|
||||
// who manages an organization.
|
||||
Org bool
|
||||
// Images offers the pictures page, while some model makes them.
|
||||
Images bool
|
||||
}
|
||||
|
||||
type navKey struct{}
|
||||
@@ -49,6 +52,7 @@ func (p *AccountPages) navFor(r *http.Request, user store.User, current string)
|
||||
Guard: p.hasGuard,
|
||||
Settings: p.hasSettings && user.IsAdmin,
|
||||
CSRF: csrfFor(r),
|
||||
Images: p.images != nil && p.images(),
|
||||
}
|
||||
// Only when there is an organization to show: a link that leads to "there
|
||||
// are none" on every page is noise.
|
||||
|
||||
@@ -915,3 +915,15 @@ a.sel-link.small { font-size: .78rem; color: var(--muted); margin-left: .4rem; }
|
||||
.settings-form label { display: block; margin: .8rem 0; }
|
||||
.settings-form input[type=text] { width: 100%; box-sizing: border-box; }
|
||||
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
||||
|
||||
/* The pictures page: the chat's bar and composer, and a gallery under them. */
|
||||
.images-num { width: 12rem; font: inherit; padding: .3rem .45rem; margin-left: .4rem;
|
||||
border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--fg); }
|
||||
#status.error { color: var(--bad, #b42318); }
|
||||
.images-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 18rem), 1fr));
|
||||
gap: 1rem; margin-top: 1rem; }
|
||||
.images-gallery figure { margin: 0; border: 1px solid var(--line); border-radius: 8px; background: var(--card);
|
||||
overflow: hidden; }
|
||||
.images-gallery img { display: block; width: 100%; height: auto; }
|
||||
.images-gallery figcaption { font-size: .8rem; color: var(--muted); padding: .45rem .6rem 0; }
|
||||
.images-prompt { font-size: .82rem; margin: .25rem .6rem .55rem; overflow-wrap: anywhere; }
|
||||
@@ -0,0 +1,176 @@
|
||||
{{define "images"}}<!doctype html>
|
||||
<html lang="{{lang}}"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>{{t "images.title"}} · llmgw</title><link rel="stylesheet" href="{{stylesheet}}">
|
||||
<script defer src="/_static/pages.js"></script>
|
||||
<meta name="csrf" content="{{.CSRF}}">
|
||||
</head><body><div class="wrap">
|
||||
<header>
|
||||
<h1>{{t "images.title"}}</h1>
|
||||
<p class="sub">{{.User.Email}}</p>
|
||||
{{template "nav" .Nav}}
|
||||
</header>
|
||||
|
||||
{{if gt (len .Contexts) 1}}
|
||||
<form method="get" action="/images" class="chat-context">
|
||||
<label class="inline-label">{{t "chat.context.using"}}
|
||||
<select id="context" name="context" data-autosubmit>
|
||||
{{range .Contexts}}<option value="{{.Value}}"{{if eq .Value $.Context}} selected{{end}}>{{.Label}}</option>{{end}}
|
||||
</select></label>
|
||||
<noscript translate="no"><button type="submit" class="link">{{t "chat.context.switch"}}</button></noscript>
|
||||
</form>
|
||||
{{else if .Contexts}}{{with index .Contexts 0}}<input type="hidden" id="context" value="{{.Value}}">{{end}}{{end}}
|
||||
|
||||
{{if .Models}}
|
||||
<form id="make" class="images-form">
|
||||
<div class="chat-bar">
|
||||
<label class="inline-label">{{t "chat.model"}}
|
||||
<select id="model">{{range .Models}}<option value="{{.Name}}">{{.Name}}{{with .Runs}} · {{.}}{{end}}</option>{{end}}</select>
|
||||
</label>
|
||||
<label class="inline-label">{{t "images.size"}}
|
||||
<select id="size">
|
||||
<option value="1024x1024">1024 × 1024</option>
|
||||
<option value="1024x768">1024 × 768</option>
|
||||
<option value="768x1024">768 × 1024</option>
|
||||
<option value="1280x720">1280 × 720</option>
|
||||
<option value="768x768">768 × 768</option>
|
||||
<option value="512x512">512 × 512</option>
|
||||
</select></label>
|
||||
<label class="inline-label">{{t "images.count"}}
|
||||
<select id="n"><option>1</option><option>2</option><option>3</option><option>4</option></select></label>
|
||||
</div>
|
||||
<details class="chat-settings">
|
||||
<summary>{{t "chat.settings"}}</summary>
|
||||
<label>{{t "images.negative"}} <span class="opt">{{t "images.negative_hint"}}</span>
|
||||
<textarea id="negative" rows="2"></textarea></label>
|
||||
<label>{{t "images.seed"}} <span class="opt">{{t "images.seed_hint"}}</span>
|
||||
<input type="number" id="seed" min="0" step="1" class="images-num"></label>
|
||||
<label>{{t "images.steps"}} <span class="opt">{{t "images.steps_hint"}}</span>
|
||||
<input type="number" id="steps" min="1" max="100" step="1" class="images-num"></label>
|
||||
</details>
|
||||
<div class="chat-composer">
|
||||
<textarea id="prompt" rows="3" placeholder="{{t "images.prompt_placeholder"}}" required></textarea>
|
||||
<button type="submit" id="go">{{t "images.make"}}</button>
|
||||
</div>
|
||||
</form>
|
||||
<p id="status" class="note" aria-live="polite"></p>
|
||||
<div id="gallery" class="images-gallery"></div>
|
||||
<p class="note">{{t "images.kept"}}</p>
|
||||
{{else}}
|
||||
<p class="empty">{{t "images.empty"}}</p>
|
||||
<p class="note">{{th "images.empty_why"}}</p>
|
||||
{{end}}
|
||||
|
||||
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span>{{template "langs"}}</footer>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const say = {
|
||||
making: {{t "images.js.making"}},
|
||||
made: {{t "images.js.made"}},
|
||||
failed: {{t "images.js.failed"}},
|
||||
download: {{t "images.js.download"}},
|
||||
reuseSeed: {{t "images.js.reuse_seed"}},
|
||||
seed: {{t "images.js.seed"}},
|
||||
make: {{t "images.make"}},
|
||||
decimal: {{t "_meta.decimal"}},
|
||||
};
|
||||
function fill(s, vals) { return s.replace(/\{(\w+)\}/g, (m, k) => (k in vals ? String(vals[k]) : m)); }
|
||||
function fixed(n, places) { return n.toFixed(places).replace(".", say.decimal); }
|
||||
// One request at a time, and nothing kept: a picture is a few megabytes,
|
||||
// and this page is for trying a model, not for keeping what it made.
|
||||
(function () {
|
||||
const form = document.getElementById("make");
|
||||
if (!form) return;
|
||||
const csrf = document.querySelector('meta[name="csrf"]').content;
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const status = $("status"), gallery = $("gallery"), go = $("go");
|
||||
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const prompt = $("prompt").value.trim();
|
||||
if (!prompt || go.disabled) return;
|
||||
const body = {
|
||||
model: $("model").value, prompt, size: $("size").value, n: Number($("n").value),
|
||||
context: $("context") ? $("context").value : "",
|
||||
};
|
||||
const neg = $("negative").value.trim();
|
||||
if (neg) body.negative_prompt = neg;
|
||||
if ($("seed").value !== "") body.seed = Number($("seed").value);
|
||||
if ($("steps").value !== "") body.steps = Number($("steps").value);
|
||||
|
||||
go.disabled = true;
|
||||
status.classList.remove("error");
|
||||
const start = performance.now();
|
||||
const tick = () => { status.textContent = fill(say.making, { s: fixed((performance.now() - start) / 1000, 0) }); };
|
||||
tick();
|
||||
const timer = setInterval(tick, 1000);
|
||||
try {
|
||||
const resp = await fetch("/images/generate", {
|
||||
method: "POST", headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const text = await resp.text();
|
||||
let data = null;
|
||||
try { data = JSON.parse(text); } catch (_) {}
|
||||
if (!resp.ok || !data || !data.data) {
|
||||
const why = (data && data.error && data.error.message) || text.trim() || resp.status;
|
||||
throw new Error(String(why));
|
||||
}
|
||||
const seed = resp.headers.get("x-llmgw-seed") || "";
|
||||
const took = (performance.now() - start) / 1000;
|
||||
status.textContent = fill(say.made, { n: data.data.length, s: fixed(took, 1) });
|
||||
show(data.data, seed, body);
|
||||
} catch (err) {
|
||||
status.textContent = fill(say.failed, { why: err.message });
|
||||
status.classList.add("error");
|
||||
} finally {
|
||||
clearInterval(timer);
|
||||
go.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
function show(items, seed, body) {
|
||||
items.forEach((it, i) => {
|
||||
const src = "data:image/png;base64," + it.b64_json;
|
||||
const fig = document.createElement("figure");
|
||||
const img = document.createElement("img");
|
||||
img.src = src;
|
||||
img.alt = body.prompt;
|
||||
img.loading = "lazy";
|
||||
const cap = document.createElement("figcaption");
|
||||
const line = document.createElement("span");
|
||||
line.textContent = body.model + " · " + body.size.replace("x", " × ") +
|
||||
(seed ? " · " + fill(say.seed, { seed }) + (items.length > 1 ? " #" + (i + 1) : "") : "");
|
||||
const dl = document.createElement("a");
|
||||
dl.href = src;
|
||||
dl.download = (body.model + "-" + (seed || "x") + "-" + (i + 1) + ".png").replace(/[^\w.-]+/g, "_");
|
||||
dl.textContent = say.download;
|
||||
dl.className = "link";
|
||||
cap.append(line, " ", dl);
|
||||
if (seed) {
|
||||
const again = document.createElement("button");
|
||||
again.type = "button";
|
||||
again.className = "link";
|
||||
again.textContent = say.reuseSeed;
|
||||
again.addEventListener("click", () => {
|
||||
$("seed").value = seed;
|
||||
form.querySelector("details").open = true;
|
||||
$("prompt").focus();
|
||||
});
|
||||
cap.append(" ", again);
|
||||
}
|
||||
const p = document.createElement("p");
|
||||
p.className = "images-prompt";
|
||||
p.textContent = body.prompt;
|
||||
fig.append(img, cap, p);
|
||||
gallery.prepend(fig);
|
||||
});
|
||||
}
|
||||
|
||||
$("prompt").addEventListener("keydown", (e) => {
|
||||
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); form.requestSubmit(); }
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body></html>{{end}}
|
||||
@@ -28,6 +28,9 @@
|
||||
<span class="menu-gap" aria-hidden="true"></span>
|
||||
{{- end}}
|
||||
<a href="/chat"{{if eq .Current "chat"}} aria-current="page"{{end}}>{{t "nav.chat"}}</a>
|
||||
{{- if .Images}}
|
||||
<a href="/images"{{if eq .Current "images"}} aria-current="page"{{end}}>{{t "nav.images"}}</a>
|
||||
{{- end}}
|
||||
<a href="/profile"{{if eq .Current "keys"}} aria-current="page"{{end}}>{{t "nav.account"}}</a>
|
||||
<a href="/docs"{{if eq .Current "docs"}} aria-current="page"{{end}}>{{t "nav.docs"}}</a>
|
||||
<form method="post" action="/logout" class="inline">
|
||||
|
||||
Reference in new issue
Block a user