seawolf2357 commited on
Commit
cf409bd
โ€ข
1 Parent(s): 6b7bdbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -58
app.py CHANGED
@@ -1,87 +1,56 @@
1
  import discord
2
- import logging
3
  import os
4
- import requests
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.messages = True
14
 
15
  # ๋ฒˆ์—ญ ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
16
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
17
 
18
- # Hugging Face API ํ† ํฐ
19
- hf_token = os.getenv("HF_TOKEN")
 
 
20
 
21
- # ๋ชจ๋ธ URL ์„ค์ •
22
- model_urls = {
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} has logged in!')
 
 
45
 
46
  async def on_message(self, message):
47
- if message.author == self.user:
48
  return
49
  if message.content.startswith('!image '):
50
- parts = message.content.split(' ', 2)
51
- if len(parts) < 3 or parts[1] not in model_urls:
52
- await message.channel.send("Invalid model name or usage.")
53
- return
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
- translated_text = translation[0]['translation_text']
81
- logging.debug(f'Translated text: {translated_text}')
82
- return translated_text
 
 
 
 
 
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)