File size: 4,280 Bytes
5546580
 
7fa9e07
f42e7d1
fe6d40e
7fa9e07
 
 
 
 
 
11ad260
 
 
7fa9e07
fe6d40e
956c50d
3f98253
448abf5
 
 
3d12a7c
e0461c3
11ad260
b56f69c
 
c378b04
9650ea7
11ad260
 
d7fdc0e
11ad260
9650ea7
fad843f
a697e01
06ddfd4
 
3c5ffd1
06ddfd4
 
2afeadf
69dff14
f42e7d1
41e66b2
 
 
 
3c5ffd1
 
 
69dff14
3c5ffd1
f42e7d1
3c5ffd1
 
 
 
 
 
 
 
41e66b2
3c5ffd1
 
 
 
 
 
2afeadf
3189729
9b3563d
a661bc3
3189729
 
3c5ffd1
 
69dff14
3c5ffd1
 
3d12a7c
ab54bd2
 
834b337
ab54bd2
 
 
 
3c5ffd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf54bc6
3c5ffd1
 
 
 
41e66b2
8d38d87
448abf5
 
 
 
f42e7d1
448abf5
f42e7d1
 
 
 
448abf5
5546580
11ad260
5546580
dbb572b
5546580
dbb572b
 
740e062
dbb572b
 
8d38d87
 
 
3c5ffd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import discord
import os
import threading
from threading import active_count
import gradio as gr 
import requests
import json
import random
import time
import re
from discord import Embed, Color
from discord.ext import commands
from gradio_client import Client
from PIL import Image
import asyncio
import shutil # for doing image movement magic 
import concurrent.futures
# reboot
import multiprocessing


semaphore = asyncio.Semaphore(4)

DFIF_TOKEN = os.getenv('HF_TOKEN')
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)

jojogan = Client("akhaliq/JoJoGAN", DFIF_TOKEN)

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)


@bot.command()
async def active(ctx):
    # get the number of active threads
    activethreadcount = active_count()
    await ctx.message.reply(f'active threads: {activethreadcount}')
    
@bot.command()
async def jojo(ctx):

    # get the number of active threads
    activethreadcount = active_count()
    # report the number of active threads
    print(activethreadcount)
    start_time = time.time()
    attachment = await discord_before(ctx)
    if attachment:
        asyncio.create_task(generation(ctx, attachment, start_time))
        #await generation(ctx, attachment)
        
async def discord_before(ctx):
    if ctx.message.attachments: 
        await asyncio.gather(
            ctx.message.add_reaction('πŸ€–'),
            asyncio.sleep(0.5),
            ctx.message.add_reaction('βŒ›')  
        )
        attachment = ctx.message.attachments[0]

        # get the number of active threads
        activethreadcount = active_count()
        # report the number of active threads
        print(activethreadcount)
        
        return attachment
    else:
        await asyncio.gather(
            ctx.send(f"{ctx.author.mention} No attachments to be found... Can't animify dat! Try sending me an image πŸ˜‰"),
            asyncio.sleep(0.5),
            ctx.message.add_reaction('❌') 
        )
        return None

async def generation(ctx, attachment, start_time):
    if attachment:
        style = 'JoJo'

        await semaphore.acquire()
        try:
            im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
            semaphore.release()
        except Exception as e:
            semaphore.release()
            raise e
        #im = await asyncio.to_thread(jojogan.predict, attachment.url, style)
        #im = jojogan.predict(attachment.url, style)

        # get the number of active threads
        activethreadcount = active_count()
        # report the number of active threads
        print(activethreadcount)
        
        end_time = time.time()
        total_time = end_time - start_time
        
        await asyncio.gather(
            ctx.send(f'{ctx.author.mention} Jojo image generated in {total_time:.2f} seconds.', file=discord.File(im)),
            asyncio.sleep(0.5),
            ctx.message.remove_reaction('βŒ›', bot.user),
            asyncio.sleep(0.5),
            ctx.message.add_reaction('βœ…')
        )   
        


 


#--------------------------------------------------------
def get_total_threads():
    return multiprocessing.cpu_count()

total_threads = get_total_threads()
print("Total cpus:", total_threads)

# get the number of active threads
activethreadcount = active_count()
# report the number of active threads
print(activethreadcount)
    
def run_bot():
    bot.run(DISCORD_TOKEN)

threading.Thread(target=run_bot).start()

def greet(name):
    return "Hello " + name + "!"

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
#--------------------------------------------------------



'''
 async def discord_after(ctx, im):
    await asyncio.gather(
        ctx.send(f'{ctx.author.mention} Here is the jojo version of it'),
        ctx.send(file=discord.File(im)),
        ctx.message.remove_reaction('βŒ›', bot.user),
        ctx.message.add_reaction('βœ…')
    )  

'''


'''
def blocking_io():
    #generate
    #return image

async def main():
    loop = asyncio.get_running_loop()


    
    exeresult = await loop.run_in_executor(
        None, blocking_io)
    print('default thread pool', exeresult)

'''