Spaces:
Runtime error
Runtime error
Create app-backup.py
Browse files- app-backup.py +109 -0
app-backup.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
import logging
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
from gradio_client import Client
|
6 |
+
|
7 |
+
# λ‘κΉ
μ€μ
|
8 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
9 |
+
|
10 |
+
# μΈν
νΈ μ€μ
|
11 |
+
intents = discord.Intents.default()
|
12 |
+
intents.message_content = True
|
13 |
+
|
14 |
+
# API ν΄λΌμ΄μΈνΈ μ€μ
|
15 |
+
api_client = Client("http://211.233.58.202:7960/")
|
16 |
+
|
17 |
+
# λμ€μ½λ λ΄ ν΄λμ€
|
18 |
+
class MyClient(discord.Client):
|
19 |
+
def __init__(self, *args, **kwargs):
|
20 |
+
super().__init__(*args, **kwargs)
|
21 |
+
self.is_processing = False
|
22 |
+
|
23 |
+
async def on_ready(self):
|
24 |
+
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
25 |
+
# μνΈ λν κΈ°λ₯ μλ¦Ό
|
26 |
+
channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")))
|
27 |
+
await channel.send("μ λ μ΄λ―Έμ§ μμ±μ μνν μ μμΌλ©°, μμ±λ μ΄λ―Έμ§μ λν μ€λͺ
μ νκΈλ‘ μ 곡νκ³ μνΈ λνλ₯Ό ν μ μμ΅λλ€. '!image <ν둬ννΈ>'λ₯Ό μ¬μ©νμ¬ μ΄λ―Έμ§λ₯Ό μμ²νμΈμ.")
|
28 |
+
|
29 |
+
async def on_message(self, message):
|
30 |
+
if message.author == self.user:
|
31 |
+
return
|
32 |
+
if message.content.startswith('!image '):
|
33 |
+
if self.is_processing:
|
34 |
+
await message.channel.send("μ΄λ―Έμ§ μμ±μ΄ μ΄λ―Έ μ§ν μ€μ
λλ€. μ μλ§ κΈ°λ€λ € μ£ΌμΈμ.")
|
35 |
+
return
|
36 |
+
self.is_processing = True
|
37 |
+
try:
|
38 |
+
prompt = message.content[len('!image '):]
|
39 |
+
image_path, used_seed, translated_prompt = await self.generate_image(prompt)
|
40 |
+
user_id = message.author.id
|
41 |
+
await message.channel.send(
|
42 |
+
f"<@{user_id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ
λλ€.\n"
|
43 |
+
f"μ¬μ©λ μλ: {used_seed}\n"
|
44 |
+
f"λ²μλ ν둬ννΈ: {translated_prompt}",
|
45 |
+
file=discord.File(image_path)
|
46 |
+
)
|
47 |
+
# μ΄λ―Έμ§ μμ± ν μ€λͺ
μ 곡 λ° λν
|
48 |
+
await initiate_conversation(prompt, message)
|
49 |
+
except Exception as e:
|
50 |
+
logging.error(f'μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ: {e}')
|
51 |
+
await message.channel.send("μ¬μ© μμ: !image κ³ μμ΄κ° 'HAPPY WORLD'λΌκ³ μ°μ¬μ§ νμ§νμ λ€κ³ μλ€. ")
|
52 |
+
finally:
|
53 |
+
self.is_processing = False
|
54 |
+
|
55 |
+
async def generate_image(self, prompt):
|
56 |
+
if not prompt:
|
57 |
+
raise ValueError("Prompt is empty or None")
|
58 |
+
|
59 |
+
logging.debug(f"Sending request to API with prompt: {prompt}")
|
60 |
+
|
61 |
+
try:
|
62 |
+
result = api_client.predict(
|
63 |
+
prompt=prompt,
|
64 |
+
seed=123,
|
65 |
+
randomize_seed=False,
|
66 |
+
width=1024,
|
67 |
+
height=576,
|
68 |
+
guidance_scale=5,
|
69 |
+
num_inference_steps=28,
|
70 |
+
api_name="/infer_t2i"
|
71 |
+
)
|
72 |
+
logging.debug(f"API response received: {result}")
|
73 |
+
|
74 |
+
if isinstance(result, tuple) and len(result) == 3:
|
75 |
+
image_path, used_seed, translated_prompt = result
|
76 |
+
logging.info(f"Image generated at: {image_path}")
|
77 |
+
logging.info(f"Used seed: {used_seed}")
|
78 |
+
logging.info(f"Translated prompt: {translated_prompt}")
|
79 |
+
|
80 |
+
return image_path, used_seed, translated_prompt
|
81 |
+
else:
|
82 |
+
raise ValueError("Unexpected API response format")
|
83 |
+
except gradio_client.exceptions.AppError as e:
|
84 |
+
logging.error(f"Gradio App Error: {e}")
|
85 |
+
raise RuntimeError(f"API μλ² μ€λ₯: {str(e)}. μλ² κ΄λ¦¬μμκ² λ¬ΈμνμΈμ.")
|
86 |
+
except Exception as e:
|
87 |
+
logging.error(f'API μμ² μ€ μ€λ₯ λ°μ: {e}', exc_info=True)
|
88 |
+
raise RuntimeError(f"API μμ² μ€ μ€λ₯ λ°μ: {str(e)}")
|
89 |
+
|
90 |
+
|
91 |
+
async def initiate_conversation(prompt, message):
|
92 |
+
logging.debug(f'λν μμ μ€: {prompt}')
|
93 |
+
# μ΄λ―Έμ§ μ€λͺ
μ νκΈλ‘ μμ±
|
94 |
+
description = "μμ±λ μ΄λ―Έμ§μ
λλ€."
|
95 |
+
logging.debug(f'μ΄λ―Έμ§ μ€λͺ
: {description}')
|
96 |
+
|
97 |
+
await message.channel.send(f"μ΄λ―Έμ§ μ€λͺ
: {description}")
|
98 |
+
await continue_conversation(prompt, message)
|
99 |
+
|
100 |
+
async def continue_conversation(prompt, message):
|
101 |
+
# λν μ§μ κΈ°λ₯
|
102 |
+
logging.debug(f'λν μ§μ μ€: {prompt}')
|
103 |
+
# κ³μ λν λ΄μ©μ λ°μ μνΈμμ©νλλ‘ κ΅¬ν
|
104 |
+
|
105 |
+
# λμ€μ½λ ν ν° λ° λ΄ μ€ν
|
106 |
+
if __name__ == "__main__":
|
107 |
+
discord_token = os.getenv('DISCORD_TOKEN')
|
108 |
+
discord_client = MyClient(intents=intents)
|
109 |
+
discord_client.run(discord_token)
|