Skip to content

Commit

Permalink
add stt partial simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
xqdoo00o committed Jun 9, 2024
1 parent 273c4b5 commit 5e42ed6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
39 changes: 39 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,42 @@ func tts(c *gin.Context) {
}
chatgpt.RemoveConversation(&secret, deviceId, convId, proxy_url)
}

func stt(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
println(err.Error())
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must has proper file",
"type": "invalid_request_error",
"param": nil,
"code": err.Error(),
}})
return
}
defer file.Close()
lang := c.Request.FormValue("language")

account, secret := getSecret()
if account == "" {
c.JSON(500, gin.H{"error": "Logined user only"})
return
}
var proxy_url string
if len(proxies) == 0 {
proxy_url = ""
} else {
proxy_url = proxies[0]
// Push used proxy to the back of the list
proxies = append(proxies[1:], proxies[0])
}
var deviceId = generateUUID(account)
chatgpt.SetOAICookie(deviceId)

data := chatgpt.GetSTT(file, header, lang, &secret, deviceId, proxy_url)
if data != nil {
c.Data(200, "application/json", data)
} else {
c.JSON(500, gin.H{"error": "transcribe error"})
}
}
55 changes: 55 additions & 0 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
chatgpt_types "freechatgpt/typings/chatgpt"
"io"
"math/rand"
"mime/multipart"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -603,6 +604,60 @@ func GetTTS(secret *tokens.Secret, deviceId string, url string, proxy string) []
return blob
}

func generateRandomString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, n)
for i := range bytes {
bytes[i] = letters[rand.Intn(len(letters))]
}
return string(bytes)
}

func GetSTT(file multipart.File, header *multipart.FileHeader, lang string, secret *tokens.Secret, deviceId string, proxy string) []byte {
if proxy != "" {
client.SetProxy(proxy)
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
boundary := "----WebKitFormBoundary" + generateRandomString(16)
w.SetBoundary(boundary)
part, err := w.CreatePart(header.Header)
if err != nil {
return nil
}
_, err = io.Copy(part, file)
if err != nil {
return nil
}
if lang != "" {
part, err := w.CreateFormField("language")
if err != nil {
return nil
}
part.Write([]byte(lang))
}
w.Close()
request, err := newRequest(http.MethodPost, "https://chatgpt.com/backend-api/transcribe", &b, secret, deviceId)
request.Header.Set("Content-Type", "multipart/form-data; boundary="+w.Boundary())
if err != nil {
return nil
}
response, err := client.Do(request)
if err != nil {
return nil
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil
}
body, err := io.ReadAll(response.Body)
if err != nil {
return nil
}
return body
}

func RemoveConversation(secret *tokens.Secret, deviceId string, id string, proxy string) {
if proxy != "" {
client.SetProxy(proxy)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func main() {
router.POST("/v1/chat/completions", Authorization, nightmare)
router.OPTIONS("/v1/audio/speech", optionsHandler)
router.POST("/v1/audio/speech", Authorization, tts)
router.OPTIONS("/v1/audio/transcriptions", optionsHandler)
router.POST("/v1/audio/transcriptions", Authorization, stt)
router.OPTIONS("/v1/models", optionsHandler)
router.GET("/v1/models", Authorization, simulateModel)
endless.ListenAndServe(HOST+":"+PORT, router)
Expand Down

0 comments on commit 5e42ed6

Please sign in to comment.