# img_bot.py import discord, os, io, re, random, asyncio, logging, requests, replicate, subprocess from transformers import pipeline as transformers_pipeline from gradio_client import Client, handle_file # ── 환경 변수 ──────────────────────────────────────────────── TOKEN = os.getenv("DISCORD_TOKEN") CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip() HF_TOKEN = (os.getenv("HF_TOKEN") or "").strip() if not TOKEN or not CHANNEL_ID: raise RuntimeError("DISCORD_TOKEN 과 DISCORD_CHANNEL_ID 환경 변수를 모두 지정하세요.") if not REPL_TOKEN: raise RuntimeError("OPENAI_API_KEY 에 Replicate Personal Access Token 값을 넣어주세요.") os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # 구조 유지 (Replicate 미사용) # ── Gradio 서버 ───────────────────────────────────────────── GRADIO_URL = "http://211.233.58.201:7896" GRADIO_API = "/generate_image" DUMMY_IMG = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png" # ── 번역 파이프라인 (CPU) ─────────────────────────────────── translator = transformers_pipeline( "translation", model="Helsinki-NLP/opus-mt-ko-en", device=-1, **({"token": HF_TOKEN} if HF_TOKEN else {}) ) async def ko2en_async(text: str) -> str: """한글 포함 시 별도 쓰레드에서 영어 번역.""" if not re.search(r"[가-힣]", text): return text loop = asyncio.get_running_loop() try: return await loop.run_in_executor( None, lambda: translator(text, max_length=256, num_beams=1)[0]["translation_text"].strip() ) except Exception as e: logging.warning(f"번역 실패, 원문 사용: {e}") return text # ── 로깅 ──────────────────────────────────────────────────── logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler()]) # ── Discord 인텐트 ────────────────────────────────────────── intents = discord.Intents.default() intents.message_content = True # Portal에서도 Message-Content Intent ON class ImageBot(discord.Client): async def on_ready(self): logging.info(f"Logged in as {self.user} (id={self.user.id})") try: subprocess.Popen(["python", "web.py"]) logging.info("web.py server has been started.") except Exception as e: logging.warning(f"web.py 실행 실패: {e}") async def on_message(self, message: discord.Message): if message.author.id == self.user.id or message.channel.id != CHANNEL_ID: return prompt_raw = message.content.strip() if not prompt_raw: return prompt_en = await ko2en_async(prompt_raw) await message.channel.typing() # ── Gradio 호출 ──────────────────────────────────── def generate_image(): client = Client(GRADIO_URL) return client.predict( prompt=prompt_en, width=768, height=768, guidance=3.5, inference_steps=30, seed=random.randint(0, 2**32 - 1), do_img2img=False, init_image=handle_file(DUMMY_IMG), # path·mime 포함 dict image2image_strength=0.8, resize_img=True, api_name=GRADIO_API )[0] # str or dict try: img_info = await asyncio.get_running_loop().run_in_executor(None, generate_image) except Exception as e: logging.error(f"Gradio API error: {e}") await message.reply("⚠️ 이미지 생성 실패!") return # ── Discord 전송 ────────────────────────────────── files = [] try: if isinstance(img_info, str): # 경우 1: 문자열 경로/URL if img_info.startswith("http"): data = requests.get(img_info).content else: with open(img_info, "rb") as f: data = f.read() files.append(discord.File(io.BytesIO(data), filename="generated.webp")) elif isinstance(img_info, dict): # 경우 2: dict(path|url) if img_info.get("path"): with open(img_info["path"], "rb") as f: data = f.read() elif img_info.get("url"): data = requests.get(img_info["url"]).content else: data = None if data: files.append(discord.File(io.BytesIO(data), filename="generated.webp")) except Exception as e: logging.warning(f"이미지 처리 실패: {e}") await message.reply( files=files if files else None, content=None if files else "⚠️ 이미지를 전송할 수 없습니다." ) # ── 실행 ──────────────────────────────────────────────────── if __name__ == "__main__": replicate.Client(api_token=REPL_TOKEN) # 구조 유지(사용 안 함) ImageBot(intents=intents).run(TOKEN)