Skip to content

Commit

Permalink
Merge pull request #263 from dvonthenen/implement-chan-transport
Browse files Browse the repository at this point in the history
Implement Channel Callback Interface
  • Loading branch information
davidvonthenen authored Aug 23, 2024
2 parents 4de178e + 7ef18d7 commit 0d19615
Show file tree
Hide file tree
Showing 38 changed files with 3,354 additions and 1,184 deletions.
24 changes: 21 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,24 @@ issues:
- linters:
- gocritic
text: "unnecessaryDefer:"
- path: pkg/api/speak/v1/speak.go
- path: pkg/api/listen/v1/websocket/chan_router.go
linters:
- staticcheck
text: SA1019
- gocritic
- path: pkg/client/common/v1/websocket.go
linters:
- gocritic
- path: pkg/client/listen/client.go
linters:
- gocritic
- path: pkg/client/listen/v1/websocket/new_using_chan.go
linters:
- gocritic
- path: pkg/client/listen/v1/websocket/client_callback.go
linters:
- dupl
- path: pkg/client/listen/v1/websocket/client_channel.go
linters:
- dupl
- path: pkg/api/manage/v1/manage.go
linters:
- staticcheck
Expand All @@ -122,3 +136,7 @@ issues:
linters:
- staticcheck
text: SA1019
- path: pkg/api/speak/v1/speak.go
linters:
- staticcheck
text: SA1019
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
}

// create a Deepgram client
dgClient, err := client.NewWebSocketForDemo(ctx, transcriptOptions)
dgClient, err := client.NewWSUsingCallbackForDemo(ctx, transcriptOptions)
if err != nil {
fmt.Println("ERROR creating LiveTranscription connection:", err)
return
Expand All @@ -51,7 +51,6 @@ func main() {
fmt.Printf("httpClient.Get failed. Err: %v\n", err)
return
}

fmt.Printf("Stream is up and running %s\n", reflect.TypeOf(res))

// connect the websocket to Deepgram
Expand Down
83 changes: 83 additions & 0 deletions examples/speech-to-text/websocket/http_channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

// streaming
import (
"bufio"
"context"
"fmt"
"net/http"
"os"
"reflect"

interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
client "github.com/deepgram/deepgram-go-sdk/pkg/client/listen"
)

const (
STREAM_URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
)

func main() {
// init library
client.InitWithDefault()
// client.Init(client.InitLib{
// LogLevel: client.LogLevelTrace, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace
// })

// Go context
ctx := context.Background()

// print instructions
fmt.Print("\n\nPress ENTER to exit!\n\n")

// set the Transcription options
transcriptOptions := &interfaces.LiveTranscriptionOptions{
Language: "en-US",
Punctuate: true,
}

// create a Deepgram client
dgClient, err := client.NewWSUsingChanForDemo(ctx, transcriptOptions)
if err != nil {
fmt.Println("ERROR creating LiveTranscription connection:", err)
return
}

// get the HTTP stream
httpClient := new(http.Client)

res, err := httpClient.Get(STREAM_URL)
if err != nil {
fmt.Printf("httpClient.Get failed. Err: %v\n", err)
return
}
fmt.Printf("Stream is up and running %s\n", reflect.TypeOf(res))

// connect the websocket to Deepgram
bConnected := dgClient.Connect()
if !bConnected {
fmt.Println("Client.Connect failed")
os.Exit(1)
}

go func() {
// feed the HTTP stream to the Deepgram client (this is a blocking call)
dgClient.Stream(bufio.NewReader(res.Body))
}()

// wait for user input to exit
input := bufio.NewScanner(os.Stdin)
input.Scan()

// close HTTP stream
res.Body.Close()

// close DG client
dgClient.Stop()

fmt.Printf("\n\nProgram exiting...\n")
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func main() {
}

// create a Deepgram client
dgClient, err := client.NewWebSocket(ctx, "", cOptions, tOptions, callback)
dgClient, err := client.NewWSUsingCallback(ctx, "", cOptions, tOptions, callback)
if err != nil {
fmt.Println("ERROR creating LiveTranscription connection:", err)
return
Expand Down
20 changes: 20 additions & 0 deletions examples/speech-to-text/websocket/microphone_channel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Live API (Real-Time) Example

This example uses the Microphone as input in order to detect conversation insights in what is being said. This example required additional components (for the microphone) to be installed in order for this example to function correctly.

## Configuration

The SDK (and this example) needs to be initialized with your account's credentials `DEEPGRAM_API_KEY`, which are available in your [Deepgram Console][dg-console]. If you don't have a Deepgram account, you can [sign up here][dg-signup] for free.

You must add your `DEEPGRAM_API_KEY` to your list of environment variables. We use environment variables because they are easy to configure, support PaaS-style deployments, and work well in containerized environments like Docker and Kubernetes.

```sh
export DEEPGRAM_API_KEY=YOUR-APP-KEY-HERE
```

## Installation

The Live API (Real-Time) example makes use of a [microphone package](https://github.com/deepgram/deepgram-go-sdk/tree/main/pkg/audio/microphone) contained within the repository. That package makes use of the [PortAudio library](http://www.portaudio.com/) which is a cross-platform open source audio library. If you are on Linux, you can install this library using whatever package manager is available (yum, apt, etc.) on your operating system. If you are on macOS, you can install this library using [brew](https://brew.sh/).

[dg-console]: https://console.deepgram.com/
[dg-signup]: https://console.deepgram.com/signup
Loading

0 comments on commit 0d19615

Please sign in to comment.