Spaces:
Runtime error
Runtime error
File size: 4,966 Bytes
78efe79 e552e5d b1c63d1 49c60e7 b1c63d1 fdd1a4b e552e5d 440418c 22dee1c e552e5d 49c60e7 ee1b591 b1c63d1 fdd1a4b b1c63d1 fdd1a4b 49c60e7 b1c63d1 a2fb30b b1c63d1 a2fb30b 49c60e7 b1c63d1 49c60e7 b1c63d1 471c2f6 b1c63d1 a2fb30b b1c63d1 5c6c963 49c60e7 b1c63d1 49c60e7 b1c63d1 204d278 b1c63d1 204d278 b1c63d1 49c60e7 a2fb30b 49c60e7 a2fb30b 5c6c963 b1c63d1 a2fb30b 49c60e7 5c6c963 b1c63d1 49c60e7 b1c63d1 49c60e7 b1c63d1 49c60e7 b1c63d1 34428f1 b1c63d1 49c60e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import discord
import logging
import os
from io import BytesIO
from gradio_client import Client
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 ν΄λΌμ΄μΈνΈ μ€μ
api_client = Client("http://211.233.58.202:7960/")
# λμ€μ½λ λ΄ ν΄λμ€
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("μ λ μ΄λ―Έμ§ μμ±μ μνν μ μμΌλ©°, μμ±λ μ΄λ―Έμ§μ λν μ€λͺ
μ νκΈλ‘ μ 곡νκ³ μνΈ λνλ₯Ό ν μ μμ΅λλ€. '!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, used_seed, translated_prompt = await self.generate_image(prompt)
user_id = message.author.id
await message.channel.send(
f"<@{user_id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ
λλ€.\n"
f"μ¬μ©λ μλ: {used_seed}\n"
f"λ²μλ ν둬ννΈ: {translated_prompt}",
file=discord.File(image_path)
)
# μ΄λ―Έμ§ μμ± ν μ€λͺ
μ 곡 λ° λν
await initiate_conversation(prompt, message)
except Exception as e:
logging.error(f'μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ: {e}')
await message.channel.send("μ¬μ© μμ: !image κ³ μμ΄κ° 'HAPPY WORLD'λΌκ³ μ°μ¬μ§ νμ§νμ λ€κ³ μλ€. ")
finally:
self.is_processing = False
async def generate_image(self, prompt):
if not prompt:
raise ValueError("Prompt is empty or None")
logging.debug(f"Sending request to API with prompt: {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 response received: {result}")
if isinstance(result, tuple) and len(result) == 3:
image_path, used_seed, translated_prompt = result
logging.info(f"Image generated at: {image_path}")
logging.info(f"Used seed: {used_seed}")
logging.info(f"Translated prompt: {translated_prompt}")
return image_path, used_seed, translated_prompt
else:
raise ValueError("Unexpected API response format")
except gradio_client.exceptions.AppError as e:
logging.error(f"Gradio App Error: {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) |