handleTranscription handles /v1/audio/transcriptions and /v1/audio/translations.
(c *gin.Context, isTranslation bool)
| 2055 | |
| 2056 | // handleTranscription handles /v1/audio/transcriptions and /v1/audio/translations. |
| 2057 | func (h *Handler) handleTranscription(c *gin.Context, isTranslation bool) { |
| 2058 | contentType := strings.Split(c.GetHeader("Content-Type"), ";")[0] |
| 2059 | contentType = strings.ToLower(strings.TrimSpace(contentType)) |
| 2060 | if !strings.HasPrefix(contentType, "multipart/form-data") { |
| 2061 | c.JSON(400, gin.H{"error": gin.H{ |
| 2062 | "message": "Request must be multipart/form-data", |
| 2063 | "type": "invalid_request_error", |
| 2064 | "param": "Content-Type", |
| 2065 | "code": "invalid_content_type", |
| 2066 | }}) |
| 2067 | return |
| 2068 | } |
| 2069 | |
| 2070 | if err := c.Request.ParseMultipartForm(50 << 20); err != nil { |
| 2071 | c.JSON(400, gin.H{"error": gin.H{ |
| 2072 | "message": "Failed to parse multipart form: " + err.Error(), |
| 2073 | "type": "invalid_request_error", |
| 2074 | "param": nil, |
| 2075 | "code": "parse_error", |
| 2076 | }}) |
| 2077 | return |
| 2078 | } |
| 2079 | |
| 2080 | model := strings.TrimSpace(c.Request.FormValue("model")) |
| 2081 | language := strings.TrimSpace(c.Request.FormValue("language")) |
| 2082 | prompt := c.Request.FormValue("prompt") |
| 2083 | responseFormat := strings.TrimSpace(c.Request.FormValue("response_format")) |
| 2084 | |
| 2085 | if model == "" { |
| 2086 | model = "whisper-1" |
| 2087 | } |
| 2088 | if responseFormat == "" { |
| 2089 | responseFormat = "json" |
| 2090 | } |
| 2091 | |
| 2092 | respContentType, formatOK := transcriptionsSupportedFormats[responseFormat] |
| 2093 | if !formatOK { |
| 2094 | c.JSON(400, gin.H{"error": gin.H{ |
| 2095 | "message": fmt.Sprintf("Unsupported response_format: %s. Supported values: json, text, verbose_json", responseFormat), |
| 2096 | "type": "invalid_request_error", |
| 2097 | "param": "response_format", |
| 2098 | "code": "invalid_response_format", |
| 2099 | }}) |
| 2100 | return |
| 2101 | } |
| 2102 | |
| 2103 | if len(prompt) > 1000 { |
| 2104 | prompt = prompt[:1000] |
| 2105 | } |
| 2106 | |
| 2107 | formFile, fileHeader, err := c.Request.FormFile("file") |
| 2108 | if err != nil { |
| 2109 | c.JSON(400, gin.H{"error": gin.H{ |
| 2110 | "message": "Missing required multipart field: file", |
| 2111 | "type": "invalid_request_error", |
| 2112 | "param": "file", |
| 2113 | "code": "missing_required_parameter", |
| 2114 | }}) |
no test coverage detected