-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
234 lines (195 loc) · 6.2 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"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"bufio"
)
// Program constants
const BaseURL = "https://api.zerogpt.com/api/detect/detectText"
const MaxAllowedCharacters = 10000
// To process the response from GptZero
type Response struct {
Data struct {
PhrasesToHumanize []string `json:"h"`
PercentageOfAI float64 `json:"fakePercentage"`
} `json:"data"`
}
// Error handling
func check(e error) {
if e != nil {
panic(e)
}
}
// Getting the contents of the file
func readFile(path string) string {
data, err := os.ReadFile(path)
check(err)
return string(data)
}
// File path generation
func getFilePath() string {
if len(os.Args) < 2 {
fmt.Println("Usage: recycleMe <file_path>")
os.Exit(1)
}
// Just a welcome message :)
fmt.Println("Loading :D")
// Crafting the absolute file path
absolutePath, err := filepath.Abs(os.Args[1])
check(err)
return absolutePath
}
// This is were the HTTP request is crafted
func makeRequest(fileContent string) []byte {
// Headers
const UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0"
const Accept = "application/json, text/plain, */*"
const AcceptLanguage = "en-US,en;q=0.5"
const AcceptEncoding = "gzip, deflate, br"
const ContentType = "application/json"
const Origin = "https://www.zerogpt.com"
const Connection = "keep-alive"
const Referer = "https://www.zerogpt.com/"
const SecFetchDest = "empty"
const SecFetchMode = "cors"
const SecFetchSite = "same-site"
// Prepare the post body
postBody, err := json.Marshal(map[string]string {
"input_text": fileContent,
})
check(err)
// Create a new request
req, err := http.NewRequest("POST", BaseURL, bytes.NewBuffer(postBody))
check(err)
// Append the headers
req.Header.Add("User-Agent", UserAgent)
req.Header.Add("Accept", Accept)
req.Header.Add("Accept-Language", AcceptLanguage)
req.Header.Add("Accept-Encoding", AcceptEncoding)
req.Header.Add("Content-Type", ContentType)
req.Header.Add("Origin", Origin)
req.Header.Add("Connection", Connection)
req.Header.Add("Referer", Referer)
req.Header.Add("Sec-Fetch-Dest", SecFetchDest)
req.Header.Add("Sec-Fetch-Mode", SecFetchMode)
req.Header.Add("Sec-Fetch-Site", SecFetchSite)
resp, err := http.DefaultClient.Do(req)
check(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
return body
}
// Extracting the content details from the response
func extractContentDetails(rawJson []byte) ([]string, float64) {
var parsedJson Response
json.Unmarshal(rawJson, &parsedJson)
return parsedJson.Data.PhrasesToHumanize, parsedJson.Data.PercentageOfAI
}
// Creating a duplicate file for editing
func createDuplicateFile(originalFilePath string, fileContent string) string {
// Extract file data
originalFileName := filepath.Base(originalFilePath)
fileExtension := filepath.Ext(originalFilePath)
fileName := strings.TrimSuffix(originalFileName, fileExtension)
// Create a new duplicate file in the current working directory
newFileName := fileName + "_humanized" + fileExtension
currentWorkingDirectory, err := os.Getwd()
check(err)
newFilePath := filepath.Join(currentWorkingDirectory, newFileName)
newFile, err := os.Create(newFilePath)
check(err)
// Write to the file and close it
_, err = newFile.WriteString(fileContent)
check(err)
newFile.Close()
return newFilePath
}
// Replacing AI content in the duplicate file
func replaceString(replaceString string, matchString string, filePath string) {
fileContent := readFile(filePath)
newFileContent := strings.ReplaceAll(fileContent, matchString, replaceString)
err := os.WriteFile(filePath, []byte(newFileContent), 0644)
check(err)
}
// The iteration that starts the humanization process
func startHumanization(arrToHumanize []string, newFilePath string) {
// Colors
blue := color.New(color.FgBlue)
yellow := color.New(color.FgYellow)
green := color.New(color.FgGreen)
fmt.Printf("Rewrite these sentences in your own words\n")
scanner := bufio.NewScanner(os.Stdin)
for i, v := range arrToHumanize {
yellow.Printf("\n[%d/%d] ", i+1, len(arrToHumanize))
blue.Printf("%s\n", v)
green.Printf("Humanized: ")
// Taking in user input from stdin
var userInput string
scanner.Scan()
userInput = scanner.Text()
// Editing the duplicated file
replaceString(userInput, v, newFilePath)
}
}
// Checks if the text needs to be segmented
func isTooBig(fileContent string) bool {
return len(fileContent) > MaxAllowedCharacters
}
// Segments the text into allowable chunks
func segmentText(fileContent string) []string {
var segments []string
segment := ""
for _, char := range fileContent {
segment += string(char)
if len(segment) > MaxAllowedCharacters {
segments = append(segments, segment)
segment = ""
}
}
segments = append(segments, segment)
return segments
}
// Send the segmented text to ZeroGPT
func sendSegments(segments []string) ([]string, float64) {
toHumanizeSlice := make([]string, 0)
avgAiPercentage := 0.0
for i, segment := range segments {
fmt.Printf("[%d/%d] Sending current segment\n", i+1, len(segments))
rawJson := makeRequest(segment)
stringsToHumanize, aiPercentage := extractContentDetails(rawJson)
toHumanizeSlice = append(toHumanizeSlice, stringsToHumanize...)
avgAiPercentage += aiPercentage / float64(len(segments))
}
return toHumanizeSlice, avgAiPercentage
}
func main() {
// Preparing a duplicated file for editing
path := getFilePath()
fileContent := readFile(path)
newFilePath := createDuplicateFile(path, fileContent)
// Initialize slice & float for the humanization process
stringsToHumanize := make([]string, 0)
aiPercentage := 0.0
// Check if file needs segmentation
if isTooBig(fileContent) {
fmt.Printf("Uh oh! The file is too big. We need to segment it.\n\n")
segments := segmentText(fileContent)
stringsToHumanize, aiPercentage = sendSegments(segments)
} else {
rawJson := makeRequest(fileContent)
stringsToHumanize, aiPercentage = extractContentDetails(rawJson)
}
// Printing out the flagged AI percentage
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("\nAI Percentage (%%): %s\n", red(int(aiPercentage)))
// Starting the editing iteration
startHumanization(stringsToHumanize, newFilePath)
}