Spaces:
Runtime error
Runtime error
import discord | |
from discord.ext import commands | |
from gradio_client import Client | |
import asyncio | |
import multiprocessing | |
import subprocess | |
import sys | |
# Set up Discord bot | |
intents = discord.Intents.default() | |
intents.message_content = True | |
bot = commands.Bot(command_prefix='!', intents=intents) | |
# Gradio client setup | |
gradio_client = Client("http://211.233.58.202:7960/") | |
# Discord channel ID to monitor | |
CHANNEL_ID = 1269529561914413106 | |
async def on_ready(): | |
print(f'{bot.user} has connected to Discord!') | |
try: | |
# Simple API call to test connection | |
gradio_client.predict("test", api_name="/infer_t2i") | |
print("Gradio API connection successful") | |
except Exception as e: | |
print(f"Gradio API connection failed: {str(e)}") | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
if message.channel.id != CHANNEL_ID and not isinstance(message.channel, discord.Thread): | |
return | |
try: | |
result = await asyncio.wait_for( | |
asyncio.to_thread( | |
gradio_client.predict, | |
prompt=message.content, | |
seed=123, | |
randomize_seed=False, | |
width=1024, | |
height=576, | |
guidance_scale=5, | |
num_inference_steps=30, | |
api_name="/infer_t2i" | |
), | |
timeout=60 # 60 seconds timeout | |
) | |
# ๋ก๊น ์ ์ถ๊ฐํ์ฌ ๋ฐํ๊ฐ ํ์ธ | |
print(f"Predict result: {result}") | |
# ๊ฒฐ๊ณผ๊ฐ ํํ์ด๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ฒซ ๋ฒ์งธ ์์๋ง ์ฌ์ฉ | |
if isinstance(result, tuple): | |
result = result[0] | |
await message.channel.send(file=discord.File(result)) | |
except asyncio.TimeoutError: | |
await message.channel.send("Image generation timed out. Please try again.") | |
except Exception as e: | |
error_message = f"An error occurred: {str(e)}" | |
await message.channel.send(error_message) | |
print(f"Error in on_message: {error_message}") | |
async def on_disconnect(): | |
print("Bot disconnected. Attempting to reconnect...") | |
async def on_resume(): | |
print("Bot connection resumed.") | |
def run_web(): | |
subprocess.run([sys.executable, "web.py"]) | |
def run_discord_bot(): | |
bot.run('MTIyODQyNTQxNDk0MzQ0MTEwNw.Gfd_ri.rrG_6-Sfp0FYvSIbv-zZ98dpHI-G_Fh9MFCzco') | |
if __name__ == "__main__": | |
# Start web.py in a separate process | |
web_process = multiprocessing.Process(target=run_web) | |
web_process.start() | |
# Run the Discord bot | |
run_discord_bot() | |
# Wait for the web process to finish (which it likely won't) | |
web_process.join() |