-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
140 lines (125 loc) · 3.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/k8scat/flux-api/api"
"github.com/k8scat/flux-api/translate"
"github.com/sashabaranov/go-openai"
)
func translatePrompt(prompt string) string {
enable := strings.ToLower(os.Getenv("TRANSLATE_ENABLE"))
if enable == "true" {
translator := translate.NewOpenAITranslator(
os.Getenv("TRANSLATE_API_BASE"),
os.Getenv("TRANSLATE_API_KEY"),
os.Getenv("TRANSLATE_MODEL"),
os.Getenv("TRANSLATE_PROMPT_TEMPLATE"),
)
var err error
prompt, err = translator.Translate(prompt)
if err != nil {
log.Printf("failed to translate prompt: %v", err)
return prompt
}
log.Printf("translated prompt: %s", prompt)
}
return prompt
}
func createImage(c *gin.Context, obj any) *openai.ImageResponse {
if err := c.BindJSON(&obj); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return nil
}
var req *openai.ImageRequest
switch obj.(type) {
case *openai.ImageRequest:
req = obj.(*openai.ImageRequest)
case *openai.ChatCompletionRequest:
tmp := obj.(*openai.ChatCompletionRequest)
if len(tmp.Messages) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return nil
}
req = &openai.ImageRequest{
Model: tmp.Model,
Prompt: tmp.Messages[len(tmp.Messages)-1].Content,
}
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return nil
}
authHeader := c.GetHeader("Authorization")
auth := strings.TrimSpace(strings.TrimPrefix(authHeader, "Bearer "))
if auth == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid auth"})
return nil
}
var client api.OpenAI
if req.Model == "FLUX.1-schnell" {
client = api.NewSiliconFlow(auth)
} else if req.Model == "flux-v1" {
client = api.NewGetimgAI(auth)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid model"})
return nil
}
req.Prompt = translatePrompt(req.Prompt)
resp, err := client.CreateImage(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return nil
}
return resp
}
func main() {
r := gin.Default()
r.POST("/v1/images/generations", func(c *gin.Context) {
var req openai.ImageRequest
resp := createImage(c, &req)
if resp != nil {
c.JSON(http.StatusOK, resp)
}
})
r.POST("/v1/chat/completions", func(c *gin.Context) {
var req openai.ChatCompletionRequest
resp := createImage(c, &req)
if resp == nil {
return
}
url := resp.Data[0].URL
now := time.Now().Unix()
ret := gin.H{
"id": fmt.Sprintf("chatcmpl-%d", now),
"object": "chat.completion",
"created": now,
"model": req.Model,
"choices": []gin.H{
{
"index": 0,
"message": gin.H{
"role": "assistant",
"content": fmt.Sprintf("", url, url),
},
"logprobs": nil,
"finish_reason": "stop",
},
},
"usage": gin.H{
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
},
}
if req.Stream {
c.SSEvent("image", ret)
} else {
c.JSON(http.StatusOK, ret)
}
})
r.Run()
}