lunarflu commited on
Commit ·
07cd144
1
Parent(s): 54389af
[dfif] fix
Browse files
app.py
CHANGED
|
@@ -1,190 +1,3 @@
|
|
| 1 |
-
import discord
|
| 2 |
-
import os
|
| 3 |
-
import threading
|
| 4 |
-
from threading import active_count
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import requests
|
| 7 |
-
import json
|
| 8 |
-
import random
|
| 9 |
-
import time
|
| 10 |
-
import re
|
| 11 |
-
from discord import Embed, Color
|
| 12 |
-
from discord.ext import commands
|
| 13 |
-
from gradio_client import Client
|
| 14 |
-
from PIL import Image
|
| 15 |
-
import asyncio
|
| 16 |
-
import shutil # for doing image movement magic
|
| 17 |
-
import concurrent.futures
|
| 18 |
-
# reboot
|
| 19 |
-
import multiprocessing
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#semaphore = asyncio.Semaphore(2)
|
| 23 |
-
|
| 24 |
-
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
| 25 |
-
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
|
| 26 |
-
|
| 27 |
-
jojogan = Client("akhaliq/JoJoGAN", DFIF_TOKEN)
|
| 28 |
-
|
| 29 |
-
intents = discord.Intents.default()
|
| 30 |
-
intents.message_content = True
|
| 31 |
-
|
| 32 |
-
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
@bot.command()
|
| 36 |
-
async def active(ctx):
|
| 37 |
-
# get the number of active threads
|
| 38 |
-
activethreadcount = active_count()
|
| 39 |
-
await ctx.message.reply(f'active threads: {activethreadcount}')
|
| 40 |
-
|
| 41 |
-
@bot.command()
|
| 42 |
-
async def jojo(ctx):
|
| 43 |
-
|
| 44 |
-
# get the number of active threads
|
| 45 |
-
activethreadcount = active_count()
|
| 46 |
-
# report the number of active threads
|
| 47 |
-
print(activethreadcount)
|
| 48 |
-
start_time = time.time()
|
| 49 |
-
attachment = await discord_before(ctx)
|
| 50 |
-
if attachment:
|
| 51 |
-
asyncio.create_task(generation(ctx, attachment, start_time))
|
| 52 |
-
#await generation(ctx, attachment)
|
| 53 |
-
|
| 54 |
-
async def discord_before(ctx):
|
| 55 |
-
if ctx.message.attachments:
|
| 56 |
-
await asyncio.gather(
|
| 57 |
-
ctx.message.add_reaction('🤖'),
|
| 58 |
-
asyncio.sleep(0.5),
|
| 59 |
-
ctx.message.add_reaction('⌛')
|
| 60 |
-
)
|
| 61 |
-
attachment = ctx.message.attachments[0]
|
| 62 |
-
|
| 63 |
-
# get the number of active threads
|
| 64 |
-
activethreadcount = active_count()
|
| 65 |
-
# report the number of active threads
|
| 66 |
-
print(activethreadcount)
|
| 67 |
-
|
| 68 |
-
return attachment
|
| 69 |
-
else:
|
| 70 |
-
await asyncio.gather(
|
| 71 |
-
ctx.send(f"{ctx.author.mention} No attachments to be found... Can't animify dat! Try sending me an image 😉"),
|
| 72 |
-
asyncio.sleep(0.5),
|
| 73 |
-
ctx.message.add_reaction('❌')
|
| 74 |
-
)
|
| 75 |
-
return None
|
| 76 |
-
|
| 77 |
-
async def generation(ctx, attachment, start_time):
|
| 78 |
-
if attachment:
|
| 79 |
-
style = 'JoJo'
|
| 80 |
-
|
| 81 |
-
im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
|
| 82 |
-
#semaphore.release()
|
| 83 |
-
|
| 84 |
-
#im = await asyncio.to_thread(jojogan.predict, attachment.url, style)
|
| 85 |
-
#im = jojogan.predict(attachment.url, style)
|
| 86 |
-
|
| 87 |
-
# get the number of active threads
|
| 88 |
-
activethreadcount = active_count()
|
| 89 |
-
# report the number of active threads
|
| 90 |
-
print(activethreadcount)
|
| 91 |
-
|
| 92 |
-
end_time = time.time()
|
| 93 |
-
total_time = end_time - start_time
|
| 94 |
-
|
| 95 |
-
await asyncio.gather(
|
| 96 |
-
ctx.send(f'{ctx.author.mention} Jojo image generated in {total_time:.2f} seconds.', file=discord.File(im)),
|
| 97 |
-
asyncio.sleep(0.5),
|
| 98 |
-
ctx.message.remove_reaction('⌛', bot.user),
|
| 99 |
-
asyncio.sleep(0.5),
|
| 100 |
-
ctx.message.add_reaction('✅'),
|
| 101 |
-
asyncio.sleep(0.5)
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
#--------------------------------------------------------
|
| 110 |
-
def get_total_threads():
|
| 111 |
-
return multiprocessing.cpu_count()
|
| 112 |
-
|
| 113 |
-
total_threads = get_total_threads()
|
| 114 |
-
print("Total cpus:", total_threads)
|
| 115 |
-
|
| 116 |
-
# get the number of active threads
|
| 117 |
-
activethreadcount = active_count()
|
| 118 |
-
# report the number of active threads
|
| 119 |
-
print(activethreadcount)
|
| 120 |
-
|
| 121 |
-
def run_bot():
|
| 122 |
-
bot.run(DISCORD_TOKEN)
|
| 123 |
-
|
| 124 |
-
threading.Thread(target=run_bot).start()
|
| 125 |
-
|
| 126 |
-
def greet(name):
|
| 127 |
-
return "Hello " + name + "!"
|
| 128 |
-
|
| 129 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 130 |
-
demo.queue(concurrency_count=10)
|
| 131 |
-
demo.launch()
|
| 132 |
-
#--------------------------------------------------------
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
'''
|
| 137 |
-
async def discord_after(ctx, im):
|
| 138 |
-
await asyncio.gather(
|
| 139 |
-
ctx.send(f'{ctx.author.mention} Here is the jojo version of it'),
|
| 140 |
-
ctx.send(file=discord.File(im)),
|
| 141 |
-
ctx.message.remove_reaction('⌛', bot.user),
|
| 142 |
-
ctx.message.add_reaction('✅')
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
'''
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
'''
|
| 149 |
-
def blocking_io():
|
| 150 |
-
#generate
|
| 151 |
-
#return image
|
| 152 |
-
|
| 153 |
-
async def main():
|
| 154 |
-
loop = asyncio.get_running_loop()
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
exeresult = await loop.run_in_executor(
|
| 159 |
-
None, blocking_io)
|
| 160 |
-
print('default thread pool', exeresult)
|
| 161 |
-
|
| 162 |
-
'''
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
import discord
|
| 189 |
import os
|
| 190 |
import threading
|
|
@@ -632,4 +445,5 @@ def greet(name):
|
|
| 632 |
return "Hello " + name + "!"
|
| 633 |
|
| 634 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
|
|
|
| 635 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import discord
|
| 2 |
import os
|
| 3 |
import threading
|
|
|
|
| 445 |
return "Hello " + name + "!"
|
| 446 |
|
| 447 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 448 |
+
demo.queue(concurrency_count=10)
|
| 449 |
demo.launch()
|