File size: 2,623 Bytes
78efe79
99580d4
 
204d278
99580d4
d14b0d3
99580d4
295a949
99580d4
440418c
22dee1c
99580d4
 
 
 
7262aa5
99580d4
 
021392e
99580d4
 
 
4f3c619
 
 
 
 
 
60f5cc0
99580d4
 
 
 
539a18a
99580d4
 
8af719d
99580d4
204d278
 
 
 
 
 
 
 
 
 
 
 
 
99580d4
ac850a7
 
 
3d78f38
 
 
204d278
ac850a7
99580d4
4f3c619
 
 
204d278
ac850a7
204d278
 
 
 
 
 
 
13bad59
99580d4
 
d14b0d3
99580d4
 
021392e
34428f1
99580d4
 
 
60f5cc0
99580d4
 
903369f
99580d4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()