From fb54d208c593d0fd871b9431c3960cb6d90a2a2f Mon Sep 17 00:00:00 2001 From: Abhi Date: Fri, 31 May 2024 14:38:06 +0530 Subject: [PATCH] Update VisionCraft API --- modules/aiutils.py | 174 +-------------------------------------------- 1 file changed, 2 insertions(+), 172 deletions(-) diff --git a/modules/aiutils.py b/modules/aiutils.py index 42e7057..4b678be 100644 --- a/modules/aiutils.py +++ b/modules/aiutils.py @@ -16,15 +16,7 @@ api_url = "https://visioncraft.top" async def fetch_models(): """Get all available SDXL models""" async with aiohttp.ClientSession() as session, session.get( - "https://visioncraft.top/sd/models" - ) as response: - return await response.json() - - -async def fetch_upscale_models(): - """Get all available upscale models""" - async with aiohttp.ClientSession() as session, session.get( - "https://visioncraft.top/models-upscale" + "https://visioncraft.top/sd/models-sdxl" ) as response: return await response.json() @@ -45,14 +37,6 @@ async def generate_images(data): return await response.read() -async def generate_dalle(data): - """Helper Function to generate image using DALL-E 3""" - async with aiohttp.ClientSession() as session, session.post( - f"{api_url}/dalle", json=data - ) as response: - return await response.read() - - async def download_image(session, image_url, filename): """Get The Image Data From Response""" async with session.get(image_url) as response: @@ -61,44 +45,6 @@ async def download_image(session, image_url, filename): f.write(image_data) -async def upscale_request(api_key, image_data): - """Request Maker Helper function to upscale image for VisionCraft API""" - image_base64 = base64.b64encode(image_data).decode("utf-8") - - payload = { - "token": api_key, - "image": image_base64, - "model": "R-ESRGAN 4x+", - "resize": 4, - } - - # Set up the headers - headers = {"Content-Type": "application/json"} - - async with aiohttp.ClientSession() as session, session.post( - f"{api_url}/upscale", json=payload, headers=headers - ) as response: - return await response.read() - - -async def transcribe_audio(api_key, audio_data, language, task): - """Request Maker Helper function to transcribe audio""" - - audio_base64 = base64.b64encode(audio_data).decode("utf-8") - - payload = { - "token": api_key, - "audio": audio_base64, - "language": language, - "task": task, - } - - async with aiohttp.ClientSession() as session, session.post( - f"{api_url}/whisper", json=payload - ) as response: - return await response.json() - - @Client.on_message(filters.command("vdxl", prefix) & filters.me) async def vdxl(c: Client, message: Message): """Text to Image Generation Using SDXL""" @@ -171,7 +117,7 @@ async def vdxl(c: Client, message: Message): except MessageTooLong: await message.edit_text( - "Model List is too long See the Full List Here " + "Model List is too long See the Full List Here " ) return @@ -179,50 +125,6 @@ async def vdxl(c: Client, message: Message): await message.edit_text(f"An error occurred: {format_exc(e)}") -@Client.on_message(filters.command("dalle", prefix) & filters.me) -async def dalle(c: Client, message: Message): - """Text to Image Generation Using DALL-E 3""" - try: - chat_id = message.chat.id - - await message.edit_text("Please Wait...") - - if len(message.command) >= 2: - prompt = message.text.split(maxsplit=1)[1] - elif message.reply_to_message: - prompt = message.reply_to_message.text - else: - await message.edit_text( - f"Usage: {prefix}vgif [prompt/reply to prompt]*" - ) - return - - data = {"prompt": prompt, "token": vca_api_key, "size": "1792x1024"} - - response = await generate_dalle(data) - - try: - with open("generated_image.png", "wb") as f: - f.write(response) - await message.delete() - await c.send_document( - chat_id, - document="generated_image.png", - caption=f"Prompt: {prompt}\nModel: DALL-E 3", - ) - os.remove("generated_image.png") - except KeyError: - try: - error = response["error"] - await message.edit_text(f"{error}") - except KeyError: - detail = response["detail"] - await message.edit_text(f"{detail}") - - except Exception as e: - await message.edit_text(f"An error occurred: {format_exc(e)}") - - @Client.on_message(filters.command("vgif", prefix) & filters.me) async def vgif(c: Client, message: Message): """Text2GIF Using VisionCraft API""" @@ -274,79 +176,7 @@ async def vgif(c: Client, message: Message): await message.edit_text(f"An error occurred: {format_exc(e)}") -@Client.on_message(filters.command("upscale", prefix) & filters.me) -async def upscale(c: Client, message: Message): - """Default Upscaler of Moon-Userbot: Uses VisionCraft APi""" - try: - photo_data = await message.download() - message_id = message.id - except ValueError: - try: - photo_data = await message.reply_to_message.download() - message_id = message.reply_to_message.id - except ValueError: - await message.edit("File not found") - return - i = await message.edit("Processing...") - - api_key = vca_api_key - with open(photo_data, "rb") as image_file: - image = image_file.read() - upscaled_image_data = await upscale_request(api_key, image) - with open("upscaled_image.png", "wb") as file: - file.write(upscaled_image_data) - await i.delete() - await c.send_document( - message.chat.id, - "upscaled_image.png", - caption="Upscaled!", - reply_to_message_id=message_id, - ) - os.remove("upscaled_image.png") - - -@Client.on_message(filters.command("whisp", prefix) & filters.me) -async def whisp(message: Message): - """Get Text From Audio: Uses VisionCraft API""" - try: - audio_data = await message.reply_to_message.download() - try: - if ( - audio_data == enums.MessageMediaType.AUDIO - or enums.MessageMediaType.VOICE - ): - await message.edit("Processing...") - - api_key = vca_api_key - with open(audio_data, "rb") as audio_file: - audio = audio_file.read() - language = "auto" - task = "transcribe" - task_result = await transcribe_audio(api_key, audio, language, task) - # print(task_result) - ouput = task_result["text"] - await message.edit_text(f"Transcribed result:\n {ouput}") - else: - await message.edit("To be used on AUIDO files only") - except KeyError: - try: - error = task_result["error"] - await message.edit_text(f"{error}") - except KeyError: - detail = task_result["detail"] - await message.edit_text(f"{detail}") - except ValueError: - await message.edit("File not found") - return - except Exception as e: - await message.edit_text(f"An error occurred: {format_exc(e)}") - - modules_help["aiutils"] = { "vdxl [model]* [prompt/reply to prompt]*": "Text to Image with SDXL model", "vgif [prompt/reply to prompt]*": "Text to GIF", - "upscale [cap/reply to image]*": "Upscale Image through VisionCraft API", - "lupscale [cap/reply to image]*": "Upscale Image through Lexica API", - "whisp": "Audio transcription or translation", - "dalle [prompt/reply to prompt]": "Generate image using DALL-E 3", }