Spaces:
seawolf2357
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
40d0e92
โ€ข
1 Parent(s): 74ccf1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -37
app.py CHANGED
@@ -1,63 +1,55 @@
1
  import discord
2
  import logging
3
  import os
4
- import asyncio
5
- import subprocess
6
- from huggingface_hub import InferenceClient
7
- from PIL import Image
8
- import io
9
- import base64
10
 
11
  # ๋กœ๊น… ์„ค์ •
12
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
13
 
14
  # ์ธํ…ํŠธ ์„ค์ •
15
  intents = discord.Intents.default()
16
- intents.message_content = True
17
  intents.messages = True
18
  intents.guilds = True
19
  intents.guild_messages = True
20
 
21
- # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
22
- hf_client = InferenceClient(token=os.getenv("HF_TOKEN"))
23
-
24
-
25
- # SPECIFIC_CHANNEL_ID๋ฅผ ์›ํ•˜๋Š” ์ฑ„๋„์˜ ์ˆซ์ž ID๋กœ ์ •์˜
26
- SPECIFIC_CHANNEL_ID = 1251774883113734167 # ์‹ค์ œ ์ฑ„๋„ ID๋กœ ๊ต์ฒดํ•˜์„ธ์š”
27
-
28
-
29
 
30
  class MyClient(discord.Client):
31
  def __init__(self, *args, **kwargs):
32
  super().__init__(*args, **kwargs)
33
- self.is_processing = False
34
 
35
  async def on_ready(self):
36
- logging.info(f'{self.user} has logged in!')
37
- subprocess.Popen(["python", "web.py"])
38
- logging.info("Web.py server has been started.")
39
 
40
  async def on_message(self, message):
41
- if message.author == self.user or not message.content:
42
  return
43
- if message.channel.id != SPECIFIC_CHANNEL_ID and not isinstance(message.channel, discord.Thread):
44
- return
45
- if self.is_processing:
46
- return
47
- self.is_processing = True
48
- try:
49
- image_path = await generate_image(message.content)
50
  if image_path:
51
- await send_image(message.channel, image_path)
52
  else:
53
- await message.channel.send("Failed to generate the image.")
54
- finally:
55
- self.is_processing = False
56
-
57
- async def send_image(channel, image_path):
58
- file = discord.File(image_path)
59
- await channel.send(file=file)
 
 
 
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
- discord_client = MyClient(intents=intents)
63
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
1
  import discord
2
  import logging
3
  import os
4
+ from gradio_client import Client
 
 
 
 
 
5
 
6
  # ๋กœ๊น… ์„ค์ •
7
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
8
 
9
  # ์ธํ…ํŠธ ์„ค์ •
10
  intents = discord.Intents.default()
11
+ intents.message_content = True # ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ ์ˆ˜์‹  ์ธํ…ํŠธ ํ™œ์„ฑํ™”
12
  intents.messages = True
13
  intents.guilds = True
14
  intents.guild_messages = True
15
 
16
+ # ํŠน์ • ์ฑ„๋„ ID ์„ค์ •
17
+ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
 
 
 
 
 
18
 
19
  class MyClient(discord.Client):
20
  def __init__(self, *args, **kwargs):
21
  super().__init__(*args, **kwargs)
 
22
 
23
  async def on_ready(self):
24
+ logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
 
 
25
 
26
  async def on_message(self, message):
27
+ if message.author == self.user or message.channel.id != SPECIFIC_CHANNEL_ID:
28
  return
29
+ if message.content.startswith("!generate"): # ํ”„๋กฌํ”„ํŠธ ์‹œ์ž‘ ๋ช…๋ น์–ด
30
+ prompt = message.content[len("!generate "):] # ๋ช…๋ น์–ด ์ œ๊ฑฐ ํ›„ ํ”„๋กฌํ”„ํŠธ ์ถ”์ถœ
31
+ image_path = await self.generate_image(prompt)
 
 
 
 
32
  if image_path:
33
+ await message.channel.send(file=discord.File(image_path))
34
  else:
35
+ await message.channel.send("์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.")
36
+
37
+ async def generate_image(self, prompt):
38
+ client = Client("ginipick/komodel")
39
+ result = client.predict(
40
+ prompt=prompt,
41
+ negative="low quality, low quality, (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)",
42
+ width=1024,
43
+ height=1024,
44
+ scale=7,
45
+ steps=50,
46
+ api_name="/generate_image"
47
+ )
48
+ if 'image_path' in result:
49
+ return result['image_path'] # API๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ
50
+ return None
51
 
52
  if __name__ == "__main__":
53
+ discord_token = os.getenv('DISCORD_TOKEN')
54
+ client = MyClient(intents=intents)
55
+ client.run(discord_token)