Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pschroedl committed Jul 25, 2024
1 parent aeb257f commit 3072fcb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 73 deletions.
23 changes: 16 additions & 7 deletions runner/app/routes/text_to_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,33 @@ class HTTPError(BaseModel):
}
}

@router.post("/text-to-audio", responses=responses)
@router.post("/text-to-audio",
summary="Text to Audio",
operation_id="text_to_audio",
responses=responses)
async def TextToAudio(
text_input: Optional[str] = Form(None),
text_input: str = Form(...),
pipeline: Pipeline = Depends(get_pipeline),
):

try:
result = pipeline(
text_input
if not text_input:
raise ValueError("text_input is required and cannot be empty.")

result = pipeline(text_input)

except ValueError as ve:
logger.error(f"Validation error: {ve}")
return JSONResponse(
status_code=400,
content={"detail": str(ve)},
)

except Exception as e:
logger.error(f"TextToAudioPipeline error: {e}")
return JSONResponse(
status_code=500,
content={
"detail": f"Internal Server Error: {str(e)}"
},
content={"detail": f"Internal Server Error: {str(e)}"},
)

if os.path.exists(result):
Expand Down
1 change: 1 addition & 0 deletions runner/gen_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
image_to_video,
text_to_image,
upscale,
text_to_audio,
)
from fastapi.openapi.utils import get_openapi

Expand Down
51 changes: 31 additions & 20 deletions runner/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,21 +407,17 @@
},
"/text-to-audio": {
"post": {
"summary": "Texttoaudio",
"summary": "Text to Audio",
"operationId": "TextToAudio",
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Body_TextToAudio_text_to_audio_post"
}
],
"title": "Body"
"$ref": "#/components/schemas/Body_text_to_audio"
}
}
}
},
"required": true
},
"responses": {
"200": {
Expand Down Expand Up @@ -502,22 +498,24 @@
],
"title": "APIError"
},
"Body_TextToAudio_text_to_audio_post": {
"Body_audio_to_text_audio_to_text_post": {
"properties": {
"text_input": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Text Input"
"audio": {
"type": "string",
"format": "binary",
"title": "Audio"
},
"model_id": {
"type": "string",
"title": "Model Id",
"default": ""
}
},
"type": "object",
"title": "Body_TextToAudio_text_to_audio_post"
"required": [
"audio"
],
"title": "Body_audio_to_text_audio_to_text_post"
},
"Body_image_to_image_image_to_image_post": {
"properties": {
Expand Down Expand Up @@ -640,6 +638,19 @@
],
"title": "Body_image_to_video_image_to_video_post"
},
"Body_text_to_audio": {
"properties": {
"text_input": {
"type": "string",
"title": "Text Input"
}
},
"type": "object",
"required": [
"text_input"
],
"title": "Body_text_to_audio"
},
"Body_upscale_upscale_post": {
"properties": {
"prompt": {
Expand Down
49 changes: 3 additions & 46 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,57 +251,14 @@ func (w *Worker) Upscale(ctx context.Context, req UpscaleMultipartRequestBody) (
}


func (w *Worker) AudioToText(ctx context.Context, req TextToAudioJSONRequestBody) (*AudioResponse, error) {
c, err := w.borrowContainer(ctx, "audio-to-text", *req.ModelId)
if err != nil {
return nil, err
}
defer w.returnContainer(c)

resp, err := c.Client.AudioToTextWithResponse(ctx, req)
if err != nil {
return nil, err
}

if resp.JSON422 != nil {
val, err := json.Marshal(resp.JSON422)
if err != nil {
return nil, err
}
slog.Error("audio-to-text container returned 422", slog.String("err", string(val)))
return nil, errors.New("audio-to-text container returned 422")
}

if resp.JSON400 != nil {
val, err := json.Marshal(resp.JSON400)
if err != nil {
return nil, err
}
slog.Error("audio-to-text container returned 400", slog.String("err", string(val)))
return nil, errors.New("audio-to-text container returned 400")
}

if resp.JSON500 != nil {
val, err := json.Marshal(resp.JSON500)
if err != nil {
return nil, err
}
slog.Error("audio-to-text container returned 500", slog.String("err", string(val)))
return nil, errors.New("audio-to-text container returned 500")
}

return resp.JSON200, nil
}


func (w *Worker) AudioToText(ctx context.Context, req TextToAudioJSONRequestBody) (*AudioResponse, error) {
func (w *Worker) TextToAudio(ctx context.Context, req TextToAudioJSONRequestBody) (*AudioResponse, error) {
c, err := w.borrowContainer(ctx, "text-to-audio", *req.ModelId)
if err != nil {
return nil, err
}
defer w.returnContainer(c)

resp, err := c.Client.AudioToTextWithResponse(ctx, req)
resp, err := c.Client.TextToAudioWithResponse(ctx, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -330,7 +287,7 @@ func (w *Worker) AudioToText(ctx context.Context, req TextToAudioJSONRequestBody
return nil, err
}
slog.Error("text-to-audio container returned 500", slog.String("err", string(val)))
return nil, errors.New("text-to-image container returned 500")
return nil, errors.New("text-to-audio container returned 500")
}

return resp.JSON200, nil
Expand Down

0 comments on commit 3072fcb

Please sign in to comment.