Giter Site home page Giter Site logo

deepgram-go-client-unofficial's Introduction

deepgram-go-client-unofficial

Unofficial client for the Deepgram API

Steps to use

Record an audio PCM 16 bit, rate 8k, mono

pw-cat --record audio.raw --channels 1 --rate 8000 

Download the Golang package

go get -v github.com/pablodz/deepgram-go-client-unofficial

Create a client

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	"github.com/gorilla/websocket"
	"github.com/pablodz/deepgram-go-client-unofficial/config"
	"github.com/pablodz/deepgram-go-client-unofficial/models"
	"github.com/pablodz/deepgram-go-client-unofficial/senders"
)

func SendInBackground(audioRaw []byte, clientWss *websocket.Conn) {
	err := senders.Send2Deepgram(audioRaw, clientWss)
	if err != nil {
		panic(err)
	}
}

func main() {
	// Create websocket client
	clientWss := config.ConfigSTTDeepgram("YOUR_APIKEY_HERE", "8000", "es", "1")
	// Close websocket when finish
	defer clientWss.Close()

	// Get audio data from file PCM 16 bits, 8000 Hz, mono
	audioRaw, err := ioutil.ReadFile("audio.raw") // the file is inside the local directory
	if err != nil {
		panic(err)
	}

	// Start sending audio data in background
	go SendInBackground(audioRaw, clientWss)

	// Receive messages from Deepgram
	for {
		_, message, err := clientWss.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
				// Closed from server, dont print close 1011 (internal server error): NET-0001
				break
			}
			fmt.Println("Error reading websocket message response:", err)
			break
		}

		var resp models.Response
		if err := json.Unmarshal(message, &resp); err != nil {
			fmt.Println("Error parsing json response:", err)
		}

		if resp.IsFinal {
			// Only get final transcript
			fmt.Println("Confidence", resp.Channel.Alternatives[0].Confidence, "\tMessage: ", resp.Channel.Alternatives[0].Transcript)
		}
	}
}

Run the program

go run main.go

Get responses

Client created at  wss://api.deepgram.com/v1/listen?sample_rate=8000&language=es&channels=1&
Confidence 0.8496094    Message:  hola
Confidence 0.99902344   Message:  esto es una prueba
Confidence 0.9975586    Message:  uno
Confidence 0.9970703    Message:  dos
Confidence 0.9921875    Message:  tres
Confidence 0.9951172    Message:  cuatro

deepgram-go-client-unofficial's People

Contributors

pablodz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.