|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/labstack/echo/v4" |
| 15 | + "github.com/mudler/xlog" |
| 16 | + |
| 17 | + "github.com/mudler/LocalAI/core/backend" |
| 18 | + "github.com/mudler/LocalAI/core/config" |
| 19 | + "github.com/mudler/LocalAI/core/http/middleware" |
| 20 | + "github.com/mudler/LocalAI/core/schema" |
| 21 | + model "github.com/mudler/LocalAI/pkg/model" |
| 22 | +) |
| 23 | + |
| 24 | +// UpscaleEndpoint handles POST /v1/images/upscale |
| 25 | +// |
| 26 | +// @Summary Image upscaling |
| 27 | +// @Description Upscale an image using a specified model (e.g. stable-diffusion-x4-upscaler). Accepts multipart/form-data. |
| 28 | +// @Tags images |
| 29 | +// @Accept multipart/form-data |
| 30 | +// @Produce application/json |
| 31 | +// @Param model formData string true "Upscaler model identifier (e.g. stable-diffusion-x4-upscaler)" |
| 32 | +// @Param image formData file true "Input image file" |
| 33 | +// @Param scale formData int false "Upscale factor: 2 or 4 (default 2)" |
| 34 | +// @Success 200 {object} schema.OpenAIResponse |
| 35 | +// @Failure 400 {object} map[string]string |
| 36 | +// @Failure 500 {object} map[string]string |
| 37 | +// @Router /v1/images/upscale [post] |
| 38 | +func UpscaleEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { |
| 39 | + return func(c echo.Context) error { |
| 40 | + modelName := c.FormValue("model") |
| 41 | + scaleStr := c.FormValue("scale") |
| 42 | + |
| 43 | + if modelName == "" { |
| 44 | + xlog.Error("Upscale Endpoint - missing model") |
| 45 | + return echo.NewHTTPError(http.StatusBadRequest, "missing model") |
| 46 | + } |
| 47 | + |
| 48 | + scale := 2 |
| 49 | + if scaleStr != "" { |
| 50 | + v, err := strconv.Atoi(scaleStr) |
| 51 | + if err != nil || (v != 2 && v != 4) { |
| 52 | + return echo.NewHTTPError(http.StatusBadRequest, "scale must be 2 or 4") |
| 53 | + } |
| 54 | + scale = v |
| 55 | + } |
| 56 | + |
| 57 | + // Read uploaded image |
| 58 | + imageFile, err := c.FormFile("image") |
| 59 | + if err != nil { |
| 60 | + xlog.Error("Upscale Endpoint - missing image file", "error", err) |
| 61 | + return echo.NewHTTPError(http.StatusBadRequest, "missing image file") |
| 62 | + } |
| 63 | + |
| 64 | + imgSrc, err := imageFile.Open() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + defer imgSrc.Close() |
| 69 | + imgBytes, err := io.ReadAll(imgSrc) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + // Get model config from middleware context |
| 75 | + cfg, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig) |
| 76 | + if !ok || cfg == nil { |
| 77 | + xlog.Error("Upscale Endpoint - model config not found in context") |
| 78 | + return echo.ErrBadRequest |
| 79 | + } |
| 80 | + |
| 81 | + tmpDir := filepath.Join(appConfig.GeneratedContentDir, "images") |
| 82 | + if err := os.MkdirAll(tmpDir, 0750); err != nil { |
| 83 | + return echo.NewHTTPError(http.StatusInternalServerError, "failed to prepare storage") |
| 84 | + } |
| 85 | + |
| 86 | + // Write input image to a temp file |
| 87 | + srcTmp, err := os.CreateTemp(tmpDir, "upscale_src_") |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if _, err := srcTmp.Write(imgBytes); err != nil { |
| 92 | + _ = srcTmp.Close() |
| 93 | + _ = os.Remove(srcTmp.Name()) |
| 94 | + return err |
| 95 | + } |
| 96 | + if err := srcTmp.Close(); err != nil { |
| 97 | + xlog.Warn("Upscale Endpoint - failed to close src temp file", "error", err) |
| 98 | + } |
| 99 | + srcPath := srcTmp.Name() |
| 100 | + defer os.Remove(srcPath) |
| 101 | + |
| 102 | + // Prepare output file path |
| 103 | + id := uuid.New().String() |
| 104 | + dstPath := filepath.Join(tmpDir, fmt.Sprintf("upscale_%s.png", id)) |
| 105 | + |
| 106 | + fn, err := backend.ImageUpscaleFunc(c.Request().Context(), srcPath, dstPath, scale, ml, *cfg, appConfig) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if err := fn(); err != nil { |
| 111 | + _ = os.Remove(dstPath) |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + baseURL := middleware.BaseURL(c) |
| 116 | + imgURL, err := url.JoinPath(baseURL, "generated-images", filepath.Base(dstPath)) |
| 117 | + if err != nil { |
| 118 | + _ = os.Remove(dstPath) |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + created := int(time.Now().Unix()) |
| 123 | + resp := &schema.OpenAIResponse{ |
| 124 | + ID: id, |
| 125 | + Created: created, |
| 126 | + Data: []schema.Item{{URL: imgURL}}, |
| 127 | + Usage: &schema.OpenAIUsage{ |
| 128 | + InputTokensDetails: &schema.InputTokensDetails{}, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + return c.JSON(http.StatusOK, resp) |
| 133 | + } |
| 134 | +} |
0 commit comments