Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# img_bot.py
|
2 |
import discord, os, io, re, random, asyncio, logging, requests, replicate, subprocess
|
3 |
from transformers import pipeline as transformers_pipeline
|
4 |
-
from gradio_client import Client, handle_file
|
5 |
|
6 |
# ── 환경 변수 ────────────────────────────────────────────────
|
7 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
@@ -14,7 +14,7 @@ if not TOKEN or not 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
|
18 |
|
19 |
# ── Gradio 서버 ─────────────────────────────────────────────
|
20 |
GRADIO_URL = "http://211.233.58.201:7896"
|
@@ -45,7 +45,8 @@ async def ko2en_async(text: str) -> str:
|
|
45 |
|
46 |
# ── 로깅 ────────────────────────────────────────────────────
|
47 |
logging.basicConfig(level=logging.INFO,
|
48 |
-
format="%(asctime)s [%(levelname)s] %(message)s"
|
|
|
49 |
|
50 |
# ── Discord 인텐트 ──────────────────────────────────────────
|
51 |
intents = discord.Intents.default()
|
@@ -82,11 +83,11 @@ class ImageBot(discord.Client):
|
|
82 |
inference_steps=30,
|
83 |
seed=random.randint(0, 2**32 - 1),
|
84 |
do_img2img=False,
|
85 |
-
init_image=handle_file(DUMMY_IMG),
|
86 |
image2image_strength=0.8,
|
87 |
resize_img=True,
|
88 |
api_name=GRADIO_API
|
89 |
-
)[0]
|
90 |
|
91 |
try:
|
92 |
img_info = await asyncio.get_running_loop().run_in_executor(None, generate_image)
|
@@ -98,12 +99,23 @@ class ImageBot(discord.Client):
|
|
98 |
# ── Discord 전송 ──────────────────────────────────
|
99 |
files = []
|
100 |
try:
|
101 |
-
if img_info
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
files.append(discord.File(io.BytesIO(data), filename="generated.webp"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
except Exception as e:
|
108 |
logging.warning(f"이미지 처리 실패: {e}")
|
109 |
|
@@ -114,5 +126,5 @@ class ImageBot(discord.Client):
|
|
114 |
|
115 |
# ── 실행 ────────────────────────────────────────────────────
|
116 |
if __name__ == "__main__":
|
117 |
-
replicate.Client(api_token=REPL_TOKEN) # 구조 유지(
|
118 |
ImageBot(intents=intents).run(TOKEN)
|
|
|
1 |
# img_bot.py
|
2 |
import discord, os, io, re, random, asyncio, logging, requests, replicate, subprocess
|
3 |
from transformers import pipeline as transformers_pipeline
|
4 |
+
from gradio_client import Client, handle_file
|
5 |
|
6 |
# ── 환경 변수 ────────────────────────────────────────────────
|
7 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
|
|
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 |
# ── Gradio 서버 ─────────────────────────────────────────────
|
20 |
GRADIO_URL = "http://211.233.58.201:7896"
|
|
|
45 |
|
46 |
# ── 로깅 ────────────────────────────────────────────────────
|
47 |
logging.basicConfig(level=logging.INFO,
|
48 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
49 |
+
handlers=[logging.StreamHandler()])
|
50 |
|
51 |
# ── Discord 인텐트 ──────────────────────────────────────────
|
52 |
intents = discord.Intents.default()
|
|
|
83 |
inference_steps=30,
|
84 |
seed=random.randint(0, 2**32 - 1),
|
85 |
do_img2img=False,
|
86 |
+
init_image=handle_file(DUMMY_IMG), # path·mime 포함 dict
|
87 |
image2image_strength=0.8,
|
88 |
resize_img=True,
|
89 |
api_name=GRADIO_API
|
90 |
+
)[0] # str or dict
|
91 |
|
92 |
try:
|
93 |
img_info = await asyncio.get_running_loop().run_in_executor(None, generate_image)
|
|
|
99 |
# ── Discord 전송 ──────────────────────────────────
|
100 |
files = []
|
101 |
try:
|
102 |
+
if isinstance(img_info, str): # 경우 1: 문자열 경로/URL
|
103 |
+
if img_info.startswith("http"):
|
104 |
+
data = requests.get(img_info).content
|
105 |
+
else:
|
106 |
+
with open(img_info, "rb") as f:
|
107 |
+
data = f.read()
|
108 |
files.append(discord.File(io.BytesIO(data), filename="generated.webp"))
|
109 |
+
elif isinstance(img_info, dict): # 경우 2: dict(path|url)
|
110 |
+
if img_info.get("path"):
|
111 |
+
with open(img_info["path"], "rb") as f:
|
112 |
+
data = f.read()
|
113 |
+
elif img_info.get("url"):
|
114 |
+
data = requests.get(img_info["url"]).content
|
115 |
+
else:
|
116 |
+
data = None
|
117 |
+
if data:
|
118 |
+
files.append(discord.File(io.BytesIO(data), filename="generated.webp"))
|
119 |
except Exception as e:
|
120 |
logging.warning(f"이미지 처리 실패: {e}")
|
121 |
|
|
|
126 |
|
127 |
# ── 실행 ────────────────────────────────────────────────────
|
128 |
if __name__ == "__main__":
|
129 |
+
replicate.Client(api_token=REPL_TOKEN) # 구조 유지(사용 안 함)
|
130 |
ImageBot(intents=intents).run(TOKEN)
|