Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,8 @@ import logging
|
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import asyncio
|
6 |
-
import
|
|
|
7 |
|
8 |
# ๋ก๊น
์ค์
|
9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
@@ -16,14 +17,11 @@ intents.guilds = True
|
|
16 |
intents.guild_messages = True
|
17 |
|
18 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
19 |
-
hf_client = InferenceClient("
|
20 |
|
21 |
# ํน์ ์ฑ๋ ID
|
22 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
23 |
|
24 |
-
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
25 |
-
conversation_history = []
|
26 |
-
|
27 |
class MyClient(discord.Client):
|
28 |
def __init__(self, *args, **kwargs):
|
29 |
super().__init__(*args, **kwargs)
|
@@ -38,8 +36,8 @@ class MyClient(discord.Client):
|
|
38 |
return
|
39 |
self.is_processing = True
|
40 |
try:
|
41 |
-
response = await
|
42 |
-
await message.channel.send(response)
|
43 |
finally:
|
44 |
self.is_processing = False
|
45 |
|
@@ -49,36 +47,25 @@ class MyClient(discord.Client):
|
|
49 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
50 |
)
|
51 |
|
52 |
-
|
53 |
-
async def generate_response(message):
|
54 |
-
global conversation_history # ์ ์ญ ๋ณ์ ์ฌ์ฉ์ ๋ช
์
|
55 |
user_input = message.content
|
56 |
user_mention = message.author.mention
|
57 |
-
system_message = f"{user_mention},
|
58 |
-
system_prefix = """
|
59 |
-
๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค. ๋ชจ๋ ๋ต์ ๋๋ํด๋ณด์ด๊ฒ ๋๋ตํ๊ณ , pil ์ ์ง๋ฌธ์๋ ๋๋ตํ์ง๋ง
|
60 |
-
"""
|
61 |
-
conversation_history.append({"role": "user", "content": user_input})
|
62 |
-
logging.debug(f'Conversation history updated: {conversation_history}')
|
63 |
|
64 |
-
|
65 |
-
logging.debug(f'Messages to be sent to the model: {messages}')
|
66 |
|
67 |
loop = asyncio.get_event_loop()
|
68 |
-
response = await loop.run_in_executor(None, lambda: hf_client.
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
logging.debug(f'Part received from stream: {part}')
|
74 |
-
if part.choices and part.choices[0].delta and part.choices[0].delta.content:
|
75 |
-
full_response.append(part.choices[0].delta.content)
|
76 |
|
77 |
-
|
78 |
-
|
|
|
79 |
|
80 |
-
|
81 |
-
return f"{user_mention}, {full_response_text}"
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
discord_client = MyClient(intents=intents)
|
|
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import asyncio
|
6 |
+
import requests
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
# ๋ก๊น
์ค์
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
|
|
17 |
intents.guild_messages = True
|
18 |
|
19 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
20 |
+
hf_client = InferenceClient("CompVis/stable-diffusion-v1-4", token=os.getenv("HF_TOKEN"))
|
21 |
|
22 |
# ํน์ ์ฑ๋ ID
|
23 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
24 |
|
|
|
|
|
|
|
25 |
class MyClient(discord.Client):
|
26 |
def __init__(self, *args, **kwargs):
|
27 |
super().__init__(*args, **kwargs)
|
|
|
36 |
return
|
37 |
self.is_processing = True
|
38 |
try:
|
39 |
+
response = await generate_image(message)
|
40 |
+
await message.channel.send(file=response)
|
41 |
finally:
|
42 |
self.is_processing = False
|
43 |
|
|
|
47 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
48 |
)
|
49 |
|
50 |
+
async def generate_image(message):
|
|
|
|
|
51 |
user_input = message.content
|
52 |
user_mention = message.author.mention
|
53 |
+
system_message = f"{user_mention}, ํ
์คํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค."
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
logging.debug(f'User input: {user_input}')
|
|
|
56 |
|
57 |
loop = asyncio.get_event_loop()
|
58 |
+
response = await loop.run_in_executor(None, lambda: hf_client.text_to_image(
|
59 |
+
prompt=user_input, num_return_sequences=1))
|
60 |
|
61 |
+
image_url = response[0]['generated_image']
|
62 |
+
logging.debug(f'Generated image URL: {image_url}')
|
|
|
|
|
|
|
63 |
|
64 |
+
image_response = requests.get(image_url)
|
65 |
+
image = BytesIO(image_response.content)
|
66 |
+
image_file = discord.File(image, filename="generated_image.png")
|
67 |
|
68 |
+
return image_file
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
discord_client = MyClient(intents=intents)
|