fantos commited on
Commit
2edcd19
Β·
verified Β·
1 Parent(s): c4b1110

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,36 +1,33 @@
1
  # img_bot.py
2
  import discord, os, io, re, asyncio, logging, requests, replicate, subprocess
3
- from transformers import pipeline as transformers_pipeline # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ
4
 
5
  # ── ν™˜κ²½ λ³€μˆ˜ ────────────────────────────────────────────────
6
- TOKEN = os.getenv("DISCORD_TOKEN") # Discord 봇 토큰
7
- CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) # κ°μ‹œν•  채널 ID
8
- REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip() # Replicate 토큰(동일 λ³€μˆ˜ μ‚¬μš©)
9
- HF_TOKEN = (os.getenv("HF_TOKEN") or "").strip() # Hugging Face Personal Access Token
 
 
10
 
11
  if not TOKEN or not CHANNEL_ID:
12
  raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID ν™˜κ²½ λ³€μˆ˜λ₯Ό λͺ¨λ‘ μ§€μ •ν•˜μ„Έμš”.")
13
-
14
  if not REPL_TOKEN:
15
- raise RuntimeError(
16
- "OPENAI_API_KEY ν™˜κ²½ λ³€μˆ˜μ— Replicate Personal Access Token 값을 λ„£μ–΄μ£Όμ„Έμš”."
17
- )
18
 
19
  # Replicate λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ°Έμ‘°ν•˜λ„λ‘ 토큰 μ£Όμž…
20
  os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN
21
 
22
  # ── λͺ¨λΈ ────────────────────────────────────────────────────
23
  MODEL = (
24
- "nvidia/sana-sprint-1.6b:"
25
- "6ed1ce77cdc8db65550e76d5ab82556d0cb31ac8ab3c4947b168a0bda7b962e4"
26
-
27
  )
28
 
29
  # ── λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ (CPU) ───────────────────────────────────
30
  translator_kwargs = {"device": -1}
31
  if HF_TOKEN:
