Code Monkey home page Code Monkey logo

asterisk-voip's Introduction

AGI Asterisk IBM Watson Service Speech to Text (python)

Introduction

The Asterisk Gateway Interface is an interface for adding functionality to Asterisk with many different programming languages. In this case, we will integrate Asterisk with IBM Watson service to transcribe audio to text.

This python script receives one parameter (audio file) and returns the associated text through standard output.

Execution example

python -W ignore audio_parser.py custom-record0

Source Code

#!/usr/bin/python
# Overall import
import sys,os
import requests
import json

## Watson Parameters
# Credentials
username = "XXXXX"
password = "XXXXX"
# Model language
model_name = "es-ES_NarrowbandModel"
# API Url Endpoint
url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&model=" + model_name
# Timeout API
timeout = 5

## Audio Parameters
path_audio = "/var/lib/asterisk/sounds"
ext_audio = "wav"

## Init VariablesA
# Default event
event = "SOPORTE"

# Obtain audio file
data = open(path_audio + "/" + sys.argv[1] + "." + ext_audio)

# Send Data 
def send(data):
    sys.stdout.write(str(data))
    sys.stdout.flush()

# Call Watson API
def call_watson(url, data, username, password, timeout):
    try:
    	headers = {'content-type': 'audio/' + ext_audio}
    	r = requests.post(url=url,auth=(username, password),data=data,headers=headers,timeout=timeout)
    except (requests.exceptions.Timeout, requests.exceptions.TooManyRedirects, requests.exceptions.RequestException) as e:
    # Handler Exception
	return "ERROR" 
    # Return Error    
    return json.dumps(r.json())

# Call API Watson
res = call_watson(url, data, username, password, timeout)

if res != "ERROR":
    a = json.loads(res)
    # Return transcript
    text = a["results"][0]["alternatives"][0]["transcript"]
    msg = text.encode('ascii', 'ignore').decode('ascii')
    event = "XXXX"
else:
    event = "ERROR"
    

## Asterisk AGI
# Send Event
send("SET VARIABLE SPEECH_TO_TEXT " + event)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.