seawolf2357 commited on
Commit
99580d4
Β·
verified Β·
1 Parent(s): 903369f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -61
app.py CHANGED
@@ -1,77 +1,64 @@
1
- import requests
2
  import discord
3
- import logging
4
- import os
5
- from transformers import pipeline
6
  import subprocess
7
- import io
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
-
14
- # μΈν…νŠΈ μ„€μ •
15
  intents = discord.Intents.default()
16
  intents.message_content = True
 
 
 
 
17
 
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
 
27
- # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ ν•¨μˆ˜
28
- def translate_prompt(prompt):
29
- logging.debug(f'ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­ 쀑: {prompt}')
30
- translation = translator(prompt, max_length=512)
31
- translated_text = translation[0]['translation_text']
32
- logging.debug(f'λ²ˆμ—­λœ ν…μŠ€νŠΈ: {translated_text}')
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}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
50
- subprocess.Popen(["python", "web.py"]) # λ³„λ„μ˜ Python 슀크립트 μ‹€ν–‰
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
 
 
 
77
 
 
 
 
 
1
  import discord
2
+ from discord.ext import commands
3
+ from gradio_client import Client
4
+ import multiprocessing
5
  import subprocess
6
+ import sys
 
 
7
 
8
+ # Set up Discord bot
 
 
 
9
  intents = discord.Intents.default()
10
  intents.message_content = True
11
+ bot = commands.Bot(command_prefix='!', intents=intents)
12
+
13
+ # Gradio client setup
14
+ gradio_client = Client("http://211.233.58.202:7960/")
15
 
16
+ # Discord channel ID to monitor
17
+ CHANNEL_ID = 1269529561914413106
18
 
19
+ @bot.event
20
+ async def on_ready():
21
+ print(f'{bot.user} has connected to Discord!')
22
 
23
+ @bot.event
24
+ async def on_message(message):
25
+ if message.author == bot.user:
26
+ return
27
 
28
+ if message.channel.id != CHANNEL_ID and not isinstance(message.channel, discord.Thread):
29
+ return
 
 
 
 
 
30
 
31
+ # Generate image using Gradio client
32
+ try:
33
+ result = gradio_client.predict(
34
+ prompt=message.content,
35
+ seed=123,
36
+ randomize_seed=False,
37
+ width=1024,
38
+ height=576,
39
+ guidance_scale=5,
40
+ num_inference_steps=30,
41
+ api_name="/infer_t2i"
42
+ )
43
+
44
+ # Send the generated image back to the user
45
+ await message.channel.send(file=discord.File(result))
46
+ except Exception as e:
47
+ await message.channel.send(f"An error occurred: {str(e)}")
48
 
49
+ def run_web():
50
+ subprocess.run([sys.executable, "web.py"])
 
 
 
51
 
52
+ def run_discord_bot():
53
+ bot.run('MTIyODQyNTQxNDk0MzQ0MTEwNw.Gfd_ri.rrG_6-Sfp0FYvSIbv-zZ98dpHI-G_Fh9MFCzco')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
55
  if __name__ == "__main__":
56
+ # Start web.py in a separate process
57
+ web_process = multiprocessing.Process(target=run_web)
58
+ web_process.start()
59
 
60
+ # Run the Discord bot
61
+ run_discord_bot()
62
 
63
+ # Wait for the web process to finish (which it likely won't)
64
+ web_process.join()