File size: 2,469 Bytes
78efe79 440418c f3985af 407a575 98c1b1b 75b9ca0 fc74fc1 98c1b1b 407a575 32c38ef f3985af 440418c 1831164 440418c 98c1b1b 440418c 98c1b1b 08baccf 32c38ef 75b9ca0 cb69e60 64f1359 8954f0b 64f1359 78efe79 08baccf 8954f0b 08baccf 78efe79 98c1b1b 34428f1 78efe79 98c1b1b 78efe79 fc74fc1 64f1359 8954f0b 64f1359 98c1b1b fc74fc1 64f1359 8954f0b 1831164 98c1b1b 57dc30c 75b9ca0 57dc30c 98c1b1b 0926d14 34428f1 |
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 |
import discord
import logging
import os
import asyncio
import subprocess
from huggingface_hub import InferenceClient
import base64
from PIL import Image
import io
# ๋ก๊น
์ค์
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
intents.messages = True
intents.guilds = True
intents.guild_messages = True
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
hf_client = InferenceClient(api_token=os.getenv("HF_TOKEN"), model_id="stabilityai/stable-diffusion-3-medium")
# ํน์ ์ฑ๋ ID
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
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} has logged in!')
subprocess.Popen(["python", "web.py"]) # Start the web.py server as a separate process
logging.info("Web.py server has been started.")
async def on_message(self, message):
if message.author == self.user or not message.content:
return
if message.channel.id != SPECIFIC_CHANNEL_ID and not isinstance(message.channel, discord.Thread):
return
if self.is_processing:
return
self.is_processing = True
try:
image_path = await generate_image(message.content)
if image_path:
await send_image(message.channel, image_path)
else:
await message.channel.send("Failed to generate the image.")
finally:
self.is_processing = False
async def generate_image(prompt):
try:
response = hf_client(inputs={"inputs": prompt})
image_data = response['images'][0] # Assuming the response contains image data
image_bytes = io.BytesIO(base64.b64decode(image_data))
image = Image.open(image_bytes)
image_path = "output.png"
image.save(image_path)
return image_path
except Exception as e:
logging.error(f"Failed to generate image: {str(e)}")
return None
async def send_image(channel, image_path):
file = discord.File(image_path)
await channel.send(file=file)
if __name__ == "__main__":
discord_client = MyClient(intents=intents)
discord_client.run(os.getenv('DISCORD_TOKEN'))
|