Skip to content

Commit 99a351b

Browse files
authored
Merge pull request #18 from AssemblyAI/feat-upload-feedback
feat - add upload percentage
2 parents 8f17992 + eb96702 commit 99a351b

35 files changed

Lines changed: 2459 additions & 79 deletions

cmd/checkToken.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Cobra is a CLI library for Go that empowers applications.
2121
This application is a tool to generate the needed files
2222
to quickly create a Cobra application.`,
2323
Run: func(cmd *cobra.Command, args []string) {
24-
token := GetStoredToken()
25-
if token != "" {
26-
fmt.Printf("Your Token is %s\n", token)
24+
Token = GetStoredToken()
25+
if Token != "" {
26+
fmt.Printf("Your Token is %s\n", Token)
2727
return
2828
} else {
2929
fmt.Println("You must login first. Run `assemblyai config <token>`")

cmd/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
var configFolderPath = ".config/assemblyai"
1818
var configFileName = "config.toml"
19+
var Token string
1920

2021
// configCmd represents the config command
2122
var configCmd = &cobra.Command{
@@ -66,7 +67,7 @@ func init() {
6667

6768
func CheckIfTokenValid(token string) bool {
6869

69-
response := QueryApi(token, "/account", "GET", nil)
70+
response := QueryApi("/account", "GET", nil)
7071

7172
if response == nil {
7273
return false

cmd/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ to quickly create a Cobra application.`,
3131
flags.Poll, _ = cmd.Flags().GetBool("poll")
3232
flags.Json, _ = cmd.Flags().GetBool("json")
3333

34-
token := GetStoredToken()
35-
if token == "" {
34+
Token = GetStoredToken()
35+
if Token == "" {
3636
fmt.Println("You must login first. Run `assemblyai config <token>`")
3737
return
3838
}
3939

40-
PollTranscription(token, id, flags)
40+
PollTranscription(id, flags)
4141
},
4242
}
4343

cmd/transcribe.go

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"bytes"
8+
"context"
89
"encoding/json"
910
"fmt"
1011
"math"
@@ -18,11 +19,12 @@ import (
1819
"text/tabwriter"
1920
"time"
2021

22+
"github.com/briandowns/spinner"
2123
"github.com/spf13/cobra"
2224
"golang.org/x/term"
25+
pb "gopkg.in/cheggaaa/pb.v1"
2326
)
2427

25-
// transcribeCmd represents the transcribe command
2628
var transcribeCmd = &cobra.Command{
2729
Use: "transcribe <url | path | youtube URL>",
2830
Short: "A brief description of your command",
@@ -85,9 +87,8 @@ func init() {
8587
}
8688

8789
func transcribe(params TranscribeParams, flags TranscribeFlags) {
88-
token := GetStoredToken()
89-
90-
if token == "" {
90+
Token = GetStoredToken()
91+
if Token == "" {
9192
fmt.Println("You must login first. Run `assemblyai config <token>`")
9293
return
9394
}
@@ -109,7 +110,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
109110
}
110111
youtubeVideoURL := YoutubeDownload(youtubeId)
111112
if youtubeVideoURL == "" {
112-
fmt.Println("Please try again with a different one.")
113+
fmt.Println(" Please try again with a different one.")
113114
return
114115
}
115116
params.AudioURL = youtubeVideoURL
@@ -122,7 +123,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
122123
}
123124
}
124125
} else {
125-
uploadedURL := uploadFile(token, params.AudioURL)
126+
uploadedURL := UploadFile(params.AudioURL)
126127
if uploadedURL == "" {
127128
fmt.Println("The file doesn't exist. Please try again with a different one.")
128129
return
@@ -135,7 +136,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
135136

136137
TelemetryCaptureEvent("CLI transcription created", nil)
137138
body := bytes.NewReader(paramsJSON)
138-
response := QueryApi(token, "/transcript", "POST", body)
139+
response := QueryApi("/transcript", "POST", body)
139140
var transcriptResponse TranscriptResponse
140141
if err := json.Unmarshal(response, &transcriptResponse); err != nil {
141142
fmt.Println("Can not unmarshal JSON")
@@ -153,7 +154,7 @@ func transcribe(params TranscribeParams, flags TranscribeFlags) {
153154
return
154155
}
155156

156-
PollTranscription(token, *id, flags)
157+
PollTranscription(*id, flags)
157158
}
158159

159160
func isUrl(str string) bool {
@@ -179,7 +180,7 @@ func checkAAICDN(url string) bool {
179180
return strings.HasPrefix(url, "https://cdn.assemblyai.com/")
180181
}
181182

182-
func uploadFile(token string, path string) string {
183+
func UploadFile(path string) string {
183184
isAbs := filepath.IsAbs(path)
184185
if !isAbs {
185186
wd, err := os.Getwd()
@@ -196,47 +197,91 @@ func uploadFile(token string, path string) string {
196197
}
197198

198199
TelemetryCaptureEvent("CLI upload started", nil)
199-
s := CallSpinner(" Your file is being uploaded...")
200-
response := QueryApi(token, "/upload", "POST", file)
201200

201+
fileInfo, _ := file.Stat()
202+
bar := pb.New(int(fileInfo.Size()))
203+
bar.SetUnits(pb.U_BYTES_DEC)
204+
bar.Prefix(" Uploading file to our servers: ")
205+
bar.ShowBar = false
206+
bar.ShowTimeLeft = false
207+
bar.Start()
208+
209+
response := QueryApi("/upload", "POST", bar.NewProxyReader(file))
210+
211+
bar.Finish()
202212
var uploadResponse UploadResponse
203213
if err := json.Unmarshal(response, &uploadResponse); err != nil {
204214
return ""
205215
}
206-
s.Stop()
207216
TelemetryCaptureEvent("CLI upload ended", nil)
208217

209218
return uploadResponse.UploadURL
210219
}
211220

212-
func PollTranscription(token string, id string, flags TranscribeFlags) {
213-
fmt.Println("Your file is being transcribed (id " + id + ")...")
214-
s := CallSpinner(" Processing time is usually 20% of the file's duration.")
215-
for {
216-
response := QueryApi(token, "/transcript/"+id, "GET", nil)
221+
func PollTranscription(id string, flags TranscribeFlags) {
222+
fmt.Println(" Transcribing file with id " + id)
223+
showProgressBar := TranscriptionLength != 0
224+
timePercentage := (TranscriptionLength * 30) / 100
225+
226+
ctx, cancelCtx := context.WithCancel(context.Background())
227+
defer cancelCtx()
228+
229+
var s *spinner.Spinner
230+
var bar *pb.ProgressBar
231+
232+
if showProgressBar {
233+
fmt.Println(" Processing time is usually 20% of the file's duration.")
234+
bar = pb.StartNew(timePercentage)
235+
go showProgress(timePercentage, ctx, bar)
236+
} else {
237+
s = CallSpinner(" Processing time is usually 20% of the file's duration.")
238+
}
217239

240+
for {
241+
response := QueryApi("/transcript/"+id, "GET", nil)
218242
if response == nil {
219-
s.Stop()
220-
fmt.Printf("\033[1A\033[2K")
243+
if showProgressBar {
244+
cancelCtx()
245+
bar.Set(timePercentage)
246+
bar.Finish()
247+
} else {
248+
s.Stop()
249+
}
221250
fmt.Println("Something went wrong. Please try again later.")
222251
return
223252
}
224253
var transcript TranscriptResponse
225254
if err := json.Unmarshal(response, &transcript); err != nil {
226255
fmt.Println(err)
227-
s.Stop()
228-
fmt.Printf("\033[1A\033[2K")
256+
if showProgressBar {
257+
cancelCtx()
258+
bar.Set(timePercentage)
259+
bar.Finish()
260+
} else {
261+
s.Stop()
262+
}
229263
return
230264
}
231265
if transcript.Error != nil {
232-
s.Stop()
233-
fmt.Printf("\033[1A\033[2K")
266+
if showProgressBar {
267+
cancelCtx()
268+
bar.Set(timePercentage)
269+
bar.Finish()
270+
} else {
271+
s.Stop()
272+
}
234273
fmt.Println(*transcript.Error)
235274
return
236275
}
237276
if *transcript.Status == "completed" {
277+
if showProgressBar {
278+
cancelCtx()
279+
bar.Set(timePercentage)
280+
bar.Finish()
281+
} else {
282+
s.Stop()
283+
}
238284
var properties *PostHogProperties = new(PostHogProperties)
239-
240285
properties.Poll = flags.Poll
241286
properties.Json = flags.Json
242287
properties.AutoChapters = *transcript.AutoChapters
@@ -252,8 +297,7 @@ func PollTranscription(token string, id string, flags TranscribeFlags) {
252297
properties.TopicDetection = *transcript.IabCategories
253298

254299
TelemetryCaptureEvent("CLI transcription finished", properties)
255-
s.Stop()
256-
fmt.Printf("\033[1A\033[2K")
300+
257301
if flags.Json {
258302
print := BeutifyJSON(response)
259303
fmt.Println(string(print))
@@ -262,7 +306,7 @@ func PollTranscription(token string, id string, flags TranscribeFlags) {
262306
getFormattedOutput(transcript, flags)
263307
return
264308
}
265-
time.Sleep(5 * time.Second)
309+
time.Sleep(3 * time.Second)
266310
}
267311
}
268312

@@ -271,7 +315,7 @@ func getFormattedOutput(transcript TranscriptResponse, flags TranscribeFlags) {
271315
if err != nil {
272316
width = 512
273317
}
274-
318+
fmt.Print("\033[H\033[2J")
275319
fmt.Println("Transcript")
276320
if transcript.SpeakerLabels == true {
277321
speakerLabelsPrintFormatted(transcript.Utterances, width)

cmd/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,19 @@ const (
221221
DriversLicense RedactPiiPolicy = "drivers_license"
222222
BankingInformation RedactPiiPolicy = "banking_information"
223223
)
224+
225+
type PostHogProperties struct {
226+
Poll bool `json:"poll"`
227+
Json bool `json:"json"`
228+
SpeakerLabels bool `json:"speaker_labels"`
229+
Punctuate bool `json:"punctuate"`
230+
FormatText bool `json:"format_text"`
231+
DualChannel *bool `json:"dual_channel"`
232+
RedactPii bool `json:"redact_pii"`
233+
AutoHighlights bool `json:"auto_highlights"`
234+
ContentModeration bool `json:"content_safety"`
235+
TopicDetection bool `json:"iab_categories"`
236+
SentimentAnalysis bool `json:"sentiment_analysis"`
237+
AutoChapters bool `json:"auto_chapters"`
238+
EntityDetection bool `json:"entity_detection"`
239+
}

cmd/utils.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"bytes"
8+
"context"
89
"encoding/json"
910
"io"
1011
"io/ioutil"
@@ -18,9 +19,9 @@ import (
1819
"github.com/joho/godotenv"
1920
"github.com/posthog/posthog-go"
2021
"golang.org/x/term"
22+
pb "gopkg.in/cheggaaa/pb.v1"
2123
)
2224

23-
var AAITokenEnvName = "ASSMEBLYAI_TOKEN"
2425
var AAIURL = "https://api.assemblyai.com/v2"
2526
var PH_TOKEN string
2627

@@ -106,12 +107,12 @@ func PrintError(err error) {
106107
}
107108
}
108109

109-
func QueryApi(token string, path string, method string, body io.Reader) []byte {
110+
func QueryApi(path string, method string, body io.Reader) []byte {
110111
resp, err := http.NewRequest(method, AAIURL+path, body)
111112
PrintError(err)
112113

113114
resp.Header.Add("Accept", "application/json")
114-
resp.Header.Add("Authorization", token)
115+
resp.Header.Add("Authorization", Token)
115116
resp.Header.Add("Transfer-Encoding", "chunked")
116117

117118
response, err := http.DefaultClient.Do(resp)
@@ -132,18 +133,19 @@ func BeutifyJSON(data []byte) []byte {
132133
return prettyJSON.Bytes()
133134
}
134135

135-
type PostHogProperties struct {
136-
Poll bool `json:"poll"`
137-
Json bool `json:"json"`
138-
SpeakerLabels bool `json:"speaker_labels"`
139-
Punctuate bool `json:"punctuate"`
140-
FormatText bool `json:"format_text"`
141-
DualChannel *bool `json:"dual_channel"`
142-
RedactPii bool `json:"redact_pii"`
143-
AutoHighlights bool `json:"auto_highlights"`
144-
ContentModeration bool `json:"content_safety"`
145-
TopicDetection bool `json:"iab_categories"`
146-
SentimentAnalysis bool `json:"sentiment_analysis"`
147-
AutoChapters bool `json:"auto_chapters"`
148-
EntityDetection bool `json:"entity_detection"`
136+
func showProgress(total int, ctx context.Context, bar *pb.ProgressBar) {
137+
for {
138+
bar.Prefix(" Transcribing file: ")
139+
bar.ShowBar = false
140+
bar.ShowTimeLeft = false
141+
bar.ShowCounters = false
142+
bar.ShowFinalTime = true
143+
for i := 0; i < total-1; i++ {
144+
bar.Set(i * total / 300)
145+
time.Sleep(100 * time.Millisecond)
146+
}
147+
bar.Finish()
148+
}
149149
}
150+
151+
var TranscriptionLength int

0 commit comments

Comments
 (0)