alexander-lazarin commited on
Commit
3823333
1 Parent(s): 0a823ca

Add remove mj from reqs, copy mj file manually

Browse files
requirements.txt CHANGED
@@ -5,4 +5,3 @@ requests==2.31.0
5
  urllib3==2.0.2
6
  gradio
7
  openai
8
- unofficial_midjourney_python_api @ git+https://github.com/yachty66/unofficial_midjourney_python_api
 
5
  urllib3==2.0.2
6
  gradio
7
  openai
 
unofficial_midjourney_python_api/midjourney_api.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from urllib.parse import urlparse
3
+ import os
4
+ import random
5
+ import time
6
+ import json
7
+
8
+ class MidjourneyApi():
9
+ def __init__(self, prompt, application_id, guild_id, channel_id, version, id, authorization):
10
+ self.application_id = application_id
11
+ self.guild_id = guild_id
12
+ self.channel_id = channel_id
13
+ self.version = version
14
+ self.id = id
15
+ self.authorization = authorization
16
+ self.prompt = prompt
17
+ self.message_id = ""
18
+ self.custom_id = ""
19
+ self.image_path_str = ""
20
+ self.send_message()
21
+ self.get_message()
22
+ self.choose_images()
23
+ self.download_image()
24
+
25
+ def send_message(self):
26
+ url = "https://discord.com/api/v9/interactions"
27
+ data = {
28
+ "type": 2,
29
+ "application_id": self.application_id,
30
+ "guild_id": self.guild_id,
31
+ "channel_id": self.channel_id,
32
+ "session_id": "cannot be empty",
33
+ "data": {
34
+ "version": self.version,
35
+ "id": self.id,
36
+ "name": "imagine",
37
+ "type": 1,
38
+ "options": [
39
+ {
40
+ "type": 3,
41
+ "name": "prompt",
42
+ "value": self.prompt
43
+ }
44
+ ],
45
+ "application_command": {
46
+ "id": self.id,
47
+ "application_id": self.application_id,
48
+ "version": self.version,
49
+ "default_member_permissions": None,
50
+ "type": 1,
51
+ "nsfw": False,
52
+ "name": "imagine",
53
+ "description": "Create images with Midjourney",
54
+ "dm_permission": True,
55
+ "contexts": None,
56
+ "options": [
57
+ {
58
+ "type": 3,
59
+ "name": "prompt",
60
+ "description": "The prompt to imagine",
61
+ "required": True
62
+ }
63
+ ]
64
+ },
65
+ "attachments": []
66
+ },
67
+ }
68
+ headers = {
69
+ 'Authorization': self.authorization,
70
+ 'Content-Type': 'application/json',
71
+ }
72
+ response = requests.post(url, headers=headers, json=data)
73
+
74
+ def get_message(self):
75
+ headers = {
76
+ 'Authorization': self.authorization,
77
+ "Content-Type": "application/json",
78
+ }
79
+ for i in range(3):
80
+ time.sleep(30)
81
+ try:
82
+ response = requests.get(f'https://discord.com/api/v9/channels/{self.channel_id}/messages', headers=headers)
83
+ messages = response.json()
84
+ most_recent_message_id = messages[0]['id']
85
+ self.message_id = most_recent_message_id
86
+ components = messages[0]['components'][0]['components']
87
+ buttons = [comp for comp in components if comp.get('label') in ['U1', 'U2', 'U3', 'U4']]
88
+ custom_ids = [button['custom_id'] for button in buttons]
89
+ random_custom_id = random.choice(custom_ids)
90
+ self.custom_id = random_custom_id
91
+ break
92
+ except:
93
+ ValueError("Timeout")
94
+
95
+ def choose_images(self):
96
+ url = "https://discord.com/api/v9/interactions"
97
+ headers = {
98
+ "Authorization": self.authorization,
99
+ "Content-Type": "application/json",
100
+ }
101
+ data = {
102
+ "type": 3,
103
+ "guild_id": self.guild_id,
104
+ "channel_id": self.channel_id,
105
+ "message_flags": 0,
106
+ "message_id": self.message_id,
107
+ "application_id": self.application_id,
108
+ "session_id": "cannot be empty",
109
+ "data": {
110
+ "component_type": 2,
111
+ "custom_id": self.custom_id,
112
+ }
113
+ }
114
+ response = requests.post(url, headers=headers, data=json.dumps(data))
115
+
116
+ def download_image(self):
117
+ headers = {
118
+ 'Authorization': self.authorization,
119
+ "Content-Type": "application/json",
120
+ }
121
+ for i in range(3):
122
+ time.sleep(30)
123
+ try:
124
+ response = requests.get(f'https://discord.com/api/v9/channels/{self.channel_id}/messages', headers=headers)
125
+ messages = response.json()
126
+ most_recent_message_id = messages[0]['id']
127
+ self.message_id = most_recent_message_id
128
+ image_url = messages[0]['attachments'][0]['url']
129
+ image_response = requests.get(image_url)
130
+ a = urlparse(image_url)
131
+ image_name = os.path.basename(a.path)
132
+ self.image_path_str = f"images/{image_name}"
133
+ with open(f"images/{image_name}", "wb") as file:
134
+ file.write(image_response.content)
135
+ break
136
+ except:
137
+ raise ValueError("Timeout")
138
+
139
+ def image_path(self):
140
+ return self.image_path_str
141
+