-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zjzjzjzj1874 <[email protected]>
- Loading branch information
1 parent
2db6e5d
commit a8976cc
Showing
8 changed files
with
265 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
chatgpt | ||
.idea | ||
cmd/gptx | ||
cmd/gptx | ||
|
||
cmd/*.mp3 | ||
cmd/*.mp4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package audio | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultModel = "whisper-1" // 默认模型 | ||
) | ||
|
||
var ( | ||
file string // 文件 | ||
model string // gpt模型 | ||
prompt string // 提示 | ||
language string // Supplying the input language in ISO-639-1 format will improve accuracy and latency. link-at:https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | ||
) | ||
|
||
func init() { | ||
Cmd.AddCommand(transCmd) | ||
Cmd.AddCommand(transcCmd) | ||
} | ||
|
||
var ( | ||
Cmd = &cobra.Command{ | ||
Use: "audio", | ||
Short: "turn audio into text.", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package audio | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/zjzjzjzj1874/chatgpt/pkg" | ||
) | ||
|
||
func init() { | ||
transcCmd.Flags().StringVarP(&file, "file", "f", "", "The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.") | ||
transcCmd.Flags().StringVarP(&model, "model", "m", defaultModel, "ID of the model to use. Only whisper-1 is currently available.") | ||
transcCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.") | ||
transcCmd.Flags().StringVarP(&language, "language", "l", "", "The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.") | ||
transcCmd.MarkFlagsRequiredTogether("file") | ||
} | ||
|
||
var ( | ||
transcCmd = &cobra.Command{ | ||
Use: "transc", | ||
Short: "Transcribes audio into the input language.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(file) == 0 { | ||
color.Red("%s", "Please input your file") | ||
return | ||
} | ||
fi, err := os.Open(file) | ||
if err != nil { | ||
color.Red("Open file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
|
||
body := new(bytes.Buffer) | ||
writer := multipart.NewWriter(body) | ||
part, err := writer.CreateFormFile("file", file) | ||
if err != nil { | ||
color.Red("CreateFormFile file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
_, err = io.Copy(part, fi) | ||
if err != nil { | ||
color.Red("Copy file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
if len(model) != 0 { | ||
_ = writer.WriteField("model", model) | ||
} | ||
if len(prompt) != 0 { | ||
_ = writer.WriteField("prompt", prompt) | ||
} | ||
if len(language) != 0 { | ||
_ = writer.WriteField("language", language) | ||
} | ||
_ = writer.Close() | ||
var ( | ||
resp pkg.AudioTranslationResponse | ||
) | ||
|
||
client, err := pkg.NewClient(pkg.WithMethod(http.MethodPost), pkg.WithContentType(writer.FormDataContentType()), pkg.WithUrl(pkg.AUDIO_TRANSCRIPTION_URL), pkg.WithBody(body)) | ||
if err != nil { | ||
color.Red("New Client Err:%s", err.Error()) | ||
return | ||
} | ||
|
||
err = client.Send(&resp) | ||
if err != nil { | ||
color.Red("Send Err:%s", err.Error()) | ||
return | ||
} | ||
|
||
color.Cyan("转录结果:%s", resp.Text) | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package audio | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/zjzjzjzj1874/chatgpt/pkg" | ||
) | ||
|
||
func init() { | ||
transCmd.Flags().StringVarP(&file, "file", "f", "", "The audio file to translate, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.") | ||
transCmd.Flags().StringVarP(&model, "model", "m", defaultModel, "ID of the model to use. Only whisper-1 is currently available.") | ||
transCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "An optional text to guide the model's style or continue a previous audio segment. The prompt should be in English.") | ||
transCmd.Flags().StringVarP(&language, "language", "l", "", "The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.") | ||
transCmd.MarkFlagsRequiredTogether("file") | ||
} | ||
|
||
var ( | ||
transCmd = &cobra.Command{ | ||
Use: "trans", | ||
Short: "Translates audio into into text.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(file) == 0 { | ||
color.Red("%s", "Please input your file") | ||
return | ||
} | ||
fi, err := os.Open(file) | ||
if err != nil { | ||
color.Red("Open file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
|
||
body := new(bytes.Buffer) | ||
writer := multipart.NewWriter(body) | ||
part, err := writer.CreateFormFile("file", file) | ||
if err != nil { | ||
color.Red("CreateFormFile file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
_, err = io.Copy(part, fi) | ||
if err != nil { | ||
color.Red("Copy file(%s) failure:%s", file, err.Error()) | ||
return | ||
} | ||
if len(model) != 0 { | ||
_ = writer.WriteField("model", model) | ||
} | ||
if len(prompt) != 0 { | ||
_ = writer.WriteField("prompt", prompt) | ||
} | ||
if len(language) != 0 { | ||
_ = writer.WriteField("language", language) | ||
} | ||
_ = writer.Close() | ||
var ( | ||
resp pkg.AudioTranslationResponse | ||
) | ||
|
||
client, err := pkg.NewClient(pkg.WithMethod(http.MethodPost), pkg.WithContentType(writer.FormDataContentType()), pkg.WithUrl(pkg.AUDIO_TRANSLATION_URL), pkg.WithBody(body)) | ||
if err != nil { | ||
color.Red("New Client Err:%s", err.Error()) | ||
return | ||
} | ||
|
||
err = client.Send(&resp) | ||
if err != nil { | ||
color.Red("Send Chat Err:%s", err.Error()) | ||
return | ||
} | ||
|
||
color.Cyan("翻译结果:%s", resp.Text) | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters