not-lain commited on
Commit
fa43b29
·
1 Parent(s): d971fec

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. app.py +187 -0
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import gradio as gr
 
2
  import dotenv
3
  import discord
4
  import os
5
  import threading
6
  from threading import Event
 
 
7
  event = Event()
8
 
9
  dotenv.load_dotenv()
@@ -14,6 +17,37 @@ intents = discord.Intents.default()
14
  intents.message_content = True
15
  bot = discord.Bot(intents=intents)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  @bot.event
19
  async def on_ready():
@@ -21,11 +55,164 @@ async def on_ready():
21
  event.set()
22
 
23
 
 
24
  @bot.slash_command(name='ping',description='ping')
25
  async def ping(ctx):
26
  await ctx.respond(f"{bot.latency*1000:.0f}ms")
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # running in thread
30
  def run_bot():
31
  if not DISCORD_TOKEN:
 
1
  import gradio as gr
2
+ import gradio_client as grc
3
  import dotenv
4
  import discord
5
  import os
6
  import threading
7
  from threading import Event
8
+ from typing import Literal,Optional
9
+ import asyncio
10
  event = Event()
11
 
12
  dotenv.load_dotenv()
 
17
  intents.message_content = True
18
  bot = discord.Bot(intents=intents)
19
 
20
+ # limit command to channel_name
21
+ channel_name = "🤸🏻♀anime"
22
+
23
+
24
+ # Limited to only 25 models
25
+ models = Literal['3Guofeng3_v34.safetensors [50f420de]',
26
+ 'absolutereality_V16.safetensors [37db0fc3]',
27
+ 'absolutereality_v181.safetensors [3d9d4d2b]',
28
+ 'anythingV5_PrtRE.safetensors [893e49b9]',
29
+ 'blazing_drive_v10g.safetensors [ca1c1eab]',
30
+ 'cetusMix_Version35.safetensors [de2f2560]',
31
+ 'childrensStories_v1ToonAnime.safetensors [2ec7b88b]',
32
+ 'Counterfeit_v30.safetensors [9e2a8f19]',
33
+ 'cuteyukimixAdorable_midchapter3.safetensors [04bdffe6]',
34
+ 'cyberrealistic_v33.safetensors [82b0d085]',
35
+ 'dreamlike-anime-1.0.safetensors [4520e090]',
36
+ 'dreamlike-photoreal-2.0.safetensors [fdcf65e7]',
37
+ 'dreamshaper_8.safetensors [9d40847d]',
38
+ 'edgeOfRealism_eorV20.safetensors [3ed5de15]',
39
+ 'elldreths-vivid-mix.safetensors [342d9d26]',
40
+ 'epicrealism_naturalSinRC1VAE.safetensors [90a4c676]',
41
+ 'juggernaut_aftermath.safetensors [5e20c455]',
42
+ 'lofi_v4.safetensors [ccc204d6]',
43
+ 'lyriel_v16.safetensors [68fceea2]',
44
+ 'neverendingDream_v122.safetensors [f964ceeb]',
45
+ 'openjourney_V4.ckpt [ca2f377f]',
46
+ 'pastelMixStylizedAnime_pruned_fp16.safetensors [793a26e8]',
47
+ 'Realistic_Vision_V5.0.safetensors [614d1063]',
48
+ 'revAnimated_v122.safetensors [3f4fefd9]',
49
+ 'rundiffusionFX25D_v10.safetensors [cd12b0ee]',
50
+ ]
51
 
52
  @bot.event
53
  async def on_ready():
 
55
  event.set()
56
 
57
 
58
+
59
  @bot.slash_command(name='ping',description='ping')
60
  async def ping(ctx):
61
  await ctx.respond(f"{bot.latency*1000:.0f}ms")
62
 
63
 
