Spaces:
Runtime error
Runtime error
seawolf2357
commited on
Commit
โข
9919e94
1
Parent(s):
084ff4a
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,14 @@
|
|
|
|
1 |
import discord
|
2 |
-
import os
|
3 |
import logging
|
|
|
4 |
from transformers import pipeline
|
5 |
import subprocess
|
|
|
6 |
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
7 |
|
8 |
# ๋ก๊น
์ค์
|
9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
@@ -15,13 +20,33 @@ intents.message_content = True
|
|
15 |
# ๋ฒ์ญ ํ์ดํ๋ผ์ธ ์ค์
|
16 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
-
image_pipeline = DiffusionPipeline.from_pretrained(model_id).to(device)
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
class MyClient(discord.Client):
|
27 |
async def on_ready(self):
|
@@ -33,22 +58,27 @@ class MyClient(discord.Client):
|
|
33 |
if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
|
34 |
return
|
35 |
if message.content.startswith('!image '):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# ๋ด ์คํ
|
54 |
if __name__ == "__main__":
|
|
|
1 |
+
import requests
|
2 |
import discord
|
|
|
3 |
import logging
|
4 |
+
import os
|
5 |
from transformers import pipeline
|
6 |
import subprocess
|
7 |
+
import torch
|
8 |
from diffusers import DiffusionPipeline
|
9 |
+
import io
|
10 |
+
from PIL import Image
|
11 |
+
import time
|
12 |
|
13 |
# ๋ก๊น
์ค์
|
14 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
|
|
20 |
# ๋ฒ์ญ ํ์ดํ๋ผ์ธ ์ค์
|
21 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
22 |
|
23 |
+
# ํ๊ฒฝ ๋ณ์์์ ์ง์ ๋ ์ฑ๋ ID ๊ฐ์ ธ์ค๊ธฐ
|
24 |
+
TARGET_CHANNEL_ID = int(os.getenv('TARGET_CHANNEL_ID')) # ์ฑ๋ ID๋ ์ ์ํ์ด์ด์ผ ํจ
|
25 |
+
|
26 |
+
# ๊ณ ์ ๋ ๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ
|
27 |
+
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"
|
28 |
+
|
29 |
+
# ๋๋ฐ์ด์ค ์ค์
|
30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
31 |
|
32 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ์ดํ๋ผ์ธ ์ค์
|
33 |
+
pipeline = DiffusionPipeline.from_pretrained("fluently/Fluently-XL-Final", torch_dtype=torch.float32)
|
34 |
+
pipeline = pipeline.to(device)
|
35 |
+
|
36 |
+
# ํ๋กฌํํธ ๋ฒ์ญ ํจ์
|
37 |
+
def translate_prompt(prompt):
|
38 |
+
logging.debug(f'ํ๋กฌํํธ ๋ฒ์ญ ์ค: {prompt}')
|
39 |
+
translation = translator(prompt, max_length=512)
|
40 |
+
translated_text = translation[0]['translation_text']
|
41 |
+
logging.debug(f'๋ฒ์ญ๋ ํ
์คํธ: {translated_text}')
|
42 |
+
return translated_text
|
43 |
+
|
44 |
+
def generate_image(prompt, negative_prompt):
|
45 |
+
combined_prompt = f"{prompt}. {negative_prompt}"
|
46 |
+
result = pipeline(combined_prompt)
|
47 |
+
image = result.images[0] # ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง ์ ํ
|
48 |
+
# ์ด๋ฏธ์ง URL ๋์ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํ๋๋ก ์์
|
49 |
+
return image
|
50 |
|
51 |
class MyClient(discord.Client):
|
52 |
async def on_ready(self):
|
|
|
58 |
if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
|
59 |
return
|
60 |
if message.content.startswith('!image '):
|
61 |
+
self.is_processing = True
|
62 |
+
try:
|
63 |
+
prompt = message.content[len('!image '):]
|
64 |
+
prompt_en = translate_prompt(prompt)
|
65 |
+
image = generate_image(prompt_en, negative_prompt)
|
66 |
+
user_id = message.author.id
|
67 |
+
if image:
|
68 |
+
# ์ด๋ฏธ์ง๋ฅผ Discord์ ์ง์ ์
๋ก๋
|
69 |
+
with io.BytesIO() as image_binary:
|
70 |
+
image.save(image_binary, 'PNG')
|
71 |
+
image_binary.seek(0)
|
72 |
+
await message.channel.send(
|
73 |
+
f"<@{user_id}> ๋์ด ์์ฒญํ์ ์ด๋ฏธ์ง์
๋๋ค:",
|
74 |
+
file=discord.File(fp=image_binary, filename='image.png')
|
75 |
+
)
|
76 |
+
else:
|
77 |
+
await message.channel.send(f"<@{user_id}> ์ด๋ฏธ์ง ์์ฑ์ ์คํจํ์์ต๋๋ค.")
|
78 |
+
finally:
|
79 |
+
self.is_processing = False
|
80 |
+
else:
|
81 |
+
await message.channel.send('์ฌ๋ฐ๋ฅธ ๋ช
๋ น์ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์. ์) "!image ๊ท์ฌ์ด ๊ณ ์์ด๊ฐ ์ ์ ์๊ณ ์๋ค." ๋ฑ์ผ๋ก ์
๋ ฅํ์๋ฉด ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.')
|
82 |
|
83 |
# ๋ด ์คํ
|
84 |
if __name__ == "__main__":
|