/
album.go
188 lines (172 loc) · 5.19 KB
/
album.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package album
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"path"
"github.com/chinese-slacking-party/dtt-game-backend/config"
"github.com/chinese-slacking-party/dtt-game-backend/db"
"github.com/chinese-slacking-party/dtt-game-backend/db/dao"
"github.com/gin-gonic/gin"
repl "github.com/replicate/replicate-go"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type AddPhotoReq struct {
Desc string `json:"desc"`
File string `json:"filename"`
}
func AddPhoto(c *gin.Context) {
userid, err := c.Cookie("userid")
if err != nil || userid == "" {
c.JSON(http.StatusForbidden, gin.H{"code": 1003, "message": "Not logged in"})
return
}
userObj, err := dao.GetUserByName(c.Request.Context(), userid)
if err != nil {
if err == db.ErrNotFound {
c.JSON(http.StatusForbidden, gin.H{"code": 1006, "message": "User not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"code": 1000, "message": "Query failed"})
}
return
}
var req AddPhotoReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 1000, "message": err.Error()})
return
}
dao.IncrPhotoSeq(c.Request.Context(), userObj.ID)
picKey := fmt.Sprintf("%s-%03d", userObj.Name, userObj.NextPicSeq)
origFilePath := path.Join(config.PhotoDir, userObj.Name, req.File)
newFilePath := path.Join(config.PhotoDir, userObj.Name, picKey+config.AIGenSuffix)
origFileURL := config.OurAddr + path.Join(config.APIFilesFull, userObj.Name, req.File)
newFileURL := config.OurAddr + path.Join(config.APIFilesFull, userObj.Name, picKey+config.AIGenSuffix)
var x = db.Photo{
Seq: userObj.NextPicSeq,
Key: picKey,
Desc: req.Desc,
Original: origFilePath,
UserID: userObj.ID.Hex(),
URLs: map[string]string{
"normal": origFileURL,
"changed": newFileURL,
},
}
if err = dao.AddPhoto(c.Request.Context(), &x); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 1000, "message": "AddPhoto() failed"})
return
}
if err = changeClothes(c.Request.Context(), x.ID, origFileURL, newFilePath); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 1000, "message": "changeClothes() failed " + err.Error()})
}
c.JSON(http.StatusOK, gin.H{
"id": userObj.NextPicSeq,
"key": picKey,
"progress": 0, // Generate
"message": "OK", // Generate
})
}
func changeClothes(ctx context.Context, objectID primitive.ObjectID, inURL string, outFile string) error {
// TODO: global client in `db` package
client, err := repl.NewClient(repl.WithToken(config.ReplicateAPIKey))
if err != nil {
return err
}
prediction, err := client.CreatePredictionWithDeployment(ctx,
config.ReplicateDeploymentOwner, config.ReplicateDeploymentName,
repl.PredictionInput{
"image": inURL,
"prompt": "a person wearing " + getOutfit(),
},
nil, // No webhook for now
false, // This model does not support streaming
)
if err != nil {
return err
}
log.Println("The prediction is", mustMarshalJSONString(prediction))
go waitForClothes(context.Background(), client, objectID, prediction, outFile)
return nil
}
func waitForClothes(ctx context.Context, client *repl.Client, objectID primitive.ObjectID, prediction *repl.Prediction, outFile string) {
predFinish, predError := client.WaitAsync(context.TODO(), prediction)
for predFinish != nil || predError != nil {
select {
case pred, ok := <-predFinish:
if !ok {
predFinish = nil
break
}
if pred == nil {
continue
}
switch pred.Status {
case repl.Starting:
log.Println("Model still starting for", outFile)
case repl.Processing:
progress := pred.Progress()
if progress == nil {
log.Println("Progress not yet available for", outFile)
continue
}
log.Println("Progress for", outFile, "is", progress.Percentage)
dao.UpdatePhotoInitProgress(ctx, objectID, pred.Status.String(), int(100.0*progress.Percentage))
case repl.Succeeded:
log.Println("Downloading", pred.Output.([]interface{})[3])
if err := downloadFile(pred.Output.([]interface{})[3].(string), outFile); err != nil {
log.Println("ERROR! Unable to download result for", outFile, "from", pred.Output.([]interface{})[3], "with error", err)
return
}
dao.UpdatePhotoInitProgress(ctx, objectID, pred.Status.String(), 100)
default:
log.Println("Unexpected prediction status", pred.Status, "for", outFile)
}
case err, ok := <-predError:
if !ok {
predError = nil
}
if err != nil {
log.Println("ERROR!", err)
}
}
if predFinish == nil && predError == nil {
break
}
}
log.Println("Prediction complete with", mustMarshalJSONString(prediction))
}
func getOutfit() string {
return config.Outfits[rand.Intn(len(config.Outfits))]
}
func mustMarshalJSONString(obj any) string {
bts, err := json.Marshal(obj)
if err != nil {
panic(err)
}
return string(bts)
}
func downloadFile(url string, filepath string) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}