Телеграм бот. Python. Нужна помощь!
Цель - человек вводит в телеграме /generate {слово}
и должна выдаваться картинка.
Выводит ошибку и очень много символов. Все токены верны. Помогите, пожалуйста, исправить!
import json
import time
import requests
import telebot
# Установите свой токен бота
TOKEN = 'мой токен'
bot = telebot.TeleBot(TOKEN)
class Text2ImageAPI:
def __init__(self, url, api_key, secret_key):
self.URL = url
self.AUTH_HEADERS = {
'X-Key': f'Key {api_key}',
'X-Secret': f'Secret {secret_key}',
}
def get_model(self):
response = requests.get(self.URL + 'key/api/v1/models', headers=self.AUTH_HEADERS)
data = response.json()
return data[0]['id']
def generate(self, prompt, model, images=1, width=512, height=512):
params = {
"type": "GENERATE",
"numImages": images,
"width": width,
"height": height,
"generateParams": {
"query": f"{prompt}"
}
}
data = {
'model_id': (None, model),
'params': (None, json.dumps(params), 'application/json')
}
response = requests.post(self.URL + 'key/api/v1/text2image/run', headers=self.AUTH_HEADERS, files=data)
data = response.json()
return data['uuid']
def check_generation(self, request_id, attempts=10, delay=10):
while attempts > 0:
response = requests.get(self.URL + 'key/api/v1/text2image/status/' + request_id, headers=self.AUTH_HEADERS)
data = response.json()
if data['status'] == 'DONE':
return data['images']
attempts -= 1
time.sleep(delay)
api = Text2ImageAPI('https://api-key.fusionbrain.ai/', 'токен', 'секретный токен')
@bot.message_handler(commands=['generate'])
def generate_image(message):
model_id = api.get_model()
uuid = api.generate("Sun in sky", model_id)
images = api.check_generation(uuid)
if images:
for image_url in images:
# Скачиваем изображение
print(image_url)
response = requests.get(image_url)
# Отправляем изображение пользователю
bot.send_photo(message.chat.id, response.content)
else:
bot.send_message(message.chat.id, "Image generation failed.")
if __name__ == '__main__':
bot.polling(none_stop=True)
Вот ошибка:
Traceback (most recent call last):
File "/home/runner/images/main.py", line 69, in <module>
bot.polling(none_stop=True)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/__init__.py", line 1178, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/__init__.py", line 1253, in __threaded_polling
raise e
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/__init__.py", line 1215, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/util.py", line 150, in raise_exceptions
raise self.exception_info
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/telebot/__init__.py", line 8449, in _run_middlewares_and_handler
result = handler['function'](message)
File "/home/runner/images/main.py", line 62, in generate_image
response = requests.get(image_url)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/sessions.py", line 575, in request
prep = self.prepare_request(req)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/sessions.py", line 484, in prepare_request
p.prepare(
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/models.py", line 367, in prepare
self.prepare_url(url, params)
File "/home/runner/images/.pythonlibs/lib/python3.10/site-packages/requests/models.py", line 438, in prepare_url
raise MissingSchema( {гигантское кол-во символов})