-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
334 lines (299 loc) · 17 KB
/
main.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import github
import os
import requests
import random
import time
import re
import json
from better_profanity import profanity
ISSUE_NUMBER = int(os.getenv("ISSUE_NUMBER"))
GITHUB_REPO = os.getenv("GITHUB_REPOSITORY")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
DISALLOWED_WORDS = os.getenv("DISALLOWED_WORDS").split(", ")
headers = {
'authority': 'deepnarrationapi.matissetec.dev',
'accept': '/',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'content-type': 'text/plain;charset=UTF-8',
'origin': 'https://deepnarration.matissetec.dev/',
'pragma': 'no-cache',
'referer': 'https://deepnarration.matissetec.dev/',
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
matisseId = "630649313860780043" # matisse's discord id, not sensitive info
allowedListDropDown = ["Dog", "Cat", "Building", "Tree", "Flower", "Book", "Car", "Boat", "Helicopter", "Plane", "Train", "Bus", "Bicycle", "Motorcycle", "Truck"]
allowedListMultiple = ["Grass", "Blue sky", "Clouds", "Rain", "Sun", "Foggy", "Snow", "Sand", "Hail", "Rainbow", "Moon", "Stars", "Mountains", "Ocean", "Desert"]
def close_with_error(issue, msg):
print(f"We are closing the issue with the following error: {msg} label: Invalid")
issue.create_comment(f"ERROR: {msg}")
issue.edit(state="closed", labels=["Invalid"])
def render_readme(imageLocations, gifLocation, bgrmGifLocation):
imageLocation1, imageLocation2, imageLocation3, imageLocation4 = imageLocations
lines = [
"# GenerateImage",
"Click the image below to generate a new image.",
"",
"## How to use",
"",
"<ol>",
" <li>Create a new image</li>",
" <ul>",
" <li>Select the link <a href='https://github.com/MatissesProjects/GenerateImage/issues/new?title=CreateImage%20Dont%20modify%20the%20title%20just%20use%20the%20form&template=NewImage.yml'>New image request form</a></li>",
" <li>Follow the issue creation steps</li>",
" <li>Submit the issue</li>",
" </ul>",
" <li>You can transform any of the images below</li>",
" <ul>",
" <li>Click the link below the image you want to transform</li>",
" <li>Add your transformation text to the title</li>",
" <li>Submit the issue</li>",
" </ul>",
" <li>Take the selected image and make it into a gif</li>",
" <ul>",
" <li>Select the link to make the image into a gif below the image</li>",
" <li>Submit the issue</li>",
" </ul>",
" <li>Take the current gif and remove the background</li>",
" <ul>",
" <li>Select the link <a href='https://github.com/MatissesProjects/GenerateImage/issues/new?title=GifBackgroundRemoval%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title'>Create a background removed gif from current gif</a></li>",
" <li>Submit the issue</li>",
" <li>As a bonus, if I am streaming this will show up on the stream</li>",
" </ul>",
" <li>Wait for the new image to be generated, around 30-50 seconds</li>",
" <li>Optional, <a href='https://github.com/MatissesProjects/GenerateImage/tree/main/PlayGame'>play the game!</a></li>",
"</ol>",
"",
"## Generated Images",
"| image1 | image2 | image3 | image4 |",
"| --- | --- | --- | --- |",
f"| ![image1]({imageLocation1}) | ![image2]({imageLocation2}) | ![image3]({imageLocation3}) | ![image4]({imageLocation4}) ",
f"| [transform image 1](https://github.com/{GITHUB_REPO}/issues/new?title=Transform%20Image1:%20&body=No%20need%20to%20modify%20the%20body,%20just%20add%20your%20transformation%20to%20the%20photo%20in%20the%20title) | [transform image 2](https://github.com/{GITHUB_REPO}/issues/new?title=Transform%20Image2:%20&body=No%20need%20to%20modify%20the%20body,%20just%20add%20your%20transformation%20to%20the%20photo%20in%20the%20title) | [transform image 3](https://github.com/{GITHUB_REPO}/issues/new?title=Transform%20Image3:%20&body=No%20need%20to%20modify%20the%20body,%20just%20add%20your%20transformation%20to%20the%20photo%20in%20the%20title) | [transform image 4](https://github.com/{GITHUB_REPO}/issues/new?title=Transform%20Image4:%20&body=No%20need%20to%20modify%20the%20body,%20just%20add%20your%20transformation%20to%20the%20photo%20in%20the%20title) |",
f"| [image 1 to gif](https://github.com/{GITHUB_REPO}/issues/new?title=ImageToGif%20Image1:%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title) | [image 2 to gif](https://github.com/{GITHUB_REPO}/issues/new?title=ImageToGif%20Image2:%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title) | [image 3 to gif](https://github.com/{GITHUB_REPO}/issues/new?title=ImageToGif%20Image3:%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title) | [image 4 to gif](https://github.com/{GITHUB_REPO}/issues/new?title=ImageToGif%20Image4:%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title) |",
"",
"## Current Generated Gif",
f"<img src='{gifLocation}' width='256' height='256' alt='gif'>",
"",
"## Current Generated Background Removed Gif",
f"<img src='{bgrmGifLocation}' width='256' height='256' alt='gif'>",
"",
"## Want faster results?",
"Try the page these APIs are based on: [Maitisse](https://deepnarration.matissetec.dev/)",
]
return "\n".join(lines)
def transformFunction(issue):
# with open("currentImageURL.txt", "r+") as f:
# targetLocalImage = f.read()
targetLocalImage = getImageFromJson(issue)
if targetLocalImage is None:
return
title = issue.title
allowedStart = "Transform"
if allowedStart not in title:
close_with_error(issue, "Invalid input format, include Transform Image#:")
return
if len(title) == len(allowedStart):
close_with_error(issue, "Input must contain a string to transform")
return
profanity.load_censor_words()
profanity.add_censor_words(DISALLOWED_WORDS)
if profanity.contains_profanity(title.lower()):
close_with_error(issue, "Profanity is not allowed")
return
textToRiffWith = title[len(allowedStart):]
print(f"text we are trying to use: {textToRiffWith}")
data1 = {"discordId":matisseId,"discordUsername":"matisse","targetPicture":targetLocalImage,"prompt":textToRiffWith,"id":random.randint(1000,9999), "accessToken": DISCORD_TOKEN}
print("starting request to backend")
response1 = requests.post('https://deepnarrationapi.matissetec.dev/startSimilarImages', headers=headers, json=data1)
imageLocation = response1.text
if len(imageLocation) > 300:
close_with_error(issue, "Error generating image, the response was wrong")
return
return imageLocation
def inAllowedList(word, allowedList):
return word in allowedList
def parseImageString(issue):
issueBody = issue.body
# Extract the item under "Choose one"
choose_one_match = re.search(r'### Choose one\n+(.+)', issueBody)
choose_one = choose_one_match.group(1).strip() if choose_one_match else None
choose_one = choose_one if inAllowedList(choose_one, allowedListDropDown) else ""
if choose_one == "":
close_with_error(issue, "Used a choice that is not in the allowed list")
return
# Extract the items under "Choose multiple" that have an [X]
choose_multiple_matches = re.findall(r'- \[X\] (.+)', issueBody)
choiceLength = len(choose_multiple_matches)
# filter choose_multiple_matches to only allowed list of words
choose_multiple_matches = list(filter(lambda x: inAllowedList(x, allowedListMultiple), choose_multiple_matches))
if choiceLength != len(choose_multiple_matches):
close_with_error(issue, "Used a choice that is not in the allowed list")
return
# Construct the result string
result = f"{choose_one}, {', '.join(choose_multiple_matches)}"
return result
def createImageFunction(issue):
newImagePrompt = parseImageString(issue)
if newImagePrompt is None:
return
print(newImagePrompt)
data1 = {"discordId":matisseId,"discordUsername":"matisse","prompt":newImagePrompt,"id":random.randint(1000,9999), "accessToken": DISCORD_TOKEN}
print("starting request to backend")
response1 = requests.post('https://deepnarrationapi.matissetec.dev/startCreateImage', headers=headers, json=data1)
imageLocation = response1.text
if len(imageLocation) > 300:
close_with_error(issue, "Error generating image, the response was wrong")
return
print("response from backend", imageLocation)
return imageLocation
def getImageFromJson(issue):
with open("currentImageURLs.json", "r") as f:
currentImageUrls = json.load(f)
imageLocations = currentImageUrls['currentImageURLs']
# index based on the title image#
title = issue.title
try:
# Image#: example- Transform Image3:
# turn into regex?
imageIndex = int(title.split(" ")[1][-2])
except:
close_with_error(issue, "Exception second word in title must be Image# where # is a number from 1-4")
return
if imageIndex < 1 or imageIndex > 4:
close_with_error(issue, "Second word in title must be Image# where # is a number from 1-4")
return
print("image index", imageIndex)
targetLocalImage = imageLocations[imageIndex - 1]
print("targetLocalImage", targetLocalImage)
return targetLocalImage
def imageToGif(issue):
# with open("currentImageURL.txt", "r+") as f:
# targetLocalImage = f.read()
# choose which image from currentImageUrls.json to use
targetLocalImage = getImageFromJson(issue)
if targetLocalImage is None:
return
data1 = {"discordId":matisseId,"targetPicture":targetLocalImage,"discordUsername":"matisse","id":random.randint(1000,9999), "width":128,"height":128, "accessToken": DISCORD_TOKEN}
response1 = requests.post('https://deepnarrationapi.matissetec.dev/startBackgroundExtenderGif', headers=headers, json=data1)
gifLocation = response1.text
if len(gifLocation) > 300:
close_with_error(issue, "Error generating image, the response was wrong")
return
print("response from backend", gifLocation)
return gifLocation
def detectLanguageEnglish(text):
for letter in text:
# If the letter is not alphabetic or a space, return False
if ord(letter) > 128:
return False
return True
def gifBackgroundRemoval(issue):
with open("currentGifURL.txt", "r+") as f:
targetLocalGif = f.read()
data1 = {"discordId":matisseId,"videoUrl":targetLocalGif,"discordUsername":"matisse","id":random.randint(1000,9999), "accessToken": DISCORD_TOKEN}
response1 = requests.post('https://deepnarrationapi.matissetec.dev/startVideoBackgroundRemoval', headers=headers, json=data1)
bgrmGifLocation = response1.text
if len(bgrmGifLocation) > 300:
close_with_error(issue, "Error generating image, the response was wrong")
return
print("response from backend", bgrmGifLocation)
return bgrmGifLocation
def main():
client = github.Github(GITHUB_TOKEN)
repo = client.get_repo(GITHUB_REPO)
issue = repo.get_issue(number=ISSUE_NUMBER)
imageLocation = ""
gifLocation = ""
gifBgrmLocation = ""
checkLang = issue.title
isTransform = "Transform" in issue.title
print(checkLang)
if len(checkLang) < 2 or not detectLanguageEnglish(checkLang):
close_with_error(issue, "Only english is supported")
return
if "Transform" in issue.title:
print("starting Transform")
imageLocation = transformFunction(issue)
elif "CreateImage" in issue.title:
print("starting CreateImage")
imageLocation = createImageFunction(issue)
elif "ImageToGif" in issue.title:
print("starting ImageToGif")
gifLocation = imageToGif(issue)
elif "GifBackgroundRemoval" in issue.title:
print("starting GifBackgroundRemoval")
gifBgrmLocation = gifBackgroundRemoval(issue)
if imageLocation is None or gifLocation is None or gifBgrmLocation is None:
return
print("response from backend: ", imageLocation, gifLocation, gifBgrmLocation)
currentImageNew = False
currentGifNew = False
currentBgrmNew = False
if imageLocation == "" and gifLocation == "":
with open("currentImageURL.txt", "r+") as f:
imageLocation = f.read()
with open("currentGifURL.txt", "r+") as f:
gifLocation = f.read()
currentBgrmNew = True
if imageLocation == "" and gifBgrmLocation == "":
currentGifNew = True
with open("currentImageURL.txt", "r+") as f:
imageLocation = f.read()
with open("currentBgrmGifURL.txt", "r+") as f:
gifBgrmLocation = f.read()
if gifLocation == "" and gifBgrmLocation == "":
with open("currentGifURL.txt", "r+") as f:
gifLocation = f.read()
with open("currentBgrmGifURL.txt", "r+") as f:
gifBgrmLocation = f.read()
currentImageNew = True
with open("currentImageURL.txt", "w+") as f:
f.write(imageLocation)
with open("currentGifURL.txt", "w+") as f:
f.write(gifLocation)
with open("currentBgrmGifURL.txt", "w+") as f:
f.write(gifBgrmLocation)
if currentImageNew:
if isTransform:
currentImage = getImageFromJson(issue)
issue.create_comment(f"The creation of images is about 30 second, if the image come back blank refresh in a few seconds\nThis is based on the image\n[<img src='{currentImage}'>]('{currentImage}')")
else:
issue.create_comment(f"The creation of images is about 30 second, if the image come back blank refresh in a few seconds")
if currentGifNew:
currentImage = getImageFromJson(issue)
issue.create_comment(f"The creation of gifs is about 50 second, if the image come back blank refresh in a few seconds\nThis is based on the image\n[<img src='{currentImage}'>]('{currentImage}')")
if currentBgrmNew:
issue.create_comment(f"The creation of background removed gifs about 40 second, if the image come back blank refresh in a few seconds\nThis is based on the gif\n[<img src='{gifLocation}'>]('{gifLocation}')")
time.sleep(15)
if currentImageNew:
issue.create_comment(f"Your photo is here! \n![new image]({imageLocation}) \n\nif the image doesnt populate refresh in a few seconds\nNext steps are to [transform the image](https://github.com/{GITHUB_REPO}/issues/new?title=Transform%20Image1:%20&body=No%20need%20to%20modify%20the%20body,%20just%20add%20your%20transformation%20to%20the%20photo%20in%20the%20title) or [create a gif](https://github.com/MatissesProjects/GenerateImage/issues/new?title=ImageToGif%20Image1:%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title)")
with open("currentImageURLs.json", "r") as f:
currentImageUrls = json.load(f)
imageLocations = currentImageUrls['currentImageURLs']
imageLocations.pop()
imageLocations.insert(0, imageLocation)
with open("currentImageURLs.json", "w+") as f:
f.write(json.dumps({"currentImageURLs": imageLocations}))
if currentGifNew:
time.sleep(25)
issue.create_comment(f"Your gif is here! \n![new gif]({gifLocation}) \n\nif the gif doesnt populate refresh in a few seconds\nNext step is to [remove the gif background](https://github.com/{GITHUB_REPO}/issues/new?title=GifBackgroundRemoval%20Dont%20modify%20the%20title&body=No%20need%20to%20modify%20the%20body%20or%20the%20title)")
if currentBgrmNew:
time.sleep(20)
issue.create_comment(f"Your background removed gif is here! \n![new gif]({gifBgrmLocation}) \n\nif the gif doesnt populate refresh in a few seconds\nIf I am streaming this will show up on the stream")
with open("currentImageURLs.json", "r") as f:
imageLocations = json.load(f)['currentImageURLs']
readme = render_readme(imageLocations, gifLocation, gifBgrmLocation)
# issue.create_comment(readme)
with open("README.md", "w+") as f:
f.write(readme)
issue.edit(state="closed")
if __name__ == "__main__":
print("RUNNING SCRIPT")
main()