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