Update chatbot/plugins/chat.py
Browse files- chatbot/plugins/chat.py +96 -9
chatbot/plugins/chat.py
CHANGED
@@ -17,13 +17,24 @@
|
|
17 |
# You should have received a copy of the GNU Affero General Public License
|
18 |
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
import requests
|
21 |
import time
|
22 |
import json
|
23 |
-
import asyncio
|
24 |
import io
|
25 |
-
import os
|
26 |
-
import re
|
27 |
|
28 |
from PIL import Image
|
29 |
from pyrogram import *
|
@@ -104,6 +115,71 @@ class TaskManager:
|
|
104 |
|
105 |
task_manager = TaskManager()
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
@Client.on_callback_query(filters.regex("^cancels"))
|
108 |
async def cancel_(client, callback_query):
|
109 |
chat_id = callback_query.message.chat.id
|
@@ -126,15 +202,21 @@ async def handle_photo(client, message):
|
|
126 |
]
|
127 |
try:
|
128 |
spam_chats.append(chat_id)
|
129 |
-
file_path = await message.download()
|
130 |
-
caption = message.caption or "What's this?"
|
131 |
-
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
132 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
133 |
await asyncio.sleep(1.5)
|
134 |
ai_reply = await message.reply_text(
|
135 |
"Uploading file..",
|
136 |
reply_markup=InlineKeyboardMarkup(buttons)
|
137 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
|
139 |
backup_chat.append({"role": "user", "parts": [{"text": caption}]})
|
140 |
response_reads = x.get_response_image(caption, file_path)
|
@@ -175,15 +257,20 @@ async def handle_video(client, message, model_):
|
|
175 |
]
|
176 |
try:
|
177 |
spam_chats.append(chat_id)
|
178 |
-
video_file_name = await message.download(file_name="newvideo.mp4")
|
179 |
-
caption = message.caption or "What's this?"
|
180 |
-
model = genai.GenerativeModel(model_name=model_)
|
181 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
182 |
await asyncio.sleep(1.5)
|
183 |
ai_reply = await message.reply_text(
|
184 |
"Uploading file..",
|
185 |
reply_markup=InlineKeyboardMarkup(buttons)
|
186 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
video_file = genai.upload_file(path=video_file_name)
|
188 |
while video_file.state.name == "PROCESSING":
|
189 |
await asyncio.sleep(10)
|
|
|
17 |
# You should have received a copy of the GNU Affero General Public License
|
18 |
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
|
20 |
+
import asyncio
|
21 |
+
import importlib
|
22 |
+
import math
|
23 |
+
import os
|
24 |
+
import re
|
25 |
+
import shlex
|
26 |
+
import subprocess
|
27 |
+
import sys
|
28 |
+
import time
|
29 |
+
import traceback
|
30 |
+
from io import BytesIO
|
31 |
+
from types import ModuleType
|
32 |
+
from typing import Dict, List, Optional, Tuple, Union
|
33 |
+
|
34 |
import requests
|
35 |
import time
|
36 |
import json
|
|
|
37 |
import io
|
|
|
|
|
38 |
|
39 |
from PIL import Image
|
40 |
from pyrogram import *
|
|
|
115 |
|
116 |
task_manager = TaskManager()
|
117 |
|
118 |
+
async def progress(current, total, message, start, type_of_ps, file_name=None):
|
119 |
+
"""Progress Bar For Showing Progress While Uploading / Downloading File - Normal"""
|
120 |
+
now = time.time()
|
121 |
+
diff = now - start
|
122 |
+
if round(diff % 10.00) == 0 or current == total:
|
123 |
+
percentage = current * 100 / total
|
124 |
+
speed = current / diff
|
125 |
+
elapsed_time = round(diff) * 1000
|
126 |
+
if elapsed_time == 0:
|
127 |
+
return
|
128 |
+
time_to_completion = round((total - current) / speed) * 1000
|
129 |
+
estimated_total_time = elapsed_time + time_to_completion
|
130 |
+
progress_str = f"{''.join(['▰' for i in range(math.floor(percentage / 10))])}"
|
131 |
+
progress_str += (
|
132 |
+
f"{''.join(['▱' for i in range(10 - math.floor(percentage / 10))])}"
|
133 |
+
)
|
134 |
+
progress_str += f"{round(percentage, 2)}%\n"
|
135 |
+
tmp = f"{progress_str}{humanbytes(current)} of {humanbytes(total)}\n"
|
136 |
+
tmp += f"ETA: {time_formatter(estimated_total_time)}"
|
137 |
+
if file_name:
|
138 |
+
try:
|
139 |
+
await message.edit(
|
140 |
+
f"{type_of_ps}\n**File Name:** `{file_name}`\n{tmp}"
|
141 |
+
)
|
142 |
+
except FloodWait as e:
|
143 |
+
await asyncio.sleep(e.x)
|
144 |
+
except MessageNotModified:
|
145 |
+
pass
|
146 |
+
else:
|
147 |
+
try:
|
148 |
+
await message.edit(
|
149 |
+
f"{type_of_ps}\n{tmp}", parse_mode=enums.ParseMode.MARKDOWN
|
150 |
+
)
|
151 |
+
except FloodWait as e:
|
152 |
+
await asyncio.sleep(e.x)
|
153 |
+
except MessageNotModified:
|
154 |
+
pass
|
155 |
+
|
156 |
+
def humanbytes(size):
|
157 |
+
"""Convert Bytes To Bytes So That Human Can Read It"""
|
158 |
+
if not size:
|
159 |
+
return ""
|
160 |
+
power = 2**10
|
161 |
+
raised_to_pow = 0
|
162 |
+
dict_power_n = {0: "", 1: "Ki", 2: "Mi", 3: "Gi", 4: "Ti"}
|
163 |
+
while size > power:
|
164 |
+
size /= power
|
165 |
+
raised_to_pow += 1
|
166 |
+
return str(round(size, 2)) + " " + dict_power_n[raised_to_pow] + "B"
|
167 |
+
|
168 |
+
def time_formatter(milliseconds: int) -> str:
|
169 |
+
"""Time Formatter"""
|
170 |
+
seconds, milliseconds = divmod(int(milliseconds), 1000)
|
171 |
+
minutes, seconds = divmod(seconds, 60)
|
172 |
+
hours, minutes = divmod(minutes, 60)
|
173 |
+
days, hours = divmod(hours, 24)
|
174 |
+
tmp = (
|
175 |
+
((str(days) + " day(s), ") if days else "")
|
176 |
+
+ ((str(hours) + " hour(s), ") if hours else "")
|
177 |
+
+ ((str(minutes) + " minute(s), ") if minutes else "")
|
178 |
+
+ ((str(seconds) + " second(s), ") if seconds else "")
|
179 |
+
+ ((str(milliseconds) + " millisecond(s), ") if milliseconds else "")
|
180 |
+
)
|
181 |
+
return tmp[:-2]
|
182 |
+
|
183 |
@Client.on_callback_query(filters.regex("^cancels"))
|
184 |
async def cancel_(client, callback_query):
|
185 |
chat_id = callback_query.message.chat.id
|
|
|
202 |
]
|
203 |
try:
|
204 |
spam_chats.append(chat_id)
|
|
|
|
|
|
|
205 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
206 |
await asyncio.sleep(1.5)
|
207 |
ai_reply = await message.reply_text(
|
208 |
"Uploading file..",
|
209 |
reply_markup=InlineKeyboardMarkup(buttons)
|
210 |
)
|
211 |
+
seconds_time = time.time()
|
212 |
+
file_path = await message.download(
|
213 |
+
progress=progress,
|
214 |
+
progress_args=(ai_reply, seconds_time, "Uploading...")
|
215 |
+
)
|
216 |
+
caption = message.caption or "What's this?"
|
217 |
+
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
218 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
219 |
+
await asyncio.sleep(1.5)
|
220 |
backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
|
221 |
backup_chat.append({"role": "user", "parts": [{"text": caption}]})
|
222 |
response_reads = x.get_response_image(caption, file_path)
|
|
|
257 |
]
|
258 |
try:
|
259 |
spam_chats.append(chat_id)
|
|
|
|
|
|
|
260 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
261 |
await asyncio.sleep(1.5)
|
262 |
ai_reply = await message.reply_text(
|
263 |
"Uploading file..",
|
264 |
reply_markup=InlineKeyboardMarkup(buttons)
|
265 |
)
|
266 |
+
seconds_time = time.time()
|
267 |
+
video_file_name = await message.download(
|
268 |
+
file_name="newvideo.mp4",
|
269 |
+
progress=progress,
|
270 |
+
progress_args=(ai_reply, seconds_time, "Uploading...")
|
271 |
+
)
|
272 |
+
caption = message.caption or "What's this?"
|
273 |
+
model = genai.GenerativeModel(model_name=model_)
|
274 |
video_file = genai.upload_file(path=video_file_name)
|
275 |
while video_file.state.name == "PROCESSING":
|
276 |
await asyncio.sleep(10)
|