kai-flx / app.py
seawolf2357's picture
Update app.py
ea6d816 verified
import discord
import logging
import os
from io import BytesIO
from gradio_client import Client
from gradio_client import exceptions as gradio_client_exceptions
import subprocess
# λ‘œκΉ… μ„€μ •
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
# API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ • + id/pw 반영 λ°©λ²•μž„μž„
api_client = Client("http://211.233.58.202:7953")
# λ””μŠ€μ½”λ“œ 봇 클래슀
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.is_processing = False
async def on_ready(self):
logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
# web.py μ‹€ν–‰
try:
subprocess.Popen(["python", "web.py"])
logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
except Exception as e:
logging.error(f"web.py μ‹€ν–‰ 쀑 였λ₯˜ λ°œμƒ: {e}")
# μƒν˜Έ λŒ€ν™” κΈ°λŠ₯ μ•Œλ¦Ό
channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")))
await channel.send("μ €λŠ” 이미지 생성을 μˆ˜ν–‰ν•  수 있으며, μƒμ„±λœ 이미지에 λŒ€ν•œ μ„€λͺ…을 ν•œκΈ€λ‘œ μ œκ³΅ν•˜κ³  μƒν˜Έ λŒ€ν™”λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 'ν”„λ‘¬ν”„νŠΈμ— ν•œκΈ€ λ˜λŠ” μ˜λ¬Έμ„ μž…λ ₯ ν•˜μ„Έμš”.")
async def on_message(self, message):
# μ§€μ •λœ 채널 ID
TARGET_CHANNEL_ID = 1269529561914413106
# λ©”μ‹œμ§€κ°€ μ§€μ •λœ μ±„λ„μ—μ„œ μ˜€μ§€ μ•Šμ•˜κ±°λ‚˜ 봇 μžμ‹ μ˜ λ©”μ‹œμ§€μΈ 경우 λ¬΄μ‹œ
if message.channel.id != TARGET_CHANNEL_ID or message.author == self.user:
return
# λ©”μ‹œμ§€μ—μ„œ '!'λ₯Ό κΈ°μ€€μœΌλ‘œ λͺ…령어와 ν”„λ‘¬ν”„νŠΈ 뢄리
if message.content.startswith('!'):
command, *prompt_parts = message.content.split(' ')
prompt = ' '.join(prompt_parts)
else:
prompt = message.content # λͺ…λ Ήμ–΄ 없이 ν”„λ‘¬ν”„νŠΈλ§Œ 있으면 κ·ΈλŒ€λ‘œ μ‚¬μš©
if self.is_processing:
await message.channel.send("이미지 생성이 이미 μ§„ν–‰ μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ € μ£Όμ„Έμš”.")
return
self.is_processing = True
try:
image_path, used_seed, translated_prompt = await self.generate_image(prompt)
user_id = message.author.id
await message.channel.send(
content=f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€.\nμ‚¬μš©λœ μ‹œλ“œ: {used_seed}\n",
file=discord.File(image_path)
)
# 이미지 생성 ν›„ μ„€λͺ… 제곡 및 λŒ€ν™”
await initiate_conversation(prompt, message)
except RuntimeError as e:
await message.channel.send(f"이미지 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}")
except Exception as e:
logging.error(f'이미지 생성 쀑 μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜ λ°œμƒ: {e}')
await message.channel.send("이미지 생성 쀑 μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. λ‚˜μ€‘μ— λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.")
finally:
self.is_processing = False
async def generate_image(self, prompt):
if not prompt:
raise ValueError("ν”„λ‘¬ν”„νŠΈκ°€ λΉ„μ–΄μžˆκ±°λ‚˜ Noneμž…λ‹ˆλ‹€")
logging.debug(f"API에 μš”μ²­ 전솑 쀑. ν”„λ‘¬ν”„νŠΈ: {prompt}")
try:
result = api_client.predict(
prompt=prompt,
seed=123,
randomize_seed=False,
width=1024,
height=576,
guidance_scale=5,
num_inference_steps=28,
api_name="/infer_t2i"
)
logging.debug(f"API 응닡 μˆ˜μ‹ : {result}")
if isinstance(result, tuple) and len(result) == 3:
image_path, used_seed, translated_prompt = result
logging.info(f"μƒμ„±λœ 이미지 경둜: {image_path}")
logging.info(f"μ‚¬μš©λœ μ‹œλ“œ: {used_seed}")
return image_path, used_seed, translated_prompt
else:
raise ValueError("μ˜ˆμƒμΉ˜ λͺ»ν•œ API 응닡 ν˜•μ‹")
except gradio_client_exceptions.AppError as e:
logging.error(f"Gradio μ•± 였λ₯˜: {e}")
raise RuntimeError(f"API μ„œλ²„ 였λ₯˜: {str(e)}. μ„œλ²„ κ΄€λ¦¬μžμ—κ²Œ λ¬Έμ˜ν•˜μ„Έμš”.")
except Exception as e:
logging.error(f'API μš”μ²­ 쀑 였λ₯˜ λ°œμƒ: {e}', exc_info=True)
raise RuntimeError(f"API μš”μ²­ 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
async def initiate_conversation(prompt, message):
logging.debug(f'λŒ€ν™” μ‹œμž‘ 쀑: {prompt}')
# 이미지 μ„€λͺ…을 ν•œκΈ€λ‘œ 생성
description = "μƒμ„±λœ μ΄λ―Έμ§€μž…λ‹ˆλ‹€."
logging.debug(f'이미지 μ„€λͺ…: {description}')
await message.channel.send(f"이미지 μ„€λͺ…: {description}")
await continue_conversation(prompt, message)
async def continue_conversation(prompt, message):
# λŒ€ν™” 지속 κΈ°λŠ₯
logging.debug(f'λŒ€ν™” 지속 쀑: {prompt}')
# 계속 λŒ€ν™” λ‚΄μš©μ„ λ°›μ•„ μƒν˜Έμž‘μš©ν•˜λ„λ‘ κ΅¬ν˜„
# λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰
if __name__ == "__main__":
discord_token = os.getenv('DISCORD_TOKEN')
discord_client = MyClient(intents=intents)
discord_client.run(discord_token)