Jump to content

Recommended Posts

Hello,
I am needing help to build a UDF that uses an API, this API is about fakeyou.com, which has text to speech features using artificial intelligence (AI) I have read the API documentation on the page (https:// docs.fakeyou.com/#/) and have found some UDFs (not in AutoIt) that use this API.
I found a python UDF and had the idea to write it in autoit using json.au3 library and WinHttp object.
I hope you can help me, here is the code that I was able to do. In fact, in it is the python file.
Thank you!
 

#include "json.au3"
;API for FakeYou.com in Autoit (work in progress).
;Original API writen in pithon at: https://github.com/mmattDonk/AI-TTS-Donations/blob/main/API/fakeyou.py
func _get_job($sText, $sVoice_Name)
#cs
Get the FakeYou Voice job.
param $sText: The text to be spoken.
param $sVoice_name: The name of the voice to be used.
return: The job.
#ce
;Alternative for this? (python)
;uuid_str = str(uuid4())
;autoit:
$uuid_str = "uuid4"
local $hObj, $sParams, $uuid, $detail
$hObj = __POSTObjCreate()
If $hObj = -1 Then Return 0
$hObj.Open("POST", "https://api.fakeyou.com/tts/inference", False)
$hObj.WaitForResponse()
$sParams = 'json={"inference_text": ' &$sText &',' &@lf & _
'"tts_model_token": ' &$sVoice_name &',' &@lf & _
'"uuid_idempotency_token": ' &$uuid_str &',' &@lf & _
"},"
$uuid = false
$detail = false
$detail = _JSON_Get($sParams, "success")
if $detail = False then
$detail = "That voice does not exist"
EndIf
$uuid = _JSON_Get($sParams, "inference_job_token"]
$hObj.Send($sParams)
$hObj.WaitForResponse()
$Response = $hObj.ResponseText
msgbox(0, "Debug", $Response)
return '{"detail": ' &$detail &',' &@lf & _
'"uuid": ' &$uuid &',' &@lf & _
"}"
EndFunc
;Get winHttp object:
Func __POSTObjCreate()
Local $o_lHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Return SetError(@error, 0, ((IsObj($o_lHTTP) = 1) ? $o_lHTTP : -1))
EndFunc   ;==>__POSTObjCreate

 

Edited by Mateocedillo
Link to post
Share on other sites
  • Mateocedillo changed the title to I need help to build an UDF

Hello. here is an example. You still need to download the Audio and maybe create some extra functions.

#include <WinAPICom.au3>
#include "Json\Json.au3"
#include <Array.au3>
Global Const $HTTP_STATUS_OK = 200



_TestFakeAPI()


Func _TestFakeAPI()
;~  Local $aVoices = _FakeYouGetVoicesList()
;~  _ArrayDisplay($aVoices)

;~  Local $aVoicesCategories = _FakeYouGetVoicesCategoriesList()
;~  _ArrayDisplay($aVoicesCategories)

;~  Local $sInferenceJobToken = _FakeYouGenerateAudio("Hello World","TM:7wbtjphx8h8v")
;~  ConsoleWrite($sInferenceJobToken & @CRLF)

;~  Local $sJsonStatus=_FakeYouGetAudioStatus("JTINF:2mjzmgfph3g912cpp3rpnf9czm")
;~  ConsoleWrite($sJsonStatus & @CRLF)

EndFunc   ;==>_TestFakeAPI


Func _FakeYouWaitForAudioComplete()

EndFunc


Func _FakeYouGetAudioStatus($sInferenceJobToken)
    Return __HttpGet("https://api.fakeyou.com/tts/job/" & $sInferenceJobToken)
EndFunc

Func _FakeYouGenerateAudio($sText,$sTSSModelToken, $sUUID = Default)
    If $sUUID = Default Then $sUUID = __GenerateUUID()
    ConsoleWrite($sUUID & @CRLF)
    Local $oJson=Null
    Json_Put($oJson, ".tts_model_token", $sTSSModelToken)
    Json_Put($oJson, ".uuid_idempotency_token", $sUUID)
    Json_Put($oJson, ".inference_text", $sText)

    Local $sJson=Json_Encode($oJson)
    ConsoleWrite($sJson & @CRLF)

    Local $sJsonReturn=__HttpPost("https://api.fakeyou.com/tts/inference",$sJson)
    ConsoleWrite($sJsonReturn & @CRLF)
    $oJson = Json_Decode($sJsonReturn)
    If $sJsonReturn = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Return  Json_Get($oJson, '["inference_job_token"]')
EndFunc   ;==>_FakeYouGenerateAudio


Func __GenerateUUID()
    Return StringLower(StringReplace(StringReplace(_WinAPI_CreateGUID(), "{", ""), "}", ""))
EndFunc   ;==>__GenerateUUID

Func _FakeYouGetVoicesCategoriesList()
    Local $sJson = __HttpGet("https://api.fakeyou.com/category/list/tts")
;~  ConsoleWrite($sJson & @CRLF)
    Local $oJson = Json_Decode($sJson)
    If $sJson = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Local $aoJson = Json_Get($oJson, '["categories"]')
    If Not IsArray($aoJson) Then Return SetError(2, 0, "")
    Local $aVoicesCategories[UBound($aoJson)][12]
    Local $aMembers[] = ["category_token", "model_type", "maybe_super_category_token", "can_directly_have_models", "can_have_subcategories", _
            "can_only_mods_apply", "name", "name_for_dropdown", "is_mod_approved", "created_at", _
            "updated_at", "deleted_at"]
    For $i = 0 To UBound($aoJson) - 1
        For $x = 0 To UBound($aMembers) - 1
            $aVoicesCategories[$i][$x] = Json_Get($aoJson[$i], '["' & $aMembers[$x] & '"]')
        Next
    Next
    Return $aVoicesCategories
EndFunc   ;==>_FakeYouGetVoicesCategoriesList

Func _FakeYouGetVoicesList()
    Local $sJson = __HttpGet("https://api.fakeyou.com/tts/list")
    ConsoleWrite($sJson & @CRLF)
    Local $oJson = Json_Decode($sJson)
    If $sJson = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Local $aoJson = Json_Get($oJson, '["models"]')
    If Not IsArray($aoJson) Then Return SetError(2, 0, "")

    Local $aVoices[UBound($aoJson)][15]
    Local $aMembers[] = ["model_token", "tts_model_type", "creator_user_token", "creator_username", "creator_display_name", _
            "creator_gravatar_hash", "title", "ietf_language_tag", "ietf_primary_language_subtag", "is_front_page_featured", _
            "is_twitch_featured", "maybe_suggested_unique_bot_command", "category_tokens", "created_at", "updated_at"]

    Local $sMemberValue = ""
    For $i = 0 To UBound($aoJson) - 1
        For $x = 0 To UBound($aMembers) - 1
            $sMemberValue = Json_Get($aoJson[$i], '["' & $aMembers[$x] & '"]')
            If $aMembers[$x] = "category_tokens" Then
                For $n = 0 To UBound($sMemberValue) - 1
                    $aVoices[$i][$x] &= Json_Get($sMemberValue, '[' & $n & ']') & (($n < UBound($sMemberValue) - 1) ? "|" : "")
                Next
            Else
                $aVoices[$i][$x] = $sMemberValue
            EndIf
        Next
    Next
    Return $aVoices
EndFunc   ;==>_FakeYouGetVoicesList


Func __FakeYou_IsSuccess(ByRef $oJson)
    Return Json_ObjExists($oJson, 'success')
EndFunc   ;==>__FakeYou_IsSuccess


Func __HttpGet($sURL, $sData = "")
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("GET", $sURL & "?" & $sData, False)
    If (@error) Then Return SetError(1, 0, 0)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.Send()
    If (@error) Then Return SetError(2, 0, 0)
    If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0)
    Return SetError(0, 0, $oHTTP.ResponseText)
