<jupyter_start><jupyter_text>0. Installing ibm_watson on your virtual environment<jupyter_code>#UNCOMMENT THE LINE ACCORDING TO YOUR PYTHON DEVELOPMENT CONFIGURATION ENVIRONMENT. 

#default option to install ibm_watson if you are running jupyter over your virtualenv with no other interfaces. 
#!pip install ibm_watson

#if you use anaconda
#!conda install -c conda-forge ibm-watson<jupyter_output><empty_output><jupyter_text>1. Imports<jupyter_code>from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource 
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

import os
import json<jupyter_output><empty_output><jupyter_text>2. Setup STT Service<jupyter_code>#set your API KEY and URL. You can get access to these values in the section Manage in Speech to Text API. 
apikey = 'YOUR API KEY'
url = 'YOUR URL'
authenticator = IAMAuthenticator(apikey)
stt = SpeechToTextV1(authenticator=authenticator)
stt.set_service_url(url)<jupyter_output><empty_output><jupyter_text>3. Functions and Data Structures<jupyter_code>FOLDER_DATA_PATH = 'your_folder/'       #replace by the folder where you have your audio files. 

C_TYPE = 'audio/mp3'                    #replace by the content type. 

FORMAT = '.mp3'                         #put the extension of your audio files. 

MODEL_LANGUAGE = 'es-CO_BroadbandModel' # Replace the model. 
                                        # 'es-CO_BroadbandModel' is for Spanish - Colombian. 
                                        # en-US_BroadbandModel is for English US. 
                                        # To see available models, check http://shorturl.at/mxNSU
def extract_transcript(results):
    text = ""
    confidence = 0
    for r in results:
        text += r['alternatives'][0]['transcript']
        confidence += r['alternatives'][0]['confidence']
    if len(results) > 0:
        confidence = confidence/len(results)     
    return text, confidence
def extract_transcripts_from_folder(folder_path, c_type, model_language):
    i = 0
    print("Files in process in", folder_path)
    for filename in os.listdir(folder_path):
        if filename.endswith(FORMAT):
            with open(os.path.join(folder_path, filename), 'rb') as f:
                res = stt.recognize(audio=f, content_type=c_type, model=model_language, continuous=True).get_result()
                text, confidence = extract_transcript(res['results'])
                
                video_dict = {"filename": filename, "transcript" : text,"confidence": confidence}
                
                data_transcripts.append(video_dict)
                i+=1
                print(i," - ", filename)
                
    #number of videos processed
    print('Number of files processed in',folder_path, len(data_transcripts))
def save_json_file(data_dict, out_file):
    with open(out_file, 'w') as fp:
         fp.write( '[' + ',\n'.join(json.dumps(i) for i in data_dict) + ']\n')<jupyter_output><empty_output><jupyter_text>4. Convert and Save<jupyter_code>data_transcripts = []
transcripts_out_json_file = FOLDER_DATA_PATH + '_data_transcripst.json'
extract_transcripts_from_folder(FOLDER_DATA_PATH, C_TYPE, MODEL_LANGUAGE)
save_json_file(data_transcripts, transcripts_out_json_file)
len(data_transcripts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08994868699064293, 'fraction_numerical': 0.004527618472683369, 'mean_word_length': 3.337696335078534, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '462052', 'license': 'Apache-2.0', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1001, 'n_words': 249, 'path': 'speech_to_text_ylizama.ipynb', 'repo_name': 'ylizama/speech_to_text_ibm_watson', 'size': '3313'}