diff --git a/backend/DESIGN.md b/backend/DESIGN.md new file mode 100644 index 0000000..405d61e --- /dev/null +++ b/backend/DESIGN.md @@ -0,0 +1,4 @@ +API endpoints: /example + - GET -> Returns JSON from example + - POST -> Add JSON :q + diff --git a/backend/install b/backend/install new file mode 100644 index 0000000..1a1af7b --- /dev/null +++ b/backend/install @@ -0,0 +1,3 @@ +#!/bin/zsh + +# Install Docker diff --git a/backend/src/learning-go/web-services-gin/.idea/.gitignore b/backend/src/learning-go/web-services-gin/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/backend/src/learning-go/web-services-gin/.idea/modules.xml b/backend/src/learning-go/web-services-gin/.idea/modules.xml new file mode 100644 index 0000000..48939b7 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/backend/src/learning-go/web-services-gin/.idea/vcs.xml b/backend/src/learning-go/web-services-gin/.idea/vcs.xml new file mode 100644 index 0000000..4fce1d8 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/backend/src/learning-go/web-services-gin/.idea/web-services-gin.iml b/backend/src/learning-go/web-services-gin/.idea/web-services-gin.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/.idea/web-services-gin.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/backend/src/learning-go/web-services-gin/curl.sh b/backend/src/learning-go/web-services-gin/curl.sh new file mode 100755 index 0000000..d68ee52 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/curl.sh @@ -0,0 +1,6 @@ +curl http://localhost:8080/prompts -d '{ + "prompt": "68 D7 B8 23 C2 78 73 70 85 15 A0 9E B 88 C9 A0 E9 3 C 1D 85 D3 CF 62 23 C7 59 1F CB E6 27 D2 E1 63 60 65 3 5 C6 52 F3 B3 B0 D9 8 4 3F 16 86 B8 44 66 FF B7 B0 AC 6 B0 F9 1C 7F 78 EB 87 49 EB F9 76 C2 4 89 1B 9E BE 86 44 15 69 1A 86 A E0 E9 73 1C 0 99 D1", + "sum": "DGyij1YH2G5UJrRnleD6gn8WJ9Y=" +}' + +#curl http://localhost:8080/prompts diff --git a/backend/src/learning-go/web-services-gin/go.mod b/backend/src/learning-go/web-services-gin/go.mod new file mode 100644 index 0000000..a9a42e4 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/go.mod @@ -0,0 +1,34 @@ +module example.com/web-service-gin + +go 1.22.5 + +require github.com/gin-gonic/gin v1.10.0 + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/backend/src/learning-go/web-services-gin/go.sum b/backend/src/learning-go/web-services-gin/go.sum new file mode 100644 index 0000000..b657a9f --- /dev/null +++ b/backend/src/learning-go/web-services-gin/go.sum @@ -0,0 +1,79 @@ +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/backend/src/learning-go/web-services-gin/main.go b/backend/src/learning-go/web-services-gin/main.go new file mode 100644 index 0000000..f561d25 --- /dev/null +++ b/backend/src/learning-go/web-services-gin/main.go @@ -0,0 +1,191 @@ +package main + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "crypto/sha1" + "encoding/base64" + "errors" + "fmt" + "github.com/gin-gonic/gin" + "io" + "net/http" + "strconv" + "strings" +) + +// TEST VALUES: +// KEY: passphrasewhichneedstobe32bytes! // TODO: Test with 256 bytes +// INPUT: The ultimate question: of life, the universe, of everything! +// SHA-1 HASH: 0c6ca28f5607d86e5426b46795e0fa827f1627d6 +// BASE64 HASH: string DGyij1YH2G5UJrRnleD6gn8WJ9Y= +// AES OUTPUT: []uint8{104, 215, 184, 35, 194, 120, 115, 112, 133, 21, 160, 158, 11, 136, 201, 160, 233, 3, 12, 29, 133, 211, 207, 98, 35, 199, 89, 31, 203, 230, 39, 210, 225, 99, 96, 101, 3, 5, 198, 82, 243, 179, 176, 217, 8, 4, 63, 22, 134, 184, 68, 102, 255, 183, 176, 172, 6, 176, 249, 28, 127, 120, 235, 135, 73, 235, 249, 118, 194, 4, 137, 27, 158, 190, 134, 68, 21, 105, 26, 134, 10, 224, 233, 115, 28, 0, 153, 209} +// HEX AES: "68 D7 B8 23 C2 78 73 70 85 15 A0 9E B 88 C9 A0 E9 3 C 1D 85 D3 CF 62 23 C7 59 1F CB E6 27 D2 E1 63 60 65 3 5 C6 52 F3 B3 B0 D9 8 4 3F 16 86 B8 44 66 FF B7 B0 AC 6 B0 F9 1C 7F 78 EB 87 49 EB F9 76 C2 4 89 1B 9E BE 86 44 15 69 1A 86 A E0 E9 73 1C 0 99 D1" + +var rawHash = "DGyij1YH2G5UJrRnleD6gn8WJ9Y=" + +type request struct { + //UserID string `json:"id"` + Prompt string `json:"prompt"` + Sum string `json:"sum"` +} + +var requests []request + +// TODO: Test if 256 byte key works. +// TODO: Dynamically get key? Need to research best way to store private keys between two devices. +var key = []byte("passphrasewhichneedstobe32bytes!") + +func encrypt() { + text := []byte("The ultimate question: of life, the universe, of everything!") + + // generate a new aes cipher using our 32 byte long key + c, err := aes.NewCipher(key) + // if there are any errors, handle them + if err != nil { + fmt.Println(err) + } + + // gcm or Galois/Counter Mode, is a mode of operation + // for symmetric key cryptographic block ciphers + // - https://en.wikipedia.org/wiki/Galois/Counter_Mode + gcm, err := cipher.NewGCM(c) + // if any error generating new GCM + // handle them + if err != nil { + fmt.Println(err) + } + + // creates a new byte array the size of the nonce + // which must be passed to Seal + nonce := make([]byte, gcm.NonceSize()) + // populates our nonce with a cryptographically secure + // random sequence + if _, err = io.ReadFull(rand.Reader, nonce); err != nil { + fmt.Println(err) + } + + // here we encrypt our text using the Seal function + // Seal encrypts and authenticates plaintext, authenticates the + // additional data and appends the result to dst, returning the updated + // slice. The nonce must be NonceSize() bytes long and unique for all + // time, for a given key. + fmt.Println(gcm.Seal(nonce, nonce, text, nil)) + + //// the WriteFile method returns an error if unsuccessful + //err = ioutil.WriteFile("myfile", gcm.Seal(nonce, nonce, text, nil), 0777) + //// handle this error + //if err != nil { + // // print it out + // fmt.Println(err) + //} +} + +func convertHexToBytes(hexString *string) ([]uint8, error) { + // Handle nil pointer case + if hexString == nil { + return nil, errors.New("nil pointer provided for hex string") + } + + // Split the hex string by spaces + hexBytes := strings.Fields(*hexString) + + // Initialize an empty slice for uint8 + data := make([]uint8, len(hexBytes)) + + // Iterate and convert each hex byte + for i, hexByte := range hexBytes { + // Convert each hex string to a uint8 value (handling errors) + value, err := strconv.ParseUint(hexByte, 16, 8) + if err != nil { + return nil, fmt.Errorf("error parsing hex byte '%s': %w", hexByte, err) + } + + // Assign the converted value to the slice + data[i] = uint8(value) + } + + // Return the slice of uint8 and any errors encountered + return data, nil +} + +func decrypt(input *request) bool { + //ciphertext, err := ioutil.ReadFile("myfile") + ciphertext, err := convertHexToBytes(&input.Prompt) + + // if our program was unable to read the file + // print out the reason why it can't + if err != nil { + fmt.Println(err) + } + + c, err := aes.NewCipher(key) + if err != nil { + fmt.Println(err) + } + + gcm, err := cipher.NewGCM(c) + if err != nil { + fmt.Println(err) + } + + nonceSize := gcm.NonceSize() + if len(ciphertext) < nonceSize { + fmt.Println(err) + } + + nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] + plaintext, err := gcm.Open(nil, nonce, ciphertext, nil) + if err != nil { + fmt.Println(err) + } + s := string(plaintext) + if validateHash(s) { + input.Prompt = s + return true + } + input.Prompt = "DATA CORRUPTED OR TAMPERED" + return false +} + +// Validate there's no tampering with SHA-1 sum. The decrypted hash and the transmitted hash should be identical. +func validateHash(decrypted string) bool { + // Calculate the SHA256 sum of the decrypted request + hasher := sha1.New() + hasher.Write([]byte(decrypted)) + decryptedHashString := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + + // TODO: Add error handling if hash doesn't match. + return decryptedHashString == rawHash +} + +// User HTTP GET request has the prompt decrypted and verified with a SHA-1 checksum. +// If there's been no data corruption, re-construct user prompt for ollama +// TODO: Eventually, add additional logic that will allow for re-direction and contextual awareness (pre-processing) +func postRequest(c *gin.Context) { + var newInput request + + //Call BindJSON to bind the received JSON to + if err := c.BindJSON(&newInput); err != nil { + return + } + + if decrypt(&newInput) { + requests = append(requests, newInput) + c.IndentedJSON(http.StatusCreated, newInput) // TODO: Formulate prompt and transfer to AI layer to formulate new JSON response back + } else { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Failed checksum. Presumably data corrupted."}) + + } +} + +func main() { + router := gin.Default() + router.POST("/prompts", postRequest) + + err := router.Run("localhost:8080") + if err != nil { + return + } +} diff --git a/server/requirements.txt b/server/requirements.txt deleted file mode 100644 index fd5ad5d..0000000 --- a/server/requirements.txt +++ /dev/null @@ -1,267 +0,0 @@ -aiodns==3.0.0 -aiohttp==3.9.5 -aiosignal==1.3.1 -antlr4-python3-runtime==4.9.3 -anyio==3.7.1 -appdirs==1.4.4 -argcomplete==3.3.0 -argon2-cffi==21.3.0 -argon2-cffi-bindings==21.2.0 -arrow==1.2.3 -asttokens==2.4.1 -async-lru==2.0.4 -attrs==23.2.0 -Babel==2.15.0 -backcall==0.1.0 -Beaker==1.12.1 -beautifulsoup4==4.12.3 -bleach==6.1.0 -blivet==3.10.0 -blivet-gui==2.5.0 -blosc2==2.5.1 -boto3==1.34.99 -botocore==1.34.99 -Bottleneck==1.3.7 -Brlapi==0.8.5 -Brotli==1.1.0 -cachetools==5.3.2 -certifi==2023.5.7 -cffi==1.16.0 -chardet==5.2.0 -charset-normalizer==3.3.2 -click==8.1.7 -click-plugins==1.1.1 -colorama==0.4.6 -comm==0.2.1 -contourpy==1.2.0 -cryptography==41.0.7 -cssselect==1.1.0 -cupshelpers==1.0 -cycler==0.11.0 -dasbus==1.7 -dbus-python==1.3.2 -decorator==5.1.1 -defusedxml==0.7.1 -distro==1.9.0 -entrypoints==0.4 -et-xmlfile==1.1.0 -executing==2.0.1 -fastjsonschema==2.18.0 -file-magic==0.4.0 -filelock==3.14.0 -fonttools==4.50.0 -fqdn==1.5.1 -fros==1.1 -frozenlist==1.4.1 -fs==2.4.16 -fsspec==2024.3.1 -gcsfs==2023.6.0+1.g7cc53d9 -google-api-core==2.11.1 -google-auth==2.29.0 -google-auth-oauthlib==0.8.0 -google-cloud-core==2.3.3 -google-cloud-storage==2.14.0 -google-crc32c==1.5.0 -google-resumable-media==2.7.0 -googleapis-common-protos==1.63.0 -gpg==1.23.2 -greenlet==3.0.3 -grpcio==1.48.4 -grpcio-status==1.48.4 -h11==0.14.0 -html5lib==1.1 -httpcore==1.0.2 -httpx==0.26.0 -humanize==3.13.1 -idna==3.7 -inkex==1.3.1 -ipykernel==6.29.3 -ipython==8.23.0 -ipython-genutils==0.2.0 -iso639==0.1.4 -isoduration==20.11.0 -jdcal==1.4.1 -jedi==0.19.1 -Jinja2==3.1.4 -jmespath==1.0.1 -json5==0.9.24 -jsonpointer==2.3 -jsonschema==4.19.1 -jsonschema-specifications==2023.11.2 -jupyter-events==0.9.0 -jupyter-lsp==2.2.2 -jupyter_client==7.4.9 -jupyter_core==5.1.0 -jupyter_server==2.12.5 -jupyter_server_terminals==0.4.2 -jupyterlab==4.1.0 -jupyterlab_pygments==0.3.0 -jupyterlab_server==2.25.2 -kiwisolver==1.4.5 -langtable==0.0.66 -libcomps==0.1.20 -louis==3.28.0 -lxml==5.1.0 -Mako==1.2.3 -MarkupSafe==2.1.3 -matplotlib==3.8.4 -matplotlib-inline==0.1.6 -mistune==2.0.4 -mpmath==1.3.0 -msgpack==1.0.6 -multidict==6.0.4 -munkres==1.1.2 -nbclassic==0.5.6 -nbclient==0.9.0 -nbconvert==7.16.0 -nbformat==5.9.2 -ndindex==1.7 -nest-asyncio==1.6.0 -netifaces==0.11.0 -networkx==3.3 -nftables==0.1 -notebook==7.0.7 -notebook_shim==0.2.3 -numexpr==2.8.5 -numpy==1.26.4 -nvidia-cublas-cu12==12.1.3.1 -nvidia-cuda-cupti-cu12==12.1.105 -nvidia-cuda-nvrtc-cu12==12.1.105 -nvidia-cuda-runtime-cu12==12.1.105 -nvidia-cudnn-cu12==8.9.2.26 -nvidia-cufft-cu12==11.0.2.54 -nvidia-curand-cu12==10.3.2.106 -nvidia-cusolver-cu12==11.4.5.107 -nvidia-cusparse-cu12==12.1.0.106 -nvidia-nccl-cu12==2.20.5 -nvidia-nvjitlink-cu12==12.5.40 -nvidia-nvtx-cu12==12.1.105 -oauthlib==3.2.2 -odfpy==1.4.1 -olefile==0.47 -omegaconf==2.3.0 -openpyxl==3.1.2 -packaging==23.2 -pandas==2.2.1 -pandocfilters==1.5.1 -parso==0.8.3 -Paste==3.7.1 -pexpect==4.9.0 -pickleshare==0.7.5 -pid==2.2.3 -pillow==10.3.0 -platformdirs==3.11.0 -ply==3.11 -pooch==1.5.2 -productmd==1.38 -progressbar2==3.53.2 -prometheus-client==0.19.0 -prompt-toolkit==3.0.41 -protobuf==3.19.6 -psutil==5.9.8 -psycopg2==2.9.9 -ptyprocess==0.7.0 -pure-eval==0.2.2 -pwquality==1.4.5 -py-cpuinfo==9.0.0 -pyarrow==15.0.2 -pyasn1==0.5.1 -pyasn1-modules==0.3.0 -pycairo==1.25.1 -pycares==4.3.0 -pycparser==2.20 -pycrypto==2.6.1 -pycups==2.0.4 -pycurl==7.45.2 -pydub==0.25.1 -pyenchant==3.2.2 -pygit2==1.14.0 -Pygments==2.17.2 -PyGObject==3.48.2 -pykickstart==3.52 -PyMySQL==1.1.0 -PyOpenGL==3.1.7 -PyOpenGL-accelerate==3.1.7 -pyOpenSSL==23.2.0 -pyparsing==3.1.2 -pyparted==3.13.0 -PyQt5==5.15.10 -PyQt5-sip==12.13.0 -pyqtgraph==0.13.3 -pyrsistent==0.20.0 -pyserial==3.5 -PySocks==1.7.1 -python-augeas==1.1.0 -python-dateutil==2.8.2 -python-json-logger==2.0.4 -python-meh==0.51 -python-pam==2.0.2 -python-snappy==0.6.1 -python-utils==3.7.0 -pytz==2024.1 -pyudev==0.24.1 -pyxdg==0.27 -PyYAML==6.0.1 -pyzmq==25.1.1 -QtPy==2.4.1 -referencing==0.31.1 -regex==2024.4.16 -requests==2.31.0 -requests-file==2.0.0 -requests-ftp==0.3.1 -requests-oauthlib==1.3.1 -rfc3339-validator==0.1.4 -rfc3986-validator==0.1.1 -rpds-py==0.18.0 -rpm==4.19.1.1 -rpmautospec==0.6.3 -rpmautospec-core==0.1.4 -rsa==4.9 -s3transfer==0.10.1 -SciPy==1.11.3 -scour==0.38.2 -selinux @ file:///builddir/build/BUILD/libselinux-3.6/src -Send2Trash==1.8.2 -sepolicy @ file:///builddir/build/BUILD/selinux-3.6/python/sepolicy -setools==4.5.1 -setuptools==69.0.3 -shtab==1.6.1 -silero==0.4.1 -simpleaudio==1.0.4 -simpleline==1.9.0 -six==1.16.0 -sniffio==1.3.0 -sos==4.7.1 -sounddevice==0.4.6 -soupsieve==2.5 -SQLAlchemy==2.0.30 -stack-data==0.6.3 -sympy==1.12 -systemd-python==235 -tables==3.9.2 -tabulate==0.9.0 -Tempita==0.5.2 -termcolor==2.3.0 -terminado==0.18.0 -thrift==0.15.0 -tinycss2==1.2.1 -tldr==3.2.0 -torch==2.3.0 -torchaudio==2.3.0 -tornado==6.3.3 -traitlets==5.14.1 -typing_extensions==4.9.0 -uri-template==1.2.0 -urllib3==1.26.18 -wavio==0.0.9 -wcwidth==0.2.13 -webcolors==1.13 -webencodings==0.5.1 -websocket-client==1.3.3 -xarray==2023.8.0 -xkbregistry==0.3 -xlrd==2.0.1 -XlsxWriter==3.1.9 -xlwt==1.3.0 -yarl==1.9.2 -zstandard==0.22.0 diff --git a/server/stt.py b/server/stt.py deleted file mode 100644 index 323ad20..0000000 --- a/server/stt.py +++ /dev/null @@ -1,72 +0,0 @@ -# import required libraries -from glob import glob -from scipy.io.wavfile import write -from pydub import AudioSegment -from pydub.playback import play - -import os -import torch -import zipfile -import torchaudio -import sounddevice as sd -import wave -import wavio as wv - -########### Record audio -def record_audio(audio, duration=5, freq=192000): - # Start recorder with the given values of duration and sample frequency - recording = sd.rec(int(duration * freq), samplerate=freq, channels=2) - - print('Please say your prompt. You have ' + str(duration) + ' seconds.') - # Record audio for the given number of seconds - sd.wait() - - # This will convert the NumPy array to an audio file with the given sampling frequency - write(audio, freq, recording) - -def speech_to_text(audio): - device = torch.device('cuda') # cuda also works, but our models are fast enough for CPU - model, decoder, utils = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_stt', language='en', device=device) - (read_batch, split_into_batches, read_audio, prepare_model_input) = utils # see function signature for details - - test_files = glob(audio) - batches = split_into_batches(test_files, batch_size=100) - input = prepare_model_input(read_batch(batches[0]), device=device) - - text = "" - output = model(input) - for example in output: - text += decoder(example.cpu()) - - return text - -def text_to_speech(): - language = 'en' - model_id = 'v3_en' - sample_rate = 8000 - speaker = 'en_1' - device = torch.device('cuda') - - model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=language, speaker=model_id) - model.to(device) # cuda or cpu - - text = open("output.txt").read() - audio = model.apply_tts(text, speaker=speaker, sample_rate=sample_rate) - torchaudio.save("output.mp3", audio.unsqueeze(0), sample_rate=8000) - - song = AudioSegment.from_mp3("output.mp3") - play(song) - -def main(): - audio = "input.wav" - record_audio(audio) - prompt = "in 50 words or less, " + speech_to_text(audio) - print(prompt) - - command = "ollama run llama3 " + prompt + " > output.txt" - os.system(command) - text_to_speech() - - -if __name__ == "__main__": - main()