Feiyun API Voice dictation Python3 Call Routines

Source: Internet
Author: User
Tags base64

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.