kai-image-xl / app.py
seawolf2357's picture
Update app.py
6367c56 verified
raw history blame
No virus
3.71 kB
import requests
import discord
import logging
import os
from transformers import pipeline
import subprocess
import torch
from diffusers import DiffusionPipeline
import io
from PIL import Image
import time
from dotenv import load_dotenv
# .env ํŒŒ์ผ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
load_dotenv()
# ๋กœ๊น… ์„ค์ •
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
# ์ธํ…ํŠธ ์„ค์ •
intents = discord.Intents.default()
intents.message_content = True
# ๋ฒˆ์—ญ ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ง€์ •๋œ ์ฑ„๋„ ID ๊ฐ€์ ธ์˜ค๊ธฐ, ๊ธฐ๋ณธ๊ฐ’ ์‚ฌ์šฉ
TARGET_CHANNEL_ID = int(os.getenv('TARGET_CHANNEL_ID', '123456789012345678')) # ๊ธฐ๋ณธ๊ฐ’ ์‚ฌ์šฉ, ์‹ค์ œ ์ฑ„๋„ ID๋กœ ๋Œ€์ฒด ํ•„์š”
# ๊ณ ์ •๋œ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ
negative_prompt = "blur, low quality, bad composition, ugly, disfigured, weird colors, low quality, jpeg artifacts, lowres, grainy, deformed structures, blurry, opaque, low contrast, distorted details, details are low"
# ๋””๋ฐ”์ด์Šค ์„ค์ •
device = "cuda" if torch.cuda.is_available() else "cpu"
# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
pipeline = DiffusionPipeline.from_pretrained("fluently/Fluently-XL-Final", torch_dtype=torch.float32)
pipeline = pipeline.to(device)
# ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ ํ•จ์ˆ˜
def translate_prompt(prompt):
logging.debug(f'ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ ์ค‘: {prompt}')
translation = translator(prompt, max_length=512)
translated_text = translation[0]['translation_text']
logging.debug(f'๋ฒˆ์—ญ๋œ ํ…์ŠคํŠธ: {translated_text}')
return translated_text
def generate_image(prompt, negative_prompt):
combined_prompt = f"{prompt}. {negative_prompt}"
result = pipeline(combined_prompt)
image = result.images[0] # ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€ ์„ ํƒ
return image
class MyClient(discord.Client):
async def on_ready(self):
logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
subprocess.Popen(["python", "web.py"]) # ๋ณ„๋„์˜ Python ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰
logging.info("web.py ์„œ๋ฒ„๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
async def on_message(self, message):
if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
return
if message.content.startswith('!image '):
self.is_processing = True
try:
prompt = message.content[len('!image '):]
prompt_en = translate_prompt(prompt)
image = generate_image(prompt_en, negative_prompt)
user_id = message.author.id
if image:
# ์ด๋ฏธ์ง€๋ฅผ Discord์— ์ง์ ‘ ์—…๋กœ๋“œ
with io.BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await message.channel.send(
f"<@{user_id}> ๋‹˜์ด ์š”์ฒญํ•˜์‹  ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค:",
file=discord.File(fp=image_binary, filename='image.png')
)
else:
await message.channel.send(f"<@{user_id}> ์ด๋ฏธ์ง€ ์ƒ์„ฑ์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
finally:
self.is_processing = False
else:
await message.channel.send('์˜ฌ๋ฐ”๋ฅธ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. ์˜ˆ) "!image ๊ท€์—ฌ์šด ๊ณ ์–‘์ด๊ฐ€ ์ž ์„ ์ž๊ณ ์žˆ๋‹ค." ๋“ฑ์œผ๋กœ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.')
# ๋ด‡ ์‹คํ–‰
if __name__ == "__main__":
discord_token = os.getenv('DISCORD_TOKEN')
discord_client = MyClient(intents=intents)
discord_client.run(discord_token)