seawolf2357 commited on
Commit
9919e94
โ€ข
1 Parent(s): 084ff4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -22
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
- model_id = "fluently/Fluently-XL-Final"
 
 
 
 
 
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
- image_pipeline = DiffusionPipeline.from_pretrained(model_id).to(device)
22
 
23
- # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ง€์ •๋œ ์ฑ„๋„ ID ๊ฐ€์ ธ์˜ค๊ธฐ
24
- TARGET_CHANNEL_ID = os.getenv('TARGET_CHANNEL_ID')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- prompt = message.content[len('!image '):]
37
- translated_prompt = translate_prompt(prompt)
38
- image_path = await generate_image(translated_prompt)
39
- await message.channel.send(file=discord.File(image_path))
40
-
41
- # ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ ํ•จ์ˆ˜
42
- def translate_prompt(prompt):
43
- translation = translator(prompt, max_length=512)
44
- return translation[0]['translation_text']
45
-
46
- # ๋น„๋™๊ธฐ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ•จ์ˆ˜
47
- async def generate_image(prompt):
48
- image = image_pipeline(prompt)["sample"][0] # ์ด๋ฏธ์ง€ ์ƒ์„ฑ
49
- image_path = f"./generated_images/{prompt.replace(' ', '_')}.png"
50
- image.save(image_path)
51
- return image_path
 
 
 
 
 
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__":