File size: 3,352 Bytes
13bad59
78efe79
440418c
f3985af
021392e
407a575
32c38ef
f3985af
440418c
1831164
440418c
22dee1c
7262aa5
021392e
 
 
539a18a
 
 
13bad59
 
 
 
 
 
efe93ef
13bad59
 
 
 
 
 
 
f714b05
13bad59
 
 
 
 
 
 
 
 
 
9b2f51a
78efe79
 
40d0e92
78efe79
 
dc80b35
 
7262aa5
 
 
9b2f51a
021392e
539a18a
 
 
13bad59
 
 
 
 
 
 
 
7262aa5
 
13bad59
 
 
021392e
9b2f51a
34428f1
9b2f51a
dc80b35
9b2f51a
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
import requests
import discord
import logging
import os
from transformers import pipeline

# ๋กœ๊น… ์„ค์ •
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

# ๋ฒˆ์—ญ ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")

# ๊ณ ์ •๋œ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ
negative_prompt = "blur, low quality, bad composition, ugly, disfigured, weird colors, low quality, jpeg artifacts, lowres, grainy, deformed structures, blurry, opaque, low contrast, distorted details, details are low"

# ์ธํผ๋Ÿฐ์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
def generate_image(prompt, negative_prompt):
    headers = {
        "Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
    }
    data = {
        "inputs": prompt + " " + negative_prompt  # ํ”„๋กฌํ”„ํŠธ์™€ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๊ฒฐํ•ฉ
    }
    api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
    response = requests.post(api_url, headers=headers, json=data)
    if response.status_code == 200:
        image_url = response.json()[0]['url']  # ์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ URL ์ถ”์ถœ
        return image_url
    else:
        logging.error(f"API ์š”์ฒญ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค: {response.text}")
        return None

# ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ ํ•จ์ˆ˜
def translate_prompt(prompt):
    logging.debug(f'ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ ์ค‘: {prompt}')
    translation = translator(prompt, max_length=512)
    translated_text = translation[0]['translation_text']
    logging.debug(f'๋ฒˆ์—ญ๋œ ํ…์ŠคํŠธ: {translated_text}')
    return translated_text

# ๋””์Šค์ฝ”๋“œ ๋ด‡ ํด๋ž˜์Šค
class MyClient(discord.Client):
    async def on_ready(self):
        logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')

    async def on_message(self, message):
        if message.author == self.user:
            return
        if message.content.startswith('!image '):
            self.is_processing = True
            try:
                prompt = message.content[len('!image '):]
                prompt_en = translate_prompt(prompt)
                logging.debug(f'๋ฒˆ์—ญ๋œ ํ”„๋กฌํ”„ํŠธ: {prompt_en}')
                logging.debug(f'๊ณ ์ •๋œ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ: {negative_prompt}')

                image_url = generate_image(prompt_en, negative_prompt)
                user_id = message.author.id
                if image_url:
                    await message.channel.send(
                        f"<@{user_id}> ๋‹˜์ด ์š”์ฒญํ•˜์‹  ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค: {image_url}"
                    )
                else:
                    await message.channel.send(f"<@{user_id}> ์ด๋ฏธ์ง€ ์ƒ์„ฑ์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
            finally:
                self.is_processing = False
        else:
            # "!image" ๋ช…๋ น์–ด๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€ ์ „์†ก
            await message.channel.send('์˜ฌ๋ฐ”๋ฅธ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. ์˜ˆ) "!image ๊ท€์—ฌ์šด ๊ณ ์–‘์ด๊ฐ€ ์ž ์„ ์ž๊ณ ์žˆ๋‹ค." ๋“ฑ์œผ๋กœ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.')

# ๋””์Šค์ฝ”๋“œ ํ† ํฐ ๋ฐ ๋ด‡ ์‹คํ–‰
if __name__ == "__main__":
    discord_token = os.getenv('DISCORD_TOKEN')
    discord_client = MyClient(intents=intents)
    discord_client.run(discord_token)