Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import io
|
|
8 |
from PIL import Image
|
9 |
import time
|
10 |
|
11 |
-
|
12 |
# λ‘κΉ
μ€μ
|
13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
14 |
|
@@ -19,6 +18,9 @@ intents.message_content = True
|
|
19 |
# λ²μ νμ΄νλΌμΈ μ€μ
|
20 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
21 |
|
|
|
|
|
|
|
22 |
# κ³ μ λ λ€κ±°ν°λΈ ν둬ννΈ
|
23 |
negative_prompt = "blur, low quality, bad composition, ugly, disfigured, weird colors, low quality, jpeg artifacts, lowres, grainy, deformed structures, blurry, opaque, low contrast, distorted details, details are low"
|
24 |
|
@@ -31,24 +33,17 @@ def translate_prompt(prompt):
|
|
31 |
return translated_text
|
32 |
|
33 |
def generate_image(prompt, negative_prompt):
|
34 |
-
headers = {
|
35 |
-
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
|
36 |
-
}
|
37 |
-
# ν둬ννΈμ λ€κ±°ν°λΈ ν둬ννΈλ₯Ό κ²°ν©νμ¬ νλμ λ¬Έμμ΄λ‘ μ²λ¦¬
|
38 |
combined_prompt = f"{prompt}. {negative_prompt}"
|
39 |
-
data = {
|
40 |
-
"inputs": combined_prompt
|
41 |
-
}
|
42 |
api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
|
43 |
response = requests.post(api_url, headers=headers, json=data)
|
44 |
if response.status_code == 200:
|
45 |
-
|
46 |
-
return image_url
|
47 |
else:
|
48 |
logging.error(f"API μμ²μ μ€ν¨νμ΅λλ€: {response.text}")
|
49 |
return None
|
50 |
|
51 |
-
|
52 |
class MyClient(discord.Client):
|
53 |
async def on_ready(self):
|
54 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
@@ -56,52 +51,26 @@ class MyClient(discord.Client):
|
|
56 |
logging.info("web.py μλ²κ° μμλμμ΅λλ€.")
|
57 |
|
58 |
async def on_message(self, message):
|
59 |
-
if message.author == self.user:
|
60 |
return
|
61 |
if message.content.startswith('!image '):
|
62 |
self.is_processing = True
|
63 |
try:
|
64 |
prompt = message.content[len('!image '):]
|
65 |
prompt_en = translate_prompt(prompt)
|
66 |
-
logging.debug(f'λ²μλ ν둬ννΈ: {prompt_en}')
|
67 |
-
logging.debug(f'κ³ μ λ λ€κ±°ν°λΈ ν둬ννΈ: {negative_prompt}')
|
68 |
-
|
69 |
image_url = generate_image(prompt_en, negative_prompt)
|
70 |
-
user_id = message.author.id
|
71 |
if image_url:
|
72 |
-
await message.channel.send(
|
73 |
-
f"<@{user_id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ
λλ€: {image_url}"
|
74 |
-
)
|
75 |
else:
|
76 |
-
await message.channel.send(f"<@{
|
77 |
finally:
|
78 |
self.is_processing = False
|
79 |
else:
|
80 |
-
# "!image" λͺ
λ Ήμ΄κ° μλ κ²½μ° μλ΄ λ©μμ§ μ μ‘
|
81 |
await message.channel.send('μ¬λ°λ₯Έ λͺ
λ Ήμ΄λ₯Ό μ
λ ₯ν΄ μ£ΌμΈμ. μ) "!image κ·μ¬μ΄ κ³ μμ΄κ° μ μ μκ³ μλ€." λ±μΌλ‘ μ
λ ₯νμλ©΄ μ΄λ―Έμ§κ° μμ±λ©λλ€.')
|
82 |
|
83 |
-
|
84 |
-
def generate_image_with_retry(prompt, negative_prompt, max_retries=3):
|
85 |
-
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
86 |
-
combined_prompt = f"{prompt}. {negative_prompt}"
|
87 |
-
data = {"inputs": combined_prompt}
|
88 |
-
api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
|
89 |
-
|
90 |
-
for attempt in range(max_retries):
|
91 |
-
response = requests.post(api_url, headers=headers, json=data)
|
92 |
-
if response.status_code == 200:
|
93 |
-
return response.json()[0]['url']
|
94 |
-
elif response.status_code == 503 and 'estimated_time' in response.json():
|
95 |
-
wait_time = response.json()['estimated_time']
|
96 |
-
logging.info(f"λͺ¨λΈ λ‘λ© μ€, {wait_time}μ΄ ν μ¬μλν©λλ€.")
|
97 |
-
time.sleep(wait_time) # λκΈ° ν μ¬μλ
|
98 |
-
else:
|
99 |
-
logging.error(f"API μμ²μ μ€ν¨νμ΅λλ€: {response.text}")
|
100 |
-
return None
|
101 |
-
logging.error("μ΅λ μ¬μλ νμ μ΄κ³Ό")
|
102 |
-
return None
|
103 |
# λ΄ μ€ν
|
104 |
if __name__ == "__main__":
|
105 |
discord_token = os.getenv('DISCORD_TOKEN')
|
106 |
discord_client = MyClient(intents=intents)
|
107 |
discord_client.run(discord_token)
|
|
|
|
8 |
from PIL import Image
|
9 |
import time
|
10 |
|
|
|
11 |
# λ‘κΉ
μ€μ
|
12 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
13 |
|
|
|
18 |
# λ²μ νμ΄νλΌμΈ μ€μ
|
19 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
20 |
|
21 |
+
# νκ²½ λ³μμμ μ§μ λ μ±λ ID κ°μ Έμ€κΈ°
|
22 |
+
TARGET_CHANNEL_ID = os.getenv('TARGET_CHANNEL_ID')
|
23 |
+
|
24 |
# κ³ μ λ λ€κ±°ν°λΈ ν둬ννΈ
|
25 |
negative_prompt = "blur, low quality, bad composition, ugly, disfigured, weird colors, low quality, jpeg artifacts, lowres, grainy, deformed structures, blurry, opaque, low contrast, distorted details, details are low"
|
26 |
|
|
|
33 |
return translated_text
|
34 |
|
35 |
def generate_image(prompt, negative_prompt):
|
36 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
|
|
|
|
|
|
37 |
combined_prompt = f"{prompt}. {negative_prompt}"
|
38 |
+
data = {"inputs": combined_prompt}
|
|
|
|
|
39 |
api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
|
40 |
response = requests.post(api_url, headers=headers, json=data)
|
41 |
if response.status_code == 200:
|
42 |
+
return response.json()[0]['url']
|
|
|
43 |
else:
|
44 |
logging.error(f"API μμ²μ μ€ν¨νμ΅λλ€: {response.text}")
|
45 |
return None
|
46 |
|
|
|
47 |
class MyClient(discord.Client):
|
48 |
async def on_ready(self):
|
49 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
|
|
51 |
logging.info("web.py μλ²κ° μμλμμ΅λλ€.")
|
52 |
|
53 |
async def on_message(self, message):
|
54 |
+
if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
|
55 |
return
|
56 |
if message.content.startswith('!image '):
|
57 |
self.is_processing = True
|
58 |
try:
|
59 |
prompt = message.content[len('!image '):]
|
60 |
prompt_en = translate_prompt(prompt)
|
|
|
|
|
|
|
61 |
image_url = generate_image(prompt_en, negative_prompt)
|
|
|
62 |
if image_url:
|
63 |
+
await message.channel.send(f"<@{message.author.id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ
λλ€: {image_url}")
|
|
|
|
|
64 |
else:
|
65 |
+
await message.channel.send(f"<@{message.author.id}> μ΄λ―Έμ§ μμ±μ μ€ν¨νμμ΅λλ€.")
|
66 |
finally:
|
67 |
self.is_processing = False
|
68 |
else:
|
|
|
69 |
await message.channel.send('μ¬λ°λ₯Έ λͺ
λ Ήμ΄λ₯Ό μ
λ ₯ν΄ μ£ΌμΈμ. μ) "!image κ·μ¬μ΄ κ³ μμ΄κ° μ μ μκ³ μλ€." λ±μΌλ‘ μ
λ ₯νμλ©΄ μ΄λ―Έμ§κ° μμ±λ©λλ€.')
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# λ΄ μ€ν
|
72 |
if __name__ == "__main__":
|
73 |
discord_token = os.getenv('DISCORD_TOKEN')
|
74 |
discord_client = MyClient(intents=intents)
|
75 |
discord_client.run(discord_token)
|
76 |
+
|