kai-flx / app.py
seawolf2357's picture
Update app.py
d14b0d3 verified
raw
history blame
3.17 kB
import requests
import discord
import logging
import os
from transformers import pipeline
import subprocess
# λ‘œκΉ… μ„€μ •
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
# μΈν…νŠΈ μ„€μ •
intents = discord.Intents.default()
intents.message_content = True
# λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ„€μ •
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
# κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ
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"
# 이미지 생성 및 응닡 처리 ν•¨μˆ˜
def generate_image(prompt, negative_prompt):
headers = {
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
}
data = {
"inputs": {
"prompt": prompt,
"negative_prompt": negative_prompt,
"num_inference_steps": 50
}
}
api_url = "https://api-inference.huggingface.co/models/fluently/Fluently-XL-Final"
response = requests.post(api_url, headers=headers, json=data)
if response.status_code == 200:
image_url = response.json()[0]['url'] # μ‘λ‹΅μ—μ„œ 이미지 URL μΆ”μΆœ
return image_url
else:
logging.error(f"API μš”μ²­μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€: {response.text}")
return None
class MyClient(discord.Client):
async def on_ready(self):
logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
subprocess.Popen(["python", "web.py"]) # λ³„λ„μ˜ Python 슀크립트 μ‹€ν–‰
logging.info("web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('!image '):
self.is_processing = True
try:
prompt = message.content[len('!image '):]
prompt_en = translate_prompt(prompt)
logging.debug(f'λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {prompt_en}')
logging.debug(f'κ³ μ •λœ λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ: {negative_prompt}')
image_url = generate_image(prompt_en, negative_prompt)
user_id = message.author.id
if image_url:
await message.channel.send(
f"<@{user_id}> λ‹˜μ΄ μš”μ²­ν•˜μ‹  μ΄λ―Έμ§€μž…λ‹ˆλ‹€: {image_url}"
)
else:
await message.channel.send(f"<@{user_id}> 이미지 생성에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.")
finally:
self.is_processing = False
else:
# "!image" λͺ…λ Ήμ–΄κ°€ 아닐 경우 μ•ˆλ‚΄ λ©”μ‹œμ§€ 전솑
await message.channel.send('μ˜¬λ°”λ₯Έ λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”. 예) "!image κ·€μ—¬μš΄ 고양이가 μž μ„ μžκ³ μžˆλ‹€." λ“±μœΌλ‘œ μž…λ ₯ν•˜μ‹œλ©΄ 이미지가 μƒμ„±λ©λ‹ˆλ‹€.')
# 봇 μ‹€ν–‰
if __name__ == "__main__":
discord_token = os.getenv('DISCORD_TOKEN')
discord_client = MyClient(intents=intents)
discord_client.run(discord_token)