EndFunc   ;==>__HttpGet

Func __HttpPost($sURL, $sData = "")
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("POST", $sURL, False)
    If (@error) Then Return SetError(1, 0, 0)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.Send($sData)
    If (@error) Then Return SetError(2, 0, 0)
    If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0)
    Return SetError(0, 0, $oHTTP.ResponseText)
EndFunc   ;==>__HttpPost

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

 

Saludos

Link to post
Share on other sites
  • 3 weeks later...
On 5/4/2022 at 7:25 PM, Danyfirex said:

Hello. here is an example. You still need to download the Audio and maybe create some extra functions.

#include <WinAPICom.au3>
#include "Json\Json.au3"
#include <Array.au3>
Global Const $HTTP_STATUS_OK = 200



_TestFakeAPI()


Func _TestFakeAPI()
;~  Local $aVoices = _FakeYouGetVoicesList()
;~  _ArrayDisplay($aVoices)

;~  Local $aVoicesCategories = _FakeYouGetVoicesCategoriesList()
;~  _ArrayDisplay($aVoicesCategories)

;~  Local $sInferenceJobToken = _FakeYouGenerateAudio("Hello World","TM:7wbtjphx8h8v")
;~  ConsoleWrite($sInferenceJobToken & @CRLF)

;~  Local $sJsonStatus=_FakeYouGetAudioStatus("JTINF:2mjzmgfph3g912cpp3rpnf9czm")
;~  ConsoleWrite($sJsonStatus & @CRLF)

EndFunc   ;==>_TestFakeAPI


Func _FakeYouWaitForAudioComplete()

EndFunc


Func _FakeYouGetAudioStatus($sInferenceJobToken)
    Return __HttpGet("https://api.fakeyou.com/tts/job/" & $sInferenceJobToken)
EndFunc

Func _FakeYouGenerateAudio($sText,$sTSSModelToken, $sUUID = Default)
    If $sUUID = Default Then $sUUID = __GenerateUUID()
    ConsoleWrite($sUUID & @CRLF)
    Local $oJson=Null
    Json_Put($oJson, ".tts_model_token", $sTSSModelToken)
    Json_Put($oJson, ".uuid_idempotency_token", $sUUID)
    Json_Put($oJson, ".inference_text", $sText)

    Local $sJson=Json_Encode($oJson)
    ConsoleWrite($sJson & @CRLF)

    Local $sJsonReturn=__HttpPost("https://api.fakeyou.com/tts/inference",$sJson)
    ConsoleWrite($sJsonReturn & @CRLF)
    $oJson = Json_Decode($sJsonReturn)
    If $sJsonReturn = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Return  Json_Get($oJson, '["inference_job_token"]')
