seawolf2357 commited on
Commit
e2f2c7a
Β·
verified Β·
1 Parent(s): fdd1a4b

Upload app (13).py

Browse files
Files changed (1) hide show
  1. app (13).py +117 -0
app (13).py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ import logging
3
+ import os
4
+ from io import BytesIO
5
+ from gradio_client import Client
6
+ import subprocess
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
+ # web.py μ‹€ν–‰
27
+ try:
28
+ subprocess.Popen(["python", "web.py"])
29
+ logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
30
+ except Exception as e:
31
+ logging.error(f"web.py μ‹€ν–‰ 쀑 였λ₯˜ λ°œμƒ: {e}")
32
+
33
+ # μƒν˜Έ λŒ€ν™” κΈ°λŠ₯ μ•Œλ¦Ό
34
+ channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")))
35
+ await channel.send("μ €λŠ” 이미지 생성을 μˆ˜ν–‰ν•  수 있으며, μƒμ„±λœ 이미지에 λŒ€ν•œ μ„€λͺ…을 ν•œκΈ€λ‘œ μ œκ³΅ν•˜κ³  μƒν˜Έ λŒ€ν™”λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€. '!image <ν”„λ‘¬ν”„νŠΈ>'λ₯Ό μ‚¬μš©ν•˜μ—¬ 이미지λ₯Ό μš”μ²­ν•˜μ„Έμš”.")
36
+
37
+ async def on_message(self, message):
38
+ if message.author == self.user:
39
+ return
40
+ if message.content.startswith('!image '):
41
+ if self.is_processing:
42
+ await message.channel.send("이미지 생성이 이미 μ§„ν–‰ μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ € μ£Όμ„Έμš”.")
43
+ return
44
+ self.is_processing = True
45
+ try:
46
+ prompt = message.content[len('!image '):]
47
+ image_path, used_seed, translated_prompt = await self.generate_image(prompt)
48
+ user_id = message.author.id
49
+ await message.channel.send(
50
+ f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€.\n"
51
+ f"μ‚¬μš©λœ μ‹œλ“œ: {used_seed}\n"
52
+ f"λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {translated_prompt}",
53
+ file=discord.File(image_path)
54
+ )
55
+ # 이미지 생성 ν›„ μ„€λͺ… 제곡 및 λŒ€ν™”
56
+ await initiate_conversation(prompt, message)
57
+ except Exception as e:
58
+ logging.error(f'이미지 생성 쀑 였λ₯˜ λ°œμƒ: {e}')
59
+ await message.channel.send("μ‚¬μš© μ˜ˆμ‹œ: !image 고양이가 'HAPPY WORLD'라고 μ“°μ—¬μ§„ ν‘œμ§€νŒμ„ λ“€κ³ μžˆλ‹€. ")
60
+ finally:
61
+ self.is_processing = False
62
+
63
+ async def generate_image(self, prompt):
64
+ if not prompt:
65
+ raise ValueError("Prompt is empty or None")
66
+
67
+ logging.debug(f"Sending request to API with prompt: {prompt}")
68
+
69
+ try:
70
+ result = api_client.predict(
71
+ prompt=prompt,
72
+ seed=123,
73
+ randomize_seed=False,
74
+ width=1024,
75
+ height=576,
76
+ guidance_scale=5,
77
+ num_inference_steps=28,
78
+ api_name="/infer_t2i"
79
+ )
80
+ logging.debug(f"API response received: {result}")
81
+
82
+ if isinstance(result, tuple) and len(result) == 3:
83
+ image_path, used_seed, translated_prompt = result
84
+ logging.info(f"Image generated at: {image_path}")
85
+ logging.info(f"Used seed: {used_seed}")
86
+ logging.info(f"Translated prompt: {translated_prompt}")
87
+
88
+ return image_path, used_seed, translated_prompt
89
+ else:
90
+ raise ValueError("Unexpected API response format")
91
+ except gradio_client.exceptions.AppError as e:
92
+ logging.error(f"Gradio App Error: {e}")
93
+ raise RuntimeError(f"API μ„œλ²„ 였λ₯˜: {str(e)}. μ„œλ²„ κ΄€λ¦¬μžμ—κ²Œ λ¬Έμ˜ν•˜μ„Έμš”.")
94
+ except Exception as e:
95
+ logging.error(f'API μš”μ²­ 쀑 였λ₯˜ λ°œμƒ: {e}', exc_info=True)
96
+ raise RuntimeError(f"API μš”μ²­ 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
97
+
98
+
99
+ async def initiate_conversation(prompt, message):
100
+ logging.debug(f'λŒ€ν™” μ‹œμž‘ 쀑: {prompt}')
101
+ # 이미지 μ„€λͺ…을 ν•œκΈ€λ‘œ 생성
102
+ description = "μƒμ„±λœ μ΄λ―Έμ§€μž…λ‹ˆλ‹€."
103
+ logging.debug(f'이미지 μ„€λͺ…: {description}')
104
+
105
+ await message.channel.send(f"이미지 μ„€λͺ…: {description}")
106
+ await continue_conversation(prompt, message)
107
+
108
+ async def continue_conversation(prompt, message):
109
+ # λŒ€ν™” 지속 κΈ°λŠ₯
110
+ logging.debug(f'λŒ€ν™” 지속 쀑: {prompt}')
111
+ # 계속 λŒ€ν™” λ‚΄μš©μ„ λ°›μ•„ μƒν˜Έμž‘μš©ν•˜λ„λ‘ κ΅¬ν˜„
112
+
113
+ # λ””μŠ€μ½”λ“œ 토큰 및 봇 μ‹€ν–‰
114
+ if __name__ == "__main__":
115
+ discord_token = os.getenv('DISCORD_TOKEN')
116
+ discord_client = MyClient(intents=intents)
117
+ discord_client.run(discord_token)