-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
102 lines (89 loc) · 2.46 KB
/
image.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
package main
import (
"fmt"
"github.com/meinside/openai-go"
tele "gopkg.in/telebot.v3"
"os"
)
func (s *Server) handleImage(c tele.Context) {
photo := c.Message().Photo.File
var fileName string
//var err error
//var reader io.ReadCloser
if s.conf.TelegramServerURL != "" {
f, err := c.Bot().FileByID(photo.FileID)
if err != nil {
Log.Warn("Error getting file ID", "error=", err)
return
}
// start reader from f.FilePath
//reader, err = os.Open(f.FilePath)
//if err != nil {
// Log.Warn("Error opening file", "error=", err)
// return
//}
fileName = f.FilePath
} else {
out, err := os.Create("uploads/" + photo.FileID + ".jpg")
if err != nil {
Log.Warn("Error creating file", "error=", err)
return
}
if err := c.Bot().Download(&photo, out.Name()); err != nil {
Log.Warn("Error getting file content", "error=", err)
return
}
fileName = out.Name()
}
//defer reader.Close()
//
//bytes, err := io.ReadAll(reader)
//if err != nil {
// Log.Warn("Error reading file content", "error=", err)
// return
//}
//
//var base64Encoding string
//
//// Determine the content type of the image file
//mimeType := http.DetectContentType(bytes)
//
//// Prepend the appropriate URI scheme header depending
//// on the MIME type
//switch mimeType {
//case "image/jpeg":
// base64Encoding += "data:image/jpeg;base64,"
//case "image/png":
// base64Encoding += "data:image/png;base64,"
//}
//
//// Append the base64 encoded output
//encoded := base64Encoding + toBase64(bytes)
chat := s.getChat(c.Chat(), c.Sender())
chat.addImageToDialog(c.Message().Caption, fileName)
s.db.Save(&chat)
s.complete(c, "", true)
}
func (s *Server) textToImage(c tele.Context, text string, hd bool) error {
Log.WithField("user", c.Sender().Username).Info("generating image")
options := openai.ImageOptions{}.SetResponseFormat(openai.IamgeResponseFormatURL).
SetSize(openai.ImageSize1024x1024_DallE3).
SetN(1).
SetModel("dall-e-3")
if hd {
options.SetQuality("hd")
}
created, err := s.ai.CreateImage(text, options)
if err != nil {
return fmt.Errorf("failed to create image: %s", err)
}
if len(created.Data) <= 0 {
return fmt.Errorf("no items returned")
}
Log.WithField("user", c.Sender().Username).WithField("results", len(created.Data)).Info("image generation complete")
for _, item := range created.Data {
m := &tele.Photo{File: tele.FromURL(*item.URL)}
_ = c.Send(m, "text", &tele.SendOptions{ReplyTo: c.Message()})
}
return nil
}