Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,30 +2,36 @@ import discord
|
|
2 |
from discord.ext import commands
|
3 |
from gradio_client import Client
|
4 |
import asyncio
|
5 |
-
import
|
|
|
6 |
import subprocess
|
7 |
import sys
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
10 |
intents = discord.Intents.default()
|
11 |
intents.message_content = True
|
|
|
|
|
12 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
13 |
|
14 |
-
# Gradio
|
15 |
gradio_client = Client("http://211.233.58.202:7960/")
|
16 |
|
17 |
-
# Discord
|
18 |
CHANNEL_ID = 1269529561914413106
|
19 |
|
20 |
@bot.event
|
21 |
async def on_ready():
|
22 |
-
|
23 |
try:
|
24 |
-
#
|
25 |
gradio_client.predict("test", api_name="/infer_t2i")
|
26 |
-
|
27 |
except Exception as e:
|
28 |
-
|
29 |
|
30 |
@bot.event
|
31 |
async def on_message(message):
|
@@ -51,40 +57,44 @@ async def on_message(message):
|
|
51 |
timeout=60 # 60 seconds timeout
|
52 |
)
|
53 |
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
await message.channel.send(file=discord.File(file_path))
|
59 |
except asyncio.TimeoutError:
|
60 |
await message.channel.send("Image generation timed out. Please try again.")
|
61 |
except Exception as e:
|
62 |
error_message = f"An error occurred: {str(e)}"
|
63 |
await message.channel.send(error_message)
|
64 |
-
|
65 |
-
|
66 |
|
67 |
@bot.event
|
68 |
async def on_disconnect():
|
69 |
-
|
70 |
|
71 |
@bot.event
|
72 |
async def on_resume():
|
73 |
-
|
74 |
|
75 |
def run_web():
|
76 |
subprocess.run([sys.executable, "web.py"])
|
77 |
|
78 |
def run_discord_bot():
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
#
|
83 |
-
web_process =
|
84 |
-
|
85 |
|
86 |
-
#
|
87 |
run_discord_bot()
|
88 |
|
89 |
-
#
|
90 |
-
web_process.
|
|
|
2 |
from discord.ext import commands
|
3 |
from gradio_client import Client
|
4 |
import asyncio
|
5 |
+
import os
|
6 |
+
import logging
|
7 |
import subprocess
|
8 |
import sys
|
9 |
|
10 |
+
# ๋ก๊น
์ค์
|
11 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
12 |
+
|
13 |
+
# ์ธํ
ํธ ์ค์
|
14 |
intents = discord.Intents.default()
|
15 |
intents.message_content = True
|
16 |
+
|
17 |
+
# Discord Bot ์ค์
|
18 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
19 |
|
20 |
+
# Gradio API ํด๋ผ์ด์ธํธ ์ค์
|
21 |
gradio_client = Client("http://211.233.58.202:7960/")
|
22 |
|
23 |
+
# Discord ์ฑ๋ ID
|
24 |
CHANNEL_ID = 1269529561914413106
|
25 |
|
26 |
@bot.event
|
27 |
async def on_ready():
|
28 |
+
logging.info(f'{bot.user.name} has connected to Discord!')
|
29 |
try:
|
30 |
+
# Gradio ์ฐ๊ฒฐ ํ
์คํธ
|
31 |
gradio_client.predict("test", api_name="/infer_t2i")
|
32 |
+
logging.info("Gradio API connection successful")
|
33 |
except Exception as e:
|
34 |
+
logging.error(f"Gradio API connection failed: {str(e)}")
|
35 |
|
36 |
@bot.event
|
37 |
async def on_message(message):
|
|
|
57 |
timeout=60 # 60 seconds timeout
|
58 |
)
|
59 |
|
60 |
+
logging.debug(f"Predict result: {result}")
|
61 |
|
62 |
+
if isinstance(result, tuple):
|
63 |
+
file_path = result[0]
|
64 |
await message.channel.send(file=discord.File(file_path))
|
65 |
except asyncio.TimeoutError:
|
66 |
await message.channel.send("Image generation timed out. Please try again.")
|
67 |
except Exception as e:
|
68 |
error_message = f"An error occurred: {str(e)}"
|
69 |
await message.channel.send(error_message)
|
70 |
+
logging.error(f"Error in on_message: {error_message}")
|
|
|
71 |
|
72 |
@bot.event
|
73 |
async def on_disconnect():
|
74 |
+
logging.info("Bot disconnected. Attempting to reconnect...")
|
75 |
|
76 |
@bot.event
|
77 |
async def on_resume():
|
78 |
+
logging.info("Bot connection resumed.")
|
79 |
|
80 |
def run_web():
|
81 |
subprocess.run([sys.executable, "web.py"])
|
82 |
|
83 |
def run_discord_bot():
|
84 |
+
# ํ๊ฒฝ ๋ณ์์์ ๋์ค์ฝ๋ ํ ํฐ ์ฝ๊ธฐ
|
85 |
+
discord_token = os.getenv('DISCORD_TOKEN')
|
86 |
+
if discord_token:
|
87 |
+
bot.run(discord_token)
|
88 |
+
else:
|
89 |
+
logging.error("Discord token is not set. Please check your environment variables.")
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
+
# ์น ์๋ฒ๋ฅผ ๋ณ๋์ ํ๋ก์ธ์ค๋ก ์์
|
93 |
+
web_process = subprocess.Popen([sys.executable, "web.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
94 |
+
logging.info("Web server process started.")
|
95 |
|
96 |
+
# Discord ๋ด ์คํ
|
97 |
run_discord_bot()
|
98 |
|
99 |
+
# ์น ํ๋ก์ธ์ค ์ข
๋ฃ ๋๊ธฐ
|
100 |
+
web_process.wait()
|