Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

feat: add cover generation config

wulienda160a25b436
diff --git a/config/config-example.toml b/config/config-example.toml
index 46d6a57c..8a1bde11 100644
--- a/config/config-example.toml
+++ b/config/config-example.toml
@@ -60,4 +60,11 @@
[tts.aliyun.speech]
access_key_id = ""
access_key_secret = ""
- app_key= ""
\ No newline at end of file
+ app_key= ""
+
+[image]
+ provider = "openai-compatible"
+ [image.openai]
+ base_url = ""
+ api_key = ""
+ model = "gpt-image-1"
diff --git a/config/config.go b/config/config.go
index 1f25a0da..af99ec22 100644
--- a/config/config.go
+++ b/config/config.go
@@ -83,6 +83,11 @@ type Tts struct {
Aliyun AliyunTtsConfig `toml:"aliyun"`
}
+type Image struct {
+ Provider string `toml:"provider"`
+ Openai OpenaiCompatibleConfig `toml:"openai"`
+}
+
type OpenAiWhisper struct {
BaseUrl string `toml:"base_url"`
ApiKey string `toml:"api_key"`
@@ -94,6 +99,7 @@ type Config struct {
Llm OpenaiCompatibleConfig `toml:"llm"`
Transcribe Transcribe `toml:"transcribe"`
Tts Tts `toml:"tts"`
+ Image Image `toml:"image"`
}
var Conf = Config{
@@ -136,6 +142,12 @@ var Conf = Config{
Model: "gpt-4o-mini-tts",
},
},
+ Image: Image{
+ Provider: "openai-compatible",
+ Openai: OpenaiCompatibleConfig{
+ Model: "gpt-image-1",
+ },
+ },
}
// 检查必要的配置是否完整
diff --git a/config/config_test.go b/config/config_test.go
new file mode 100644
index 00000000..8fbd9f1d
--- /dev/null
+++ b/config/config_test.go
@@ -0,0 +1,12 @@
+package config
+
+import "testing"
+
+func TestDefaultImageConfig(t *testing.T) {
+ if Conf.Image.Provider != "openai-compatible" {
+ t.Fatalf("Image.Provider = %q, want openai-compatible", Conf.Image.Provider)
+ }
+ if Conf.Image.Openai.Model == "" {
+ t.Fatalf("Image.Openai.Model is empty")
+ }
+}
diff --git a/internal/pipeline/cover.go b/internal/pipeline/cover.go
new file mode 100644
index 00000000..36e108c1
--- /dev/null
+++ b/internal/pipeline/cover.go
@@ -0,0 +1,22 @@
+package pipeline
+
+import "strings"
+
+type CoverPromptData struct {
+ Title string
+ Description string
+ OriginLanguage string
+ TargetLanguage string
+ StyleHint string
+}
+
+func RenderCoverPrompt(tmpl string, data CoverPromptData) string {
+ replacer := strings.NewReplacer(
+ "{{title}}", data.Title,
+ "{{description}}", data.Description,
+ "{{origin_language}}", data.OriginLanguage,
+ "{{target_language}}", data.TargetLanguage,
+ "{{style_hint}}", data.StyleHint,
+ )
+ return replacer.Replace(tmpl)
+}
diff --git a/internal/pipeline/cover_test.go b/internal/pipeline/cover_test.go
new file mode 100644
index 00000000..d0b8f404
--- /dev/null
+++ b/internal/pipeline/cover_test.go
@@ -0,0 +1,16 @@
+package pipeline
+
+import "testing"
+
+func TestRenderCoverPrompt(t *testing.T) {
+ template := "{{title}}\n{{target_language}}\n{{style_hint}}"
+ got := RenderCoverPrompt(template, CoverPromptData{
+ Title: "原始标题",
+ TargetLanguage: "zh_cn",
+ StyleHint: "Bilibili 科技封面",
+ })
+ want := "原始标题\nzh_cn\nBilibili 科技封面"
+ if got != want {
+ t.Fatalf("RenderCoverPrompt() = %q, want %q", got, want)
+ }
+}
diff --git a/pkg/image/openai_compatible.go b/pkg/image/openai_compatible.go
new file mode 100644
index 00000000..eb26fd9f
--- /dev/null
+++ b/pkg/image/openai_compatible.go
@@ -0,0 +1,90 @@
+package image
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+)
+
+const defaultOpenAIImageBaseURL = "https://api.openai.com/v1"
+
+type GenerateRequest struct {
+ Prompt string
+ Size string
+}
+
+type GenerateResult struct {
+ B64JSON string
+}
+
+type OpenAICompatibleClient struct {
+ baseURL string
+ apiKey string
+ model string
+ httpClient *http.Client
+}
+
+func NewOpenAICompatibleClient(baseURL, apiKey, model string) *OpenAICompatibleClient {
+ if baseURL == "" {
+ baseURL = defaultOpenAIImageBaseURL
+ }
+ return &OpenAICompatibleClient{
+ baseURL: strings.TrimRight(baseURL, "/"),
+ apiKey: apiKey,
+ model: model,
+ httpClient: http.DefaultClient,
+ }
+}
+
+func (c *OpenAICompatibleClient) Generate(ctx context.Context, req GenerateRequest) (GenerateResult, error) {
+ size := req.Size
+ if size == "" {
+ size = "1024x1024"
+ }
+ body := map[string]string{
+ "model": c.model,
+ "prompt": req.Prompt,
+ "size": size,
+ "response_format": "b64_json",
+ }
+ data, err := json.Marshal(body)
+ if err != nil {
+ return GenerateResult{}, err
+ }
+ httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/images/generations", bytes.NewReader(data))
+ if err != nil {
+ return GenerateResult{}, err
+ }
+ httpReq.Header.Set("Content-Type", "application/json")
+ if c.apiKey != "" {
+ httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
+ }
+ resp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ return GenerateResult{}, err
+ }
+ defer resp.Body.Close()
+ respBody, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return GenerateResult{}, err
+ }
+ if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
+ return GenerateResult{}, fmt.Errorf("image generation failed: status %d: %s", resp.StatusCode, string(respBody))
+ }
+ var parsed struct {
+ Data []struct {
+ B64JSON string `json:"b64_json"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(respBody, &parsed); err != nil {
+ return GenerateResult{}, err
+ }
+ if len(parsed.Data) == 0 || parsed.Data[0].B64JSON == "" {
+ return GenerateResult{}, fmt.Errorf("image generation response missing b64_json")
+ }
+ return GenerateResult{B64JSON: parsed.Data[0].B64JSON}, nil
+}
diff --git a/pkg/image/openai_compatible_test.go b/pkg/image/openai_compatible_test.go
new file mode 100644
index 00000000..e9bd2003
--- /dev/null
+++ b/pkg/image/openai_compatible_test.go
@@ -0,0 +1,64 @@
+package image
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestOpenAICompatibleGenerateSendsJSONRequest(t *testing.T) {
+ var gotPath string
+ var gotAuth string
+ var gotBody map[string]any
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ gotPath = r.URL.Path
+ gotAuth = r.Header.Get("Authorization")
+ if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
+ t.Fatalf("decode request: %v", err)
+ }
+ _, _ = w.Write([]byte(`{"data":[{"b64_json":"abc123"}]}`))
+ }))
+ defer server.Close()
+
+ client := NewOpenAICompatibleClient(server.URL, "test-key", "gpt-image-1")
+ out, err := client.Generate(context.Background(), GenerateRequest{
+ Prompt: "bilibili cover",
+ Size: "1024x1024",
+ })
+ if err != nil {
+ t.Fatalf("Generate() error = %v", err)
+ }
+ if out.B64JSON != "abc123" {
+ t.Fatalf("B64JSON = %q, want abc123", out.B64JSON)
+ }
+ if gotPath != "/images/generations" {
+ t.Fatalf("path = %q", gotPath)
+ }
+ if gotAuth != "Bearer test-key" {
+ t.Fatalf("Authorization = %q", gotAuth)
+ }
+ if gotBody["model"] != "gpt-image-1" {
+ t.Fatalf("model = %v", gotBody["model"])
+ }
+ if gotBody["prompt"] != "bilibili cover" {
+ t.Fatalf("prompt = %v", gotBody["prompt"])
+ }
+ if gotBody["response_format"] != "b64_json" {
+ t.Fatalf("response_format = %v", gotBody["response_format"])
+ }
+}
+
+func TestOpenAICompatibleGenerateReturnsHTTPError(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ }))
+ defer server.Close()
+
+ client := NewOpenAICompatibleClient(server.URL, "test-key", "gpt-image-1")
+ _, err := client.Generate(context.Background(), GenerateRequest{Prompt: "cover"})
+ if err == nil {
+ t.Fatalf("Generate() error = nil, want error")
+ }
+}