lunarflu HF Staff commited on
Commit
f20651b
·
1 Parent(s): 29fc37a

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. wuerstchen.py +119 -0
wuerstchen.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import glob
3
+ import os
4
+ import random
5
+ import threading
6
+ from discord.ext import commands
7
+ import discord
8
+ import gradio as gr
9
+ from gradio_client import Client
10
+
11
+
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
13
+ wuerstchen_client = Client("huggingface-projects/Wuerstchen-duplicate", HF_TOKEN)
14
+ DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
15
+
16
+
17
+ intents = discord.Intents.all()
18
+ bot = commands.Bot(command_prefix="/", intents=intents)
19
+
20
+
21
+ @bot.event
22
+ async def on_ready():
23
+ print(f"Logged in as {bot.user} (ID: {bot.user.id})")
24
+ synced = await bot.tree.sync()
25
+ print(f"Synced commands: {', '.join([s.name for s in synced])}.")
26
+ print("------")
27
+
28
+
29
+ @bot.hybrid_command(
30
+ name="wuerstchen",
31
+ description="Enter a prompt to generate art!",
32
+ )
33
+ async def wuerstchen_command(ctx, prompt: str):
34
+ """Wuerstchen generation"""
35
+ try:
36
+ await run_wuerstchen(ctx, prompt)
37
+ except Exception as e:
38
+ print(f"Error wuerstchen: (app.py){e}")
39
+
40
+
41
+ def wuerstchen_inference(prompt):
42
+ """Inference for Wuerstchen"""
43
+ negative_prompt = ""
44
+ seed = random.randint(0, 1000)
45
+ width = 1024
46
+ height = 1024
47
+ prior_num_inference_steps = 60
48
+ prior_guidance_scale = 4
49
+ decoder_num_inference_steps = 12
50
+ decoder_guidance_scale = 0
51
+ num_images_per_prompt = 1
52
+
53
+ result_path = wuerstchen_client.predict(
54
+ prompt,
55
+ negative_prompt,
56
+ seed,
57
+ width,
58
+ height,
59
+ prior_num_inference_steps,
60
+ prior_guidance_scale,
61
+ decoder_num_inference_steps,
62
+ decoder_guidance_scale,
63
+ num_images_per_prompt,
64
+ api_name="/run",
65
+ )
66
+ png_file = list(glob.glob(f"{result_path}/**/*.png"))
67
+ return png_file[0]
68
+
69
+
70
+ async def run_wuerstchen(ctx, prompt):
71
+ """Responds to /Wuerstchen command"""
72
+ try:
73
+ message = await ctx.send(f"**{prompt}** - {ctx.author.mention} (generating...)")
74
+
75
+ loop = asyncio.get_running_loop()
76
+ result_path = await loop.run_in_executor(None, wuerstchen_inference, prompt)
77
+
78
+ await message.delete()
79
+ with open(result_path, "rb") as f:
80
+ await ctx.channel.send(f"**{prompt}** - {ctx.author.mention}", file=discord.File(f, "wuerstchen.png"))
81
+ except Exception as e:
82
+ print(f"Error: {e}")
83
+
84
+
85
+ def run_bot():
86
+ bot.run(DISCORD_TOKEN)
87
+
88
+
89
+ threading.Thread(target=run_bot).start()
90
+ """This allows us to run the Discord bot in a Python thread"""
91
+
92
+
93
+ welcome_message = """
94
+ ## Add this bot to your server by clicking this link:
95
+
96
+ https://discord.com/api/oauth2/authorize?client_id=1155489509518098565&permissions=51200&scope=bot
97
+
98
+ ## How to use it?
99
+
100
+ The bot can be triggered via `/wuerstchen` followed by your text prompt.
101
+
102
+ This will generate an image based on your prompt, which is then posted in the channel!
103
+
104
+ ⚠️ Note ⚠️: Please make sure this bot's command does have the same name as another command in your server.
105
+
106
+ ⚠️ Note ⚠️: Bot commands do not work in DMs with the bot as of now.
107
+ """
108
+
109
+
110
+ with gr.Blocks() as demo:
111
+ gr.Markdown(f"""
112
+ # Discord bot of https://huggingface.co/spaces/warp-ai/Wuerstchen
113
+ {welcome_message}
114
+ """)
115
+
116
+
117
+ demo.queue(concurrency_count=100)
118
+ demo.queue(max_size=100)
119
+ demo.launch()