64
+ def get_client(session: Optional[str] = None) -> grc.Client:
65
+ client = grc.Client("prodia/fast-stable-diffusion")
66
+ if session:
67
+ client.session_hash = session
68
+ return client
69
+
70
+
71
+ async def text2img(pos_prompt: str, neg_promt: str = "",model="absolutereality_v181.safetensors [3d9d4d2b]"):
72
+ """
73
+ Generates an image based on the given positive prompt and optional negative prompt.
74
+
75
+ Args:
76
+ pos_prompt (str): The positive prompt to generate the image.
77
+ neg_prompt (str, optional): The negative prompt to generate the image. Defaults to "".
78
+
79
+ Returns:
80
+ The generated image.
81
+ """
82
+
83
+ txt2img_conf = {
84
+ "parameter_11" : pos_prompt,
85
+ "parameter_12" : neg_promt,
86
+ "stable_diffusion_checkpoint" : model,
87
+ "sampling_steps" : 25,
88
+ "sampling_method" : "DPM++ 2M Karras",
89
+ "cfg_scale" : 7,
90
+ "width" : 512,
91
+ "height" : 512,
92
+ "seed" : -1
93
+ }
94
+
95
+ loop = asyncio.get_running_loop()
96
+ client = await loop.run_in_executor(None, get_client, None)
97
+ txt2img_args = txt2img_conf.values()
98
+ job = client.predict(*txt2img_args, fn_index=0)
99
+
100
+ return job.result()
101
+
102
+
103
+ async def img2img(pos_prompt: str, neg_promt: str = "", img = None,model="absolutereality_v181.safetensors [3d9d4d2b]"):
104
+ """
105
+ Generates an image based on the given positive prompt, optional negative prompt and image path.
106
+
107
+ Args:
108
+ pos_prompt (str): The positive prompt for the image generation.
109
+ neg_promt (str, optional): The negative prompt for the image generation. Defaults to "".
110
+ img (filepath or URL to image): The input image for the image generation. Defaults to None.
111
+
112
+ Returns:
113
+ The generated image.
114
+ """
115
+
116
+ img2img_conf = {
117
+ "parameter_52" : img,
118
+ "denoising_strength" : 0.7,
119
+ "parameter_44" : pos_prompt,
120
+ "parameter_45" : neg_promt,
121
+ "stable_diffusion_checkpoint" : model,
122
+ "sampling_steps" : 25,
123
+ "sampling_method" : "DPM++ 2M Karras",
124
+ "cfg_scale" : 7,
125
+ "width" : 512,
126
+ "height" : 512,
127
+ "seed" : -1
128
+ }
129
+
130
+ loop = asyncio.get_running_loop()
131
+ client = await loop.run_in_executor(None, get_client, None)
132
+ img2img_args = img2img_conf.values()
133
+ job = client.submit(*img2img_args, fn_index=1)
134
+
135
+ # img = Image.open(img)
136
+ # img.show()
137
+ await wait(job)
138
+ return job.result()
139
+
140
+ def run_dffusion(pos_prompt: str,neg_promt: str = "",img_url= None,model="absolutereality_v181.safetensors [3d9d4d2b]"):
141
+ """Runs the diffusion model."""
142
+
143
+ if img_url == None:
144
+ # Support for text prompts
145
+ generated_image = text2img(pos_prompt, neg_promt,model)
146
+ else:
147
+ # Support for image prompts
148
+ generated_image = img2img(pos_prompt, neg_promt, img_url,model)
149
+
150
+ return generated_image
151
+
152
+
153
+ # AI prediction command
154
+ @bot.hybrid_command(
155
+ name="diffusion",
156
+ description="creates an AI generated image"
157
+ )
158
+ async def diffusion(ctx, pos_prompt: str="",neg_promt: str = "", img_url= None,improve : Literal["True","False"] = "False",model : models = "absolutereality_v181.safetensors [3d9d4d2b]"):
159
+ """
160
+ AI command to create the thread and ask the AI
161
+ """
162
+ # if channel name is == channel_name
163
+ try:
164
+ if ctx.channel.name == channel_name:
165
+ await ctx.respond(f"Creating image for {ctx.author.mention} ...")
166
+ try :
167
+ # preparing the prediction before creating the thread
168
+ _improved_pos_prompt = "ultrarealistic,8k"
169
+ _improved_neg_promt = "3d, cartoon, (deformed eyes, nose, ears, nose), bad anatomy, ugly,blur"
170
+ pos_prompt =pos_prompt.strip()
171
+ neg_promt = neg_promt.strip()
172
+ # check if the user wants to improve the results
173
+ if improve == "True":
174
+ if len(pos_prompt) == 0:
175
+ pos_prompt = _improved_pos_prompt
176
+ elif pos_prompt[-1] != ",":
177
+ pos_prompt += ", " + _improved_pos_prompt
178
+ else:
179
+ pos_prompt += _improved_pos_prompt
180
+ if len(neg_promt) == 0:
181
+ neg_promt = _improved_neg_promt
182
+ elif neg_promt[-1] != ",":
183
+ neg_promt += ", " + _improved_neg_promt
184
+ else:
185
+ neg_promt += _improved_neg_promt
186
+
187
+ # need to make sure AI sends the first message
188
+ result = run_dffusion(pos_prompt , neg_promt,img_url,model=model)
189
+ await ctx.resepond(result)
190
+ except Exception as e:
191
+ await ctx.resepond(e)
192
+ else:
193
+ # TODO:
194
+ # tag the channel #channel_name
195
+ # create the channel if we can't find it, tag it and let the user know that we created it
196
+ await ctx.respond(f"""
197
+ use this command in the channel #falcon-180b-demo\nuse `/setup` to create the channel if it doesn't exist""")
198
+ except Exception as e:
199
+ await ctx.respond(e)
200
+
201
+
202
+ # setup create the falcon-180b-demo channel
203
+ @bot.slash_command(name='setup',description='setup the bot')
204
+ async def setup(ctx):
205
+ """
206
+ create the #falcon-180b-demo channel
207
+ """
208
+ # if channel falcon-180b-demo doesn't exist create it
209
+ if not discord.utils.get(ctx.guild.channels, name=channel_name):
210
+ await ctx.guild.create_text_channel(channel_name,category=ctx.channel.category)
211
+ await ctx.respond(channel_name)
212
+ else:
213
+ # TODO: tag the channel
214
+ await ctx.respond("#falcon-180b-demo channel already exist")
215
+
216
  # running in thread
217
  def run_bot():
218
  if not DISCORD_TOKEN: