From 5ff50737d32974a92a688b5607578db0f51e6be6 Mon Sep 17 00:00:00 2001 From: Lukasz Date: Tue, 19 Mar 2024 18:00:05 +0100 Subject: [PATCH] Move language map to json file in order to not trigger microsoft antivirus --- README.md | 4 +- internal/resources/cli_arguments.go | 62 +++++++++++++++++---- internal/resources/lang_mapper.go | 86 ----------------------------- languageMap.json | 84 ++++++++++++++++++++++++++++ main.go | 31 ++++++----- 5 files changed, 153 insertions(+), 114 deletions(-) delete mode 100644 internal/resources/lang_mapper.go create mode 100644 languageMap.json diff --git a/README.md b/README.md index d351204..2375202 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ go build -ldflags "-s -w" -o server.exe main.go Make a request to the server using the following command: ```sh -curl http://localhost:3000/v1/audio/transcriptions \ +curl http://localhost:3031/v1/audio/transcriptions \ -H "Content-Type: multipart/form-data" \ -F file="@/path/to/file/audio.mp3" \ ``` @@ -61,7 +61,7 @@ Receive a response in JSON format: 2. Open the plugin's settings. 3. Set the following values: - API KEY: `sk-1` - - API URL: `http://localhost:3000/v1/audio/transcriptions` + - API URL: `http://localhost:3031/v1/audio/transcriptions` - Model: `whisper-1` # Roadmap diff --git a/internal/resources/cli_arguments.go b/internal/resources/cli_arguments.go index 27dd6df..3a29a48 100644 --- a/internal/resources/cli_arguments.go +++ b/internal/resources/cli_arguments.go @@ -1,8 +1,12 @@ package resources import ( + "encoding/json" "flag" "fmt" + "io" + "os" + "strconv" "strings" ) @@ -10,10 +14,46 @@ import ( type Arguments struct { Language string ModelPath string + Port int } type ParsedArguments struct { Language int32 ModelPath string + Port int +} + +type LanguageMap map[string]string + +func processLanguageAndCode(args *Arguments) (int32, error) { + // Read the language map from JSON file + jsonFile, err := os.Open("languageMap.json") + if err != nil { + return 0x6E65, fmt.Errorf("error opening language map: %w", err) // Wrap error for context + } + defer jsonFile.Close() + + byteData, err := io.ReadAll(jsonFile) + if err != nil { + return 0x6E65, fmt.Errorf("error reading language map: %w", err) + } + + var languageMap LanguageMap + err = json.Unmarshal(byteData, &languageMap) + if err != nil { + return 0x6E65, fmt.Errorf("error parsing language map: %w", err) + } + + hexCode, ok := languageMap[strings.ToLower(args.Language)] + if !ok { + return 0x6E65, fmt.Errorf("unsupported language: %s", args.Language) + } + + languageCode, err := strconv.ParseInt(hexCode, 0, 32) + if err != nil { + return 0x6E65, fmt.Errorf("error converting hex code: %w", err) + } + + return int32(languageCode), nil } // ParseFlags parses command line arguments and returns an Arguments struct @@ -24,6 +64,8 @@ func ParseFlags() (*ParsedArguments, error) { flag.StringVar(&args.Language, "language", "", "Language to be processed") // Optional: Redundant to demonstrate flag.StringVar(&args.ModelPath, "m", "", "Path to the model file (required)") flag.StringVar(&args.ModelPath, "modelPath", "", "Path to the model file (required)") // Optional: Redundant + flag.IntVar(&args.Port, "p", 3031, "Port to start the server on") + flag.IntVar(&args.Port, "port", 3031, "Port to start the server on") // Optional: Redundant flag.Usage = func() { fmt.Println("Usage: your_program [OPTIONS]") @@ -36,23 +78,19 @@ func ParseFlags() (*ParsedArguments, error) { args.Language = strings.ToLower(args.Language) - var pickedCode int32 - // Validate against LanguageMap and get associated code - if code, exists := LanguageMap[args.Language]; exists { - fmt.Println("Language code:", code) // Use the code as needed - pickedCode = code - } else { - fmt.Println("unsupported language: ", args.Language, " Defaulting to english") - pickedCode = 0x6E65 // Default to english - } - // Check for required flags - if args.ModelPath == "" { return nil, fmt.Errorf("modelPath argument is required") } + languageCode, err := processLanguageAndCode(args) + if err != nil { + fmt.Println("Error setting language, defaulting to English:", err) + // Use default language code directly as the result here + } + return &ParsedArguments{ - Language: pickedCode, + Language: languageCode, ModelPath: args.ModelPath, + Port: args.Port, }, nil } diff --git a/internal/resources/lang_mapper.go b/internal/resources/lang_mapper.go deleted file mode 100644 index 1e0126c..0000000 --- a/internal/resources/lang_mapper.go +++ /dev/null @@ -1,86 +0,0 @@ -package resources - -var LanguageMap = map[string]int32{ - "af": 0x6661, // Afrikaans - "sq": 0x7173, // Albanian - "am": 0x6D61, // Amharic - "ar": 0x7261, // Arabic - "hy": 0x7968, // Armenian - "as": 0x7361, // Assamese - "az": 0x7A61, // Azerbaijani - "ba": 0x6162, // Bashkir - "eu": 0x7565, // Basque - "be": 0x6562, // Belarusian - "bn": 0x6E62, // Bengali - "bs": 0x7362, // Bosnian - "br": 0x7262, // Breton - "bg": 0x6762, // Bulgarian - "ca": 0x6163, // Catalan - "zh": 0x687A, // Chinese - "hr": 0x7268, // Croatian - "cs": 0x7363, // Czech - "da": 0x6164, // Danish - "nl": 0x6C6E, // Dutch - "en": 0x6E65, // English - "et": 0x7465, // Estonian - "fo": 0x6F66, // Faroese - "fi": 0x6966, // Finnish - "fr": 0x7266, // French - "gl": 0x6C67, // Galician - "ka": 0x616B, // Georgian - "de": 0x7265, // German - "el": 0x6C61, // Greek - "gu": 0x7567, // Gujarati - "he": 0x6568, // Hebrew - "hi": 0x6968, // Hindi - "hu": 0x7568, // Hungarian - "is": 0x7369, // Icelandic - "id": 0x6469, // Indonesian - "it": 0x7469, // Italian - "ja": 0x616A, // Japanese - "kn": 0x6E6B, // Kannada - "kk": 0x6B6B, // Kazakh - "km": 0x6D6B, // Khmer - "ko": 0x6F6B, // Korean - "ky": 0x796B, // Kyrgyz - "lo": 0x6F6C, // Lao - "lv": 0x766C, // Latvian - "lt": 0x746C, // Lithuanian - "mk": 0x6B6D, // Macedonian - "ms": 0x736D, // Malay - "ml": 0x6C6D, // Malayalam - "mr": 0x726D, // Marathi - "mn": 0x6E6D, // Mongolian - "ne": 0x6570, // Nepali - "no": 0x6F6E, // Norwegian - "or": 0x726F, // Oriya - "ps": 0x7368, // Pashto - "fa": 0x6172, // Persian - "pl": 0x6C70, // Polish - "pt": 0x7470, // Portuguese - "pa": 0x6170, // Punjabi - "ro": 0x6F72, // Romanian - "ru": 0x7572, // Russian - "sa": 0x6173, // Sanskrit - "sr": 0x7273, // Serbian - "sd": 0x6473, // Sindhi - "si": 0x6973, // Sinhalese - "sk": 0x6B73, // Slovak - "sl": 0x6C73, // Slovenian - "es": 0x6573, // Spanish - "sw": 0x7773, // Swahili - "sv": 0x6576, // Swedish - "tg": 0x6769, // Tajik - "ta": 0x6174, // Tamil - "te": 0x6574, // Telugu - "th": 0x6874, // Thai - "tr": 0x7274, // Turkish - "uk": 0x6B75, // Ukrainian - "ur": 0x7275, // Urdu - "uz": 0x7A75, // Uzbek - "vi": 0x6976, // Vietnamese - "cy": 0x7963, // Welsh - "xh": 0x6877, // Xhosa - "yi": 0x6979, // Yiddish - "yo": 0x6F79, // Yoruba -} diff --git a/languageMap.json b/languageMap.json new file mode 100644 index 0000000..936a47a --- /dev/null +++ b/languageMap.json @@ -0,0 +1,84 @@ +{ + "af": "0x6661", + "sq": "0x7173", + "am": "0x6D61", + "ar": "0x7261", + "hy": "0x7968", + "as": "0x7361", + "az": "0x7A61", + "ba": "0x6162", + "eu": "0x7565", + "be": "0x6562", + "bn": "0x6E62", + "bs": "0x7362", + "br": "0x7262", + "bg": "0x6762", + "ca": "0x6163", + "zh": "0x687A", + "hr": "0x7268", + "cs": "0x7363", + "da": "0x6164", + "nl": "0x6C6E", + "en": "0x6E65", + "et": "0x7465", + "fo": "0x6F66", + "fi": "0x6966", + "fr": "0x7266", + "gl": "0x6C67", + "ka": "0x616B", + "de": "0x7265", + "el": "0x6C61", + "gu": "0x7567", + "he": "0x6568", + "hi": "0x6968", + "hu": "0x7568", + "is": "0x7369", + "id": "0x6469", + "it": "0x7469", + "ja": "0x616A", + "kn": "0x6E6B", + "kk": "0x6B6B", + "km": "0x6D6B", + "ko": "0x6F6B", + "ky": "0x796B", + "lo": "0x6F6C", + "lv": "0x766C", + "lt": "0x746C", + "mk": "0x6B6D", + "ms": "0x736D", + "ml": "0x6C6D", + "mr": "0x726D", + "mn": "0x6E6D", + "ne": "0x6570", + "no": "0x6F6E", + "or": "0x726F", + "ps": "0x7368", + "fa": "0x6172", + "pl": "0x6C70", + "pt": "0x7470", + "pa": "0x6170", + "ro": "0x6F72", + "ru": "0x7572", + "sa": "0x6173", + "sr": "0x7273", + "sd": "0x6473", + "si": "0x6973", + "sk": "0x6B73", + "sl": "0x6C73", + "es": "0x6573", + "sw": "0x7773", + "sv": "0x6576", + "tg": "0x6769", + "ta": "0x6174", + "te": "0x6574", + "th": "0x6874", + "tr": "0x7274", + "uk": "0x6B75", + "ur": "0x7275", + "uz": "0x7A75", + "vi": "0x6976", + "cy": "0x7963", + "xh": "0x6877", + "yi": "0x6979", + "yo": "0x6F79" +} diff --git a/main.go b/main.go index a138a33..5465df9 100644 --- a/main.go +++ b/main.go @@ -12,19 +12,7 @@ import ( "github.com/xzeldon/whisper-api-server/internal/resources" ) -func main() { - - e := echo.New() - e.HideBanner = true - - args, errParsing := resources.ParseFlags() - if errParsing != nil { - e.Logger.Error("Error parsing flags: ", errParsing) - return - } - - e.Use(middleware.CORS()) - +func change_working_directory(e *echo.Echo) { exePath, errs := os.Executable() if errs != nil { e.Logger.Error(errs) @@ -42,6 +30,21 @@ func main() { cwd, _ := os.Getwd() fmt.Println("Current working directory:", cwd) +} + +func main() { + + e := echo.New() + e.HideBanner = true + change_working_directory(e) + + args, errParsing := resources.ParseFlags() + if errParsing != nil { + e.Logger.Error("Error parsing flags: ", errParsing) + return + } + + e.Use(middleware.CORS()) if l, ok := e.Logger.(*log.Logger); ok { l.SetHeader("${time_rfc3339} ${level}") @@ -58,5 +61,5 @@ func main() { return api.Transcribe(c, whisperState) }) - e.Logger.Fatal(e.Start("127.0.0.1:3000")) + e.Logger.Fatal(e.Start(fmt.Sprintf("127.0.0.1:%d", args.Port))) }