Skip to content

Commit

Permalink
Merge pull request #3 from deepgram-devs/new-params
Browse files Browse the repository at this point in the history
added tier/replace params, updated error handling in usage, fixed opt…
  • Loading branch information
briancbarrow authored Jul 18, 2022
2 parents 3158f3c + afcb83e commit ce76e7c
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 28 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Deepgram Devs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

This is a community driven SDK. There is minimal functionality on the SDK but we hope to add more features soon.

While we don't have a stable release, that is because we don't have feature parity with the other SDKs. It is safe to use, but not all features are available.

To process live transcriptions, see the [example](/examples/liveTranscription_example.go).
2 changes: 2 additions & 0 deletions deepgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
)

var sdkVersion string = "0.1.0"

type Deepgram struct {
ApiKey string
}
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/deepgram-devs/go-sdk

go 1.18

require github.com/gorilla/websocket v1.5.0 // indirect
require (
github.com/google/go-querystring v1.1.0
github.com/gorilla/websocket v1.5.0
)

require github.com/Jeffail/gabs/v2 v2.6.1
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
github.com/Jeffail/gabs/v2 v2.6.1 h1:wwbE6nTQTwIMsMxzi6XFQQYRZ6wDc1mSdxoAN+9U4Gk=
github.com/Jeffail/gabs/v2 v2.6.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6 changes: 3 additions & 3 deletions invitations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (dg *Deepgram) ListInvitations(projectId string) (InvitationList, error) {
"Host": []string{"api.deepgram.com"},
"Content-Type": []string{"application/json"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/1.0.0"},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

var result InvitationList
Expand Down Expand Up @@ -58,7 +58,7 @@ func (dg *Deepgram) SendInvitation(projectId string, options InvitationOptions)
"Host": []string{"api.deepgram.com"},
"Content-Type": []string{"application/json"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/1.0.0"},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

var result Message
Expand Down Expand Up @@ -93,7 +93,7 @@ func (dg *Deepgram) DeleteInvitation(projectId string, email string) (Message, e
"Host": []string{"api.deepgram.com"},
"Content-Type": []string{"application/json"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/1.0.0"},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

var result Message
Expand Down
75 changes: 71 additions & 4 deletions transcriptions.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,98 @@
package deepgram

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"

"github.com/google/go-querystring/query"
"github.com/gorilla/websocket"
)

func (dg *Deepgram) LiveTranscription(options LiveTranscriptionOptions) (*websocket.Conn, *http.Response, error) {
type PreRecordedResponse struct {
Request_id string `json:"request_id"`
Metadata interface{} `json:"metadata"`
Results interface{} `json:"results"`
}

u := url.URL{Scheme: "wss", Host: "api.deepgram.com", Path: "/v1/listen",}
func (dg *Deepgram) LiveTranscription(options LiveTranscriptionOptions) (*websocket.Conn, *http.Response, error) {
query, _ := query.Values(options)
u := url.URL{Scheme: "wss", Host: "api.deepgram.com", Path: "/v1/listen", RawQuery: query.Encode()}
log.Printf("connecting to %s", u.String())

header := http.Header{
"Host": []string{"api.deepgram.com"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/1.0.0"},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

c, resp, err := websocket.DefaultDialer.Dial(u.String(), header);

if err != nil {
log.Printf("handshake failed with status %d", resp.StatusCode)
log.Printf("handshake failed with status %s", resp.Status)
log.Fatal("dial:", err)
}
return c, resp, nil

}

func(dg *Deepgram) PreRecordedFromURL(source UrlSource, options PreRecordedTranscriptionOptions) (PreRecordedResponse, error) {
client := new(http.Client)
query, _ := query.Values(options)
u := url.URL{Scheme: "https", Host: "api.deepgram.com", Path: "/v1/listen", RawQuery: query.Encode()}
jsonStr, err := json.Marshal(source)
if err != nil {
log.Fatal(err)
// TODO: Fix this return value
return PreRecordedResponse{}, err
}

req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonStr))
if err != nil {
//Handle Error
log.Fatal(err)
}

req.Header = http.Header{
"Host": []string{dg.Host("")},
"Content-Type": []string{"application/json"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

var result PreRecordedResponse
res, err := client.Do(req)
if err != nil {
return result, err
}
fmt.Println("CODE:", res.Body)
if res.StatusCode != 200 {
b, _ := io.ReadAll(res.Body)
log.Fatal(string(b))
}

jsonErr := GetJson(res, &result)

if jsonErr != nil {
fmt.Printf("error getting request list: %s\n", jsonErr.Error())
return result, jsonErr
} else {
return result, nil
}

// query, _ := query.Values(options)
// u := url.URL{Scheme: "https", Host: "api.deepgram.com", Path: "/v1/projects", RawQuery: query.Encode()}
// log.Printf("connecting to %s", u.String())

// header := http.Header{
// "Host": []string{"api.deepgram.com"},
// "Authorization": []string{"token " + dg.ApiKey},
// "X-DG-Agent": []string{"go-sdk/" + sdkVersion},
// }


}
85 changes: 66 additions & 19 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package deepgram

import (
"bytes"
"io"
)

type InvitationOptions struct {
Email string `json:"email"`
Scope string `json:"scope"`
Expand All @@ -14,23 +19,65 @@ type Message struct {
}

type LiveTranscriptionOptions struct {
Model string `json:"model"`
Language string `json:"language"`
Version string `json:"version"`
Punctuate bool `json:"punctuate"`
Profanity_filter bool `json:"profanity_filter"`
Redact bool `json:"redact"`
Diarize bool `json:"diarize"`
Multichannel bool `json:"multichannel"`
Alternatives int `json:"alternatives"`
Numerals bool `json:"numerals"`
Search []string `json:"search"`
Callback string `json:"callback"`
Keywords []string `json:"keywords"`
Interim_results bool `json:"interim_results"`
Endpointing bool `json:"endpointing"`
Vad_turnoff int `json:"vad_turnoff"`
Encoding string `json:"encoding"`
Channels int `json:"channels"`
Sample_rate int `json:"sample_rate"`
Model string `json:"model" url:"model,omitempty" `
Language string `json:"language" url:"language,omitempty" `
Version string `json:"version" url:"version,omitempty" `
Punctuate bool `json:"punctuate" url:"punctuate,omitempty" `
Profanity_filter bool `json:"profanity_filter" url:"profanity_filter,omitempty" `
Redact bool `json:"redact" url:"redact,omitempty" `
Diarize bool `json:"diarize" url:"diarize,omitempty" `
Multichannel bool `json:"multichannel" url:"multichannel,omitempty" `
Alternatives int `json:"alternatives" url:"alternatives,omitempty" `
Numerals bool `json:"numerals" url:"numerals,omitempty" `
Search []string `json:"search" url:"search,omitempty" `
Callback string `json:"callback" url:"callback,omitempty" `
Keywords []string `json:"keywords" url:"keywords,omitempty" `
Interim_results bool `json:"interim_results" url:"interim_results,omitempty" `
Endpointing bool `json:"endpointing" url:"endpointing,omitempty" `
Vad_turnoff int `json:"vad_turnoff" url:"vad_turnoff,omitempty" `
Encoding string `json:"encoding" url:"encoding,omitempty" `
Channels int `json:"channels" url:"channels,omitempty" `
Sample_rate int `json:"sample_rate" url:"sample_rate,omitempty" `
Tier string `json:"tier" url:"tier,omitempty" `
Replace string `json:"replace" url:"replace,omitempty" `
}

type PreRecordedTranscriptionOptions struct {
Tier string `json:"tier" url:"tier,omitempty" `
Model string `json:"model" url:"model,omitempty" `
Version string `json:"version" url:"version,omitempty" `
Language string `json:"language" url:"language,omitempty" `
Punctuate bool `json:"punctuate" url:"punctuate,omitempty" `
Profanity_filter bool `json:"profanity_filter" url:"profanity_filter,omitempty" `
Redact bool `json:"redact" url:"redact,omitempty" `
Diarize bool `json:"diarize" url:"diarize,omitempty" `
Ner bool `json:"ner" url:"ner,omitempty" `
Multichannel bool `json:"multichannel" url:"multichannel,omitempty" `
Alternatives int `json:"alternatives" url:"alternatives,omitempty" `
Numerals bool `json:"numerals" url:"numerals,omitempty" `
Search []string `json:"search" url:"search,omitempty" `
Replace string `json:"replace" url:"replace,omitempty" `
Callback string `json:"callback" url:"callback,omitempty" `
Keywords []string `json:"keywords" url:"keywords,omitempty" `
Utterances bool `json:"utterances" url:"utterances,omitempty" `
Utt_split int `json:"utt_split" url:"utt_split,omitempty" `
Tag string `json:"tag" url:"tag,omitempty"`
}

type TranscriptionSource interface {
ReadStreamSource | UrlSource | BufferSource
}

type ReadStreamSource struct {
Stream io.Reader `json:"stream"`
Mimetype string `json:"mimetype"`
}

type UrlSource struct {
Url string `json:"url"`
}

type BufferSource struct {
Buffer bytes.Buffer `json:"buffer"`
Mimetype string `json:"mimetype"`
}
58 changes: 58 additions & 0 deletions types/prerecorded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package deepgram

type Metadata struct {
RequestId string `json:"request_id"`
TransactionKey string `json:"transaction_key"`
Sha256 string `json:"sha256"`
Created string `json:"created"`
Duration int `json:"duration"`
Channels int `json:"channels"`
}

type Hit struct {
Confidence float64 `json:"confidence"`
Start float64 `json:"start"`
End float64 `json:"end"`
Snippet string `json:"snippet"`
}

type Search struct {
Query string `json:"query"`
Hits []Hit `json:"hits"`
}

type WordBase struct {
Word string `json:"word"`
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Punctuated_Word string `json:"punctuated_word"`
Speaker int `json:"speaker"`
}

type Alternative struct {
Transcript string `json:"transcript"`
Confidence float64 `json:"confidence"`
Words []WordBase `json:"words"`
}

type Channel struct {
Search []Search `json:"search"`
Alternatives []Alternative `json:"alternatives"`
}

type Utterance struct {
Start float64 `json:"start"`
End float64 `json:"end"`
Confidence float64 `json:"confidence"`
Channel int `json:"channel"`
Transcript string `json:"transcript"`
Words []WordBase `json:"words"`
Speaker int `json:"speaker"`
Id string `json:"id"`
}

type Results struct {
Utterances []Utterance `json:"utterances"`
Channels []Channel `json:"channels"`
}
9 changes: 8 additions & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
Expand Down Expand Up @@ -49,15 +50,21 @@ func (dg *Deepgram) ListRequests(projectId string, options UsageRequestListOptio
"Host": []string{dg.Host("")},
"Content-Type": []string{"application/json"},
"Authorization": []string{"token " + dg.ApiKey},
"X-DG-Agent": []string{"go-sdk/1.0.0"},
"X-DG-Agent": []string{"go-sdk/" + sdkVersion},
}

var result UsageRequestList
res, err := client.Do(req)
if err != nil {
return result, err
}
if res.StatusCode != 200 {
b, _ := io.ReadAll(res.Body)
log.Fatal(string(b))
}

jsonErr := GetJson(res, &result)

if jsonErr != nil {
fmt.Printf("error getting request list: %s\n", jsonErr.Error())
return result, jsonErr
Expand Down

0 comments on commit ce76e7c

Please sign in to comment.