#!/usr/bin/python3#-*-coding:utf-8-*-import requestsimport timeimport gzipimport urllibimport jsonimport Hashlibimport base64def audio_dictation (): "" "Flying voice dictation API Call Routines Note: You need to add native ip! to the IP whitelist in the Feiyun console before use Reference: News Feiyun official API document https://doc.xfyun.cn/rest_api/Voice dictation. html "" "# Read Appid:with open ('./appid ', mode= ' R ', Encodi ng= ' ASCII ') as Appid_file:x_appid = Appid_file.read () # Read Apikey:with open ('./apikey ', mode= ' R ', Encodi ng= ' ASCII ') as Apikey_file:api_key = Apikey_file.read () # API Url:url = ' Http://api.xfyun.cn/v1/service /v1/iat ' # Read the contents of the audio file (replace ' zhngjb_clip.wav ' with an audio file to upload): with open ('./zhngjb_clip.wav ', mode= ' RB ') as Audio_file: File_content = Audio_file.read () # base64 audio encoding for audio files: Base64_audio = Base64.b64encode (file_content) BODY = Urllib.parse.urlencode ({' Audio ': Base64_audio}) # sample rate 16k, encoding format uncompressed: param = {"Engine_type": "sms16k", "a UE ":" Raw "} # Build Header:x_param = base64.B64encode (Json.dumps (param). replace (","). Encode ()) X_time = str (int (round (time.time () *))/+) X_ch Ecksum = Hashlib.md5 (Api_key.encode () + str (x_time). Encode () + X_param). Hexdigest () X_header = {' X-appid ': x_appid, ' X-curtime ': x_time, ' X-param ': X_param, ' x-checksum ': x_checksum} req_he Ader = {' Content-type ': ' application/x-www-form-urlencoded ', ' CharSet ': ' utf-8 '} headers = {**req_ Header, **x_header} # Send request: start = Time.time () response = Requests.post (URL, headers=headers, data=body) Duration = Time.time ()-Start with open (' Result ', mode= ' W ', encoding= ' Utf-8 ') as Result_file:print (f ' Re Quest sent. Duration: {duration}s\n ' F ' status code = {response.status_code}\n ' f ' headers = {response.headers }\n ' f ' content = {Response.content.decode ("Utf-8")} ', file=result_file) if __name__ = = ' __main__ ': Audio_di Ctation ()
Feiyun API Voice dictation Python3 call Routines