seawolf2357 commited on
Commit
b40fda4
Β·
verified Β·
1 Parent(s): 471c2f6

Create app-backup.py

Browse files
Files changed (1) hide show
  1. 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)