forked from TechShreyash/AI-Image-Gen-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueHandler.py
More file actions
180 lines (141 loc) · 4.75 KB
/
queueHandler.py
File metadata and controls
180 lines (141 loc) · 4.75 KB
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
QUEUE = {}
import asyncio
from bingai import generate_image, download_images, WORKER, get_worker, COOKIE_EMAIL
from pyrogram import Client
from pyrogram.types import Message, InputMediaPhoto
import os
from db import addGenStatus
def sorter(key):
return key[1]["pos"]
async def QueueRunner(bot):
global WORKER, QUEUE
while True:
if len(QUEUE) > 0:
TASKS = sorted(QUEUE.items(), key=sorter)
for userid, data in TASKS:
if data["status"] == "waiting":
while True:
cookie = get_worker()
if cookie:
break
else:
await asyncio.sleep(10)
continue
QUEUE[userid]["status"] = "processing"
try:
asyncio.create_task(HandleProcessing(bot, userid, data, cookie))
except:
pass
await asyncio.sleep(5)
STATUS = {}
async def QueueStatus(bot: Client):
global QUEUE, STATUS
while True:
if len(QUEUE) > 0:
TASKS = sorted(QUEUE.items(), key=sorter)
total = len(TASKS)
pos = 1
for userid, data in TASKS:
if data["status"] == "processing":
continue
if userid in STATUS:
if STATUS[userid] == pos:
pos += 1
continue
else:
STATUS[userid] = pos
try:
text = f"**🔢 Queue Position:** `{pos}`\n\n**Total Queued Tasks:** {total}\n\nThis may take a while, Please Wait!"
await bot.edit_message_text(
data["chatid"],
data["msgid"],
text,
)
except:
pass
pos += 1
await asyncio.sleep(1)
await asyncio.sleep(5)
async def HandleProcessing(bot: Client, userid, data: dict, cookie: str):
global QUEUE, WORKER, COOKIE_EMAIL, STATUS
try:
try:
msg = await bot.edit_message_text(
data["chatid"],
data["msgid"],
"**🔄 Generating Images...**\n\nThis may take a while, Please Wait!")
except:
msg = None
image_url = await generate_image(data["promt"], cookie)
if not image_url:
if userid in QUEUE:
del QUEUE[userid]
if userid in STATUS:
del STATUS[userid]
WORKER[cookie] = 0
# try:
# # Send error to log group/channel
# await bot.send_message(
# int('Chat ID'),
# str(userid)
# + "\n\n"
# + str(COOKIE_EMAIL.get(cookie))
# + "\n\n"
# + str(data),
# )
# except:
# pass
try:
await bot.send_message(
data["chatid"],
"**❌ Failed to generate**\n\nTry Again or Improve your Promt, Give more details!\n\nReport Errors @TechZBots_Support",
reply_to_message_id=data["replyid"],
)
except:
pass
try:
await msg.delete()
except:
pass
return
addGenStatus(userid, data["chatid"], len(image_url))
try:
await msg.edit(
"**📥 Downloading Images...**\n\nThis may take a while, Please Wait!"
)
except:
pass
image_path = download_images(image_url, userid, cookie)
try:
await msg.edit(
"""**📤 Uploading Images...**\n\nThis may take a while, Please Wait!
"""
)
except:
pass
media_group = []
for image in image_path:
media_group.append(InputMediaPhoto(image))
await bot.send_media_group(
data["chatid"], media_group, reply_to_message_id=data["replyid"]
)
try:
await msg.delete()
except:
pass
for image in image_path:
try:
os.remove(image)
except:
pass
except Exception as e:
if msg:
await msg.delete()
await bot.send_message(
data["chatid"], str(e), reply_to_message_id=data["replyid"]
)
if userid in QUEUE:
del QUEUE[userid]
if userid in STATUS:
del STATUS[userid]
WORKER[cookie] = 0