Skip to content

Commit

Permalink
merging with ezra-new
Browse files Browse the repository at this point in the history
  • Loading branch information
benleem committed Oct 1, 2024
2 parents f4baa19 + 1e04144 commit a10cd27
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ test_audio
test.mp3

dist/

test1.mp3
test2.mp3
77 changes: 57 additions & 20 deletions cmd/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/benleem/prattl/embed"
"github.com/benleem/prattl/pysrc"

"github.com/spf13/cobra"
)

Expand All @@ -17,35 +22,59 @@ var transcribeCmd = &cobra.Command{
Use: "transcribe <filepath/to/audio.mp3>",
Short: "Transcribe the provided audio file (file path)",
Long: `This command transcribes the provided audiofile and prints the resulting string`,
Args: cobra.MaximumNArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("%s", "no file path provided\n")
}
transcription, err := transcribe(args[0])

transcriptionMap := make(map[string]string)
transcriptions, err := transcribe(args)
if err != nil {
return err
}
fmt.Println(transcription)

for i, trans := range transcriptions {
transcriptionMap[args[i]] = trans
// _, err := io.WriteString(os.Stdout, trans+"\n")
// if err != nil {
// return fmt.Errorf("error writing to stdout: %v", err)
// }
}

jsonOutput, err := json.Marshal(transcriptionMap)
if err != nil {
return fmt.Errorf("error marshaling to JSON: %v", err)
}

_, err = io.WriteString(os.Stdout, string(jsonOutput)+"\n")
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}

return nil
},
}

func transcribe(fp string) (string, error) {
// s := spinner.New(spinner.CharSets[35], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
// s.Prefix = "transcribing: "
// s.Suffix = "\n"
// s.Start()
// defer s.Stop()
func transcribe(fps []string) ([]string, error) {
returnStrings := []string{}

fileBytes, err := os.ReadFile(fp)
if err != nil {
return "", err
}
var allBytes []byte
for i, fp := range fps {
fileBytes, err := os.ReadFile(fp)
if err != nil {
return returnStrings, err
}
allBytes = append(allBytes, fileBytes...)

if i < len(fps)-1 {
allBytes = append(allBytes, embed.CodeBytes...)
}

}
program, err := pysrc.ReturnFile("transcribe.py")
if err != nil {
return "", err
return returnStrings, err
}

env, err := pysrc.PrattlEnv()
Expand All @@ -58,21 +87,29 @@ func transcribe(fp string) (string, error) {
var stderr bytes.Buffer
stdin, err := cmd.StdinPipe()
if err != nil {
return "", fmt.Errorf("error instantiating pipe: " + err.Error())
return returnStrings, fmt.Errorf("error instantiating pipe: " + err.Error())
}
cmd.Stdout = &out
cmd.Stderr = &stderr
if err = cmd.Start(); err != nil {
return "", fmt.Errorf("error starting command: " + err.Error())
return returnStrings, fmt.Errorf("error starting command: " + err.Error())
}
_, err = stdin.Write(fileBytes)
_, err = stdin.Write(allBytes)
if err != nil {
return "", fmt.Errorf("error writing to stdin: " + err.Error())
return returnStrings, fmt.Errorf("error writing to stdin: " + stderr.String())
}
stdin.Close()
if err = cmd.Wait(); err != nil {
return "", fmt.Errorf("error waiting for command: " + err.Error())
return returnStrings, fmt.Errorf("error waiting for command: " + stderr.String())
}

output := out.String()
return output, nil

returnStrings = strings.Split(strings.ToLower(output), embed.SeparatorExpectedString)

for _, str := range returnStrings {
str = fmt.Sprintf("---%s---\n", str)
}

return returnStrings, nil
}
Binary file added embed/code.mp3
Binary file not shown.
8 changes: 8 additions & 0 deletions embed/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package embed

import _ "embed"

//go:embed code.mp3
var CodeBytes []byte

const SeparatorExpectedString = "crimson falcon, silent thunder, midnight echo."

0 comments on commit a10cd27

Please sign in to comment.