-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (192 loc) · 5.12 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"image/png"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/dslipak/pdf"
openai "github.com/sashabaranov/go-openai"
)
func readPdf(path string) (string, error) {
r, err := pdf.Open(path)
// remember close file
if err != nil {
return "", err
}
var buf bytes.Buffer
b, err := r.GetPlainText()
if err != nil {
return "", err
}
buf.ReadFrom(b)
return buf.String(), nil
}
func generateImage(){
c := openai.NewClient("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")
ctx := context.Background()
// Sample image by link
reqUrl := openai.ImageRequest{
Prompt: "Street Style fashion 2023",
Size: openai.CreateImageSize256x256,
ResponseFormat: openai.CreateImageResponseFormatURL,
N: 1,
}
respUrl, err := c.CreateImage(ctx, reqUrl)
if err != nil {
fmt.Printf("Image creation error: %v\n", err)
return
}
fmt.Println(respUrl.Data[0].URL)
// Example image as base64
reqBase64 := openai.ImageRequest{
Prompt: "Street Wear fashion 2013",
Size: openai.CreateImageSize256x256,
ResponseFormat: openai.CreateImageResponseFormatB64JSON,
N: 3,
}
respBase64, err := c.CreateImage(ctx, reqBase64)
if err != nil {
fmt.Printf("Image creation error: %v\n", err)
return
}
imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON)
if err != nil {
fmt.Printf("Base64 decode error: %v\n", err)
return
}
r := bytes.NewReader(imgBytes)
imgData, err := png.Decode(r)
if err != nil {
fmt.Printf("PNG decode error: %v\n", err)
return
}
file, err := os.Create("example.png")
if err != nil {
fmt.Printf("File creation error: %v\n", err)
return
}
defer file.Close()
if err := png.Encode(file, imgData); err != nil {
fmt.Printf("PNG encode error: %v\n", err)
return
}
fmt.Println("The image was saved as example.png")
}
func uploadHandler(w http.ResponseWriter, r *http.Request){
err := r.ParseMultipartForm(52 << 20) // maxMemory 52MB
if err != nil {
http.Error(w, "Max size is 52MB", http.StatusBadRequest)
return
}
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
file, fileHeader, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
buff := make([]byte, 512)
_, err = file.Read(buff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create the uploads folder if it doesn't
// already exist
err = os.MkdirAll("./pdf", os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create a new file in the pdf directory
dst, err := os.Create(fmt.Sprintf("./pdf/%d%s", time.Now().UnixNano(), filepath.Ext(fileHeader.Filename)))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
// Copy the uploaded file to the filesystem
// at the specified destination
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// filetype := http.DetectContentType(buff)
// if filetype != "pdf" {
// http.Error(w, "The provided file format is not allowed. Please upload a pdf file", http.StatusBadRequest)
// return
// }
fmt.Fprintf(w, "File uploaded successfully: ")
fmt.Fprintf(w, fileHeader.Filename)
fmt.Println(fileHeader.Filename)
// truncated for brevity
}
type requestBody struct {
Prompt string `json:"prompt"`
Size string `json:"size"`
ResponseFormat string `json:"response_format"`
N int32 `json:"n"`
Url string `json:"url"`
}
func generateSpecImage(w http.ResponseWriter, r *http.Request){
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
}
var rb requestBody
err = json.Unmarshal(body, rb)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
}
}
func main() {
fmt.Println("Start Running")
fmt.Println("Listening in PORT 4500")
mux := http.NewServeMux()
mux.HandleFunc("/upload", uploadHandler)
//pdf.DebugOn = true
content, err := readPdf("./pdf/1692122891294243000.pdf") // Read local pdf file
if err != nil {
panic(err)
}
fmt.Println(content)
concated_string := fmt.Sprintf("%s\n\n%s", "Can you take the resume about the text below?", content)
client := openai.NewClient("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")
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleAssistant,
Content: concated_string,
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return
}
fmt.Println(resp.Choices[0].Message.Content)
if err := http.ListenAndServe(":4500", mux); err != nil {
log.Fatal(err)
}
}