EndFunc   ;==>_FakeYouGenerateAudio


Func __GenerateUUID()
    Return StringLower(StringReplace(StringReplace(_WinAPI_CreateGUID(), "{", ""), "}", ""))
EndFunc   ;==>__GenerateUUID

Func _FakeYouGetVoicesCategoriesList()
    Local $sJson = __HttpGet("https://api.fakeyou.com/category/list/tts")
;~  ConsoleWrite($sJson & @CRLF)
    Local $oJson = Json_Decode($sJson)
    If $sJson = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Local $aoJson = Json_Get($oJson, '["categories"]')
    If Not IsArray($aoJson) Then Return SetError(2, 0, "")
    Local $aVoicesCategories[UBound($aoJson)][12]
    Local $aMembers[] = ["category_token", "model_type", "maybe_super_category_token", "can_directly_have_models", "can_have_subcategories", _
            "can_only_mods_apply", "name", "name_for_dropdown", "is_mod_approved", "created_at", _
            "updated_at", "deleted_at"]
    For $i = 0 To UBound($aoJson) - 1
        For $x = 0 To UBound($aMembers) - 1
            $aVoicesCategories[$i][$x] = Json_Get($aoJson[$i], '["' & $aMembers[$x] & '"]')
        Next
    Next
    Return $aVoicesCategories
EndFunc   ;==>_FakeYouGetVoicesCategoriesList

Func _FakeYouGetVoicesList()
    Local $sJson = __HttpGet("https://api.fakeyou.com/tts/list")
    ConsoleWrite($sJson & @CRLF)
    Local $oJson = Json_Decode($sJson)
    If $sJson = "" Or Not __FakeYou_IsSuccess($oJson) Then Return SetError(1, 0, "")
    Local $aoJson = Json_Get($oJson, '["models"]')
    If Not IsArray($aoJson) Then Return SetError(2, 0, "")

    Local $aVoices[UBound($aoJson)][15]
    Local $aMembers[] = ["model_token", "tts_model_type", "creator_user_token", "creator_username", "creator_display_name", _
            "creator_gravatar_hash", "title", "ietf_language_tag", "ietf_primary_language_subtag", "is_front_page_featured", _
            "is_twitch_featured", "maybe_suggested_unique_bot_command", "category_tokens", "created_at", "updated_at"]

    Local $sMemberValue = ""
    For $i = 0 To UBound($aoJson) - 1
        For $x = 0 To UBound($aMembers) - 1
            $sMemberValue = Json_Get($aoJson[$i], '["' & $aMembers[$x] & '"]')
            If $aMembers[$x] = "category_tokens" Then
                For $n = 0 To UBound($sMemberValue) - 1
                    $aVoices[$i][$x] &= Json_Get($sMemberValue, '[' & $n & ']') & (($n < UBound($sMemberValue) - 1) ? "|" : "")
                Next
            Else
                $aVoices[$i][$x] = $sMemberValue
            EndIf
        Next
    Next
    Return $aVoices
EndFunc   ;==>_FakeYouGetVoicesList


Func __FakeYou_IsSuccess(ByRef $oJson)
    Return Json_ObjExists($oJson, 'success')
EndFunc   ;==>__FakeYou_IsSuccess


Func __HttpGet($sURL, $sData = "")
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("GET", $sURL & "?" & $sData, False)
    If (@error) Then Return SetError(1, 0, 0)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.Send()
    If (@error) Then Return SetError(2, 0, 0)
    If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0)
    Return SetError(0, 0, $oHTTP.ResponseText)
EndFunc   ;==>__HttpGet

Func __HttpPost($sURL, $sData = "")
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("POST", $sURL, False)
    If (@error) Then Return SetError(1, 0, 0)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.Send($sData)
    If (@error) Then Return SetError(2, 0, 0)
    If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0)
    Return SetError(0, 0, $oHTTP.ResponseText)
EndFunc   ;==>__HttpPost

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

 

Saludos

Hi, thanks for the help. I have reviewed it and it is great, something that had not occurred to me before. And well, I'm going to work on some missing functions, like getting the audio and audio data through an ID. And maybe some other function. Would you mind if I post it when it's all over?

Link to post
Share on other sites

Hello @Mateocedillo Sure post it when you finish it.  Feel free to ask if you have any question or write me by Telegram I still have you added.

 

Saludos

Link to post
Share on other sites
1 hour ago, Danyfirex said:

Hello @Mateocedillo Sure post it when you finish it.  Feel free to ask if you have any question or write me by Telegram I still have you added.

 

Saludos

Done, I sent you a message. In fact, I've already been working on new functions and there are things that maybe need to be clarified. I already have a version, but it has some bugs and I'd rather post it once I'm done.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...