env, docker, requirements, custom server url, gradio auth

This commit is contained in:
Alexander Borisov 2024-01-15 10:21:55 +00:00
parent 48e92b085e
commit 1f30f42f51
7 changed files with 87 additions and 45 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode
.venv
.env

View File

@ -2,6 +2,8 @@ MIT License
Copyright (c) 2023 Мишген
Copyright for portions of project soaska/sd_telegram are held by Мишген, 2023 as part of project amputator84/sd_telegram. All other copyright for project soaska/sd_telegram are held by Alex Borisov.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights

View File

@ -1,20 +1,45 @@
# sd_telegram
telegram bot on aiogram python 3.10.6 to generate images in automatic1111 locally
create bot from [BotFather](https://t.me/BotFather) and use token in [API_TOKEN](https://github.com/amputator84/sd_telegram/blob/master/bot.py#L32)
the bot is installed in automatic1111 via extensions or use _git clone_ into _C:\stable-diffusion-webui\extensions\sd_telegram_
telegram bot on aiogram to generate images with automatic1111 fast api
```bash
cp sample.env .env
```
pip install aiogram
pip install webuiapi
pip install translate
pip install transformers
pip install vk_api
pip install ok_api
cd C:\stable-diffusion-webui\extensions\sd_telegram\
create bot from [BotFather](https://t.me/BotFather) and use token in API_TOKEN variable.
add vk and ok tokens to env file.
> заходим в https://oauth.vk.com/authorize?client_id=51626357&scope=photos&redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&display=page&response_type=token \
> где 51626357 - номер вашего включенного приложения, созданного в https://vk.com/apps?act=manage, \
> photos - зона доступа. \
> После перехода и подтверждения выцепляем access_token из адресной строки \
> TODO auto requests \
> OK https://ok.ru/vitrine/myuploaded \
> Добавить приложение - https://ok.ru/app/setup \
> дбавить платформу - OAUTH \
> VALUABLE_ACCESS = Обязательно \
> PHOTO_CONTENT = Обязательно \
> Ссылка на страницу = https://apiok.ru/oauth_callback \
> Список разрешённых redirect_uri = https://apiok.ru/oauth_callback \
> сохранить, перезайти \
> Ищем ID приложения справа от "Основные настройки приложения" - ID 512002358821 \
> Открываем в браузере https://connect.ok.ru/oauth/authorize?client_id=512002358821&scope=PHOTO_CONTENT;VALUABLE_ACCESS&response_type=token&redirect_uri=https://apiok.ru/oauth_callback \
> С адресной строки копируем token в access_token ниже \
> application_key = Публичный ключ справа от "Основные настройки приложения" \
> Вечный access_token - Получить новый \
> application_secret_key = Session_secret_key \
run bot with python
```bash
python -m venv venv
source venv/bin/activate
pip install -m requirements.txt
python bot.py
``````
```
run bot in docker
```bash
docker build -t soaska/sd_bot .
docker run soaska/sd_bot
```
use ; in prompt as delimiter to be divided into several separate parts, like ```cat;dog;car```
@ -105,5 +130,4 @@ Donations are **not needed**. Who wants to subscribe to [my generations](https:/
Lifehack`s:
If Lora dont work, see [this](https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/7984?ref=blog.hinablue.me#issuecomment-1514312942)
© _Mishgen_

49
bot.py
View File

@ -1,4 +1,5 @@
# https://docs.aiogram.dev/en/latest/
from ipaddress import ip_address
from aiogram import Bot, Dispatcher, types
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.contrib.fsm_storage.memory import MemoryStorage
@ -34,6 +35,10 @@ import logging
import vk_api
from vk_api import VkUpload #https://github.com/python273/vk_api
from ok_api import OkApi, Upload # https://github.com/needkirem/ok_api
import dotenv
import os
dotenv.load_dotenv()
# Настройка логгера
logging.basicConfig(format="[%(asctime)s] %(levelname)s : %(name)s : %(message)s",
@ -44,35 +49,18 @@ logging.getLogger('aiogram').setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
# from https://t.me/BotFather
API_BOT_TOKEN = "TOKEN_HERE"
#заходим в https://oauth.vk.com/authorize?client_id=51626357&scope=photos&redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&display=page&response_type=token
# где 51626357 - номер вашего включенного приложения, созданного в https://vk.com/apps?act=manage,
# photos - зона доступа.
# После перехода и подтверждения выцепляем access_token из адресной строки
# TODO auto requests
# OK https://ok.ru/vitrine/myuploaded
# Добавить приложение - https://ok.ru/app/setup
# дбавить платформу - OAUTH
# VALUABLE_ACCESS = Обязательно
# PHOTO_CONTENT = Обязательно
# Ссылка на страницу = https://apiok.ru/oauth_callback
# Список разрешённых redirect_uri = https://apiok.ru/oauth_callback
# сохранить, перезайти
# Ищем ID приложения справа от "Основные настройки приложения" - ID 512002358821
# Открываем в браузере https://connect.ok.ru/oauth/authorize?client_id=512002358821&scope=PHOTO_CONTENT;VALUABLE_ACCESS&response_type=token&redirect_uri=https://apiok.ru/oauth_callback
# С адресной строки копируем token в access_token ниже
# application_key = Публичный ключ справа от "Основные настройки приложения"
# Вечный access_token - Получить новый
# application_secret_key = Session_secret_key
VK_TOKEN = 'VK_TOKEN_HERE'
API_BOT_TOKEN = 'API_BOT_TOKEN_HERE'
VK_ALBUM_ID = 'VK_ALBUM_ID' # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789
OK_ACCESS_TOKEN = 'OK_ACCESS_TOKEN_HERE'
OK_APPLICATION_KEY = 'OK_APPLICATION_KEY_HERE'
OK_APPLICATION_SECRET_KEY = 'OK_APPLICATION_SECRET_KEY_HERE'
OK_GROUP_ID = 'OK_GROUP_ID_HERE'
VK_TOKEN = os.getenv('VK_TOKEN')
API_BOT_TOKEN = os.getenv('API_BOT_TOKEN')
VK_ALBUM_ID = os.getenv('VK_ALBUM_ID') # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789
OK_ACCESS_TOKEN = os.getenv('OK_ACCESS_TOKEN')
OK_APPLICATION_KEY = os.getenv('OK_APPLICATION_KEY')
OK_APPLICATION_SECRET_KEY = os.getenv('OK_APPLICATION_SECRET_KEY')
OK_GROUP_ID = os.getenv('OK_GROUP_ID')
ARRAY_INLINE = []
print(API_BOT_TOKEN)
bot = Bot(token=API_BOT_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
@ -97,10 +85,11 @@ def getAttrtxt2img():
# -------- GLOBAL ----------
formatted_date = datetime.today().strftime("%Y-%m-%d")
host = "127.0.0.1"
port = "7861"
host = os.getenv('SD_HOST')
port = os.getenv('SD_PORT')
# https://github.com/mix1009/sdwebuiapi
api = webuiapi.WebUIApi(host=host, port=port)
api.set_auth(os.getenv('SD_USERNAME'), os.getenv('SD_PASSWORD'))
# TODO --share used shared link. https://123456.gradio.live/docs does not work
local = "http://" + host + ":" + port
process = None
@ -640,7 +629,7 @@ async def inl_sd(message: Union[types.Message, types.CallbackQuery]) -> None:
await message.message.edit_text(
"Запускаем SD\n" + getTxt(), reply_markup=getStart()
)
url = 'http://127.0.0.1:7861/docs'
url = f'http://{host}:port/docs/'
n = 0
while n != 200:
time.sleep(2)

6
dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM python:3.11
LABEL maintainer="info@borisov-ab.ru"
COPY . /bot
WORKDIR /bot
RUN pip install -r requirements.txt
CMD ["python", "/bot/bot.py"]

11
example.env Normal file
View File

@ -0,0 +1,11 @@
VK_TOKEN = 'VK_TOKEN_HERE'
API_BOT_TOKEN = 'API_BOT_TOKEN_HERE'
VK_ALBUM_ID = 'VK_ALBUM_ID'
OK_ACCESS_TOKEN = 'OK_ACCESS_TOKEN_HERE'
OK_APPLICATION_KEY = 'OK_APPLICATION_KEY_HERE'
OK_APPLICATION_SECRET_KEY = 'OK_APPLICATION_SECRET_KEY_HERE'
OK_GROUP_ID = 'OK_GROUP_ID_HERE'
SD_HOST = '127.0.0.1'
SD_PORT = '7861'
SD_USERNAME = 'user'
SD_PASSWORD = '1234'

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
aiogram==2.22.2
webuiapi
translate
transformers
vk_api
ok_api
python-dotenv