Spaces:
Build error
Build error
| 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) |