Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,20 @@
|
|
1 |
-
# img_bot.py
|
|
|
|
|
2 |
import os, io, re, random, asyncio, logging, subprocess, base64
|
3 |
from urllib.parse import urljoin, quote_plus
|
4 |
|
5 |
-
import discord
|
|
|
|
|
6 |
from transformers import pipeline as transformers_pipeline
|
7 |
from gradio_client import Client
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# ββββββββββββββββββ νκ²½ λ³μ ββββββββββββββββββ
|
11 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
@@ -17,7 +26,8 @@ if not TOKEN or not CHANNEL_ID:
|
|
17 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
18 |
if not REPL_TOKEN:
|
19 |
raise RuntimeError("OPENAI_API_KEY μ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
20 |
-
|
|
|
21 |
|
22 |
# ββββββββββββββββββ Gradio μλ² βββββββββββββββββ
|
23 |
GRADIO_URL = "http://211.233.58.201:7971"
|
@@ -32,6 +42,7 @@ translator = transformers_pipeline(
|
|
32 |
)
|
33 |
|
34 |
async def ko2en_async(text: str) -> str:
|
|
|
35 |
if not re.search(r"[κ°-ν£]", text):
|
36 |
return text
|
37 |
loop = asyncio.get_running_loop()
|
@@ -95,26 +106,34 @@ class ImageBot(discord.Client):
|
|
95 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
96 |
return
|
97 |
|
98 |
-
|
|
|
99 |
result = result[0]
|
100 |
|
101 |
# βββββββββββββ μ΄λ―Έμ§ λ°μ΄ν° ν보 βββββββββββββ
|
102 |
data = None
|
103 |
-
|
|
|
104 |
|
105 |
-
def add_remote(
|
106 |
-
|
|
|
|
|
107 |
|
108 |
# dict κ²°κ³Ό
|
109 |
if isinstance(result, dict):
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
116 |
try:
|
117 |
-
with open(
|
118 |
data = f.read()
|
119 |
except Exception:
|
120 |
pass
|
@@ -129,10 +148,11 @@ class ImageBot(discord.Client):
|
|
129 |
logging.warning(f"base64 λμ½λ© μ€ν¨: {e}")
|
130 |
elif s.startswith("http"):
|
131 |
candidate_urls.append(s)
|
132 |
-
|
133 |
-
candidate_urls.append(urljoin(GRADIO_URL, s))
|
134 |
else:
|
135 |
-
add_remote(s)
|
|
|
|
|
136 |
if os.path.isfile(s):
|
137 |
try:
|
138 |
with open(s, "rb") as f:
|
@@ -140,7 +160,7 @@ class ImageBot(discord.Client):
|
|
140 |
except Exception:
|
141 |
pass
|
142 |
|
143 |
-
# URL λ€μ΄λ‘λ (
|
144 |
for u in candidate_urls:
|
145 |
if data:
|
146 |
break
|
@@ -152,21 +172,28 @@ class ImageBot(discord.Client):
|
|
152 |
except Exception as e:
|
153 |
logging.warning(f"URL λ€μ΄λ‘λ μ€ν¨({u}): {e}")
|
154 |
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
return
|
158 |
|
159 |
-
# βββββββββββββ
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
await message.reply(files=[discord.File(buf, filename="image.png")])
|
165 |
-
except Exception as e:
|
166 |
-
logging.warning(f"PNG λ³ν μ€ν¨: {e}")
|
167 |
-
await message.reply(files=[discord.File(io.BytesIO(data), filename="image.webp")])
|
168 |
|
169 |
# ββββββββββββββββββ μ€ν ββββββββββββββββββββββββ
|
170 |
if __name__ == "__main__":
|
171 |
-
replicate.Client(api_token=REPL_TOKEN)
|
172 |
ImageBot(intents=intents).run(TOKEN)
|
|
|
1 |
+
# img_bot.py
|
2 |
+
# βμ΄λ―Έμ§ μ²¨λΆ β μ€ν¨ μ URL μΆλ ₯β μκ²°ν
|
3 |
+
|
4 |
import os, io, re, random, asyncio, logging, subprocess, base64
|
5 |
from urllib.parse import urljoin, quote_plus
|
6 |
|
7 |
+
import discord
|
8 |
+
import requests
|
9 |
+
import replicate
|
10 |
from transformers import pipeline as transformers_pipeline
|
11 |
from gradio_client import Client
|
12 |
+
|
13 |
+
try:
|
14 |
+
from PIL import Image # WEBP β PNG λ³ν
|
15 |
+
PIL_OK = True
|
16 |
+
except Exception:
|
17 |
+
PIL_OK = False # Pillow λ―Έμ€μΉ μ λ³ν μλ΅
|
18 |
|
19 |
# ββββββββββββββββββ νκ²½ λ³μ ββββββββββββββββββ
|
20 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
|
|
26 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
27 |
if not REPL_TOKEN:
|
28 |
raise RuntimeError("OPENAI_API_KEY μ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
29 |
+
|
30 |
+
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # (Replicate λ―Έμ¬μ©)
|
31 |
|
32 |
# ββββββββββββββββββ Gradio μλ² βββββββββββββββββ
|
33 |
GRADIO_URL = "http://211.233.58.201:7971"
|
|
|
42 |
)
|
43 |
|
44 |
async def ko2en_async(text: str) -> str:
|
45 |
+
"""νκΈ ν¬ν¨ μ μμ΄ λ²μ."""
|
46 |
if not re.search(r"[κ°-ν£]", text):
|
47 |
return text
|
48 |
loop = asyncio.get_running_loop()
|
|
|
106 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
107 |
return
|
108 |
|
109 |
+
# 리μ€νΈ κ²°κ³Ό β 첫 μμ
|
110 |
+
if isinstance(result, list):
|
111 |
result = result[0]
|
112 |
|
113 |
# βββββββββββββ μ΄λ―Έμ§ λ°μ΄ν° ν보 βββββββββββββ
|
114 |
data = None
|
115 |
+
remote_path = None # μ€ν¨ μ μ¬μ©μμκ² λ³΄μ¬μ€ URL
|
116 |
+
candidate_urls: list[str] = []
|
117 |
|
118 |
+
def add_remote(p: str):
|
119 |
+
url = urljoin(GRADIO_URL, f"/gradio_api/file={quote_plus(p)}")
|
120 |
+
candidate_urls.append(url)
|
121 |
+
return url
|
122 |
|
123 |
# dict κ²°κ³Ό
|
124 |
if isinstance(result, dict):
|
125 |
+
u = result.get("url")
|
126 |
+
if u:
|
127 |
+
if u.startswith("/"):
|
128 |
+
u = urljoin(GRADIO_URL, u)
|
129 |
+
candidate_urls.append(u)
|
130 |
+
remote_path = u
|
131 |
+
p = result.get("path")
|
132 |
+
if p:
|
133 |
+
remote_path = add_remote(p)
|
134 |
+
if os.path.isfile(p):
|
135 |
try:
|
136 |
+
with open(p, "rb") as f:
|
137 |
data = f.read()
|
138 |
except Exception:
|
139 |
pass
|
|
|
148 |
logging.warning(f"base64 λμ½λ© μ€ν¨: {e}")
|
149 |
elif s.startswith("http"):
|
150 |
candidate_urls.append(s)
|
151 |
+
remote_path = s
|
|
|
152 |
else:
|
153 |
+
remote_path = add_remote(s)
|
154 |
+
if s.startswith("/"):
|
155 |
+
candidate_urls.append(urljoin(GRADIO_URL, s))
|
156 |
if os.path.isfile(s):
|
157 |
try:
|
158 |
with open(s, "rb") as f:
|
|
|
160 |
except Exception:
|
161 |
pass
|
162 |
|
163 |
+
# URL λ€μ΄λ‘λ (ν€λ 무μ, 200 OK λ§ νμΈ)
|
164 |
for u in candidate_urls:
|
165 |
if data:
|
166 |
break
|
|
|
172 |
except Exception as e:
|
173 |
logging.warning(f"URL λ€μ΄λ‘λ μ€ν¨({u}): {e}")
|
174 |
|
175 |
+
# βββββββββββββ Discord μ μ‘ βββββββββββββ
|
176 |
+
if data:
|
177 |
+
if PIL_OK: # WEBP β PNG λ³ν
|
178 |
+
try:
|
179 |
+
buf = io.BytesIO()
|
180 |
+
Image.open(io.BytesIO(data)).convert("RGB").save(buf, format="PNG")
|
181 |
+
buf.seek(0)
|
182 |
+
await message.reply(files=[discord.File(buf, filename="image.png")])
|
183 |
+
return
|
184 |
+
except Exception as e:
|
185 |
+
logging.warning(f"PNG λ³ν μ€ν¨: {e}")
|
186 |
+
# λ³ν μ€ν¨ λλ Pillow μμ β μλ³Έ μ μ‘
|
187 |
+
await message.reply(files=[discord.File(io.BytesIO(data), filename="image.webp")])
|
188 |
return
|
189 |
|
190 |
+
# βββββββββββββ μ€ν¨ μ URL μΆλ ₯ βββββββββββββ
|
191 |
+
if remote_path:
|
192 |
+
await message.reply(content=remote_path)
|
193 |
+
else:
|
194 |
+
await message.reply("β οΈ μ΄λ―Έμ§λ₯Ό μ μ‘ν μ μμ΅λλ€.")
|
|
|
|
|
|
|
|
|
195 |
|
196 |
# ββββββββββββββββββ μ€ν ββββββββββββββββββββββββ
|
197 |
if __name__ == "__main__":
|
198 |
+
replicate.Client(api_token=REPL_TOKEN) # ꡬ쑰 μ μ§
|
199 |
ImageBot(intents=intents).run(TOKEN)
|