Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,16 +5,16 @@ from transformers import pipeline as transformers_pipeline # λ²μ νμ΄ν
|
|
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 |
|
11 |
if not TOKEN or not CHANNEL_ID:
|
12 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
13 |
if not REPL_TOKEN:
|
14 |
raise RuntimeError("OPENAI_API_KEY νκ²½ λ³μμ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
15 |
|
16 |
-
# Replicate
|
17 |
-
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN
|
18 |
|
19 |
# ββ μ΄λ―Έμ§ μμ± λͺ¨λΈ (Flux Schnell) βββββββββββββββββββββββββ
|
20 |
MODEL = "black-forest-labs/flux-schnell"
|
@@ -23,15 +23,12 @@ MODEL = "black-forest-labs/flux-schnell"
|
|
23 |
translator_kwargs = {"device": -1}
|
24 |
if HF_TOKEN:
|
25 |
translator_kwargs["token"] = HF_TOKEN
|
26 |
-
|
27 |
translator = transformers_pipeline(
|
28 |
-
"translation",
|
29 |
-
model="Helsinki-NLP/opus-mt-ko-en",
|
30 |
-
**translator_kwargs
|
31 |
)
|
32 |
|
33 |
def ko2en(text: str) -> str:
|
34 |
-
"""ν둬ννΈμ
|
35 |
if re.search(r"[κ°-ν£]", text):
|
36 |
try:
|
37 |
return translator(text, max_length=512)[0]["translation_text"].strip()
|
@@ -40,15 +37,17 @@ def ko2en(text: str) -> str:
|
|
40 |
return text
|
41 |
|
42 |
# ββ λ‘κΉ
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
43 |
-
logging.basicConfig(
|
44 |
-
|
45 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
46 |
-
handlers=[logging.StreamHandler()]
|
47 |
-
)
|
48 |
|
49 |
-
# ββ Discord μ€μ
|
50 |
intents = discord.Intents.default()
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
class ImageBot(discord.Client):
|
54 |
async def on_ready(self):
|
@@ -61,7 +60,9 @@ class ImageBot(discord.Client):
|
|
61 |
logging.warning(f"web.py μ€ν μ€ν¨: {e}")
|
62 |
|
63 |
async def on_message(self, message: discord.Message):
|
64 |
-
#
|
|
|
|
|
65 |
if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
|
66 |
return
|
67 |
|
@@ -69,17 +70,12 @@ class ImageBot(discord.Client):
|
|
69 |
if not prompt_raw:
|
70 |
return
|
71 |
|
72 |
-
prompt_en = ko2en(prompt_raw)
|
73 |
await message.channel.typing()
|
74 |
|
75 |
-
# ββ Replicate
|
76 |
def run_replicate():
|
77 |
-
return list(
|
78 |
-
replicate.run(
|
79 |
-
MODEL,
|
80 |
-
input={"prompt": prompt_en}
|
81 |
-
)
|
82 |
-
)
|
83 |
|
84 |
try:
|
85 |
images = await asyncio.get_running_loop().run_in_executor(None, run_replicate)
|
@@ -88,7 +84,7 @@ class ImageBot(discord.Client):
|
|
88 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
89 |
return
|
90 |
|
91 |
-
#
|
92 |
files = []
|
93 |
for idx, item in enumerate(images):
|
94 |
try:
|
@@ -97,12 +93,10 @@ class ImageBot(discord.Client):
|
|
97 |
except Exception as e:
|
98 |
logging.warning(f"[IMG {idx}] μ²λ¦¬ μ€ν¨: {e}")
|
99 |
|
100 |
-
await message.reply(
|
101 |
-
|
102 |
-
content=None if files else "β οΈ μ΄λ―Έμ§λ₯Ό μ μ‘ν μ μμ΅λλ€."
|
103 |
-
)
|
104 |
|
105 |
# ββ μ€ν ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
106 |
if __name__ == "__main__":
|
107 |
-
replicate.Client(api_token=REPL_TOKEN)
|
108 |
ImageBot(intents=intents).run(TOKEN)
|
|
|
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 |
+
USE_MSG_INTENT = os.getenv("MESSAGE_CONTENT_INTENT", "1") != "0" # 1=ON(κΈ°λ³Έ), 0=OFF
|
11 |
|
12 |
if not TOKEN or not CHANNEL_ID:
|
13 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
14 |
if not REPL_TOKEN:
|
15 |
raise RuntimeError("OPENAI_API_KEY νκ²½ λ³μμ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
16 |
|
17 |
+
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # Replicate κ° μ°Έμ‘°ν ν ν°
|
|
|
18 |
|
19 |
# ββ μ΄λ―Έμ§ μμ± λͺ¨λΈ (Flux Schnell) βββββββββββββββββββββββββ
|
20 |
MODEL = "black-forest-labs/flux-schnell"
|
|
|
23 |
translator_kwargs = {"device": -1}
|
24 |
if HF_TOKEN:
|
25 |
translator_kwargs["token"] = HF_TOKEN
|
|
|
26 |
translator = transformers_pipeline(
|
27 |
+
"translation", model="Helsinki-NLP/opus-mt-ko-en", **translator_kwargs
|
|
|
|
|
28 |
)
|
29 |
|
30 |
def ko2en(text: str) -> str:
|
31 |
+
"""ν둬ννΈμ νκΈ ν¬ν¨ μ μμ΄ λ²μ."""
|
32 |
if re.search(r"[κ°-ν£]", text):
|
33 |
try:
|
34 |
return translator(text, max_length=512)[0]["translation_text"].strip()
|
|
|
37 |
return text
|
38 |
|
39 |
# ββ λ‘κΉ
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
40 |
+
logging.basicConfig(level=logging.INFO,
|
41 |
+
format="%(asctime)s [%(levelname)s] %(message)s")
|
|
|
|
|
|
|
42 |
|
43 |
+
# ββ Discord μΈν
νΈ μ€μ ββββββββββββββββββββββββββββββββββββ
|
44 |
intents = discord.Intents.default()
|
45 |
+
if USE_MSG_INTENT:
|
46 |
+
intents.message_content = True
|
47 |
+
logging.info("Message Content Intent = ON "
|
48 |
+
"(Developer Portal β Bot β Privileged Intents μμ ON νμ)")
|
49 |
+
else:
|
50 |
+
logging.info("Message Content Intent = OFF (MESSAGE_CONTENT_INTENT=0)")
|
51 |
|
52 |
class ImageBot(discord.Client):
|
53 |
async def on_ready(self):
|
|
|
60 |
logging.warning(f"web.py μ€ν μ€ν¨: {e}")
|
61 |
|
62 |
async def on_message(self, message: discord.Message):
|
63 |
+
# λ©μμ§ λ΄μ© μ½μ κΆνμ΄ μμΌλ©΄ λ°λ‘ λ°ν
|
64 |
+
if not USE_MSG_INTENT:
|
65 |
+
return
|
66 |
if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
|
67 |
return
|
68 |
|
|
|
70 |
if not prompt_raw:
|
71 |
return
|
72 |
|
73 |
+
prompt_en = ko2en(prompt_raw)
|
74 |
await message.channel.typing()
|
75 |
|
76 |
+
# ββ Replicate μμ² βββββββββββββββββββββββββββββββββ
|
77 |
def run_replicate():
|
78 |
+
return list(replicate.run(MODEL, input={"prompt": prompt_en}))
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
try:
|
81 |
images = await asyncio.get_running_loop().run_in_executor(None, run_replicate)
|
|
|
84 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
85 |
return
|
86 |
|
87 |
+
# οΏ½οΏ½β Discord μ μ‘ ββββββββββββββββββββββββββββββββββ
|
88 |
files = []
|
89 |
for idx, item in enumerate(images):
|
90 |
try:
|
|
|
93 |
except Exception as e:
|
94 |
logging.warning(f"[IMG {idx}] μ²λ¦¬ μ€ν¨: {e}")
|
95 |
|
96 |
+
await message.reply(files=files if files else None,
|
97 |
+
content=None if files else "β οΈ μ΄λ―Έμ§λ₯Ό μ μ‘ν μ μμ΅λλ€.")
|
|
|
|
|
98 |
|
99 |
# ββ μ€ν ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
100 |
if __name__ == "__main__":
|
101 |
+
replicate.Client(api_token=REPL_TOKEN) # Replicate μΈμ¦
|
102 |
ImageBot(intents=intents).run(TOKEN)
|