seawolf2357 commited on
Commit
13bad59
Β·
verified Β·
1 Parent(s): 161a319

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -57
app.py CHANGED
@@ -1,11 +1,7 @@
 
1
  import discord
2
  import logging
3
  import os
4
- import uuid
5
- import torch
6
- import subprocess
7
- from huggingface_hub import snapshot_download
8
- from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
9
  from transformers import pipeline
10
 
11
  # λ‘œκΉ… μ„€μ •
@@ -15,47 +11,45 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(nam
15
  intents = discord.Intents.default()
16
  intents.message_content = True
17
 
18
- # Hugging Face λͺ¨λΈ λ‹€μš΄λ‘œλ“œ
19
- huggingface_token = os.getenv("HF_TOKEN")
20
- model_path = snapshot_download(
21
- # repo_id="SG161222/RealVisXL_V4.0",
22
- repo_id="cagliostrolab/animagine-xl-3.1",
23
-
24
- repo_type="model",
25
- # local_dir="RealVisXL_V4.0",
26
- local_dir="animagine-xl-3.1",
27
- token=huggingface_token,
28
- )
29
-
30
- # λͺ¨λΈ λ‘œλ“œ ν•¨μˆ˜
31
- def load_pipeline(pipeline_type):
32
- logging.debug(f'νŒŒμ΄ν”„λΌμΈ λ‘œλ“œ 쀑: {pipeline_type}')
33
- if pipeline_type == "text2img":
34
- return StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16, use_fast=True)
35
- elif pipeline_type == "img2img":
36
- return StableDiffusionImg2ImgPipeline.from_pretrained(model_path, torch_dtype=torch.float16, use_fast=True)
37
-
38
- # λ””λ°”μ΄μŠ€ μ„€μ •
39
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
40
-
41
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
42
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
43
 
44
  # κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
45
  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"
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # λ””μŠ€μ½”λ“œ 봇 클래슀
48
  class MyClient(discord.Client):
49
- def __init__(self, *args, **kwargs):
50
- super().__init__(*args, **kwargs)
51
- self.is_processing = False
52
- self.text2img_pipeline = load_pipeline("text2img").to(device)
53
- self.text2img_pipeline.enable_attention_slicing() # λ©”λͺ¨λ¦¬ μ΅œμ ν™”
54
-
55
  async def on_ready(self):
56
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
57
- subprocess.Popen(["python", "web.py"])
58
- logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
59
 
60
  async def on_message(self, message):
61
  if message.author == self.user:
@@ -68,29 +62,19 @@ class MyClient(discord.Client):
68
  logging.debug(f'λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {prompt_en}')
69
  logging.debug(f'κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ: {negative_prompt}')
70
 
71
- image_path = await self.generate_image(prompt_en, negative_prompt)
72
- user_id = message.author.id # μ‚¬μš©μžμ˜ ID 캑처
73
- await message.channel.send(
74
- f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€.",
75
- file=discord.File(image_path, 'generated_image.png')
76
- )
 
 
77
  finally:
78
  self.is_processing = False
79
-
80
- async def generate_image(self, prompt, negative_prompt):
81
- generator = torch.Generator(device=device).manual_seed(torch.seed())
82
- images = self.text2img_pipeline(prompt, negative_prompt=negative_prompt, num_inference_steps=50, generator=generator)["images"]
83
- image_path = f'/tmp/{uuid.uuid4()}.png'
84
- images[0].save(image_path)
85
- return image_path
86
-
87
- # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ ν•¨μˆ˜
88
- def translate_prompt(prompt):
89
- logging.debug(f'ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ 쀑: {prompt}')
90
- translation = translator(prompt, max_length=512)
91
- translated_text = translation[0]['translation_text']
92
- logging.debug(f'λ²ˆμ—­λœ ν…μŠ€νŠΈ: {translated_text}')
93
- return translated_text
94
 
95
  # λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰
96
  if __name__ == "__main__":
 
1
+ import requests
2
  import discord
3
  import logging
4
  import os
 
 
 
 
 
5
  from transformers import pipeline
6
 
7
  # λ‘œκΉ… μ„€μ •
 
11
  intents = discord.Intents.default()
12
  intents.message_content = True
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
15
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
16
 
17
  # κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
18
  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"
19
 
20
+ # 인퍼런슀 APIλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•œ ν•¨μˆ˜
21
+ def generate_image(prompt, negative_prompt):
22
+ headers = {
23
+ "Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
24
+ }
25
+ data = {
26
+ "inputs": {
27
+ "prompt": prompt,
28
+ "negative_prompt": negative_prompt,
29
+ "num_inference_steps": 50
30
+ }
31
+ }
32
+ api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
33
+ response = requests.post(api_url, headers=headers, json=data)
34
+ if response.status_code == 200:
35
+ image_url = response.json()[0]['url'] # μ‘λ‹΅μ—μ„œ 이미지 URL μΆ”μΆœ
36
+ return image_url
37
+ else:
38
+ logging.error("API μš”μ²­μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€: " + response.text)
39
+ return None
40
+
41
+ # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ ν•¨μˆ˜
42
+ def translate_prompt(prompt):
43
+ logging.debug(f'ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ 쀑: {prompt}')
44
+ translation = translator(prompt, max_length=512)
45
+ translated_text = translation[0]['translation_text']
46
+ logging.debug(f'λ²ˆμ—­λœ ν…μŠ€νŠΈ: {translated_text}')
47
+ return translated_text
48
+
49
  # λ””μŠ€μ½”λ“œ 봇 클래슀
50
  class MyClient(discord.Client):
 
 
 
 
 
 
51
  async def on_ready(self):
52
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
 
 
53
 
54
  async def on_message(self, message):
55
  if message.author == self.user:
 
62
  logging.debug(f'λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {prompt_en}')
63
  logging.debug(f'κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ: {negative_prompt}')
64
 
65
+ image_url = generate_image(prompt_en, negative_prompt)
66
+ user_id = message.author.id
67
+ if image_url:
68
+ await message.channel.send(
69
+ f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€: {image_url}"
70
+ )
71
+ else:
72
+ await message.channel.send(f"<@{user_id}> 이미지 생성에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.")
73
  finally:
74
  self.is_processing = False
75
+ else:
76
+ # "!image" λͺ…λ Ήμ–΄κ°€ 아닐 경우 μ•ˆλ‚΄ λ©”μ‹œμ§€ 전솑
77
+ await message.channel.send('μ˜¬λ°”λ₯Έ λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”. 예) "!image κ·€μ—¬μš΄ 고양이가 μž μ„ μžκ³ μžˆλ‹€." λ“±μœΌλ‘œ μž…λ ₯ν•˜μ‹œλ©΄ 이미지가 μƒμ„±λ©λ‹ˆλ‹€.')
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰
80
  if __name__ == "__main__":