Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT][API] Create an avatarization batch job #67

Open
youen opened this issue Mar 31, 2024 · 0 comments
Open

[FEAT][API] Create an avatarization batch job #67

youen opened this issue Mar 31, 2024 · 0 comments

Comments

@youen
Copy link
Collaborator

youen commented Mar 31, 2024

Description

Create an avatarization batch job that can be used to create multiple avatars at once.

Request

Method

POST

URL

/jobs/avatarization_batch

Request Parameters

None

Request Body

{
  "kind": "avatarization_batch",
  "parameters": {
    "target_image_paths": ["/tmp/target_image.jpg"],
    "avatar_configs": [
      {
        "avatar_id": "avatar_1",
        "width": 512,
        "height": 512,
        "background_color": "#FFFFFF",
        "style": "male_style_1"
      }
    ]
  }
}

Result

Result parameters

None

Result Body

{
  "id": "job-id",
  "kind": "avatarization_batch",
  "created_at": "2023-03-08T14:32:10.123456Z",
  "status": "pending",
  "error_message": null,
  "traceback": null,
  "result": null,
  "parameters": {
    "target_image_paths": ["/tmp/target_image.jpg"],
    "avatar_configs": [
      {
        "avatar_id": "avatar_1",
        "width": 512,
        "height": 512,
        "background_color": "#FFFFFF",
        "style": "male_style_1"
      }
    ]
  },
  "current_progress": null
}

Example Curl Request

Request

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"kind": "avatarization_batch", "parameters": {"target_image_paths": ["/tmp/target_image.jpg"], "avatar_configs": [{"avatar_id": "avatar_1", "width": 512, "height": 512, "background_color": "#FFFFFF", "style": "male_style_1"}]}}' \
  http://localhost:8080/jobs/avatarization_batch

Curl Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "job-id",
  "kind": "avatarization_batch",
  "created_at": "2023-03-08T14:32:10.123456Z",
  "status": "pending",
  "error_message": null,
  "traceback": null,
  "result": null,
  "parameters": {
    "target_image_paths": ["/tmp/target_image.jpg"],
    "avatar_configs": [
      {
        "avatar_id": "avatar_1",
        "width": 512,
        "height": 512,
        "background_color": "#FFFFFF",
        "style": "male_style_1"
      }
    ]
  },
  "current_progress": null
}

Go server

Struct

import (
	"github.com/octopize/avatar-api-golang/pkg/avatars"
	"github.com/octopize/avatar-api-golang/pkg/models"
)

// AvatarizationBatchJobCreateRequest is the request body for creating an avatarization batch job.
type AvatarizationBatchJobCreateRequest struct {
	Kind   models.JobKind   `json:"kind"`
	Params avatars.AvatarizationBatchParameters `json:"parameters"`
}

Handler

// createAvatarizationBatchJob creates an avatarization batch job.
func (s *Server) createAvatarizationBatchJob(w http.ResponseWriter, r *http.Request) {
	var req AvatarizationBatchJobCreateRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if req.Kind != models.JobKind.AvatarizationBatch {
		http.Error(w, "Invalid job kind", http.StatusBadRequest)
		return
	}

	job, err := s.avatarService.CreateAvatarizationBatchJob(req.Params)
	if err != nil {
		http.Error(w, "Failed to create job", http.StatusInternalServerError)
		return
	}

	json.NewEncoder(w).Encode(job)
}
```progress": {
    "total": 100,
    "completed": 100
  }
}

Go server

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/octopize/avatar-go/avatars"
	"github.com/octopize/avatar-go/avatars/api"
	"github.com/octopize/avatar-go/avatars/models"
	"github.com/rs/zerolog/log"
)

// AvatarizationBatchJobCreate godoc
// @Summary Create an avatarization batch job.
// @Description Create an avatarization batch job.
// @Tags jobs
// @Accept json
// @Produce json
// @Param request body AvatarizationBatchJobCreate true "The request body."
// @Success 201 {object} AvatarizationBatchJob
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /jobs/avatarization_batch [post]
func (s *Server) AvatarizationBatchJobCreate(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var req models.AvatarizationBatchJobCreate
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		log.Ctx(ctx).Error().Err(err).Msg("failed to decode request body")
		api.WriteError(w, http.StatusBadRequest, "invalid request body")
		return
	}

	job, err := s.JobService.CreateAvatarizationBatchJob(ctx, req.Parameters)
	if err != nil {
		log.Ctx(ctx).Error().Err(err).Msg("failed to create job")
		api.WriteError(w, http.StatusInternalServerError, "failed to create job")
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(job); err != nil {
		log.Ctx(ctx).Error().Err(err).Msg("failed to encode response")
	}
}

Links

Automated Issue Details

Dear visitor,

This issue has been automatically generated from the Octopize project avatar-python to make SIGO compatible. Please vote with a thumbs up or thumbs down to assess the quality of the automatic generation.

Best regards,
The SIGO Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant