seawolf2357 commited on
Commit
60f5cc0
Β·
verified Β·
1 Parent(s): 295a949

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -41
app.py CHANGED
@@ -8,7 +8,6 @@ import io
8
  from PIL import Image
9
  import time
10
 
11
-
12
  # λ‘œκΉ… μ„€μ •
13
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
14
 
@@ -19,6 +18,9 @@ intents.message_content = True
19
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
20
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
21
 
 
 
 
22
  # κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
23
  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"
24
 
@@ -31,24 +33,17 @@ def translate_prompt(prompt):
31
  return translated_text
32
 
33
  def generate_image(prompt, negative_prompt):
34
- headers = {
35
- "Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
36
- }
37
- # ν”„λ‘¬ν”„νŠΈμ™€ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈλ₯Ό κ²°ν•©ν•˜μ—¬ ν•˜λ‚˜μ˜ λ¬Έμžμ—΄λ‘œ 처리
38
  combined_prompt = f"{prompt}. {negative_prompt}"
39
- data = {
40
- "inputs": combined_prompt
41
- }
42
  api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
43
  response = requests.post(api_url, headers=headers, json=data)
44
  if response.status_code == 200:
45
- image_url = response.json()[0]['url'] # μ‘λ‹΅μ—μ„œ 이미지 URL μΆ”μΆœ
46
- return image_url
47
  else:
48
  logging.error(f"API μš”μ²­μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€: {response.text}")
49
  return None
50
 
51
-
52
  class MyClient(discord.Client):
53
  async def on_ready(self):
54
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
@@ -56,52 +51,26 @@ class MyClient(discord.Client):
56
  logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
57
 
58
  async def on_message(self, message):
59
- if message.author == self.user:
60
  return
61
  if message.content.startswith('!image '):
62
  self.is_processing = True
63
  try:
64
  prompt = message.content[len('!image '):]
65
  prompt_en = translate_prompt(prompt)
66
- logging.debug(f'λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {prompt_en}')
67
- logging.debug(f'κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ: {negative_prompt}')
68
-
69
  image_url = generate_image(prompt_en, negative_prompt)
70
- user_id = message.author.id
71
  if image_url:
72
- await message.channel.send(
73
- f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€: {image_url}"
74
- )
75
  else:
76
- await message.channel.send(f"<@{user_id}> 이미지 생성에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.")
77
  finally:
78
  self.is_processing = False
79
  else:
80
- # "!image" λͺ…λ Ήμ–΄κ°€ 아닐 경우 μ•ˆλ‚΄ λ©”μ‹œμ§€ 전솑
81
  await message.channel.send('μ˜¬λ°”λ₯Έ λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”. 예) "!image κ·€μ—¬μš΄ 고양이가 μž μ„ μžκ³ μžˆλ‹€." λ“±μœΌλ‘œ μž…λ ₯ν•˜μ‹œλ©΄ 이미지가 μƒμ„±λ©λ‹ˆλ‹€.')
82
 
83
-
84
- def generate_image_with_retry(prompt, negative_prompt, max_retries=3):
85
- headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
86
- combined_prompt = f"{prompt}. {negative_prompt}"
87
- data = {"inputs": combined_prompt}
88
- api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
89
-
90
- for attempt in range(max_retries):
91
- response = requests.post(api_url, headers=headers, json=data)
92
- if response.status_code == 200:
93
- return response.json()[0]['url']
94
- elif response.status_code == 503 and 'estimated_time' in response.json():
95
- wait_time = response.json()['estimated_time']
96
- logging.info(f"λͺ¨λΈ λ‘œλ”© 쀑, {wait_time}초 ν›„ μž¬μ‹œλ„ν•©λ‹ˆλ‹€.")
97
- time.sleep(wait_time) # λŒ€κΈ° ν›„ μž¬μ‹œλ„
98
- else:
99
- logging.error(f"API μš”μ²­μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€: {response.text}")
100
- return None
101
- logging.error("μ΅œλŒ€ μž¬μ‹œλ„ 횟수 초과")
102
- return None
103
  # 봇 μ‹€ν–‰
104
  if __name__ == "__main__":
105
  discord_token = os.getenv('DISCORD_TOKEN')
106
  discord_client = MyClient(intents=intents)
107
  discord_client.run(discord_token)
 
 
8
  from PIL import Image
9
  import time
10
 
 
11
  # λ‘œκΉ… μ„€μ •
12
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
13
 
 
18
  # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
19
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
20
 
21
+ # ν™˜κ²½ λ³€μˆ˜μ—μ„œ μ§€μ •λœ 채널 ID κ°€μ Έμ˜€κΈ°
22
+ TARGET_CHANNEL_ID = os.getenv('TARGET_CHANNEL_ID')
23
+
24
  # κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
25
  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"
26
 
 
33
  return translated_text
34
 
35
  def generate_image(prompt, negative_prompt):
36
+ headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
 
 
 
37
  combined_prompt = f"{prompt}. {negative_prompt}"
38
+ data = {"inputs": combined_prompt}
 
 
39
  api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
40
  response = requests.post(api_url, headers=headers, json=data)
41
  if response.status_code == 200:
42
+ return response.json()[0]['url']
 
43
  else:
44
  logging.error(f"API μš”μ²­μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€: {response.text}")
45
  return None
46
 
 
47
  class MyClient(discord.Client):
48
  async def on_ready(self):
49
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
 
51
  logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
52
 
53
  async def on_message(self, message):
54
+ if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
55
  return
56
  if message.content.startswith('!image '):
57
  self.is_processing = True
58
  try:
59
  prompt = message.content[len('!image '):]
60
  prompt_en = translate_prompt(prompt)
 
 
 
61
  image_url = generate_image(prompt_en, negative_prompt)
 
62
  if image_url:
63
+ await message.channel.send(f"<@{message.author.id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€: {image_url}")
 
 
64
  else:
65
+ await message.channel.send(f"<@{message.author.id}> 이미지 생성에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.")
66
  finally:
67
  self.is_processing = False
68
  else:
 
69
  await message.channel.send('μ˜¬λ°”λ₯Έ λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”. 예) "!image κ·€μ—¬μš΄ 고양이가 μž μ„ μžκ³ μžˆλ‹€." λ“±μœΌλ‘œ μž…λ ₯ν•˜μ‹œλ©΄ 이미지가 μƒμ„±λ©λ‹ˆλ‹€.')
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  # 봇 μ‹€ν–‰
72
  if __name__ == "__main__":
73
  discord_token = os.getenv('DISCORD_TOKEN')
74
  discord_client = MyClient(intents=intents)
75
  discord_client.run(discord_token)
76
+