kai-flx / app.py
seawolf2357's picture
Update app.py
8393c96 verified
raw
history blame
4.76 kB
import discord
import logging
import os
import uuid
import subprocess
import threading
import gradio as gr
from gradio_client import Client
# λ‘œκΉ… μ„€μ •
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
# Gradio API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
client = Client("http://211.233.58.202:7960/")
# 이미지 생성 ν•¨μˆ˜
def generate_image(prompt):
# μ‹€μ œ 이미지 생성 λ‘œμ§μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.
# μ˜ˆμ‹œλ‘œ, μ—¬κΈ°μ„œλŠ” λ‹¨μˆœνžˆ promptλ₯Ό 이미지에 ν‘œμ‹œν•˜λŠ” κ°€μ§œ 이미지 생성을 ν•  κ²ƒμž…λ‹ˆλ‹€.
return f"Image generated with prompt: {prompt}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
iface = gr.Interface(fn=generate_image, inputs="text", outputs="image")
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰ ν•¨μˆ˜
def run_gradio():
iface.launch(show_error=True)
# λ””μŠ€μ½”λ“œ 봇 클래슀
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}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
subprocess.Popen(["python", "web.py"])
logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")))
await channel.send("μ €λŠ” 이미지 생성을 μˆ˜ν–‰ν•  수 있으며, μƒμ„±λœ 이미지에 λŒ€ν•œ μ„€λͺ…을 ν•œκΈ€λ‘œ μ œκ³΅ν•˜κ³  μƒν˜Έ λŒ€ν™”λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€. '!image <ν”„λ‘¬ν”„νŠΈ>'λ₯Ό μ‚¬μš©ν•˜μ—¬ 이미지λ₯Ό μš”μ²­ν•˜μ„Έμš”.")
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('!image '):
if self.is_processing:
await message.channel.send("이미지 생성이 이미 μ§„ν–‰ μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ € μ£Όμ„Έμš”.")
return
self.is_processing = True
try:
prompt = message.content[len('!image '):]
image_path = await self.generate_image(prompt)
user_id = message.author.id
await message.channel.send(
f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€.",
file=discord.File(image_path, 'generated_image.png')
)
await initiate_conversation(prompt, image_path, message)
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("Prompt is empty or None")
try:
result = 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 call result: {result}")
if 'path' in result:
image_path = result['path']
logging.debug(f"Image successfully retrieved from {image_path}")
return image_path
else:
raise RuntimeError("이미지 생성 μ‹€νŒ¨: κ²°κ³Όκ°€ λΉ„μ–΄ μžˆκ±°λ‚˜ κ²½λ‘œκ°€ ν¬ν•¨λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
except Exception as e:
logging.error(f'이미지 생성 도쀑 μ˜ˆμ™Έ λ°œμƒ: {e}')
raise RuntimeError(f"이미지 생성 도쀑 μ˜ˆμ™Έ λ°œμƒ: {str(e)}")
async def initiate_conversation(prompt, image_path, 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)
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
iface = gr.Interface(fn=my_model_function, inputs="text", outputs="image")
# μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰, 였λ₯˜ λ³΄μ—¬μ£ΌλŠ” μ˜΅μ…˜ ν™œμ„±ν™”
iface.launch(show_error=True)