-
Notifications
You must be signed in to change notification settings - Fork 0
/
midjourney.py
355 lines (279 loc) · 12 KB
/
midjourney.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import math
import os
import re
import time
from enum import Enum
from io import BytesIO
from typing import List, Literal, Optional
import requests
from PIL import Image
from pydantic import BaseModel
from selenium import webdriver
from selenium.common.exceptions import (NoSuchElementException,
StaleElementReferenceException,
TimeoutException)
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from aiconsole.dev.credentials import load_credentials
class MidJourneyImageType(str, Enum):
MIDJOURNEY_UPSCALE = "MIDJOURNEY_UPSCALE"
MIDJOURNEY_GRID = "MIDJOURNEY_GRID"
MidJourneyImageTypeLiteral = Literal[
MidJourneyImageType.MIDJOURNEY_UPSCALE,
MidJourneyImageType.MIDJOURNEY_GRID,
]
class DiscordMessage(BaseModel):
id: str
content: str
images: list[str]
def _get_image_url(message: WebElement) -> str:
# Extract image if present
image_url = ""
# image_element = message.find_elements(By.CSS_SELECTOR, "img.lazyImg-ewiNCh")
# if image_element:
# image_url = image_element[0].get_attribute("src")
if not image_url:
image_element = message.find_elements(
By.CSS_SELECTOR, "a[class*='originalLink_']"
)
if image_element:
image_url = image_element[0].get_attribute("href")
return image_url or ""
def _split_in_four(image: Image.Image) -> list[Image.Image]:
width, height = image.size
images = [
image.crop((0, 0, math.floor(width / 2), math.floor(height / 2))),
image.crop((math.floor(width / 2), 0, width, math.floor(height / 2))),
image.crop((0, math.floor(height / 2), math.floor(width / 2), height)),
image.crop((math.floor(width / 2), math.floor(height / 2), width, height)),
]
return images
def _classify_midjourney_message(
message: DiscordMessage,
) -> Optional[MidJourneyImageTypeLiteral]:
progress_regexp = r"\((100|[1-9]?\d)%\)"
image_number_regexp = r"\*\* - Image #(\d+) <@"
variations_regexp = r" - Variations by @.+ \("
if re.search(progress_regexp, message.content):
return None
image_number_regexp = re.search(image_number_regexp, message.content)
if len(message.images) > 0 and image_number_regexp:
return MidJourneyImageType.MIDJOURNEY_UPSCALE
if len(message.images) > 0 and re.search(r" - @.+ \(", message.content):
return MidJourneyImageType.MIDJOURNEY_GRID
if len(message.images) > 0 and re.search(variations_regexp, message.content):
return MidJourneyImageType.MIDJOURNEY_GRID
return None
def _match_mid_journey_prompt_ignoring_urls(
prompt: str, message_content: str
) -> bool:
"""
Function to check if the '_match' string contains the '_input' string,
after replacing any occurrence of a URL with '<URL>' and converting
to lower case.
"""
# Regular expression pattern for URLs
url_pattern = r"(<?https?:\/\/[^\s]+>?)"
# Convert to lower case and replace URLs
prompt = re.sub(url_pattern, "<URL>", prompt.lower())
message_content = re.sub(url_pattern, "<URL>", message_content.lower())
return prompt in message_content
class MidJourneyAPI:
driver: Optional[webdriver.Chrome] = None
webdriver_service: Optional[Service] = None
def __init__(self):
credentials = load_credentials("midjourney-discord", ["email", "password", "server_id", "channel_id"])
self.email = credentials["email"]
self.password = credentials["password"]
self.server_id = credentials["server_id"]
self.channel_id = credentials["channel_id"]
self.initialize_webdriver()
def extract_messages(self) -> List[DiscordMessage]:
if not self.driver:
raise ValueError("WebDriver not initialized")
# Locate all messages
messages = self.driver.find_elements(
By.CSS_SELECTOR, 'li[class*="messageListItem_"]'
)
extracted_messages = []
for message in messages:
# Extract ID
# extract last segment from
# data-list-item-id="chat-messages___chat-messages-1142869065577283806-1153420952768626838"
try:
message_id = message.get_attribute("id")
except (
AttributeError,
StaleElementReferenceException,
NoSuchElementException,
):
continue
if message_id is None:
continue
# Extract text content
text_content = ""
content_div = message.find_element(
By.CSS_SELECTOR, 'div[class*="messageContent_"]'
)
if content_div:
text_content = content_div.text
# Extract image if present
image_url = _get_image_url(message)
if not image_url:
continue
extracted_messages.append(
DiscordMessage(id=message_id, content=text_content, images=[image_url])
)
# Print the extracted messages
return extracted_messages
def initialize_webdriver(self) -> None:
# Setup WebDriverManager
self.webdriver_service = Service(ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(
'--user-data-dir=./.aic/credentials/selenium'
)
# chrome_options.add_argument("--headless")
# Create a new instance of the Google Chrome driver
self.driver = webdriver.Chrome(
options=chrome_options, service=self.webdriver_service
)
# Navigate to the art channel
art_channel_url = f"https://discord.com/channels/{self.server_id}/{self.channel_id}"
self.driver.get(art_channel_url)
try:
continue_in_browser_button = self.driver.find_element(
By.XPATH, "//button[contains(.,'Continue in Browser')]"
)
continue_in_browser_button.click()
except NoSuchElementException:
pass
# Ensure the page is fully loaded
try:
WebDriverWait(self.driver, 10000).until(
lambda driver: driver.find_elements(
By.CSS_SELECTOR, 'input[name="email"]'
)
or driver.find_elements(
By.CSS_SELECTOR, 'div[class*="channelTextArea_"]'
)
)
except TimeoutException:
print ("Timed out waiting for page to load")
return
# if we have channelTextArea- element then we are already logged in on the right channel
if self.driver.find_elements(
By.CSS_SELECTOR, 'div[class*="channelTextArea_"]'
):
print ("Already logged in")
return
# Input Credentials and submit
self._submit_credentials()
def _submit_credentials(self) -> None:
if not self.driver:
raise ValueError("WebDriver not initialized")
email_input = self.driver.find_element(By.NAME, "email")
email_input.send_keys(self.email)
password_input = self.driver.find_element(By.NAME, "password")
password_input.send_keys(self.password)
password_input.submit()
def create_image(self, prompt: str) -> list[str]:
if not self.driver:
raise ValueError("WebDriver not initialized")
image_paths = []
# Wait for close button or message input box to appear
WebDriverWait(self.driver, 10000).until(
lambda driver: driver.find_elements(
By.CSS_SELECTOR, 'div[class*="channelTextArea_"]'
)
or driver.find_elements(
By.XPATH, "//button[contains(.,'Close')]"
)
)
# Focus on chrome
self.driver.switch_to.window(self.driver.current_window_handle)
# time.sleep(5)
# try:
# print ("Closing popup")
# continue_in_browser_button = self.driver.find_element(
# By.XPATH, "//button[contains(.,'Close')]"
# )
# continue_in_browser_button.click()
# print("Closed popup, waiting for login ...")
# time.sleep(5)
# except NoSuchElementException:
# pass
print("Logged in, sending prompt ...")
# Focus on the body of the website
self.driver.find_element(
By.CSS_SELECTOR, 'html'
).click()
print("Focused on message input box")
self.driver.find_element(By.TAG_NAME, "body").send_keys(Keys.TAB)
# Save current message ids
messages = self.extract_messages()
old_message_ids = [msg.id for msg in messages]
print("Sending prompt ...")
time.sleep(1) # Wait for Discord to load the channel
# Send keys to focused element
self.driver.switch_to.active_element.send_keys("/imagine")
WebDriverWait(self.driver, 1000).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, 'div[class*="autocompleteRowHeading_"]')
)
)
print("Prompt command sent, sending prompt ...")
self.driver.switch_to.active_element.send_keys(Keys.RETURN)
# Replace 'your_message' with the message you want to send
self.driver.switch_to.active_element.send_keys(prompt)
self.driver.switch_to.active_element.send_keys(Keys.RETURN)
print("Prompt sent, monitoring for results ...")
image_paths = self._wait_for_image(old_message_ids, prompt)
return image_paths
def _wait_for_image(self, old_message_ids: list[str], prompt: str) -> list[str]:
while True:
messages = self.extract_messages()
for message in messages:
if (
_match_mid_journey_prompt_ignoring_urls(prompt, message.content)
and message.id not in old_message_ids
and message.images
):
classification = _classify_midjourney_message(message)
if classification is None:
continue
is_midjourney_grid = (
classification == MidJourneyImageType.MIDJOURNEY_GRID
)
if (
len(message.images) == 1
and is_midjourney_grid
and _match_mid_journey_prompt_ignoring_urls(
prompt, message.content
)
):
download_url = message.images[0]
file_name = download_url.split("/")[-1].split("?")[0]
print(f"Downloading image: {file_name}")
# Assuming it should be a GET request
response = requests.get(download_url)
image = Image.open(BytesIO(response.content))
metadata = (
image.info
) # Assuming image.info gives metadata in Pillow
images = _split_in_four(image)
image_paths = []
os.makedirs("./images", exist_ok=True)
for i, image in enumerate(images):
image_name = f"{file_name}-{i}.png"
full_path = f"./images/{image_name}"
image.save(full_path, **metadata)
image_paths.append(full_path)
print(f"Saved image: {full_path}")
return image_paths
time.sleep(1)