Spaces:
Runtime error
Runtime error
seawolf2357
commited on
Commit
โข
cf409bd
1
Parent(s):
6b7bdbc
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,56 @@
|
|
1 |
import discord
|
2 |
-
import logging
|
3 |
import os
|
4 |
-
import
|
5 |
-
import uuid
|
6 |
from transformers import pipeline
|
|
|
|
|
7 |
|
8 |
# ๋ก๊น
์ค์
|
9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
10 |
|
11 |
# ์ธํ
ํธ ์ค์
|
12 |
intents = discord.Intents.default()
|
13 |
-
intents.
|
14 |
|
15 |
# ๋ฒ์ญ ํ์ดํ๋ผ์ธ ์ค์
|
16 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
"epiCPhotoGasm": "https://api-inference.huggingface.co/models/Yntec/epiCPhotoGasm",
|
24 |
-
"AM-mix1": "https://api-inference.huggingface.co/models/digiplay/AM-mix1",
|
25 |
-
"AbsoluteReality": "https://api-inference.huggingface.co/models/Yntec/AbsoluteReality",
|
26 |
-
"DreamPhotoGASM": "https://api-inference.huggingface.co/models/Yntec/DreamPhotoGASM",
|
27 |
-
"insaneRealistic_v1": "https://api-inference.huggingface.co/models/digiplay/insaneRealistic_v1",
|
28 |
-
"photoMovieXFinal": "https://api-inference.huggingface.co/models/Yntec/photoMovieXFinal",
|
29 |
-
"Memento": "https://api-inference.huggingface.co/models/Yntec/Memento",
|
30 |
-
"photoMovieRealistic": "https://api-inference.huggingface.co/models/Yntec/photoMovieRealistic",
|
31 |
-
"Timeless": "https://api-inference.huggingface.co/models/Yntec/Timeless",
|
32 |
-
"Fabulous": "https://api-inference.huggingface.co/models/Yntec/Fabulous",
|
33 |
-
"IncredibleLife": "https://api-inference.huggingface.co/models/Yntec/IncredibleLife",
|
34 |
-
"RealLife": "https://api-inference.huggingface.co/models/Yntec/RealLife",
|
35 |
-
"HyperRealism": "https://api-inference.huggingface.co/models/Yntec/HyperRealism"
|
36 |
-
}
|
37 |
|
38 |
-
# ๋์ค์ฝ๋ ๋ด ํด๋์ค
|
39 |
class MyClient(discord.Client):
|
40 |
-
def __init__(self, *args, **kwargs):
|
41 |
-
super().__init__(*args, **kwargs)
|
42 |
-
|
43 |
async def on_ready(self):
|
44 |
-
logging.info(f'{self.user}
|
|
|
|
|
45 |
|
46 |
async def on_message(self, message):
|
47 |
-
if message.author == self.user:
|
48 |
return
|
49 |
if message.content.startswith('!image '):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
model_name = parts[1]
|
56 |
-
prompt = parts[2]
|
57 |
-
prompt_en = translate_prompt(prompt) # ๋ฒ์ญ ๊ธฐ๋ฅ ์ถ๊ฐ
|
58 |
-
image_path = await self.generate_image(model_urls[model_name], prompt_en)
|
59 |
-
user_id = message.author.id
|
60 |
-
await message.channel.send(
|
61 |
-
f"<@{user_id}> Here is your requested image:",
|
62 |
-
file=discord.File(image_path, 'generated_image.png')
|
63 |
-
)
|
64 |
-
|
65 |
-
async def generate_image(self, model_url, prompt):
|
66 |
-
headers = {"Authorization": f"Bearer {hf_token}"}
|
67 |
-
response = requests.post(model_url, headers=headers, json={"inputs": prompt})
|
68 |
-
if response.status_code == 200:
|
69 |
-
image_path = f'/tmp/{uuid.uuid4()}.png'
|
70 |
-
with open(image_path, 'wb') as f:
|
71 |
-
f.write(response.content) # ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ํ์ผ๋ก ์ ์ฅ
|
72 |
-
return image_path
|
73 |
-
else:
|
74 |
-
raise Exception("Failed to generate image from the API")
|
75 |
|
76 |
# ํ๋กฌํํธ ๋ฒ์ญ ํจ์
|
77 |
def translate_prompt(prompt):
|
78 |
-
logging.debug(f'Translating prompt: {prompt}')
|
79 |
translation = translator(prompt, max_length=512)
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
#
|
85 |
if __name__ == "__main__":
|
86 |
discord_token = os.getenv('DISCORD_TOKEN')
|
87 |
discord_client = MyClient(intents=intents)
|
|
|
1 |
import discord
|
|
|
2 |
import os
|
3 |
+
import logging
|
|
|
4 |
from transformers import pipeline
|
5 |
+
import subprocess
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
|
8 |
# ๋ก๊น
์ค์
|
9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
10 |
|
11 |
# ์ธํ
ํธ ์ค์
|
12 |
intents = discord.Intents.default()
|
13 |
+
intents.message_content = True
|
14 |
|
15 |
# ๋ฒ์ญ ํ์ดํ๋ผ์ธ ์ค์
|
16 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
17 |
|
18 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ชจ๋ธ ๋ก๋
|
19 |
+
model_id = "fluently/Fluently-XL-Final"
|
20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
+
image_pipeline = DiffusionPipeline.from_pretrained(model_id).to(device)
|
22 |
|
23 |
+
# ํ๊ฒฝ ๋ณ์์์ ์ง์ ๋ ์ฑ๋ ID ๊ฐ์ ธ์ค๊ธฐ
|
24 |
+
TARGET_CHANNEL_ID = os.getenv('TARGET_CHANNEL_ID')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
26 |
class MyClient(discord.Client):
|
|
|
|
|
|
|
27 |
async def on_ready(self):
|
28 |
+
logging.info(f'{self.user}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!')
|
29 |
+
subprocess.Popen(["python", "web.py"]) # ๋ณ๋์ Python ์คํฌ๋ฆฝํธ ์คํ
|
30 |
+
logging.info("web.py ์๋ฒ๊ฐ ์์๋์์ต๋๋ค.")
|
31 |
|
32 |
async def on_message(self, message):
|
33 |
+
if message.author == self.user or message.channel.id != TARGET_CHANNEL_ID:
|
34 |
return
|
35 |
if message.content.startswith('!image '):
|
36 |
+
prompt = message.content[len('!image '):]
|
37 |
+
translated_prompt = translate_prompt(prompt)
|
38 |
+
image_path = await generate_image(translated_prompt)
|
39 |
+
await message.channel.send(file=discord.File(image_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# ํ๋กฌํํธ ๋ฒ์ญ ํจ์
|
42 |
def translate_prompt(prompt):
|
|
|
43 |
translation = translator(prompt, max_length=512)
|
44 |
+
return translation[0]['translation_text']
|
45 |
+
|
46 |
+
# ๋น๋๊ธฐ ์ด๋ฏธ์ง ์์ฑ ํจ์
|
47 |
+
async def generate_image(prompt):
|
48 |
+
image = image_pipeline(prompt)["sample"][0] # ์ด๋ฏธ์ง ์์ฑ
|
49 |
+
image_path = f"./generated_images/{prompt.replace(' ', '_')}.png"
|
50 |
+
image.save(image_path)
|
51 |
+
return image_path
|
52 |
|
53 |
+
# ๋ด ์คํ
|
54 |
if __name__ == "__main__":
|
55 |
discord_token = os.getenv('DISCORD_TOKEN')
|
56 |
discord_client = MyClient(intents=intents)
|