diff --git a/controller/prediction_controller.go b/controller/prediction_controller.go index 475632248e28f0183fa223b0b582821e89e206ce..eca91df5c93d1c77a965c83af5e2a996094962ca 100644 --- a/controller/prediction_controller.go +++ b/controller/prediction_controller.go @@ -80,7 +80,7 @@ func (c *predictionController) Predict(ctx *gin.Context) { requestImage(ctx, &predictionRequest.ImageFile, &predictionRequest.ImageFileName) requestAudio(ctx, &predictionRequest.AudioQuestionFile, &predictionRequest.AudioQuestionFilename) - predictionResult, text_output := c.service.Predict(ctx, predictionRequest) + predictionResult, text_output := c.service.Predict(ctx.Request.Context(), predictionRequest) if c.service.Error() != nil { c.Response(ctx, nil) @@ -91,5 +91,4 @@ func (c *predictionController) Predict(ctx *gin.Context) { ctx.Header("Content-Disposition", "inline; filename=response.mp3") ctx.Header("X-Response-Text", text_output) ctx.Data(200, "audio/mpeg", predictionResult) - } diff --git a/logs/error_log.txt b/logs/error_log.txt index 4551241cd6993ae3c0d921accbce16c2f160408b..b33de5212bad0512839b807354f2476f7b333fe7 100644 --- a/logs/error_log.txt +++ b/logs/error_log.txt @@ -17,3 +17,10 @@ open audio: is a directory 2025/06/17 00:30:56 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] 2025/06/17 00:32:14 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] 2025/06/17 00:33:03 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/21 20:30:08 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/21 21:21:08 Error Log : duplicated key not allowed; invalid transaction +2025/06/21 21:23:54 Error Log : duplicated key not allowed; invalid transaction +duplicated key not allowed; invalid transaction; invalid transaction; invalid transaction +2025/06/21 21:27:10 Error Log : duplicated key not allowed; invalid transaction +2025/06/21 21:48:34 Error Log : duplicated key not allowed; invalid transaction +duplicated key not allowed; invalid transaction; invalid transaction; invalid transaction diff --git a/repositories/account_repository.go b/repositories/account_repository.go index 6677a965683faa76fee9c4f3b825df67946f4c60..33a3cf86356f4330f74f9b90cbfb4a21887cc1f8 100644 --- a/repositories/account_repository.go +++ b/repositories/account_repository.go @@ -13,7 +13,7 @@ type AccountRepository interface { } type accountRepository struct { - *repository[models.Account] + repository[models.Account] } func (r *accountRepository) CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) { diff --git a/repositories/chat_history_repository.go b/repositories/chat_history_repository.go index cbc4474f45e2a04bf55b1d0e4700df8aa7db9fb2..c9359f92f3dfa628ca2453382fbf36d05f8ac863 100644 --- a/repositories/chat_history_repository.go +++ b/repositories/chat_history_repository.go @@ -13,14 +13,13 @@ type ChatHistoryRepository interface { } type chatHistoryRepository struct { - *repository[models.ChatHistory] + repository[models.ChatHistory] } func NewChatHistoryRepository() ChatHistoryRepository { return &chatHistoryRepository{ - repository: &repository[models.ChatHistory]{ + repository: repository[models.ChatHistory]{ transaction: config.DB.Begin(), - entity: models.ChatHistory{}, }, } } diff --git a/repositories/repository.go b/repositories/repository.go index d935995488d9329661fc471ff1d49b15e3d149c4..be89cb89c5cfa0b9c1f4a4bbc3fb0f5c37f6c6cb 100644 --- a/repositories/repository.go +++ b/repositories/repository.go @@ -2,7 +2,6 @@ package repositories import ( "context" - "sync" "gorm.io/gorm" ) @@ -32,7 +31,6 @@ type CustomQueryConstructor struct { } type repository[TEntity any] struct { - sync.Mutex entity TEntity pagination PaginationConstructor customQuery CustomQueryConstructor @@ -49,37 +47,37 @@ func (repo *repository[T1]) RowsCount() int { return repo.rowsCount } func (repo *repository[T1]) IsNoRecord() bool { - repo.Lock() + repo.noRecord = repo.transaction.RowsAffected == 0 - repo.Unlock() + return repo.noRecord } func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) { - repo.Lock() + act(ctx, repo.transaction) - repo.Unlock() + } func (repo *repository[T1]) Where(ctx context.Context) { tx := repo.transaction tx.WithContext(ctx).Where(&repo.entity) - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + } func (repo *repository[T1]) Find(ctx context.Context, res any) { - repo.Lock() + tx := repo.transaction tx.WithContext(ctx).Find(&res) if tx.Error != nil { tx.Rollback() } - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + } func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { @@ -89,25 +87,25 @@ func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { if tx.Error != nil { tx.Rollback() } - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + + return + } func (repo *repository[T1]) Create(ctx context.Context) { - tx := repo.transaction - tx.Create(&repo.entity).Find(&repo.entity) + tx := repo.transaction.Create(&repo.entity) if tx.Error != nil { tx.Rollback() } - repo.Lock() repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + return } func (repo *repository[T1]) Update(ctx context.Context) { @@ -117,11 +115,11 @@ func (repo *repository[T1]) Update(ctx context.Context) { if tx.Error != nil { tx.Rollback() } - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + } func (repo *repository[T1]) Delete(ctx context.Context) { @@ -131,11 +129,11 @@ func (repo *repository[T1]) Delete(ctx context.Context) { if tx.Error != nil { tx.Rollback() } - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + } func (repo *repository[T1]) Query(ctx context.Context, res any) { @@ -145,9 +143,9 @@ func (repo *repository[T1]) Query(ctx context.Context, res any) { if tx.Error != nil { tx.Rollback() } - repo.Lock() + repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error - repo.Unlock() + } diff --git a/router/prediction_route.go b/router/prediction_route.go index cda6be0278db4c0eb1228aa8b9eb90be87789c6e..f9995e9e50b34b15ef8fe6d36829259fdb1a77fd 100644 --- a/router/prediction_route.go +++ b/router/prediction_route.go @@ -6,9 +6,11 @@ import ( ) func PredictionRoute(router *gin.Engine) { - predictionModule := factory.NewPredictionModule() routerGroup := router.Group("/api/v1") { - routerGroup.POST("/predict", predictionModule.Predict) + routerGroup.POST("/predict", func(c *gin.Context) { + predictionModule := factory.NewPredictionModule() + predictionModule.Predict(c) + }) } } diff --git a/services/authentication_service.go b/services/authentication_service.go new file mode 100644 index 0000000000000000000000000000000000000000..a7f1b4d97bc419a05aa49bc4ef098959a0c38d3f --- /dev/null +++ b/services/authentication_service.go @@ -0,0 +1,11 @@ +package services + +import "context" + +type AuthenticationService interface { + Register(ctx context.Context, fingerPrint string) + Login(ctx context.Context, fingerPrint string) +} + +type authenticationService interface { +} diff --git a/services/openai_service.go b/services/openai_service.go index 6cf3214e5effe7f23b24ed1bf490f33b24874c7c..0ae09ef9d7017a67365d8eed34d28769684bf83f 100644 --- a/services/openai_service.go +++ b/services/openai_service.go @@ -72,8 +72,12 @@ func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.Fi s.ThrowsError(err) return "Failed to create transcription!" } + // if resp.Text != "" { + // outFile.Close() + // } else { + // s.ThrowsException(&s.exception.FailedTranscripting, "Failed to create transcription! [Nil text]") + // } - // // Privacy Consideration bro! // if err := os.Remove(savedPath); err != nil { // s.ThrowsError(err) // } diff --git a/services/prediction_service.go b/services/prediction_service.go index 978a61e3faebe187df3c92a6d088c7389a925120..484e709dc7384fc301443161cfff2afdea391ab6 100644 --- a/services/prediction_service.go +++ b/services/prediction_service.go @@ -51,11 +51,9 @@ func (s *predictionService) Predict(ctx context.Context, req models.PredictionRe s.ThrowsError(s.openAIService.Error()) return nil, "" } - - savePrediction := s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) - if s.ThrowsRepoException() { - return nil, "" - } - - return ttsOutput, savePrediction.Answer + // savePrediction := s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) + // if s.ThrowsRepoException() { + // return nil, "" + // } + return ttsOutput, replicateOutput } diff --git a/services/service.go b/services/service.go index acfa8a35d8596e25c0ada96963fbbd79a901cc7e..7cb37fbb35497c0bf956cd43ba836231f25c6295 100644 --- a/services/service.go +++ b/services/service.go @@ -2,16 +2,15 @@ package services import ( "errors" - "sync" "time" models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "gorm.io/gorm" ) type ( service[TRepo repositories.Repository] struct { - sync.Mutex repository TRepo exception models.Exception errors error @@ -26,16 +25,16 @@ type ( ) func (s *service[TRepo]) ThrowsException(status *bool, message string) { - s.Lock() + *status = true s.exception.Message = message - s.Unlock() + } func (s *service[TRepo]) ThrowsError(err error) { - s.Lock() + s.errors = errors.Join(s.errors, err) - s.Unlock() + } func (s *service[TRepo]) Exception() models.Exception { @@ -49,15 +48,24 @@ func CalculateDueTime(duration time.Duration) time.Time { } func (s *service[TRepo]) ThrowsRepoException() bool { - s.Lock() + if s.repository.RowsError() != nil { + + s.ThrowsException(&s.exception.QueryError, "Database error!") s.ThrowsError(s.repository.RowsError()) + + return true + } + if errors.Is(s.repository.RowsError(), gorm.ErrDuplicatedKey) { + s.ThrowsException(&s.exception.DataDuplicate, "Duplicated data!") + return true } if s.repository.IsNoRecord() { + s.ThrowsException(&s.exception.DataNotFound, "No record found") return true } - s.Unlock() + return false } diff --git a/space/repositories/chat_history_repository.go b/space/repositories/chat_history_repository.go index 7f838328567e6f415bb6bd7317e5714cb6155569..cbc4474f45e2a04bf55b1d0e4700df8aa7db9fb2 100644 --- a/space/repositories/chat_history_repository.go +++ b/space/repositories/chat_history_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" ) @@ -18,7 +19,8 @@ type chatHistoryRepository struct { func NewChatHistoryRepository() ChatHistoryRepository { return &chatHistoryRepository{ repository: &repository[models.ChatHistory]{ - entity: models.ChatHistory{}, + transaction: config.DB.Begin(), + entity: models.ChatHistory{}, }, } } diff --git a/space/repositories/repository.go b/space/repositories/repository.go index b7e50018684bb0e741b5d47284dd3aeda205cf34..d935995488d9329661fc471ff1d49b15e3d149c4 100644 --- a/space/repositories/repository.go +++ b/space/repositories/repository.go @@ -60,8 +60,7 @@ func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx conte repo.Unlock() } func (repo *repository[T1]) Where(ctx context.Context) { - - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Where(&repo.entity) repo.Lock() repo.rowsCount = int(tx.RowsAffected) @@ -71,7 +70,7 @@ func (repo *repository[T1]) Where(ctx context.Context) { } func (repo *repository[T1]) Find(ctx context.Context, res any) { repo.Lock() - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Find(&res) if tx.Error != nil { tx.Rollback() @@ -85,7 +84,7 @@ func (repo *repository[T1]) Find(ctx context.Context, res any) { func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Limit(repo.pagination.limit).Offset(repo.pagination.offset).Find(&res) if tx.Error != nil { tx.Rollback() @@ -99,7 +98,7 @@ func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { func (repo *repository[T1]) Create(ctx context.Context) { - tx := repo.transaction.Begin() + tx := repo.transaction tx.Create(&repo.entity).Find(&repo.entity) if tx.Error != nil { tx.Rollback() @@ -113,7 +112,7 @@ func (repo *repository[T1]) Create(ctx context.Context) { func (repo *repository[T1]) Update(ctx context.Context) { - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Save(&repo.entity).Find(&repo.entity) if tx.Error != nil { tx.Rollback() @@ -127,7 +126,7 @@ func (repo *repository[T1]) Update(ctx context.Context) { func (repo *repository[T1]) Delete(ctx context.Context) { - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Delete(&repo.entity) if tx.Error != nil { tx.Rollback() @@ -141,7 +140,7 @@ func (repo *repository[T1]) Delete(ctx context.Context) { func (repo *repository[T1]) Query(ctx context.Context, res any) { - tx := repo.transaction.Begin() + tx := repo.transaction tx.WithContext(ctx).Model(&repo.entity).Raw(repo.customQuery.sql, repo.customQuery.values).Scan(&res) if tx.Error != nil { tx.Rollback() diff --git a/space/space/Dockerfile b/space/space/Dockerfile index e4505a50759f154eda0d8a15bc9060c5c66f679c..0388ba888943aecc35b5c9d046caa0d080dd2a7b 100644 --- a/space/space/Dockerfile +++ b/space/space/Dockerfile @@ -1,6 +1,9 @@ # Gunakan image dasar Golang versi 1.24.1 FROM golang:1.24.1 +# Tambahkan user non-root untuk keamanan (optional tapi best practice) +RUN useradd -m -u 1001 appuser + # Set working directory WORKDIR /app @@ -29,18 +32,16 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ echo "EMAIL_VERIFICATION_DURATION=2" >> .env && \ echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env - -RUN mkdir -p /app/audio && \ - chmod 777 /app/audio && \ - chown -R /app/audio/* -RUN mkdir -p /app/logs && \ - chmod -R 777 /app/logs && \ - chown -R /app/logs/* +# Buat direktori audio dan logs, beri izin dan kepemilikan ke appuser +RUN mkdir -p /app/audio /app/logs && \ + chmod -R 777 /app/audio /app/logs && \ + chown -R appuser:appuser /app/audio /app/logs # Build aplikasi RUN go build -o main . +# Beralih ke user non-root USER appuser # Expose port untuk Hugging Face Spaces diff --git a/space/space/space/factory/prediction_factory.go b/space/space/space/factory/prediction_factory.go index c35b3ba27a13f2907fdcd9ce52aed058f963e35e..4cfca725d77199de1a81575c4b3d37731e3e0392 100644 --- a/space/space/space/factory/prediction_factory.go +++ b/space/space/space/factory/prediction_factory.go @@ -8,11 +8,10 @@ import ( ) func NewPredictionModule() controller.PredictionController { - var repo repositories.Repository - - openAIService := services.NewOpenAIService(repo, config.OpenAIClient) - replicateService := services.NewReplicateService(repo, config.ReplicateClient, "lucataco/moondream2:72ccb656353c348c1385df54b237eeb7bfa874bf11486cf0b9473e691b662d31") - predictionService := services.NewPredictionService(replicateService, openAIService) + chatHistoryRepository := repositories.NewChatHistoryRepository() + openAIService := services.NewOpenAIService(chatHistoryRepository, config.OpenAIClient) + replicateService := services.NewReplicateService(chatHistoryRepository, config.ReplicateClient, "lucataco/moondream2:72ccb656353c348c1385df54b237eeb7bfa874bf11486cf0b9473e691b662d31") + predictionService := services.NewPredictionService(chatHistoryRepository, replicateService, openAIService) predictionController := controller.NewPredictionController(predictionService) return predictionController diff --git a/space/space/space/repositories/chat_history_repository.go b/space/space/space/repositories/chat_history_repository.go index ae9a42f2a74d70cf5e37cffe56770b36fab9b50b..7f838328567e6f415bb6bd7317e5714cb6155569 100644 --- a/space/space/space/repositories/chat_history_repository.go +++ b/space/space/space/repositories/chat_history_repository.go @@ -15,6 +15,13 @@ type chatHistoryRepository struct { *repository[models.ChatHistory] } +func NewChatHistoryRepository() ChatHistoryRepository { + return &chatHistoryRepository{ + repository: &repository[models.ChatHistory]{ + entity: models.ChatHistory{}, + }, + } +} func (r *chatHistoryRepository) SaveChatHistory(ctx context.Context, imagePath string, question string, answer string) (res models.ChatHistory) { r.entity.ImagePath = imagePath r.entity.Question = question diff --git a/space/space/space/services/prediction_service.go b/space/space/space/services/prediction_service.go index 5efe951a7a9fd1afe138d8849378593bda6ed244..978a61e3faebe187df3c92a6d088c7389a925120 100644 --- a/space/space/space/services/prediction_service.go +++ b/space/space/space/services/prediction_service.go @@ -20,9 +20,11 @@ type ( } ) -func NewPredictionService(replicateService ReplicateService, openAIService OpenAIService) PredictionService { +func NewPredictionService(chatHistoryRepository repositories.ChatHistoryRepository, replicateService ReplicateService, openAIService OpenAIService) PredictionService { return &predictionService{ - service: &service[repositories.ChatHistoryRepository]{}, + service: &service[repositories.ChatHistoryRepository]{ + repository: chatHistoryRepository, + }, replicateService: replicateService, openAIService: openAIService, } diff --git a/space/space/space/space/space/Dockerfile b/space/space/space/space/space/Dockerfile index 2be9f7c83fb2684595a7f636004999459970990d..e4505a50759f154eda0d8a15bc9060c5c66f679c 100644 --- a/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/Dockerfile @@ -32,15 +32,17 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ RUN mkdir -p /app/audio && \ chmod 777 /app/audio && \ - chown -R $(whoami):$(whoami) /app/audio + chown -R /app/audio/* RUN mkdir -p /app/logs && \ - chmod 777 /app/logs && \ - chown -R $(whoami):$(whoami) /app/logs + chmod -R 777 /app/logs && \ + chown -R /app/logs/* # Build aplikasi RUN go build -o main . +USER appuser + # Expose port untuk Hugging Face Spaces EXPOSE 7860 diff --git a/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/Dockerfile index eb246297d9af31e8d6ece750f0c3bce88a05f770..2be9f7c83fb2684595a7f636004999459970990d 100644 --- a/space/space/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/space/space/Dockerfile @@ -30,6 +30,14 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env +RUN mkdir -p /app/audio && \ + chmod 777 /app/audio && \ + chown -R $(whoami):$(whoami) /app/audio + +RUN mkdir -p /app/logs && \ + chmod 777 /app/logs && \ + chown -R $(whoami):$(whoami) /app/logs + # Build aplikasi RUN go build -o main . diff --git a/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/Dockerfile index 5d6d54c97500226b75d23e0fe9535cc8201123dd..eb246297d9af31e8d6ece750f0c3bce88a05f770 100644 --- a/space/space/space/space/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/space/space/space/space/Dockerfile @@ -21,14 +21,15 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ echo "DB_USER=postgres.iuwuiuoisqnfdzlgwurl" >> .env && \ echo "DB_PASSWORD=$(cat /run/secrets/DB_PASSWORD 2>/dev/null)" >> .env && \ echo "DB_PORT=5432" >> .env && \ - echo "DB_NAME=kpppl" >> .env && \ + echo "DB_NAME=postgres" >> .env && \ echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ echo "HOST_ADDRESS=0.0.0.0" >> .env && \ echo "HOST_PORT=7860" >> .env && \ echo "LOG_PATH=logs" >> .env && \ echo "EMAIL_VERIFICATION_DURATION=2" >> .env && \ echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ - echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env && \ + echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env + # Build aplikasi RUN go build -o main . diff --git a/space/space/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/space/space/Dockerfile index 4f9a287e36c1b1a31ef7f1c39ce61e7a83cb2cb0..5d6d54c97500226b75d23e0fe9535cc8201123dd 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/space/space/space/space/space/space/Dockerfile @@ -14,13 +14,12 @@ RUN go mod download COPY . . # Buat file .env dengan variabel environment menggunakan Hugging Face secrets -RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \ - --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ +RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ --mount=type=secret,id=OPENAI_API_KEY,mode=0444,required=false \ --mount=type=secret,id=REPLICATE_API_TOKEN,mode=0444,required=false \ echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ echo "DB_USER=postgres.iuwuiuoisqnfdzlgwurl" >> .env && \ - echo "DB_PASSWORD=$(cat /run/secrets/DB_PASSWORD 2>/dev/null')" >> .env && \ + echo "DB_PASSWORD=$(cat /run/secrets/DB_PASSWORD 2>/dev/null)" >> .env && \ echo "DB_PORT=5432" >> .env && \ echo "DB_NAME=kpppl" >> .env && \ echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ @@ -37,4 +36,4 @@ RUN go build -o main . EXPOSE 7860 # Jalankan aplikasi -CMD ["./main"] \ No newline at end of file +CMD ["./main"] diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example b/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..6b4970eeb0f5d9b08b1ce4b8b800661ac6b0f490 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example @@ -0,0 +1,11 @@ +DB_HOST = +DB_USER = +DB_PASSWORD = +DB_PORT = +DB_NAME = +SALT = +HOST_ADDRESS = +HOST_PORT = +LOG_PATH = logs +OPEN_AI_API_KEY = +REPLICATE_API_TOKEN = \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml b/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml new file mode 100644 index 0000000000000000000000000000000000000000..6d5e7c86f90be02acf8fa3a8221a1c28c721ddb3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml @@ -0,0 +1,52 @@ +name: Deploy to Huggingface + +on: + push: + branches: + - main + +jobs: + deploy-to-huggingface: + runs-on: ubuntu-latest + + steps: + # Checkout repository + - name: Checkout Repository + uses: actions/checkout@v3 + + # Setup Git + - name: Setup Git for Huggingface + run: | + git config --global user.email "abdan.hafidz@gmail.com" + git config --global user.name "abdanhafidz" + + # Clone Huggingface Space Repository + - name: Clone Huggingface Space + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + git clone https://huggingface.co/spaces/lifedebugger/urna-backend space + + # Update Git Remote URL and Pull Latest Changes + - name: Update Remote and Pull Changes + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/urna-backend + git pull origin main || echo "No changes to pull" + + # Copy Files to Huggingface Space + - name: Copy Files to Space + run: | + rsync -av --exclude='.git' ./ space/ + + # Commit and Push to Huggingface Space + - name: Commit and Push to Huggingface + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git add . + git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" + git push origin main || echo "No changes to push" diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/go.yml b/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/go.yml new file mode 100644 index 0000000000000000000000000000000000000000..0b443f376a6a3de600d8cde57dbb383a426e02f1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/go.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore b/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..df8cf1c15e2917803db7f00fbc386495fe8f5479 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore @@ -0,0 +1,5 @@ +.env +vendor/ +quzuu-be.exe +README.md +.qodo diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..4f9a287e36c1b1a31ef7f1c39ce61e7a83cb2cb0 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile @@ -0,0 +1,40 @@ +# Gunakan image dasar Golang versi 1.24.1 +FROM golang:1.24.1 + +# Set working directory +WORKDIR /app + +# Copy go.mod dan go.sum +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy seluruh kode +COPY . . + +# Buat file .env dengan variabel environment menggunakan Hugging Face secrets +RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \ + --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ + --mount=type=secret,id=OPENAI_API_KEY,mode=0444,required=false \ + --mount=type=secret,id=REPLICATE_API_TOKEN,mode=0444,required=false \ + echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ + echo "DB_USER=postgres.iuwuiuoisqnfdzlgwurl" >> .env && \ + echo "DB_PASSWORD=$(cat /run/secrets/DB_PASSWORD 2>/dev/null')" >> .env && \ + echo "DB_PORT=5432" >> .env && \ + echo "DB_NAME=kpppl" >> .env && \ + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ + echo "HOST_ADDRESS=0.0.0.0" >> .env && \ + echo "HOST_PORT=7860" >> .env && \ + echo "LOG_PATH=logs" >> .env && \ + echo "EMAIL_VERIFICATION_DURATION=2" >> .env && \ + echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ + echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env && \ +# Build aplikasi +RUN go build -o main . + +# Expose port untuk Hugging Face Spaces +EXPOSE 7860 + +# Jalankan aplikasi +CMD ["./main"] \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/LICENSE b/space/space/space/space/space/space/space/space/space/space/space/space/space/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..79da4b69760d1658f4dbc42604187316b368d857 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2025 Abdan Hafidz + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 b/space/space/space/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..d6ec250b2eeedbdd36b90c6c1a2a7875bcd00f02 Binary files /dev/null and b/space/space/space/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 differ diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/config/config.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..b43609852aae4f728948d1b52ce4f04efdbcdcad --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/config.go @@ -0,0 +1,27 @@ +package config + +import ( + "os" + "strconv" + + "github.com/joho/godotenv" +) + +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string +var EMAIL_VERIFICATION_DURATION int +var OPEN_AI_API_KEY string +var REPLICATE_API_KEY string + +func init() { + godotenv.Load() + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) + OPEN_AI_API_KEY = os.Getenv("OPEN_AI_API_KEY") + REPLICATE_API_KEY = os.Getenv("REPLICATE_API_KEY") +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go new file mode 100644 index 0000000000000000000000000000000000000000..4cce339630d01c5c44454c9090f59b24442384d2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go @@ -0,0 +1,61 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/joho/godotenv" +) + +var DB *gorm.DB +var err error +var Salt string + +func init() { + godotenv.Load() + if err != nil { + fmt.Println("Gagal membaca file .env") + return + } + os.Setenv("TZ", "Asia/Jakarta") + dbHost := os.Getenv("DB_HOST") + dbPort := os.Getenv("DB_PORT") + dbUser := os.Getenv("DB_USER") + dbPassword := os.Getenv("DB_PASSWORD") + dbName := os.Getenv("DB_NAME") + Salt := os.Getenv("SALT") + dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta" + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true}) + if err != nil { + panic(err) + } + if Salt == "" { + Salt = "D3f4u|t" + } + + // Call AutoMigrateAll to perform auto-migration + AutoMigrateAll(DB) +} + +func AutoMigrateAll(db *gorm.DB) { + // Enable logger to see SQL logs + db.Logger.LogMode(logger.Info) + + // Auto-migrate all models + err := db.AutoMigrate( + &models.Account{}, + &models.ChatHistory{}, + ) + + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go new file mode 100644 index 0000000000000000000000000000000000000000..f7c3350837b4535833422da3e99601672e72ca1a --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/sashabaranov/go-openai" +) + +var OpenAIClient *openai.Client + +func init() { + OpenAIClient = openai.NewClient(OPEN_AI_API_KEY) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go new file mode 100644 index 0000000000000000000000000000000000000000..ffde651d46984521d49a56fb0a2fd15ca3794c4a --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go @@ -0,0 +1,14 @@ +package config + +import ( + "github.com/replicate/replicate-go" +) + +var ReplicateClient *replicate.Client + +func init() { + ReplicateClient, err = replicate.NewClient(replicate.WithTokenFromEnv()) + if err != nil { + panic(err) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go new file mode 100644 index 0000000000000000000000000000000000000000..0fa1dfc466312fd712e3b2353e3ea744f200630b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go @@ -0,0 +1,64 @@ +package controller + +import ( + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" +) + +type ( + Controller interface { + HeaderParse(ctx *gin.Context) + RequestJSON(ctx *gin.Context, request any) + Response(ctx *gin.Context, res any) + } + controller[TService services.Service] struct { + accountData models.AccountData + service TService + } +) + +func (c *controller[TService]) HeaderParse(ctx *gin.Context) { + cParam, _ := ctx.Get("account_data") + if cParam != nil { + c.accountData = cParam.(models.AccountData) + } +} + +func (c *controller[TService]) RequestJSON(ctx *gin.Context, request any) { + cParam, _ := ctx.Get("AccountData") + if cParam != nil { + c.accountData = cParam.(models.AccountData) + } + + errBinding := ctx.ShouldBindJSON(&request) + if errBinding != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Invalid Request!, recheck your request, there's must be some problem about required parameter or type parameter", + }) + return + } +} + +func (c *controller[TService]) Response(ctx *gin.Context, res any) { + switch { + case c.service.Error() != nil: + utils.ResponseFAIL(ctx, 500, models.Exception{ + InternalServerError: true, + Message: "Internal Server Error", + }) + utils.LogError(c.service.Error()) + case c.service.Exception().DataDuplicate: + utils.ResponseFAIL(ctx, 400, c.service.Exception()) + case c.service.Exception().Unauthorized: + utils.ResponseFAIL(ctx, 401, c.service.Exception()) + case c.service.Exception().DataNotFound: + utils.ResponseFAIL(ctx, 404, c.service.Exception()) + case c.service.Exception().Message != "": + utils.ResponseFAIL(ctx, 400, c.service.Exception()) + default: + utils.ResponseOK(ctx, res) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/home_controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/home_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..60198b440a4795562848b532dc3480e3128a7729 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/home_controller.go @@ -0,0 +1,9 @@ +package controller + +import "github.com/gin-gonic/gin" + +func HomeController(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "API Is Running Gladly!", + }) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..475632248e28f0183fa223b0b582821e89e206ce --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go @@ -0,0 +1,95 @@ +package controller + +import ( + "fmt" + "mime/multipart" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" +) + +type ( + PredictionController interface { + Controller + Predict(ctx *gin.Context) + } + + predictionController struct { + *controller[services.PredictionService] + } +) + +func requestImage(ctx *gin.Context, image *multipart.File, imageFilename *string) { + imageHeader, err := ctx.FormFile("image_file") + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Image file is required", + }) + return + } + imageFile, err := imageHeader.Open() + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Failed to open image file", + }) + return + } + *image = imageFile + *imageFilename = imageHeader.Filename + defer imageFile.Close() +} + +func requestAudio(ctx *gin.Context, audio *multipart.File, audioFilename *string) { + audioHeader, err := ctx.FormFile("audio_file") + fmt.Println(audioHeader.Filename) + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Audio file is required", + }) + return + } + audioFile, err := audioHeader.Open() + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Failed to open audio file", + }) + return + } + *audio = audioFile + *audioFilename = audioHeader.Filename + defer audioFile.Close() +} + +func NewPredictionController(predictionService services.PredictionService) PredictionController { + return &predictionController{ + controller: &controller[services.PredictionService]{ + service: predictionService, + }, + } +} +func (c *predictionController) Predict(ctx *gin.Context) { + + var predictionRequest models.PredictionRequest + + requestImage(ctx, &predictionRequest.ImageFile, &predictionRequest.ImageFileName) + requestAudio(ctx, &predictionRequest.AudioQuestionFile, &predictionRequest.AudioQuestionFilename) + + predictionResult, text_output := c.service.Predict(ctx, predictionRequest) + + if c.service.Error() != nil { + c.Response(ctx, nil) + return + } + + ctx.Header("Content-Type", "audio/mpeg") + ctx.Header("Content-Disposition", "inline; filename=response.mp3") + ctx.Header("X-Response-Text", text_output) + ctx.Data(200, "audio/mpeg", predictionResult) + +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/docker-compose.yaml b/space/space/space/space/space/space/space/space/space/space/space/space/space/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/factory.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/factory.go new file mode 100644 index 0000000000000000000000000000000000000000..4682f9f75538c0f31e2fd022c6efa25cad45ed2e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/factory.go @@ -0,0 +1 @@ +package factory diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..c35b3ba27a13f2907fdcd9ce52aed058f963e35e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go @@ -0,0 +1,19 @@ +package factory + +import ( + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/controller" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" +) + +func NewPredictionModule() controller.PredictionController { + var repo repositories.Repository + + openAIService := services.NewOpenAIService(repo, config.OpenAIClient) + replicateService := services.NewReplicateService(repo, config.ReplicateClient, "lucataco/moondream2:72ccb656353c348c1385df54b237eeb7bfa874bf11486cf0b9473e691b662d31") + predictionService := services.NewPredictionService(replicateService, openAIService) + predictionController := controller.NewPredictionController(predictionService) + + return predictionController +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..fd16864743e8c7e35500854f5cad1cfbeddd8726 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod @@ -0,0 +1,53 @@ +module github.com/abdanhafidz/ai-visual-multi-modal-backend + +go 1.24.0 + +require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/joho/godotenv v1.5.1 + github.com/satori/go.uuid v1.2.0 + golang.org/x/crypto v0.36.0 + gorm.io/driver/postgres v1.5.11 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/bytedance/sonic v1.13.1 // indirect + github.com/bytedance/sonic/loader v0.2.4 // indirect + github.com/cloudwego/base64x v0.1.5 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v1.0.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.25.0 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect + github.com/kr/text v0.2.0 // 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.3 // indirect + github.com/replicate/replicate-go v0.26.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/sashabaranov/go-openai v1.40.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + github.com/vincent-petithory/dataurl v1.0.0 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/net v0.37.0 // indirect + golang.org/x/sync v0.12.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + google.golang.org/protobuf v1.36.5 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..56ea54f781eb5a01a1a3b54a84e872d795922a0e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum @@ -0,0 +1,126 @@ +github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= +github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY= +github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= +github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= +github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= +github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= +github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= +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/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +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.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= +github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +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.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +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.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/replicate/replicate-go v0.26.0 h1:F6XceIkO0x2ft08mc9MdNJSNbkXDqEtOK9GsgjqHQeQ= +github.com/replicate/replicate-go v0.26.0/go.mod h1:mnRw0hsQuVrgWKMm/kP29pY6Ldn//79b4C2Nw9sYn5M= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sashabaranov/go-openai v1.40.1 h1:bJ08Iwct5mHBVkuvG6FEcb9MDTfsXdTYPGjYLRdeTEU= +github.com/sashabaranov/go-openai v1.40.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +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.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.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= +github.com/vincent-petithory/dataurl v1.0.0 h1:cXw+kPto8NLuJtlMsI152irrVw9fRDX8AbShPRpg2CI= +github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U= +golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw= +golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +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= +gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= +gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt b/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..4551241cd6993ae3c0d921accbce16c2f160408b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt @@ -0,0 +1,19 @@ +2025/06/16 23:53:52 Error Log : open audio: is a directory +2025/06/16 23:54:20 Error Log : open audio: is a directory +open audio: is a directory +open audio: is a directory +2025/06/16 23:54:37 Error Log : open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +2025/06/16 23:56:46 Error Log : open audio: is a directory +2025/06/16 23:57:43 Error Log : open audio: is a directory +2025/06/17 00:00:14 Error Log : open audio: is a directory +2025/06/17 00:01:06 Error Log : error, status code: 429, status: 429 Too Many Requests, message: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors. +2025/06/17 00:16:55 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/17 00:29:21 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:30:56 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:32:14 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:33:03 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/security_log.txt b/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/security_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go new file mode 100644 index 0000000000000000000000000000000000000000..e624f9e394e284357398b6f8d107672bbb6ba7a3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + router "github.com/abdanhafidz/ai-visual-multi-modal-backend/router" +) + +func main() { + fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT) + router.StartService() + +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/authentication_middleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/authentication_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..d23e075a9b088fdd26e7dd964f826b6d3ac627fb --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/authentication_middleware.go @@ -0,0 +1,70 @@ +// auth/auth.go + +package middleware + +import ( + "time" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt/v5" +) + +var salt = config.Salt +var secretKey = []byte(salt) + +// VerifyPassword verifies if the provided password matches the hashed password + +type CustomClaims struct { + jwt.RegisteredClaims + UserID int `json:"id"` +} + +func VerifyToken(bearer_token string) (int, string, error) { + // fmt.Println(bearer_token) + token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) + if err != nil { + return 0, "invalid-token", err + } + + // Extract the claims + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } + if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { + return 0, "expired", err + } + + return claims.UserID, "valid", err +} + +func AuthUser(c *gin.Context) { + var currAccData models.AccountData + if c.Request.Header["Auth-Bearer-Token"] != nil { + token := c.Request.Header["Auth-Bearer-Token"] + currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.verifyStatus) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { + currAccData.UserID = 0 + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"}) + c.Abort() + return + } else { + c.Set("accountData", currAccData) + c.Next() + } + } else { + currAccData.UserID = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) + c.Abort() + return + } + +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..cce0f4f3e125d37ff4feb157c980c9a9f14f4673 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "math" + "time" + + "gorm.io/gorm" +) + +func RecordCheck(rows *gorm.DB) (string, error) { + count := rows.RowsAffected + err := rows.Error + + if count == 0 { + return "no-record", err + } else if err != nil { + return "query-error", err + } else { + return "ok", err + } +} + +func DiffTime(t1 time.Time, t2 time.Time) (int, int, int) { + hs := t1.Sub(t2).Hours() + hs, mf := math.Modf(hs) + ms := mf * 60 + ms, sf := math.Modf(ms) + ss := sf * 60 + return int(hs), int(ms), int(ss) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/response_middleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/response_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..d52391cd3607e6ec2697e6a5813b348d49ff5ee2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/response_middleware.go @@ -0,0 +1,45 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// SendJSON200 sends a JSON response with HTTP status code 200 +func SendJSON200(c *gin.Context, data interface{}) { + c.JSON(http.StatusOK, gin.H{"status": "success", "data": data}) + return +} + +// SendJSON400 sends a JSON response with HTTP status code 400 +func SendJSON400(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message}) + return +} + +// SendJSON401 sends a JSON response with HTTP status code 401 +func SendJSON401(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message}) + return +} + +// SendJSON403 sends a JSON response with HTTP status code 403 +func SendJSON403(c *gin.Context, message *string) { + c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message}) + return +} + +// SendJSON404 sends a JSON response with HTTP status code 404 +func SendJSON404(c *gin.Context, message *string) { + c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message}) + return +} + +// SendJSON500 sends a JSON response with HTTP status code 500 +func SendJSON500(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message}) + return +} + +// JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go new file mode 100644 index 0000000000000000000000000000000000000000..42184c0b1ce6b924b8796b1e1be7a5402e2fe806 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go @@ -0,0 +1,5 @@ +package models + +type LoginRequest struct { + FingerPrintToken string `json:"email" binding:"required"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go new file mode 100644 index 0000000000000000000000000000000000000000..0203b9b340d066705491edf66830674602f89655 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go @@ -0,0 +1,7 @@ +package models + +type AccountData struct { + UserID int + VerifyStatus string + ErrVerif error +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/entities_model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/entities_model.go new file mode 100644 index 0000000000000000000000000000000000000000..72b5decb9ca3f739ff5a4126c48682d10464ae34 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/entities_model.go @@ -0,0 +1,27 @@ +package models + +import ( + "time" + + uuid "github.com/satori/go.uuid" +) + +type Account struct { + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` + Fingerprint string `gorm:"not null;"` + CreatedAt time.Time + DeletedAt *time.Time `gorm:"column:deleted_at"` // perhatikan penamaan kolom +} + +type ChatHistory struct { + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` + ImagePath string `gorm:"type:text"` + Question string `gorm:"type:text"` + Answer string `gorm:"type:text"` + CreatedAt time.Time `gorm:"column:created_at"` + DeletedAt *time.Time `gorm:"column:deleted_at"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (ChatHistory) TableName() string { return "chat_history" } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/exception_model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/exception_model.go new file mode 100644 index 0000000000000000000000000000000000000000..0394b70d9ba8cbcaef8ee1c7aa24afd7a8c9767b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/exception_model.go @@ -0,0 +1,16 @@ +package models + +type Exception struct { + Unauthorized bool `json:"unauthorized,omitempty"` + BadRequest bool `json:"bad_request,omitempty"` + DataNotFound bool `json:"data_not_found,omitempty"` + InternalServerError bool `json:"internal_server_error,omitempty"` + DataDuplicate bool `json:"data_duplicate,omitempty"` + QueryError bool `json:"query_error,omitempty"` + InvalidPasswordLength bool `json:"invalid_password_length,omitempty"` + FailedTranscripting bool `json:"failed_transcripting,omitempty"` + ReplicateConnectionRefused bool `json:"replicated_connection_refused,omitempty"` + AudioFileError bool `json:"audio_file_error,omitempty"` + FailedGenerateAudio bool `json:"audio_generation_failed,omitempty"` + Message string `json:"message"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go new file mode 100644 index 0000000000000000000000000000000000000000..3eaf40a116f2611f9c71969c2e262bd71823fca3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go @@ -0,0 +1,15 @@ +package models + +type SuccessResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Data any `json:"data"` + MetaData any `json:"meta_data"` +} + +type ErrorResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Errors Exception `json:"errors"` + MetaData any `json:"meta_data"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/model.go new file mode 100644 index 0000000000000000000000000000000000000000..a68179cd2c301ae0e474db227fec2841ea200734 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/model.go @@ -0,0 +1,3 @@ +package models + + diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go new file mode 100644 index 0000000000000000000000000000000000000000..96e18bcbc7f6a8b6e3991e6c3c76e20e14c1634e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go @@ -0,0 +1,10 @@ +package models + +import "mime/multipart" + +type PredictionRequest struct { + ImageFile multipart.File + ImageFileName string + AudioQuestionFile multipart.File + AudioQuestionFilename string +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..6677a965683faa76fee9c4f3b825df67946f4c60 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go @@ -0,0 +1,30 @@ +package repositories + +import ( + "context" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" +) + +type AccountRepository interface { + Repository + CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) + GetAccountByFingerPrint(ctx context.Context, fingePrint string) (res models.Account) +} + +type accountRepository struct { + *repository[models.Account] +} + +func (r *accountRepository) CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) { + r.entity.Fingerprint = fingerPrint + r.Create(ctx) + return r.entity +} + +func (r *accountRepository) GetAccountByFingerPrint(ctx context.Context, fingerPrint string) (res models.Account) { + r.entity.Fingerprint = fingerPrint + r.Where(ctx) + r.Find(ctx, res) + return res +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..ae9a42f2a74d70cf5e37cffe56770b36fab9b50b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go @@ -0,0 +1,24 @@ +package repositories + +import ( + "context" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" +) + +type ChatHistoryRepository interface { + Repository + SaveChatHistory(ctx context.Context, imagePath string, question string, answer string) (res models.ChatHistory) +} + +type chatHistoryRepository struct { + *repository[models.ChatHistory] +} + +func (r *chatHistoryRepository) SaveChatHistory(ctx context.Context, imagePath string, question string, answer string) (res models.ChatHistory) { + r.entity.ImagePath = imagePath + r.entity.Question = question + r.entity.Answer = answer + r.Create(ctx) + return r.entity +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repository.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repository.go new file mode 100644 index 0000000000000000000000000000000000000000..b7e50018684bb0e741b5d47284dd3aeda205cf34 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repository.go @@ -0,0 +1,154 @@ +package repositories + +import ( + "context" + "sync" + + "gorm.io/gorm" +) + +type Repository interface { + Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) + FindAllPaginate(ctx context.Context, res any) + Where(ctx context.Context) + Find(ctx context.Context, res any) + Create(ctx context.Context) + Update(ctx context.Context) + Query(ctx context.Context, res any) + Delete(ctx context.Context) + IsNoRecord() bool + RowsCount() int + RowsError() error +} +type PaginationConstructor struct { + limit int + offset int + filter string +} + +type CustomQueryConstructor struct { + sql string + values interface{} +} + +type repository[TEntity any] struct { + sync.Mutex + entity TEntity + pagination PaginationConstructor + customQuery CustomQueryConstructor + transaction *gorm.DB + rowsCount int + noRecord bool + rowsError error +} + +func (repo *repository[T1]) RowsError() error { + return repo.rowsError +} +func (repo *repository[T1]) RowsCount() int { + return repo.rowsCount +} +func (repo *repository[T1]) IsNoRecord() bool { + repo.Lock() + repo.noRecord = repo.transaction.RowsAffected == 0 + repo.Unlock() + return repo.noRecord +} +func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) { + repo.Lock() + act(ctx, repo.transaction) + repo.Unlock() +} +func (repo *repository[T1]) Where(ctx context.Context) { + + tx := repo.transaction.Begin() + tx.WithContext(ctx).Where(&repo.entity) + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} +func (repo *repository[T1]) Find(ctx context.Context, res any) { + repo.Lock() + tx := repo.transaction.Begin() + tx.WithContext(ctx).Find(&res) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} + +func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { + + tx := repo.transaction.Begin() + tx.WithContext(ctx).Limit(repo.pagination.limit).Offset(repo.pagination.offset).Find(&res) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} + +func (repo *repository[T1]) Create(ctx context.Context) { + + tx := repo.transaction.Begin() + tx.Create(&repo.entity).Find(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} + +func (repo *repository[T1]) Update(ctx context.Context) { + + tx := repo.transaction.Begin() + tx.WithContext(ctx).Save(&repo.entity).Find(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} + +func (repo *repository[T1]) Delete(ctx context.Context) { + + tx := repo.transaction.Begin() + tx.WithContext(ctx).Delete(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} + +func (repo *repository[T1]) Query(ctx context.Context, res any) { + + tx := repo.transaction.Begin() + tx.WithContext(ctx).Model(&repo.entity).Raw(repo.customQuery.sql, repo.customQuery.values).Scan(&res) + if tx.Error != nil { + tx.Rollback() + } + repo.Lock() + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.Unlock() +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go new file mode 100644 index 0000000000000000000000000000000000000000..cda6be0278db4c0eb1228aa8b9eb90be87789c6e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go @@ -0,0 +1,14 @@ +package router + +import ( + factory "github.com/abdanhafidz/ai-visual-multi-modal-backend/factory" + "github.com/gin-gonic/gin" +) + +func PredictionRoute(router *gin.Engine) { + predictionModule := factory.NewPredictionModule() + routerGroup := router.Group("/api/v1") + { + routerGroup.POST("/predict", predictionModule.Predict) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go new file mode 100644 index 0000000000000000000000000000000000000000..dccd24c6236352640d3db082572a690ae04421f1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go @@ -0,0 +1,14 @@ +package router + +import ( + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + controller "github.com/abdanhafidz/ai-visual-multi-modal-backend/controller" + "github.com/gin-gonic/gin" +) + +func StartService() { + router := gin.Default() + router.GET("/", controller.HomeController) + PredictionRoute(router) + router.Run(config.TCP_ADDRESS) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/authentictaion_service.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/authentictaion_service.go new file mode 100644 index 0000000000000000000000000000000000000000..a7f1b4d97bc419a05aa49bc4ef098959a0c38d3f --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/authentictaion_service.go @@ -0,0 +1,11 @@ +package services + +import "context" + +type AuthenticationService interface { + Register(ctx context.Context, fingerPrint string) + Login(ctx context.Context, fingerPrint string) +} + +type authenticationService interface { +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/openai_service.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/openai_service.go new file mode 100644 index 0000000000000000000000000000000000000000..6cf3214e5effe7f23b24ed1bf490f33b24874c7c --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/openai_service.go @@ -0,0 +1,106 @@ +package services + +import ( + "context" + "io" + "mime/multipart" + "os" + "path/filepath" + + "github.com/sashabaranov/go-openai" + + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +type ( + openAIService struct { + *service[repositories.Repository] + client *openai.Client + } + + OpenAIService interface { + Service + SpeechToText(ctx context.Context, audioFile multipart.File, filename string) string + TextToSpeech(ctx context.Context, text string) []byte + } +) + +func NewOpenAIService(repo repositories.Repository, openAIClient *openai.Client) OpenAIService { + return &openAIService{ + service: &service[repositories.Repository]{ + repository: repo, + }, + client: openAIClient, + } +} + +func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.File, filename string) string { + + audioDir := "audio" + + if err := os.MkdirAll(audioDir, os.ModePerm); err != nil { + s.ThrowsException(&s.exception.InternalServerError, "Failed to create directory!") + s.ThrowsError(err) + return "failed to create directory!" + } + + savedPath := filepath.Join(audioDir, filepath.Base(filename)) + outFile, err := os.Create(savedPath) + + if err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to save audio!") + s.ThrowsError(err) + return "Failed to save audio!" + } + + defer outFile.Close() + + if _, err := io.Copy(outFile, audioFile); err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to save audio!") + s.ThrowsError(err) + return "Failed to save audio!" + } + + req := openai.AudioRequest{ + Model: openai.Whisper1, + FilePath: savedPath, + } + + resp, err := s.client.CreateTranscription(ctx, req) + if err != nil { + s.ThrowsException(&s.exception.FailedTranscripting, "Failed to create transcription!") + s.ThrowsError(err) + return "Failed to create transcription!" + } + + // // Privacy Consideration bro! + // if err := os.Remove(savedPath); err != nil { + // s.ThrowsError(err) + // } + + return resp.Text +} + +func (s *openAIService) TextToSpeech(ctx context.Context, text string) []byte { + req := openai.CreateSpeechRequest{ + Model: openai.TTSModel1, + Input: text, + Voice: openai.VoiceNova, + ResponseFormat: openai.SpeechResponseFormatMp3, + } + + audioResp, err := s.client.CreateSpeech(ctx, req) + if err != nil { + s.ThrowsException(&s.exception.FailedGenerateAudio, "Failed to generate speech audio!") + s.ThrowsError(err) + return nil + } + + audioData, err := io.ReadAll(audioResp) + if err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to read audio response!") + s.ThrowsError(err) + return nil + } + return audioData +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go new file mode 100644 index 0000000000000000000000000000000000000000..5efe951a7a9fd1afe138d8849378593bda6ed244 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go @@ -0,0 +1,59 @@ +package services + +import ( + "context" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +type ( + PredictionService interface { + Service + Predict(ctx context.Context, req models.PredictionRequest) (audio []byte, text string) + } + + predictionService struct { + *service[repositories.ChatHistoryRepository] + replicateService ReplicateService + openAIService OpenAIService + } +) + +func NewPredictionService(replicateService ReplicateService, openAIService OpenAIService) PredictionService { + return &predictionService{ + service: &service[repositories.ChatHistoryRepository]{}, + replicateService: replicateService, + openAIService: openAIService, + } +} + +func (s *predictionService) Predict(ctx context.Context, req models.PredictionRequest) (audio []byte, text string) { + sttOutput := s.openAIService.SpeechToText(ctx, req.AudioQuestionFile, req.AudioQuestionFilename) + if s.openAIService.Error() != nil { + s.ThrowsException(&s.exception.BadRequest, "Failed to generate speech to text!") + s.ThrowsError(s.openAIService.Error()) + return nil, "" + } + + replicateOutput := s.replicateService.AskImage(ctx, req.ImageFile, req.ImageFileName, sttOutput) + if s.replicateService.Error() != nil { + s.ThrowsException(&s.exception.ReplicateConnectionRefused, "Replicate Connection Refused!") + s.ThrowsError(s.replicateService.Error()) + return nil, "" + } + + ttsOutput := s.openAIService.TextToSpeech(ctx, replicateOutput) + if s.openAIService.Error() != nil { + s.ThrowsException(&s.exception.FailedGenerateAudio, "Failed to convert audio output!") + s.ThrowsError(s.openAIService.Error()) + return nil, "" + } + + savePrediction := s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) + if s.ThrowsRepoException() { + return nil, "" + } + + return ttsOutput, savePrediction.Answer +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go new file mode 100644 index 0000000000000000000000000000000000000000..4457715096c3d51ab73162e504927b771416225f --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go @@ -0,0 +1,68 @@ +package services + +import ( + "bytes" + "context" + "fmt" + "io" + "mime/multipart" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/replicate/replicate-go" +) + +type ReplicateService interface { + Service + AskImage(ctx context.Context, imageFile multipart.File, filename, question string) string +} + +type replicateService struct { + *service[repositories.Repository] + client *replicate.Client + model string +} + +func NewReplicateService(repo repositories.Repository, replicateClient *replicate.Client, model string) ReplicateService { + service := replicateService{ + service: &service[repositories.Repository]{repository: repo}, + client: replicateClient, + model: model, // e.g., "owner/moondream:versionHash" + } + return &service +} + +func (s *replicateService) AskImage(ctx context.Context, imageFile multipart.File, filename, question string) string { + var buf bytes.Buffer + if _, err := io.Copy(&buf, imageFile); err != nil { + + s.ThrowsError(err) + return "" + } + + file, err := s.client.CreateFileFromBuffer(ctx, &buf, &replicate.CreateFileOptions{Filename: filename}) + if err != nil { + s.ThrowsError(err) + return "" + } + + input := replicate.PredictionInput{ + "image": file, + "question": question, + } + rawOutput, err := s.client.Run(ctx, s.model, input, nil) + if err != nil { + s.ThrowsError(err) + return "" + } + + outputSlice, _ := rawOutput.([]interface{}) + result := fmt.Sprintf("%v", outputSlice) + fmt.Println("Output slice", result) + + // if !ok { + // s.ThrowsError(errors.New("failed to parse output as string")) + // return "" + // } + + return result +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/service.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/service.go new file mode 100644 index 0000000000000000000000000000000000000000..acfa8a35d8596e25c0ada96963fbbd79a901cc7e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/service.go @@ -0,0 +1,63 @@ +package services + +import ( + "errors" + "sync" + "time" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +type ( + service[TRepo repositories.Repository] struct { + sync.Mutex + repository TRepo + exception models.Exception + errors error + } + Service interface { + ThrowsException(*bool, string) + ThrowsError(error) + Exception() models.Exception + ThrowsRepoException() bool + Error() error + } +) + +func (s *service[TRepo]) ThrowsException(status *bool, message string) { + s.Lock() + *status = true + s.exception.Message = message + s.Unlock() +} + +func (s *service[TRepo]) ThrowsError(err error) { + s.Lock() + s.errors = errors.Join(s.errors, err) + s.Unlock() +} + +func (s *service[TRepo]) Exception() models.Exception { + return s.exception +} +func (s *service[TRepo]) Error() error { + return s.errors +} +func CalculateDueTime(duration time.Duration) time.Time { + return time.Now().Add(duration) +} + +func (s *service[TRepo]) ThrowsRepoException() bool { + s.Lock() + if s.repository.RowsError() != nil { + s.ThrowsError(s.repository.RowsError()) + return true + } + if s.repository.IsNoRecord() { + s.ThrowsException(&s.exception.DataNotFound, "No record found") + return true + } + s.Unlock() + return false +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/api_response.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/api_response.go new file mode 100644 index 0000000000000000000000000000000000000000..a638c08da4c37bf82272e090fd9669522504f1f8 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/api_response.go @@ -0,0 +1,52 @@ +package utils + +import ( + "net/http" + "reflect" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + "github.com/gin-gonic/gin" +) + +func ResponseOK(c *gin.Context, data any) { + res := models.SuccessResponse{ + Status: "success", + Message: "Data retrieved successfully!", + Data: data, + MetaData: c.Request.Body, + } + c.JSON(http.StatusOK, res) + return +} + +func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { + message := exception.Message + exception.Message = "" + res := models.ErrorResponse{ + Status: "error", + Message: message, + Errors: exception, + MetaData: c.Request.Body, + } + c.AbortWithStatusJSON(status, res) + return +} + +func SendResponse(c *gin.Context, data services.Service) { + if reflect.ValueOf(data.Exception()).IsNil() { + ResponseOK(c, data) + } else { + if data.Exception().Unauthorized { + ResponseFAIL(c, 401, data.Exception()) + } else if data.Exception().BadRequest { + ResponseFAIL(c, 400, data.Exception()) + } else if data.Exception().DataNotFound { + ResponseFAIL(c, 404, data.Exception()) + } else if data.Exception().InternalServerError { + ResponseFAIL(c, 500, data.Exception()) + } else { + ResponseFAIL(c, 403, data.Exception()) + } + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/helper.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/helper.go new file mode 100644 index 0000000000000000000000000000000000000000..fee40aef724f1cee53d507aca1f16006a8bcf512 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/helper.go @@ -0,0 +1,11 @@ +package utils + +import ( + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/gin-gonic/gin" +) + +func GetAccount(c *gin.Context) models.AccountData { + cParam, _ := c.Get("accountData") + return cParam.(models.AccountData) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/logger.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/logger.go new file mode 100644 index 0000000000000000000000000000000000000000..40c86a2b23e2325776d43228382e6cc5655803a1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/logger.go @@ -0,0 +1,21 @@ +package utils + +import ( + "fmt" + "log" + "os" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" +) + +func LogError(errorLogged error) { + fmt.Println("There is an error!") + file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + log.SetOutput(file) + + log.Println("Error Log :", errorLogged) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/util.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/util.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/util.go @@ -0,0 +1,9 @@ +package utils + +func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string { + if condition { + return valueIfTrue + } else { + return valueIfFalse + } +} diff --git a/space/space/space/space/space/utils/logger.go b/space/space/space/space/space/utils/logger.go index 40c86a2b23e2325776d43228382e6cc5655803a1..fc34cd446974f0e1f416340efb6e4988184aa938 100644 --- a/space/space/space/space/space/utils/logger.go +++ b/space/space/space/space/space/utils/logger.go @@ -1,21 +1,17 @@ -package utils - -import ( - "fmt" - "log" - "os" - - config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" -) - -func LogError(errorLogged error) { - fmt.Println("There is an error!") - file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - log.Fatal(err) - } - - log.SetOutput(file) - - log.Println("Error Log :", errorLogged) -} +package utils + +import ( + "fmt" + "log" +) + +func LogError(errorLogged error) { + fmt.Println("There is an error!") + // file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + // if err != nil { + // log.Fatal(err) + // } + // log.SetOutput(file) + + log.Println("Error Log :", errorLogged) +} diff --git a/space/space/space/space/utils/logger.go b/space/space/space/space/utils/logger.go index fc34cd446974f0e1f416340efb6e4988184aa938..40c86a2b23e2325776d43228382e6cc5655803a1 100644 --- a/space/space/space/space/utils/logger.go +++ b/space/space/space/space/utils/logger.go @@ -1,17 +1,21 @@ -package utils - -import ( - "fmt" - "log" -) - -func LogError(errorLogged error) { - fmt.Println("There is an error!") - // file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - // if err != nil { - // log.Fatal(err) - // } - // log.SetOutput(file) - - log.Println("Error Log :", errorLogged) -} +package utils + +import ( + "fmt" + "log" + "os" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" +) + +func LogError(errorLogged error) { + fmt.Println("There is an error!") + file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + log.SetOutput(file) + + log.Println("Error Log :", errorLogged) +} diff --git a/tests/chat_history_repository_test.go b/tests/chat_history_repository_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ed3b76b47d91d222d937ba28a6836db34220d668 --- /dev/null +++ b/tests/chat_history_repository_test.go @@ -0,0 +1,7 @@ +package tests + +// func SaveChatHistoryTest(t *testing.T) { +// ctx := context.Background() +// chatHistoryRepo := repositories.NewChatHistoryRepository() +// chatHistoryRepo.SaveChatHistory(ctx, "" +// }