File size: 5,329 Bytes
056f521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a040879
056f521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a040879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
056f521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# CREATED BY: https://t.me/O_oKarma
# API CREDITS: @Qewertyy
# PROVIDED BY: https://github.com/Team-ProjectCodeX

# <============================================== IMPORTS =========================================================>
import base64

from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import CommandHandler, ContextTypes

from Mikobot import LOGGER as logger
from Mikobot import function
from Mikobot.state import state

# <=======================================================================================================>

# <================================================ CONSTANTS =====================================================>
API_URL = "https://lexica.qewertyy.dev/models"
PALM_MODEL_ID = 0
GPT_MODEL_ID = 5

# <================================================ FUNCTIONS =====================================================>


async def get_api_response(model_id, api_params, api_url):
    try:
        response = await state.post(api_url, params=api_params)
        if response.status_code == 200:
            data = response.json()
            return data.get(
                "content", f"Error: Empty response received from the {model_id} API."
            )
        else:
            return f"Error: Request failed with status code {response.status_code}."
    except state.RequestError as e:
        return f"Error: An error occurred while calling the {model_id} API. {e}"


async def palm_chatbot(update: Update, context: ContextTypes.DEFAULT_TYPE):
    args = context.args
    if not args:
        await context.bot.send_message(
            chat_id=update.effective_chat.id,
            text="Error: Missing input text after /palm command.",
        )
        return

    input_text = " ".join(args)

    result_msg = await context.bot.send_message(
        chat_id=update.effective_chat.id, text="🌴"
    )

    api_params = {"model_id": PALM_MODEL_ID, "prompt": input_text}
    api_response = await get_api_response("PALM", api_params, API_URL)

    await result_msg.delete()
    await context.bot.send_message(chat_id=update.effective_chat.id, text=api_response)


async def gpt_chatbot(update: Update, context: ContextTypes.DEFAULT_TYPE):
    args = context.args
    if not args:
        await context.bot.send_message(
            chat_id=update.effective_chat.id,
            text="Error: Missing input text after /askgpt command.",
        )
        return

    input_text = " ".join(args)

    result_msg = await context.bot.send_message(
        chat_id=update.effective_chat.id, text="πŸ’¬"
    )

    api_params = {"model_id": GPT_MODEL_ID, "prompt": input_text}
    api_response = await get_api_response("GPT", api_params, API_URL)

    await result_msg.delete()
    await context.bot.send_message(chat_id=update.effective_chat.id, text=api_response)


# Define the upscale_image function
async def upscale_image(update: Update, context: ContextTypes.DEFAULT_TYPE):
    try:
        # Check if the replied message contains a photo
        if update.message.reply_to_message and update.message.reply_to_message.photo:
            # Send a message indicating upscaling is in progress
            progress_msg = await update.message.reply_text(
                "Upscaling your image, please wait..."
            )

            # Access the image file_id from the replied message
            image = await update.message.reply_to_message.photo[-1].get_file()

            # Download the image and save it
            image_path = await image.download_to_drive()

            with open(image_path, "rb") as image_file:
                f = image_file.read()

            b = base64.b64encode(f).decode("utf-8")

            response = await state.post(
                "https://lexica.qewertyy.dev/upscale",
                data={"image_data": b},
            )

            # Save the upscaled image
            upscaled_file_path = "upscaled_image.png"
            with open(upscaled_file_path, "wb") as output_file:
                output_file.write(response.content)

            # Delete the progress message
            await context.bot.delete_message(
                chat_id=update.message.chat_id, message_id=progress_msg.message_id
            )

            # Send the upscaled image as a PNG file
            await update.message.reply_document(
                document=open(upscaled_file_path, "rb"),
                caption=f"<b>Upscaled your image.</b>\n<b>Generated By:</b> @{context.bot.username}",
                parse_mode=ParseMode.HTML,
            )
        else:
            await update.message.reply_text("Please reply to an image to upscale it.")

    except Exception as e:
        logger.error(f"Failed to upscale the image: {e}")
        await update.message.reply_text(
            "Failed to upscale the image. Please try again later."
        )


# <================================================ HANDLER =======================================================>
# Register the upscale_image command handler
function(CommandHandler("upscale", upscale_image, block=False))
function(CommandHandler("palm", palm_chatbot, block=False))
function(CommandHandler("askgpt", gpt_chatbot, block=False))
# <================================================ END =======================================================>