Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

remove comments please import telebot from gradioclient import Client import os import…

  1. User

    remove comments please import telebot from gradio_client import Client import os import logging

    Replace 'your-bot-token-here' with your actual Telegram bot token

    TELEGRAM_BOT_TOKEN = 'your-bot-token-here'

    logging.basicConfig(level=logging.INFO) bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN) client = Client('https://yuntian-deng-o1.hf.space/')

    MAX_TEXT_LENGTH = 4000  # Set the maximum allowed length for text messages

    @bot.message_handler(content_types=['text']) def handle_message(message):     user_input = message.text     chat_id = message.chat.id     username = message.from_user.username or message.from_user.first_name

        logging.info(f"Received message from {username}: {user_input}")

        if len(user_input) > MAX_TEXT_LENGTH:         bot.send_message(chat_id, "Your message is too long. Please put it into a file and send it to me.")         return

        bot.send_chat_action(chat_id, 'typing')

        try:         result = client.predict(user_input, api_name="/predict")         output_text = extract_ai_response(result)

            send_response(chat_id, username, output_text)

        except Exception as e:         handle_error(chat_id, username, e)

    @bot.message_handler(content_types=['document']) def handle_document(message):     chat_id = message.chat.id     username = message.from_user.username or message.from_user.first_name     file_name = message.document.file_name

        logging.info(f"Received file from {username}: {file_name}")

        try:         file_info = bot.get_file(message.document.file_id)         downloaded_file = bot.download_file(file_info.file_path)

            # Assuming the file is a text file in UTF-8 encoding         file_content = downloaded_file.decode('utf-8')

            bot.send_chat_action(chat_id, 'typing')

            # Process the file content as the user input         user_input = file_content         result = client.predict(user_input, api_name="/predict")         output_text = extract_ai_response(result)

            send_response(chat_id, username, output_text)

        except Exception as e:         handle_error(chat_id, username, e)

    def extract_ai_response(result):     """     Extracts the AI's response from the result returned by the client.predict method.     """     if isinstance(result, tuple) and len(result) >= 1:         conversation = result[0]  # Get the conversation list         if isinstance(conversation, list) and len(conversation) > 0:             last_exchange = conversation[-1]  # Get the last exchange             if isinstance(last_exchange, list) and len(last_exchange) == 2:                 output_text = last_exchange[1]  # Get just the AI's response             else:                 output_text = str(last_exchange)         else:             output_text = str(conversation)     else:         output_text = str(result)     return output_text

    def send_response(chat_id, username, output_text):     """     Sends the AI's response to the user, either as a message or a document if it's too long.     """     if len(output_text) < 4000:         bot.send_message(chat_id, output_text)         logging.info(f"Sent message to {username}: {output_text}")     else:         filename = 'output.txt'         with open(filename, 'w', encoding='utf-8') as f:             f.write(output_text)         with open(filename, 'rb') as f:             bot.send_document(chat_id, f)             logging.info(f"Sent document to {username}: {filename}")         os.remove(filename)

    def handle_error(chat_id, username, error):     """     Handles exceptions by sending an error message to the user and logging the error.     """     error_message = f"An error occurred: {error}"     bot.send_message(chat_id, error_message)     logging.error(f"Error occurred for {username}: {error}")

    if name == "main":     logging.info("Starting the bot...")     bot.polling() `

  2. Assistant

    Sure, here's your code without comments:

    import telebot
    from gradio_client import Client
    import os
    import logging
    
    TELEGRAM_BOT_TOKEN = 'your-bot-token-here'
    
    logging.basicConfig(level=logging.INFO)
    bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
    client = Client('https://yuntian-deng-o1.hf.space/')
    
    MAX_TEXT_LENGTH = 4000
    
    @bot.message_handler(content_types=['text'])
    def handle_message(message):
        user_input = message.text
        chat_id = message.chat.id
        username = message.from_user.username or message.from_user.first_name
    
        logging.info(f"Received message from {username}: {user_input}")
    
        if len(user_input) > MAX_TEXT_LENGTH:
            bot.send_message(chat_id, "Your message is too long. Please put it into a file and send it to me.")
            return
    
        bot.send_chat_action(chat_id, 'typing')
    
        try:
            result = client.predict(user_input, api_name="/predict")
            output_text = extract_ai_response(result)
    
            send_response(chat_id, username, output_text)
    
        except Exception as e:
            handle_error(chat_id, username, e)
    
    @bot.message_handler(content_types=['document'])
    def handle_document(message):
        chat_id = message.chat.id
        username = message.from_user.username or message.from_user.first_name
        file_name = message.document.file_name
    
        logging.info(f"Received file from {username}: {file_name}")
    
        try:
            file_info = bot.get_file(message.document.file_id)
            downloaded_file = bot.download_file(file_info.file_path)
    
            file_content = downloaded_file.decode('utf-8')
    
            bot.send_chat_action(chat_id, 'typing')
    
            user_input = file_content
            result = client.predict(user_input, api_name="/predict")
            output_text = extract_ai_response(result)
    
            send_response(chat_id, username, output_text)
    
        except Exception as e:
            handle_error(chat_id, username, e)
    
    def extract_ai_response(result):
        """
        Extracts the AI's response from the result returned by the client.predict method.
        """
        if isinstance(result, tuple) and len(result) >= 1:
            conversation = result[0]
            if isinstance(conversation, list) and len(conversation) > 0:
                last_exchange = conversation[-1]
                if isinstance(last_exchange, list) and len(last_exchange) == 2:
                    output_text = last_exchange[1]
                else:
                    output_text = str(last_exchange)
            else:
                output_text = str(conversation)
        else:
            output_text = str(result)
        return output_text
    
    def send_response(chat_id, username, output_text):
        """
        Sends the AI's response to the user, either as a message or a document if it's too long.
        """
        if len(output_text) < 4000:
            bot.send_message(chat_id, output_text)
            logging.info(f"Sent message to {username}: {output_text}")
        else:
            filename = 'output.txt'
            with open(filename, 'w', encoding='utf-8') as f:
                f.write(output_text)
            with open(filename, 'rb') as f:
                bot.send_document(chat_id, f)
                logging.info(f"Sent document to {username}: {filename}")
            os.remove(filename)
    
    def handle_error(chat_id, username, error):
        """
        Handles exceptions by sending an error message to the user and logging the error.
        """
        error_message = f"An error occurred: {error}"
        bot.send_message(chat_id, error_message)
        logging.error(f"Error occurred for {username}: {error}")
    
    if name == "main":
        logging.info("Starting the bot...")
        bot.polling()