Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,11 @@
|
|
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 |
-
import requests
|
7 |
-
import replicate
|
8 |
from transformers import pipeline as transformers_pipeline
|
9 |
from gradio_client import Client
|
10 |
-
from PIL import Image
|
11 |
|
12 |
# ββββββββββββββββββ νκ²½ λ³μ ββββββββββββββββββ
|
13 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
@@ -19,7 +17,7 @@ if not TOKEN or not CHANNEL_ID:
|
|
19 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
20 |
if not REPL_TOKEN:
|
21 |
raise RuntimeError("OPENAI_API_KEY μ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
22 |
-
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN
|
23 |
|
24 |
# ββββββββββββββββββ Gradio μλ² βββββββββββββββββ
|
25 |
GRADIO_URL = "http://211.233.58.201:7971"
|
@@ -34,7 +32,6 @@ translator = transformers_pipeline(
|
|
34 |
)
|
35 |
|
36 |
async def ko2en_async(text: str) -> str:
|
37 |
-
"""νκΈ ν¬ν¨ μ μμ΄ λ²μ(λΉλκΈ°)."""
|
38 |
if not re.search(r"[κ°-ν£]", text):
|
39 |
return text
|
40 |
loop = asyncio.get_running_loop()
|
@@ -98,40 +95,34 @@ class ImageBot(discord.Client):
|
|
98 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
99 |
return
|
100 |
|
101 |
-
|
102 |
-
if isinstance(result, list):
|
103 |
result = result[0]
|
104 |
|
105 |
# βββββββββββββ μ΄λ―Έμ§ λ°μ΄ν° ν보 βββββββββββββ
|
106 |
data = None
|
107 |
candidate_urls = []
|
108 |
|
109 |
-
|
|
|
|
|
|
|
110 |
if isinstance(result, dict):
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
# path νλ
|
118 |
-
path = result.get("path")
|
119 |
-
if path:
|
120 |
-
# β μ격 Gradio νμΌ κ²½λ‘
|
121 |
-
candidate_urls.append(urljoin(GRADIO_URL, f"/gradio_api/file={quote_plus(path)}"))
|
122 |
-
# β‘ λ‘컬 νμΌ κ²½λ‘
|
123 |
-
if os.path.isfile(path):
|
124 |
try:
|
125 |
-
with open(path, "rb") as f:
|
126 |
data = f.read()
|
127 |
-
except Exception
|
128 |
-
|
129 |
|
130 |
-
# str κ²°κ³Ό
|
131 |
elif isinstance(result, str):
|
132 |
s = result.strip()
|
133 |
if s.startswith("data:"):
|
134 |
-
# data URI
|
135 |
try:
|
136 |
data = base64.b64decode(re.sub(r"^data:image/[^;]+;base64,", "", s))
|
137 |
except Exception as e:
|
@@ -140,22 +131,22 @@ class ImageBot(discord.Client):
|
|
140 |
candidate_urls.append(s)
|
141 |
elif s.startswith("/"):
|
142 |
candidate_urls.append(urljoin(GRADIO_URL, s))
|
143 |
-
else:
|
144 |
-
|
145 |
if os.path.isfile(s):
|
146 |
try:
|
147 |
with open(s, "rb") as f:
|
148 |
data = f.read()
|
149 |
-
except Exception
|
150 |
-
|
151 |
|
152 |
-
# URL
|
153 |
for u in candidate_urls:
|
154 |
if data:
|
155 |
break
|
156 |
try:
|
157 |
r = requests.get(u, timeout=10)
|
158 |
-
if r.ok and r.
|
159 |
data = r.content
|
160 |
break
|
161 |
except Exception as e:
|
@@ -173,7 +164,6 @@ class ImageBot(discord.Client):
|
|
173 |
await message.reply(files=[discord.File(buf, filename="image.png")])
|
174 |
except Exception as e:
|
175 |
logging.warning(f"PNG λ³ν μ€ν¨: {e}")
|
176 |
-
# λ³ν μ€ν¨ β μλ³Έ νμ₯μ μ μ§
|
177 |
await message.reply(files=[discord.File(io.BytesIO(data), filename="image.webp")])
|
178 |
|
179 |
# ββββββββββββββββββ μ€ν ββββββββββββββββββββββββ
|
|
|
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, requests, replicate
|
|
|
|
|
6 |
from transformers import pipeline as transformers_pipeline
|
7 |
from gradio_client import Client
|
8 |
+
from PIL import Image # WEBP β PNG λ³νμ©
|
9 |
|
10 |
# ββββββββββββββββββ νκ²½ λ³μ ββββββββββββββββββ
|
11 |
TOKEN = os.getenv("DISCORD_TOKEN")
|
|
|
17 |
raise RuntimeError("DISCORD_TOKEN κ³Ό DISCORD_CHANNEL_ID νκ²½ λ³μλ₯Ό λͺ¨λ μ§μ νμΈμ.")
|
18 |
if not REPL_TOKEN:
|
19 |
raise RuntimeError("OPENAI_API_KEY μ Replicate Personal Access Token κ°μ λ£μ΄μ£ΌμΈμ.")
|
20 |
+
os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # Replicate λ―Έμ¬μ©μ΄μ§λ§ ꡬ쑰 μ μ§
|
21 |
|
22 |
# ββββββββββββββββββ Gradio μλ² βββββββββββββββββ
|
23 |
GRADIO_URL = "http://211.233.58.201:7971"
|
|
|
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 |
await message.reply("β οΈ μ΄λ―Έμ§ μμ± μ€ν¨!")
|
96 |
return
|
97 |
|
98 |
+
if isinstance(result, list): # 리μ€νΈλ©΄ 첫 νλͺ©
|
|
|
99 |
result = result[0]
|
100 |
|
101 |
# βββββββββββββ μ΄λ―Έμ§ λ°μ΄ν° ν보 βββββββββββββ
|
102 |
data = None
|
103 |
candidate_urls = []
|
104 |
|
105 |
+
def add_remote(path: str):
|
106 |
+
candidate_urls.append(urljoin(GRADIO_URL, f"/gradio_api/file={quote_plus(path)}"))
|
107 |
+
|
108 |
+
# dict κ²°κ³Ό
|
109 |
if isinstance(result, dict):
|
110 |
+
if result.get("url"):
|
111 |
+
u = result["url"]
|
112 |
+
candidate_urls.append(urljoin(GRADIO_URL, u) if u.startswith("/") else u)
|
113 |
+
if result.get("path"):
|
114 |
+
add_remote(result["path"])
|
115 |
+
if os.path.isfile(result["path"]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
try:
|
117 |
+
with open(result["path"], "rb") as f:
|
118 |
data = f.read()
|
119 |
+
except Exception:
|
120 |
+
pass
|
121 |
|
122 |
+
# str κ²°κ³Ό
|
123 |
elif isinstance(result, str):
|
124 |
s = result.strip()
|
125 |
if s.startswith("data:"):
|
|
|
126 |
try:
|
127 |
data = base64.b64decode(re.sub(r"^data:image/[^;]+;base64,", "", s))
|
128 |
except Exception as e:
|
|
|
131 |
candidate_urls.append(s)
|
132 |
elif s.startswith("/"):
|
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:
|
139 |
data = f.read()
|
140 |
+
except Exception:
|
141 |
+
pass
|
142 |
|
143 |
+
# URL λ€μ΄λ‘λ (β
ν€λ κ²μ¬ μ κ±°)
|
144 |
for u in candidate_urls:
|
145 |
if data:
|
146 |
break
|
147 |
try:
|
148 |
r = requests.get(u, timeout=10)
|
149 |
+
if r.ok and r.content:
|
150 |
data = r.content
|
151 |
break
|
152 |
except Exception as e:
|
|
|
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 |
# ββββββββββββββββββ μ€ν ββββββββββββββββββββββββ
|