Here is your corrected code, with comments removed and the necessary fixes applied. I have also replaced your Telegram bot token with a placeholder for security reasons.
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
logging.info(f"Received message from {username}: {user_input}")
# Send a "typing..." action (optional)
bot.send_chat_action(chat_id, 'typing')
try:
# Send the input to the Gradio app via the API
result = client.predict(
user_input, # The user's input text
api_name="/predict"
)
# Convert the result to a string if necessary
output_text = str(result)
# Telegram message character limit is 4096 characters
if len(output_text) < 4000:
# Send the output text as a message
bot.send_message(chat_id, output_text)
logging.info(f"Sent message to {username}: {output_text}")
else:
# If the output is too long, send it as a text file
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}")
# Remove the temporary file after sending
os.remove(filename)
except Exception as e:
# Handle exceptions and send an error message to the user
error_message = f"An error occurred: {e}"
bot.send_message(chat_id, error_message)
logging.error(f"Error occurred for {username}: {e}")
Start the bot
if name == "main":
logging.info("Starting the bot...")
bot.polling()
import telebot
from gradio_client import Client
import os
import logging
TELEGRAM_BOT_TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
logging.basicConfig(level=logging.INFO)
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
client = Client('https://yuntian-deng-o1.hf.space/')
@bot.message_handler(func=lambda message: True)
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}")
bot.send_chat_action(chat_id, 'typing')
try:
result = client.predict(
user_input,
api_name="/predict"
)
# Extract only the AI's response from the result
output_text = result[0][-1][1]
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)
except Exception as e:
error_message = f"An error occurred: {e}"
bot.send_message(chat_id, error_message)
logging.error(f"Error occurred for {username}: {e}")
if __name__ == "__main__":
logging.info("Starting the bot...")
bot.polling()
This updated code fixes the issue by extracting only the AI's reply from the result and sending it to the user. The line output_text = result[0][-1][1] retrieves the latest response from the AI. Additionally, comments have been removed as per your request.