seawolf2357
commited on
Commit
โข
fc74fc1
1
Parent(s):
0615e42
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
import asyncio
|
6 |
import subprocess
|
|
|
|
|
7 |
from PIL import Image
|
8 |
import io
|
9 |
|
@@ -18,14 +19,11 @@ intents.guilds = True
|
|
18 |
intents.guild_messages = True
|
19 |
|
20 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
21 |
-
hf_client =
|
22 |
|
23 |
# ํน์ ์ฑ๋ ID
|
24 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
25 |
|
26 |
-
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ๋ณ์
|
27 |
-
conversation_history = []
|
28 |
-
|
29 |
class MyClient(discord.Client):
|
30 |
def __init__(self, *args, **kwargs):
|
31 |
super().__init__(*args, **kwargs)
|
@@ -39,40 +37,34 @@ class MyClient(discord.Client):
|
|
39 |
async def on_message(self, message):
|
40 |
if message.author == self.user or not message.content:
|
41 |
return
|
42 |
-
if message.channel.id != SPECIFIC_CHANNEL_ID:
|
43 |
return
|
44 |
if self.is_processing:
|
45 |
return
|
46 |
self.is_processing = True
|
47 |
try:
|
48 |
image_path = await generate_image(message.content)
|
49 |
-
|
|
|
|
|
|
|
50 |
finally:
|
51 |
self.is_processing = False
|
52 |
|
53 |
async def generate_image(prompt):
|
54 |
-
"""
|
55 |
-
Generate an image using the Stable Diffusion model from Hugging Face.
|
56 |
-
"""
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
image_data = response['images'][0] # Assuming the response contains image data
|
61 |
-
|
62 |
-
# Save image data to a file
|
63 |
image_bytes = io.BytesIO(base64.b64decode(image_data))
|
64 |
image = Image.open(image_bytes)
|
65 |
image_path = "output.png"
|
66 |
image.save(image_path)
|
67 |
return image_path
|
68 |
-
|
69 |
except Exception as e:
|
70 |
logging.error(f"Failed to generate image: {str(e)}")
|
71 |
return None
|
72 |
|
73 |
-
|
74 |
async def send_image(channel, image_path):
|
75 |
-
"""Send an image to the specified Discord channel."""
|
76 |
file = discord.File(image_path)
|
77 |
await channel.send(file=file)
|
78 |
|
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
|
|
4 |
import asyncio
|
5 |
import subprocess
|
6 |
+
from huggingface_hub import InferenceApi
|
7 |
+
import base64
|
8 |
from PIL import Image
|
9 |
import io
|
10 |
|
|
|
19 |
intents.guild_messages = True
|
20 |
|
21 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
22 |
+
hf_client = InferenceApi("stabilityai/stable-diffusion-3-medium", token=os.getenv("HF_TOKEN"))
|
23 |
|
24 |
# ํน์ ์ฑ๋ ID
|
25 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
26 |
|
|
|
|
|
|
|
27 |
class MyClient(discord.Client):
|
28 |
def __init__(self, *args, **kwargs):
|
29 |
super().__init__(*args, **kwargs)
|
|
|
37 |
async def on_message(self, message):
|
38 |
if message.author == self.user or not message.content:
|
39 |
return
|
40 |
+
if message.channel.id != SPECIFIC_CHANNEL_ID and not isinstance(message.channel, discord.Thread):
|
41 |
return
|
42 |
if self.is_processing:
|
43 |
return
|
44 |
self.is_processing = True
|
45 |
try:
|
46 |
image_path = await generate_image(message.content)
|
47 |
+
if image_path:
|
48 |
+
await send_image(message.channel, image_path)
|
49 |
+
else:
|
50 |
+
await message.channel.send("Failed to generate the image.")
|
51 |
finally:
|
52 |
self.is_processing = False
|
53 |
|
54 |
async def generate_image(prompt):
|
|
|
|
|
|
|
55 |
try:
|
56 |
+
response = hf_client(payload={"inputs": prompt})
|
57 |
+
image_data = response.get('image') # Assuming the response returns an image key
|
|
|
|
|
|
|
58 |
image_bytes = io.BytesIO(base64.b64decode(image_data))
|
59 |
image = Image.open(image_bytes)
|
60 |
image_path = "output.png"
|
61 |
image.save(image_path)
|
62 |
return image_path
|
|
|
63 |
except Exception as e:
|
64 |
logging.error(f"Failed to generate image: {str(e)}")
|
65 |
return None
|
66 |
|
|
|
67 |
async def send_image(channel, image_path):
|
|
|
68 |
file = discord.File(image_path)
|
69 |
await channel.send(file=file)
|
70 |
|