Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import discord
|
2 |
-
from discord.ext import commands
|
3 |
-
from gradio_client import Client
|
4 |
-
import asyncio
|
5 |
-
import os
|
6 |
import logging
|
|
|
|
|
7 |
import subprocess
|
8 |
-
import
|
|
|
9 |
|
10 |
# λ‘κΉ
μ€μ
|
11 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
@@ -14,85 +13,93 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(nam
|
|
14 |
intents = discord.Intents.default()
|
15 |
intents.message_content = True
|
16 |
|
17 |
-
# Discord Bot μ€μ
|
18 |
-
bot = commands.Bot(command_prefix='!', intents=intents)
|
19 |
-
|
20 |
# Gradio API ν΄λΌμ΄μΈνΈ μ€μ
|
21 |
-
|
22 |
-
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
logging.info("
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
seed=123,
|
50 |
randomize_seed=False,
|
51 |
width=1024,
|
52 |
height=576,
|
53 |
guidance_scale=5,
|
54 |
-
num_inference_steps=
|
55 |
api_name="/infer_t2i"
|
56 |
-
)
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
else:
|
89 |
-
logging.error("Discord token is not set. Please check your environment variables.")
|
90 |
-
sys.exit(1) # Exit if token is not set
|
91 |
-
|
92 |
if __name__ == "__main__":
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
run_discord_bot()
|
97 |
-
|
98 |
-
web_process.wait() # Ensure the web process completes
|
|
|
1 |
import discord
|
|
|
|
|
|
|
|
|
2 |
import logging
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
import subprocess
|
6 |
+
from deep_translator import GoogleTranslator
|
7 |
+
from gradio_client import Client
|
8 |
|
9 |
# λ‘κΉ
μ€μ
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
|
|
13 |
intents = discord.Intents.default()
|
14 |
intents.message_content = True
|
15 |
|
|
|
|
|
|
|
16 |
# Gradio API ν΄λΌμ΄μΈνΈ μ€μ
|
17 |
+
client = Client("http://211.233.58.202:7960/")
|
18 |
+
|
19 |
+
# λμ€μ½λ λ΄ ν΄λμ€
|
20 |
+
class MyClient(discord.Client):
|
21 |
+
def __init__(self, *args, **kwargs):
|
22 |
+
super().__init__(*args, **kwargs)
|
23 |
+
self.is_processing = False
|
24 |
+
|
25 |
+
async def on_ready(self):
|
26 |
+
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
27 |
+
subprocess.Popen(["python", "web.py"])
|
28 |
+
logging.info("web.py μλ²κ° μμλμμ΅λλ€.")
|
29 |
+
channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")))
|
30 |
+
await channel.send("μ λ μ΄λ―Έμ§ μμ±μ μνν μ μμΌλ©°, μμ±λ μ΄λ―Έμ§μ λν μ€λͺ
μ νκΈλ‘ μ 곡νκ³ μνΈ λνλ₯Ό ν μ μμ΅λλ€. '!image <ν둬ννΈ>'λ₯Ό μ¬μ©νμ¬ μ΄λ―Έμ§λ₯Ό μμ²νμΈμ.")
|
31 |
+
|
32 |
+
async def on_message(self, message):
|
33 |
+
if message.author == self.user:
|
34 |
+
return
|
35 |
+
if message.content.startswith('!image '):
|
36 |
+
if self.is_processing:
|
37 |
+
await message.channel.send("μ΄λ―Έμ§ μμ±μ΄ μ΄λ―Έ μ§ν μ€μ
λλ€. μ μλ§ κΈ°λ€λ € μ£ΌμΈμ.")
|
38 |
+
return
|
39 |
+
self.is_processing = True
|
40 |
+
try:
|
41 |
+
prompt = message.content[len('!image '):]
|
42 |
+
prompt_en = await translate_prompt(prompt)
|
43 |
+
image_path = await self.generate_image(prompt_en)
|
44 |
+
user_id = message.author.id
|
45 |
+
await message.channel.send(
|
46 |
+
f"<@{user_id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ
λλ€.",
|
47 |
+
file=discord.File(image_path, 'generated_image.png')
|
48 |
+
)
|
49 |
+
await initiate_conversation(prompt, image_path, message)
|
50 |
+
except Exception as e:
|
51 |
+
logging.error(f'μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ: {e}')
|
52 |
+
await message.channel.send("μ΄λ―Έμ§ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€. λμ€μ λ€μ μλν΄ μ£ΌμΈμ.")
|
53 |
+
finally:
|
54 |
+
self.is_processing = False
|
55 |
+
|
56 |
+
async def generate_image(self, prompt):
|
57 |
+
if not prompt:
|
58 |
+
raise ValueError("Prompt is empty or None")
|
59 |
+
|
60 |
+
try:
|
61 |
+
result = client.predict(
|
62 |
+
prompt=prompt,
|
63 |
seed=123,
|
64 |
randomize_seed=False,
|
65 |
width=1024,
|
66 |
height=576,
|
67 |
guidance_scale=5,
|
68 |
+
num_inference_steps=28,
|
69 |
api_name="/infer_t2i"
|
70 |
+
)
|
71 |
+
logging.debug(f"API call result: {result}")
|
72 |
+
|
73 |
+
if 'path' in result:
|
74 |
+
image_path = result['path']
|
75 |
+
logging.debug(f"Image successfully retrieved from {image_path}")
|
76 |
+
return image_path
|
77 |
+
else:
|
78 |
+
raise RuntimeError("μ΄λ―Έμ§ μμ± μ€ν¨: κ²°κ³Όκ° λΉμ΄ μκ±°λ κ²½λ‘κ° ν¬ν¨λμ§ μμμ΅λλ€.")
|
79 |
+
except Exception as e:
|
80 |
+
logging.error(f'μ΄λ―Έμ§ μμ± λμ€ μμΈ λ°μ: {e}')
|
81 |
+
raise RuntimeError(f"μ΄λ―Έμ§ μμ± λμ€ μμΈ λ°μ: {str(e)}")
|
82 |
+
|
83 |
+
async def translate_prompt(prompt):
|
84 |
+
logging.debug(f'ν둬ννΈ λ²μ μ€: {prompt}')
|
85 |
+
translated_text = GoogleTranslator(source='ko', target='en').translate(prompt)
|
86 |
+
logging.debug(f'λ²μλ ν
μ€νΈ: {translated_text}')
|
87 |
+
return translated_text
|
88 |
+
|
89 |
+
async def initiate_conversation(prompt, image_path, message):
|
90 |
+
logging.debug(f'λν μμ μ€: {prompt}')
|
91 |
+
description = "μμ±λ μ΄λ―Έμ§μ
λλ€."
|
92 |
+
logging.debug(f'μ΄λ―Έμ§ μ€λͺ
: {description}')
|
93 |
+
|
94 |
+
await message.channel.send(f"μ΄λ―Έμ§ μ€λͺ
: {description}")
|
95 |
+
await continue_conversation(prompt, message)
|
96 |
+
|
97 |
+
async def continue_conversation(prompt, message):
|
98 |
+
logging.debug(f'λν μ§μ μ€: {prompt}')
|
99 |
+
# κ³μ λν λ΄μ©μ λ°μ μνΈμμ©νλλ‘ κ΅¬ν
|
100 |
+
|
101 |
+
# λμ€μ½λ ν ν° λ° λ΄ μ€ν
|
|
|
|
|
|
|
|
|
102 |
if __name__ == "__main__":
|
103 |
+
discord_token = os.getenv('DISCORD_TOKEN')
|
104 |
+
discord_client = MyClient(intents=intents)
|
105 |
+
discord_client.run(discord_token)
|
|
|
|
|
|