Spaces:
Runtime error
Runtime error
Delete app-backup.py
Browse files- app-backup.py +0 -88
app-backup.py
DELETED
@@ -1,88 +0,0 @@
|
|
1 |
-
# img_bot.py
|
2 |
-
import discord, os, io, asyncio, logging, requests, replicate, subprocess
|
3 |
-
|
4 |
-
# ── 환경 변수 ────────────────────────────────────────────────
|
5 |
-
TOKEN = os.getenv("DISCORD_TOKEN") # Discord 봇 토큰
|
6 |
-
CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) # 감시할 채널 ID
|
7 |
-
REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip() # Replicate 토큰(동일 변수 사용)
|
8 |
-
|
9 |
-
if not TOKEN or not CHANNEL_ID:
|
10 |
-
raise RuntimeError("DISCORD_TOKEN 과 DISCORD_CHANNEL_ID 환경 변수를 모두 지정하세요.")
|
11 |
-
|
12 |
-
if not REPL_TOKEN:
|
13 |
-
raise RuntimeError(
|
14 |
-
"OPENAI_API_KEY 환경 변수에 Replicate Personal Access Token 값을 넣어주세요."
|
15 |
-
)
|
16 |
-
|
17 |
-
# Replicate 라이브러리가 참조하도록 토큰 주입
|
18 |
-
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN
|
19 |
-
|
20 |
-
# ── 모델 ────────────────────────────────────────────────────
|
21 |
-
MODEL = (
|
22 |
-
"bytedance/sdxl-lightning-4step:"
|
23 |
-
"6f7a773af6fc3e8de9d5a3c00be77c17308914bf67772726aff83496ba1e3bbe"
|
24 |
-
)
|
25 |
-
|
26 |
-
# ── 로깅 ────────────────────────────────────────────────────
|
27 |
-
logging.basicConfig(
|
28 |
-
level=logging.INFO,
|
29 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
30 |
-
handlers=[logging.StreamHandler()]
|
31 |
-
)
|
32 |
-
|
33 |
-
# ── Discord 설정 ────────────────────────────────────────────
|
34 |
-
intents = discord.Intents.default()
|
35 |
-
intents.message_content = True # 메시지 콘텐츠 읽기
|
36 |
-
|
37 |
-
class ImageBot(discord.Client):
|
38 |
-
async def on_ready(self):
|
39 |
-
logging.info(f"Logged in as {self.user} (id={self.user.id})")
|
40 |
-
# web.py 병렬 실행
|
41 |
-
try:
|
42 |
-
subprocess.Popen(["python", "web.py"])
|
43 |
-
logging.info("web.py server has been started.")
|
44 |
-
except Exception as e:
|
45 |
-
logging.warning(f"web.py 실행 실패: {e}")
|
46 |
-
|
47 |
-
async def on_message(self, message: discord.Message):
|
48 |
-
# 봇 자신의 메시지 혹은 대상 아닌 채널 → 무시
|
49 |
-
if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
|
50 |
-
return
|
51 |
-
|
52 |
-
prompt = message.content.strip()
|
53 |
-
if not prompt:
|
54 |
-
return
|
55 |
-
|
56 |
-
await message.channel.typing()
|
57 |
-
|
58 |
-
# ── Replicate 호출 ──────────────────────────────────
|
59 |
-
def run_replicate():
|
60 |
-
return list(replicate.run(MODEL, input={"prompt": prompt}))
|
61 |
-
|
62 |
-
try:
|
63 |
-
images = await asyncio.get_running_loop().run_in_executor(
|
64 |
-
None, run_replicate
|
65 |
-
)
|
66 |
-
except Exception as e:
|
67 |
-
logging.error(f"Replicate error: {e}")
|
68 |
-
await message.reply("⚠️ 이미지 생성 실패!")
|
69 |
-
return
|
70 |
-
|
71 |
-
# ── 이미지 Discord 전송 ─────────────────────────────
|
72 |
-
files = []
|
73 |
-
for idx, item in enumerate(images):
|
74 |
-
try:
|
75 |
-
data = item.read() if hasattr(item, "read") else requests.get(item).content
|
76 |
-
files.append(discord.File(io.BytesIO(data), filename=f"img_{idx}.png"))
|
77 |
-
except Exception as e:
|
78 |
-
logging.warning(f"[IMG {idx}] 처리 실패: {e}")
|
79 |
-
|
80 |
-
await message.reply(
|
81 |
-
files=files if files else None,
|
82 |
-
content=None if files else "⚠️ 이미지를 전송할 수 없습니다."
|
83 |
-
)
|
84 |
-
|
85 |
-
# ── 실행 ────────────────────────────────────────────────────
|
86 |
-
if __name__ == "__main__":
|
87 |
-
replicate.Client(api_token=REPL_TOKEN) # 인증
|
88 |
-
ImageBot(intents=intents).run(TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|