Spaces:
Runtime error
Runtime error
Commit ·
f1aa0b1
1
Parent(s): 7f8da6d
Deploy files from GitHub repository
Browse filesFiles changed (49)
- Dockerfile+1-1
- space/space/Dockerfile+3-4
- space/space/space/space/.env.exampleAdded+11
- space/space/space/space/.github/workflows/deploy.ymlAdded+52
- space/space/space/space/.github/workflows/go.ymlAdded+28
- space/space/space/space/.gitignoreAdded+5
- space/space/space/space/DockerfileAdded+40
- space/space/space/space/LICENSEAdded+201
- space/space/space/space/audio/20250616_235223.mp3Added
- space/space/space/space/config/config.goAdded+27
- space/space/space/space/config/database_connection_config.goAdded+61
- space/space/space/space/config/openai_client_config.goAdded+11
- space/space/space/space/config/replicate_client_config.goAdded+14
- space/space/space/space/controller/controller.goAdded+64
- space/space/space/space/controller/home_controller.goAdded+9
- space/space/space/space/controller/prediction_controller.goAdded+95
- space/space/space/space/docker-compose.yamlAdded
- space/space/space/space/factory/factory.goAdded+1
- space/space/space/space/factory/prediction_factory.goAdded+19
- space/space/space/space/go.modAdded+53
- space/space/space/space/go.sumAdded+126
- space/space/space/space/logs/error_log.txtAdded+19
- space/space/space/space/logs/security_log.txtAdded
- space/space/space/space/main.goAdded+14
- space/space/space/space/middleware/authentication_middleware.goAdded+70
- space/space/space/space/middleware/middleware.goAdded+30
- space/space/space/space/middleware/response_middleware.goAdded+45
- space/space/space/space/models/authentication_dto.goAdded+5
- space/space/space/space/models/authentication_payload_model.goAdded+7
- space/space/space/space/models/entities_model.goAdded+27
- space/space/space/space/models/exception_model.goAdded+16
- space/space/space/space/models/http_response_dto_model.goAdded+15
- space/space/space/space/models/model.goAdded+3
- space/space/space/space/models/prediction_dto_model.goAdded+10
- space/space/space/space/repositories/account_repository.goAdded+30
- space/space/space/space/repositories/chat_history_repository.goAdded+24
- space/space/space/space/repositories/repository.goAdded+154
- space/space/space/space/router/prediction_route.goAdded+14
- space/space/space/space/router/router.goAdded+14
- space/space/space/space/services/authentictaion_service.goAdded+11
- space/space/space/space/services/openai_service.goAdded+106
- space/space/space/space/services/prediction_service.goAdded+59
- space/space/space/space/services/replicate_service.goAdded+68
- space/space/space/space/services/service.goAdded+63
- space/space/space/space/space/.gitattributesAdded+35
- space/space/space/space/utils/api_response.goAdded+52
- space/space/space/space/utils/helper.goAdded+11
- space/space/space/space/utils/logger.goAdded+21
- space/space/space/space/utils/util.goAdded+9
27 unmodified lines2829303132333427 unmodified linesecho "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 aplikasiRUN go build -o main .27 unmodified lines2829303132333427 unmodified linesecho "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 aplikasiRUN go build -o main .
13 unmodified lines1415161718192021222324252610 unmodified lines3738394013 unmodified linesCOPY . .# Buat file .env dengan variabel environment menggunakan Hugging Face secretsRUN --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 && \10 unmodified linesEXPOSE 7860# Jalankan aplikasiCMD ["./main"]13 unmodified lines14151617181920212223242510 unmodified lines3637383913 unmodified linesCOPY . .# Buat file .env dengan variabel environment menggunakan Hugging Face secretsRUN --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 && \10 unmodified linesEXPOSE 7860# Jalankan aplikasiCMD ["./main"]
1234567891011DB_HOST =DB_USER =DB_PASSWORD =DB_PORT =DB_NAME =SALT =HOST_ADDRESS = HOST_PORT = LOG_PATH = logsOPEN_AI_API_KEY =REPLICATE_API_TOKEN =12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152name: 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"12345678910111213141516171819202122232425262728# 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 ./...12345.env
vendor/
quzuu-be.exe
README.md.qodo12345678910111213141516171819202122232425262728293031323334353637383940# 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"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 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.123456789101112131415161718192021222324252627package 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")
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061package 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.")
}1234567891011package config
import (
"github.com/sashabaranov/go-openai"
)
var OpenAIClient *openai.Client
func init() {
OpenAIClient = openai.NewClient(OPEN_AI_API_KEY)
}1234567891011121314package config
import (
"github.com/replicate/replicate-go"
)
var ReplicateClient *replicate.Client
func init() {
ReplicateClient, err = replicate.NewClient(replicate.WithTokenFromEnv())
if err != nil {
panic(err)
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364package 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)
}
}123456789package controller
import "github.com/gin-gonic/gin"
func HomeController(c *gin.Context) {
c.JSON(200, gin.H{
"message": "API Is Running Gladly!",
})
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495package 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)
}1package factory12345678910111213141516171819package 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
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253module 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)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126github.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=123456789101112131415161718192025/06/16 23:53:52 Error Log : open audio: is a directory2025/06/16 23:54:20 Error Log : open audio: is a directoryopen audio: is a directoryopen audio: is a directory2025/06/16 23:54:37 Error Log : open audio: is a directoryopen audio: is a directoryopen audio: is a directoryopen audio: is a directoryopen audio: is a directoryopen audio: is a directory2025/06/16 23:56:46 Error Log : open audio: is a directory2025/06/16 23:57:43 Error Log : open audio: is a directory2025/06/17 00:00:14 Error Log : open audio: is a directory2025/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}}]1234567891011121314package 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()
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970// 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
}
}123456789101112131415161718192021222324252627282930package 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)
}123456789101112131415161718192021222324252627282930313233343536373839404142434445package 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 responses12345package models
type LoginRequest struct {
FingerPrintToken string `json:"email" binding:"required"`
}1234567package models
type AccountData struct {
UserID int
VerifyStatus string
ErrVerif error
}123456789101112131415161718192021222324252627package 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" }12345678910111213141516package 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"`
}123456789101112131415package 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"`
}123package models
12345678910package models
import "mime/multipart"
type PredictionRequest struct {
ImageFile multipart.File
ImageFileName string
AudioQuestionFile multipart.File
AudioQuestionFilename string
}123456789101112131415161718192021222324252627282930package 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
}123456789101112131415161718192021222324package 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
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154package 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()
}1234567891011121314package 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)
}
}1234567891011121314package 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)
}1234567891011package services
import "context"
type AuthenticationService interface {
Register(ctx context.Context, fingerPrint string)
Login(ctx context.Context, fingerPrint string)
}
type authenticationService interface {
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106package 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
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859package 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
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768package 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
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263package 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
}1234567891011121314151617181920212223242526272829303132333435*.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 -textsaved_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 -text12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152package 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())
}
}
}1234567891011package 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)
}123456789101112131415161718192021package 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)
}123456789package utils
func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string {
if condition {
return valueIfTrue
} else {
return valueIfFalse
}
}Dockerfile+1-1
space/space/Dockerfile+3-4
space/space/space/space/.env.exampleAdded+11
space/space/space/space/.github/workflows/deploy.ymlAdded+52
space/space/space/space/.github/workflows/go.ymlAdded+28
space/space/space/space/.gitignoreAdded+5
space/space/space/space/DockerfileAdded+40
space/space/space/space/LICENSEAdded+201
space/space/space/space/audio/20250616_235223.mp3Added
space/space/space/space/config/config.goAdded+27
space/space/space/space/config/database_connection_config.goAdded+61
space/space/space/space/config/openai_client_config.goAdded+11
space/space/space/space/config/replicate_client_config.goAdded+14
space/space/space/space/controller/controller.goAdded+64
space/space/space/space/controller/home_controller.goAdded+9
space/space/space/space/controller/prediction_controller.goAdded+95
space/space/space/space/docker-compose.yamlAdded
space/space/space/space/factory/factory.goAdded+1
space/space/space/space/factory/prediction_factory.goAdded+19
space/space/space/space/go.modAdded+53
space/space/space/space/go.sumAdded+126
space/space/space/space/logs/error_log.txtAdded+19
space/space/space/space/logs/security_log.txtAdded
space/space/space/space/main.goAdded+14
space/space/space/space/middleware/authentication_middleware.goAdded+70
space/space/space/space/middleware/middleware.goAdded+30
space/space/space/space/middleware/response_middleware.goAdded+45
space/space/space/space/models/authentication_dto.goAdded+5
space/space/space/space/models/authentication_payload_model.goAdded+7
space/space/space/space/models/entities_model.goAdded+27
space/space/space/space/models/exception_model.goAdded+16
space/space/space/space/models/http_response_dto_model.goAdded+15
space/space/space/space/models/model.goAdded+3
space/space/space/space/models/prediction_dto_model.goAdded+10
space/space/space/space/repositories/account_repository.goAdded+30
space/space/space/space/repositories/chat_history_repository.goAdded+24
space/space/space/space/repositories/repository.goAdded+154
space/space/space/space/router/prediction_route.goAdded+14
space/space/space/space/router/router.goAdded+14
space/space/space/space/services/authentictaion_service.goAdded+11
space/space/space/space/services/openai_service.goAdded+106
space/space/space/space/services/prediction_service.goAdded+59
space/space/space/space/services/replicate_service.goAdded+68
space/space/space/space/services/service.goAdded+63
space/space/space/space/space/.gitattributesAdded+35
space/space/space/space/utils/api_response.goAdded+52
space/space/space/space/utils/helper.goAdded+11
space/space/space/space/utils/logger.goAdded+21
space/space/space/space/utils/util.goAdded+9