Update VisionCraft API

This commit is contained in:
Abhi
2024-05-31 14:38:06 +05:30
parent c4502a9371
commit fb54d208c5

View File

@@ -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(
"<b>Model List is too long</b> See the Full List <a href='https://visioncraft.top/sd/models'> Here </a>"
"<b>Model List is too long</b> See the Full List <a href='https://visioncraft.top/sd/models-sdxl'> Here </a>"
)
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("<code>Please Wait...</code>")
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"<b>Usage: </b><code>{prefix}vgif [prompt/reply to prompt]*</code>"
)
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"<b>Prompt: </b><code>{prompt}</code>\n<b>Model: </b><code>DALL-E 3</code>",
)
os.remove("generated_image.png")
except KeyError:
try:
error = response["error"]
await message.edit_text(f"<code>{error}</code>")
except KeyError:
detail = response["detail"]
await message.edit_text(f"<code>{detail}</code>")
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("<b>File not found</b>")
return
i = await message.edit("<code>Processing...</code>")
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("<code>Processing...</code>")
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 <code>{ouput}</code>")
else:
await message.edit("<b>To be used on AUIDO files only</b>")
except KeyError:
try:
error = task_result["error"]
await message.edit_text(f"<code>{error}</code>")
except KeyError:
detail = task_result["detail"]
await message.edit_text(f"<code>{detail}</code>")
except ValueError:
await message.edit("<b>File not found</b>")
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",
}