generated from deepgram-starters/project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
116 lines (94 loc) · 2.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/json"
"errors"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/deepgram-devs/deepgram-go-sdk/deepgram"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
type TranscriptionResponse struct {
Model string `json:"model,omitempty"`
Version string `json:"version,omitempty"`
Tier string `json:"tier,omitempty"`
DeepgramFeatures deepgram.PreRecordedTranscriptionOptions `json:"dgFeatures,omitempty"`
Transcription deepgram.PreRecordedResponse `json:"transcription,omitempty"`
}
func main() {
godotenv.Load()
dg := deepgram.NewClient(os.Getenv("deepgram_api_key"))
r := gin.Default()
r.Use(cors.Default())
r.Static("/", "./static")
r.POST("/api", transcribe(dg))
r.Run("localhost:" + os.Getenv("port"))
}
func transcribe(dg *deepgram.Client) gin.HandlerFunc {
fn := func(c *gin.Context) {
url := c.PostForm("url")
model := c.PostForm("model")
version := c.PostForm("version")
tier := c.PostForm("tier")
features := c.PostForm("features")
var dgFeatures deepgram.PreRecordedTranscriptionOptions
err := json.Unmarshal([]byte(features), &dgFeatures)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
dgFeatures.Model = model
if len(version) > 0 {
dgFeatures.Version = version
}
if model != "whisper" {
dgFeatures.Tier = tier
}
if strings.HasPrefix(url, "https://res.cloudinary.com/deepgram") {
transcription, err := dg.PreRecordedFromURL(deepgram.UrlSource{Url: url}, dgFeatures)
if err != nil {
panic(err)
}
res := TranscriptionResponse{
Model: model,
Version: version,
Tier: tier,
DeepgramFeatures: dgFeatures,
Transcription: transcription,
}
c.JSON(http.StatusOK, res)
} else {
file, err := c.FormFile("file")
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("you need to choose a file to transcribe your own audio"))
return
}
uploadedFile, err := file.Open()
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("cannot open file"))
}
defer uploadedFile.Close()
stream := uploadedFile.(io.ReadCloser)
mime := mime.TypeByExtension(filepath.Ext(file.Filename))
transcription, err := dg.PreRecordedFromStream(
deepgram.ReadStreamSource{Stream: stream, Mimetype: mime},
dgFeatures)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
res := TranscriptionResponse{
Model: model,
Version: version,
Tier: tier,
DeepgramFeatures: dgFeatures,
Transcription: *transcription,
}
c.JSON(http.StatusOK, res)
}
}
return gin.HandlerFunc(fn)
}