lunarflu HF Staff commited on
Commit
7aa754f
·
1 Parent(s): 84b83ff
Files changed (1) hide show
  1. app.py +33 -39
app.py CHANGED
@@ -3,35 +3,43 @@ import glob
3
  import os
4
  import pathlib
5
  import random
6
- import os
7
- import random
8
  import threading
9
- from threading import Event
10
- from typing import Optional
11
- from discord import Permissions
12
- from discord.ext import commands
13
- from discord.utils import oauth_url
14
-
15
 
16
- import discord
17
  import gradio as gr
18
-
19
  import discord
20
  from gradio_client import Client
21
  from PIL import Image
22
 
23
  from discord.ui import Button, View
24
 
25
-
26
- #---------------------------------------------------------------------------------------------------------------------
27
- HF_TOKEN = os.getenv("HF_TOKEN")
28
  deepfloydif_client = Client("huggingface-projects/IF", HF_TOKEN)
29
- #---------------------------------------------------------------------------------------------------------------------
30
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
 
 
31
  intents = discord.Intents.default()
32
  intents.message_content = True
33
  bot = commands.Bot(command_prefix="/", intents=intents)
34
  #---------------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def deepfloydif_generate64_inference(prompt):
37
  """Generates four images based on a prompt"""
@@ -130,23 +138,12 @@ def combine_images(png_files, stage_1_images, partial_path):
130
  return combined_image_path
131
 
132
 
133
- @bot.hybrid_command(
134
- name="deepfloydif",
135
- description="Enter a prompt to generate an image! Can generate realistic text, too!",
136
- )
137
- async def deepfloydif(ctx, prompt: str):
138
- """DeepfloydIF stage 1 generation"""
139
- try:
140
- await deepfloydif_generate64(ctx, prompt, bot)
141
- except Exception as e:
142
- print(f"Error: {e}")
143
 
144
 
145
- async def deepfloydif_generate64(ctx, prompt, bot):
 
146
  """DeepfloydIF command (generate images with realistic text using slash commands)"""
147
  try:
148
- if os.environ.get("TEST_ENV") == "True":
149
- print("Safety checks passed for deepfloydif_generate64")
150
  channel = bot.get_channel(DEEPFLOYDIF_CHANNEL_ID)
151
  # interaction.response message can't be used to create a thread, so we create another message
152
  message = await ctx.send(f"**{prompt}** - {ctx.author.mention} <a:loading:1114111677990981692>")
@@ -275,20 +272,17 @@ async def deepfloydif_upscale1024(index: int, path_for_upscale256_upscaling, pro
275
 
276
  #---------------------------------------------------------------------------------------------------------------------
277
  def run_bot():
278
- if not DISCORD_TOKEN:
279
- print("DISCORD_TOKEN NOT SET")
280
- else:
281
- bot.run(DISCORD_TOKEN)
282
 
283
 
284
  threading.Thread(target=run_bot).start()
285
-
286
  with gr.Blocks() as demo:
287
- gr.Markdown(
288
- """
289
- # Discord bot of https://huggingface.co/spaces/facebook/MusicGen
290
- https://discord.com/api/oauth2/authorize?client_id=1151888750662664222&permissions=309237696512&scope=bot
291
- """
292
- )
293
-
294
  demo.launch()
 
3
  import os
4
  import pathlib
5
  import random
 
 
6
  import threading
 
 
 
 
 
 
7
 
 
8
  import gradio as gr
 
9
  import discord
10
  from gradio_client import Client
11
  from PIL import Image
12
 
13
  from discord.ui import Button, View
14
 
 
 
 
15
  deepfloydif_client = Client("huggingface-projects/IF", HF_TOKEN)
 
16
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
17
+
18
+ #---------------------------------------------------------------------------------------------------------------------
19
  intents = discord.Intents.default()
20
  intents.message_content = True
21
  bot = commands.Bot(command_prefix="/", intents=intents)
22
  #---------------------------------------------------------------------------------------------------------------------
23
+ @bot.event
24
+ async def on_ready():
25
+ print(f"Logged in as {bot.user} (ID: {bot.user.id})")
26
+ synced = await bot.tree.sync()
27
+ print(f"Synced commands: {', '.join([s.name for s in synced])}.")
28
+ event.set()
29
+ print("------")
30
+ #---------------------------------------------------------------------------------------------------------------------
31
+ @bot.hybrid_command(
32
+ name="deepfloydif",
33
+ description="Enter a prompt to generate an image! Can generate realistic text, too!",
34
+ )
35
+ async def deepfloydif(ctx, prompt: str):
36
+ """DeepfloydIF stage 1 generation"""
37
+ try:
38
+ await deepfloydif_generate64(ctx, prompt)
39
+ except Exception as e:
40
+ print(f"Error: {e}")
41
+
42
+
43
 
44
  def deepfloydif_generate64_inference(prompt):
45
  """Generates four images based on a prompt"""
 
138
  return combined_image_path
139
 
140
 
 
 
 
 
 
 
 
 
 
 
141
 
142
 
143
+
144
+ async def deepfloydif_generate64(ctx, prompt):
145
  """DeepfloydIF command (generate images with realistic text using slash commands)"""
146
  try:
 
 
147
  channel = bot.get_channel(DEEPFLOYDIF_CHANNEL_ID)
148
  # interaction.response message can't be used to create a thread, so we create another message
149
  message = await ctx.send(f"**{prompt}** - {ctx.author.mention} <a:loading:1114111677990981692>")
 
272
 
273
  #---------------------------------------------------------------------------------------------------------------------
274
  def run_bot():
275
+ client.run(DISCORD_TOKEN)
 
 
 
276
 
277
 
278
  threading.Thread(target=run_bot).start()
279
+ """This allows us to run the Discord bot in a Python thread"""
280
  with gr.Blocks() as demo:
281
+ gr.Markdown("""
282
+ # Huggingbots Server
283
+ This space hosts the huggingbots discord bot.
284
+ Currently supported models are Falcon and DeepfloydIF
285
+ """)
286
+ demo.queue(concurrency_count=100)
287
+ demo.queue(max_size=100)
288
  demo.launch()