kai-flx / app.py
seawolf2357's picture
Update app.py
3d78f38 verified
raw
history blame
2.62 kB
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
@bot.event
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)}")
@bot.event
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}")
# ํŒŒ์ผ ๊ฒฝ๋กœ๋งŒ ์ถ”์ถœํ•˜์—ฌ ์‚ฌ์šฉ
file_path = result[0] # ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ํŒŒ์ผ ๊ฒฝ๋กœ
await message.channel.send(file=discord.File(file_path))
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}")
@bot.event
async def on_disconnect():
print("Bot disconnected. Attempting to reconnect...")
@bot.event
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()