32
- translator_kwargs["token"] = HF_TOKEN # 인증 토큰 전달
33
-
34
  translator = transformers_pipeline(
35
  "translation",
36
  model="Helsinki-NLP/opus-mt-ko-en",
@@ -38,7 +35,7 @@ translator = transformers_pipeline(
38
  )
39
 
40
  def ko2en(text: str) -> str:
41
- """ν•œκΈ€ 포함 μ‹œ μ˜μ–΄ λ²ˆμ—­, κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ 원문 λ°˜ν™˜."""
42
  if re.search(r"[κ°€-힣]", text):
43
  try:
44
  return translator(text, max_length=512)[0]["translation_text"].strip()
@@ -55,7 +52,12 @@ logging.basicConfig(
55
 
56
  # ── Discord μ„€μ • ────────────────────────────────────────────
57
  intents = discord.Intents.default()
58
- intents.message_content = True # λ©”μ‹œμ§€ μ½˜ν…μΈ  읽기
 
 
 
 
 
59
 
60
  class ImageBot(discord.Client):
61
  async def on_ready(self):
@@ -68,15 +70,18 @@ class ImageBot(discord.Client):
68
  logging.warning(f"web.py μ‹€ν–‰ μ‹€νŒ¨: {e}")
69
 
70
  async def on_message(self, message: discord.Message):
71
- # 봇 μžμ‹ μ˜ λ©”μ‹œμ§€ ν˜Ήμ€ λŒ€μƒ μ•„λ‹Œ 채널 β†’ λ¬΄μ‹œ
 
 
 
72
  if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
73
  return
74
 
75
- prompt = message.content.strip()
76
- if not prompt:
77
  return
 
78
 
79
- prompt_en = ko2en(prompt) # ν•œκΈ€μ΄λ©΄ μ˜μ–΄λ‘œ λ³€ν™˜
80
  await message.channel.typing()
81
 
82
  # ── Replicate 호좜 ──────────────────────────────────
@@ -90,7 +95,7 @@ class ImageBot(discord.Client):
90
  await message.reply("⚠️ 이미지 생성 μ‹€νŒ¨!")
91
  return
92
 
93
- # ── 이미지 Discord 전솑 ─────────────────────────────
94
  files = []
95
  for idx, item in enumerate(images):
96
  try:
 
1
  # img_bot.py
2
  import discord, os, io, re, asyncio, logging, requests, replicate, subprocess
3
+ from transformers import pipeline as transformers_pipeline
4
 
5
  # ── ν™˜κ²½ λ³€μˆ˜ ────────────────────────────────────────────────
6
+ TOKEN = os.getenv("DISCORD_TOKEN") # Discord 봇 토큰
7
+ CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) # ν”„λ‘¬ν”„νŠΈ 받을 채널 ID
8
+ REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip() # Replicate 토큰(동일 λ³€μˆ˜ μ‚¬μš©)
9
+ HF_TOKEN = (os.getenv("HF_TOKEN") or "").strip() # Hugging Face 토큰
10
+ # message_content μΈν…νŠΈ μ‚¬μš© μ—¬λΆ€(1 = ON, 0 = OFF)
11
+ USE_MSG_INTENT = os.getenv("MESSAGE_CONTENT_INTENT", "1") != "0"
12
 
13
  if not TOKEN or not CHANNEL_ID:
14
  raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID ν™˜κ²½ λ³€μˆ˜λ₯Ό λͺ¨λ‘ μ§€μ •ν•˜μ„Έμš”.")
 
15
  if not REPL_TOKEN:
16
+ raise RuntimeError("OPENAI_API_KEY 에 Replicate Personal Access Token 값을 λ„£μ–΄μ£Όμ„Έμš”.")
 
 
17
 
18
  # Replicate λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ°Έμ‘°ν•˜λ„λ‘ 토큰 μ£Όμž…
19
  os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN
20
 
21
  # ── λͺ¨λΈ ────────────────────────────────────────────────────
22
  MODEL = (
23
+ "bytedance/sdxl-lightning-4step:"
24
+ "6f7a773af6fc3e8de9d5a3c00be77c17308914bf67772726aff83496ba1e3bbe"
 
25
  )
26
 
27
  # ── λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ (CPU) ───────────────────────────────────
28
  translator_kwargs = {"device": -1}
29
  if HF_TOKEN:
30
+ translator_kwargs["token"] = HF_TOKEN
 
31
  translator = transformers_pipeline(
32
  "translation",
33
  model="Helsinki-NLP/opus-mt-ko-en",
 
35
  )
36
 
37
  def ko2en(text: str) -> str:
38
+ """ν”„λ‘¬ν”„νŠΈμ— ν•œκΈ€μ΄ 있으면 μ˜μ–΄λ‘œ λ²ˆμ—­ν•˜μ—¬ λ°˜ν™˜."""
39
  if re.search(r"[κ°€-힣]", text):
40
  try:
41
  return translator(text, max_length=512)[0]["translation_text"].strip()
 
52
 
53
  # ── Discord μ„€μ • ────────────────────────────────────────────
54
  intents = discord.Intents.default()
55
+ if USE_MSG_INTENT:
56
+ intents.message_content = True
57
+ logging.info("Message Content Intent = ON "
58
+ "(Discord Developer Portal β†’ Bot β†’ Privileged Intents μ—μ„œ ν™œμ„±ν™” ν•„μš”)")
59
+ else:
60
+ logging.info("Message Content Intent = OFF (MESSAGE_CONTENT_INTENT=0)")
61
 
62
  class ImageBot(discord.Client):
63
  async def on_ready(self):
 
70
  logging.warning(f"web.py μ‹€ν–‰ μ‹€νŒ¨: {e}")
71
 
72
  async def on_message(self, message: discord.Message):
73
+ # μΈν…νŠΈ OFF μ‹œ ν”„λ‘¬ν”„νŠΈλ₯Ό 읽을 수 μ—†μœΌλ―€λ‘œ μ¦‰μ‹œ λ°˜ν™˜
74
+ if not USE_MSG_INTENT:
75
+ return
76
+ # 봇 μžμ‹ μ˜ λ©”μ‹œμ§€ λ˜λŠ” λ‹€λ₯Έ 채널 λ©”μ‹œμ§€ β†’ λ¬΄μ‹œ
77
  if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
78
  return
79
 
80
+ prompt_raw = message.content.strip()
81
+ if not prompt_raw:
82
  return
83
+ prompt_en = ko2en(prompt_raw)
84
 
 
85
  await message.channel.typing()
86
 
87
  # ── Replicate 호좜 ──────────────────────────────────
 
95
  await message.reply("⚠️ 이미지 생성 μ‹€νŒ¨!")
96
  return
97
 
98
+ # ── Discord 파일 μ—…λ‘œλ“œ ─────────────────────────────
99
  files = []
100
  for idx, item in enumerate(images):
101
  try: