seawolf2357 commited on
Commit
539a18a
β€’
1 Parent(s): 021392e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -28,7 +28,7 @@ model_path = snapshot_download(
28
 
29
  # λͺ¨λΈ λ‘œλ“œ ν•¨μˆ˜
30
  def load_pipeline(pipeline_type):
31
- logging.debug(f'Loading pipeline: {pipeline_type}')
32
  if pipeline_type == "text2img":
33
  return StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16, use_fast=True)
34
  elif pipeline_type == "img2img":
@@ -40,6 +40,9 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
40
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
41
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
42
 
 
 
 
43
  # λ””μŠ€μ½”λ“œ 봇 클래슀
44
  class MyClient(discord.Client):
45
  def __init__(self, *args, **kwargs):
@@ -61,25 +64,31 @@ class MyClient(discord.Client):
61
  try:
62
  prompt = message.content[len('!image '):]
63
  prompt_en = translate_prompt(prompt)
64
- logging.debug(f'Translated prompt: {prompt_en}')
65
- image_path = await self.generate_image(prompt_en)
66
- await message.channel.send(file=discord.File(image_path, 'generated_image.png'))
 
 
 
 
 
 
67
  finally:
68
  self.is_processing = False
69
 
70
- async def generate_image(self, prompt):
71
  generator = torch.Generator(device=device).manual_seed(torch.seed())
72
- images = self.text2img_pipeline(prompt, num_inference_steps=50, generator=generator)["images"]
73
  image_path = f'/tmp/{uuid.uuid4()}.png'
74
  images[0].save(image_path)
75
  return image_path
76
 
77
  # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ ν•¨μˆ˜
78
  def translate_prompt(prompt):
79
- logging.debug(f'Translating prompt: {prompt}')
80
  translation = translator(prompt, max_length=512)
81
  translated_text = translation[0]['translation_text']
82
- logging.debug(f'Translated text: {translated_text}')
83
  return translated_text
84
 
85
  # λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰
 
28
 
29
  # λͺ¨λΈ λ‘œλ“œ ν•¨μˆ˜
30
  def load_pipeline(pipeline_type):
31
+ logging.debug(f'νŒŒμ΄ν”„λΌμΈ λ‘œλ“œ 쀑: {pipeline_type}')
32
  if pipeline_type == "text2img":
33
  return StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16, use_fast=True)
34
  elif pipeline_type == "img2img":
 
40
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
41
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
42
 
43
+ # κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
44
+ 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"
45
+
46
  # λ””μŠ€μ½”λ“œ 봇 클래슀
47
  class MyClient(discord.Client):
48
  def __init__(self, *args, **kwargs):
 
64
  try:
65
  prompt = message.content[len('!image '):]
66
  prompt_en = translate_prompt(prompt)
67
+ logging.debug(f'λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {prompt_en}')
68
+ logging.debug(f'κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ: {negative_prompt}')
69
+
70
+ image_path = await self.generate_image(prompt_en, negative_prompt)
71
+ user_id = message.author.id # μ‚¬μš©μžμ˜ ID 캑처
72
+ await message.channel.send(
73
+ f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€.",
74
+ file=discord.File(image_path, 'generated_image.png')
75
+ )
76
  finally:
77
  self.is_processing = False
78
 
79
+ async def generate_image(self, prompt, negative_prompt):
80
  generator = torch.Generator(device=device).manual_seed(torch.seed())
81
+ images = self.text2img_pipeline(prompt, negative_prompt=negative_prompt, num_inference_steps=50, generator=generator)["images"]
82
  image_path = f'/tmp/{uuid.uuid4()}.png'
83
  images[0].save(image_path)
84
  return image_path
85
 
86
  # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ ν•¨μˆ˜
87
  def translate_prompt(prompt):
88
+ logging.debug(f'ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ 쀑: {prompt}')
89
  translation = translator(prompt, max_length=512)
90
  translated_text = translation[0]['translation_text']
91
+ logging.debug(f'λ²ˆμ—­λœ ν…μŠ€νŠΈ: {translated_text}')
92
  return translated_text
93
 
94
  # λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