-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Live/Streaming Structs, Modules, etc to Encapsulate Internals
- Loading branch information
dvonthenen
committed
Oct 24, 2023
1 parent
047d382
commit 39281f8
Showing
16 changed files
with
923 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 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 live | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrInvalidMessageType invalid message type | ||
ErrInvalidMessageType = errors.New("invalid message type") | ||
|
||
// ErrUserCallbackNotDefined user callback object not defined | ||
ErrUserCallbackNotDefined = errors.New("user callback object not defined") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 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 live | ||
|
||
import ( | ||
|
||
// prettyjson "github.com/hokaccha/go-prettyjson" | ||
|
||
"log" | ||
"strings" | ||
|
||
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/live/v1/interfaces" | ||
) | ||
|
||
type DefaultCallbackHandler struct { | ||
sb strings.Builder | ||
} | ||
|
||
func (dch DefaultCallbackHandler) Message(mr *interfaces.MessageResponse) error { | ||
// DEBUG: just print the transcription | ||
// data, err := json.Marshal(mr) | ||
// if err != nil { | ||
// log.Printf("RecognitionResult json.Marshal failed. Err: %v\n", err) | ||
// return err | ||
// } | ||
|
||
// prettyJson, err := prettyjson.Format(data) | ||
// if err != nil { | ||
// log.Printf("prettyjson.Marshal failed. Err: %v\n", err) | ||
// return err | ||
// } | ||
// log.Printf("\n\nMessage Object:\n%s\n\n", prettyJson) | ||
|
||
// Only print the final transcript | ||
// // handle the message | ||
// sentence := strings.TrimSpace(mr.Channel.Alternatives[0].Transcript) | ||
|
||
// if len(mr.Channel.Alternatives) == 0 || len(sentence) == 0 { | ||
// // klog.V(7).Infof("DEEPGRAM - no transcript") | ||
// return nil | ||
// } | ||
|
||
// isFinal := mr.SpeechFinal | ||
// sentence = strings.ToLower(sentence) | ||
// dch.sb.WriteString(sentence) | ||
|
||
// // // debug | ||
// // klog.V(4).Infof("transcription result: text = %s, final = %t", i.sb.String(), isFinal) | ||
|
||
// if !isFinal { | ||
// // klog.V(7).Infof("DEEPGRAM - not final") | ||
// return nil | ||
// } | ||
|
||
// // debug | ||
// log.Printf("Deepgram transcription: text = %s, final = %t", dch.sb.String(), isFinal) | ||
// dch.sb.Reset() | ||
|
||
// handle the message | ||
sentence := strings.TrimSpace(mr.Channel.Alternatives[0].Transcript) | ||
|
||
if len(mr.Channel.Alternatives) == 0 || len(sentence) == 0 { | ||
// klog.V(7).Infof("DEEPGRAM - no transcript") | ||
return nil | ||
} | ||
log.Printf("%s\n", sentence) | ||
|
||
return nil | ||
} | ||
|
||
func NewDefaultCallbackHandler() DefaultCallbackHandler { | ||
return DefaultCallbackHandler{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2023 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 interfaces | ||
|
||
const ( | ||
// message types | ||
TypeMessageResponse string = "Results" | ||
|
||
// Error type | ||
TypeErrorResponse string = "Error" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2023 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 interfaces | ||
|
||
// LiveMessageCallback is a callback used to receive notifcations for platforms messages | ||
type LiveMessageCallback interface { | ||
Message(mr *MessageResponse) error | ||
// TODO: implement other conversation insights | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2023 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 interfaces | ||
|
||
/* | ||
Shared defintions for the Deepgram API | ||
*/ | ||
type Words struct { | ||
Confidence float64 `json:"confidence,omitempty"` | ||
End float64 `json:"end,omitempty"` | ||
PunctuatedWord string `json:"punctuated_word,omitempty"` | ||
Start float64 `json:"start,omitempty"` | ||
Word string `json:"word,omitempty"` | ||
} | ||
type Alternatives struct { | ||
Confidence float64 `json:"confidence,omitempty"` | ||
Transcript string `json:"transcript,omitempty"` | ||
Words []Words `json:"words,omitempty"` | ||
} | ||
type Channel struct { | ||
Alternatives []Alternatives `json:"alternatives,omitempty"` | ||
} | ||
|
||
type ModelInfo struct { | ||
Arch string `json:"arch,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
} | ||
type Metadata struct { | ||
ModelInfo ModelInfo `json:"model_info,omitempty"` | ||
ModelUUID string `json:"model_uuid,omitempty"` | ||
RequestID string `json:"request_id,omitempty"` | ||
} | ||
|
||
/* | ||
Results from Live Transcription | ||
*/ | ||
type MessageResponse struct { | ||
Channel Channel `json:"channel,omitempty"` | ||
ChannelIndex []int `json:"channel_index,omitempty"` | ||
Duration float64 `json:"duration,omitempty"` | ||
IsFinal bool `json:"is_final,omitempty"` | ||
Metadata Metadata `json:"metadata,omitempty"` | ||
SpeechFinal bool `json:"speech_final,omitempty"` | ||
Start float64 `json:"start,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
} | ||
|
||
type ErrorResponse struct { | ||
Description string `json:"description"` | ||
Message string `json:"message"` | ||
Type string `json:"type"` | ||
Variant string `json:"variant"` | ||
} |
Oops, something went wrong.