Spaces:
Runtime error
Runtime error
import discord | |
import logging | |
import os | |
import uuid | |
import subprocess | |
import threading | |
import gradio as gr | |
from gradio_client import Client | |
# λ‘κΉ μ€μ | |
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 | |
# Gradio API ν΄λΌμ΄μΈνΈ μ€μ | |
client = Client("http://211.233.58.202:7960/") | |
# μ΄λ―Έμ§ μμ± ν¨μ | |
def generate_image(prompt): | |
# μ€μ μ΄λ―Έμ§ μμ± λ‘μ§μ ꡬνν©λλ€. | |
# μμλ‘, μ¬κΈ°μλ λ¨μν promptλ₯Ό μ΄λ―Έμ§μ νμνλ κ°μ§ μ΄λ―Έμ§ μμ±μ ν κ²μ λλ€. | |
return f"Image generated with prompt: {prompt}" | |
# Gradio μΈν°νμ΄μ€ μ€μ | |
iface = gr.Interface(fn=generate_image, inputs="text", outputs="image") | |
# Gradio μΈν°νμ΄μ€ μ€ν ν¨μ | |
def run_gradio(): | |
iface.launch(show_error=True) | |
# λμ€μ½λ λ΄ ν΄λμ€ | |
class MyClient(discord.Client): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.is_processing = False | |
async def on_ready(self): | |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!') | |
subprocess.Popen(["python", "web.py"]) | |
logging.info("web.py μλ²κ° μμλμμ΅λλ€.") | |
channel = self.get_channel(int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678"))) | |
await channel.send("μ λ μ΄λ―Έμ§ μμ±μ μνν μ μμΌλ©°, μμ±λ μ΄λ―Έμ§μ λν μ€λͺ μ νκΈλ‘ μ 곡νκ³ μνΈ λνλ₯Ό ν μ μμ΅λλ€. '!image <ν둬ννΈ>'λ₯Ό μ¬μ©νμ¬ μ΄λ―Έμ§λ₯Ό μμ²νμΈμ.") | |
async def on_message(self, message): | |
if message.author == self.user: | |
return | |
if message.content.startswith('!image '): | |
if self.is_processing: | |
await message.channel.send("μ΄λ―Έμ§ μμ±μ΄ μ΄λ―Έ μ§ν μ€μ λλ€. μ μλ§ κΈ°λ€λ € μ£ΌμΈμ.") | |
return | |
self.is_processing = True | |
try: | |
prompt = message.content[len('!image '):] | |
image_path = await self.generate_image(prompt) | |
user_id = message.author.id | |
await message.channel.send( | |
f"<@{user_id}> λμ΄ μμ²νμ μ΄λ―Έμ§μ λλ€.", | |
file=discord.File(image_path, 'generated_image.png') | |
) | |
await initiate_conversation(prompt, image_path, message) | |
except Exception as e: | |
logging.error(f'μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ: {e}') | |
await message.channel.send("μ΄λ―Έμ§ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€. λμ€μ λ€μ μλν΄ μ£ΌμΈμ.") | |
finally: | |
self.is_processing = False | |
async def generate_image(self, prompt): | |
if not prompt: | |
raise ValueError("Prompt is empty or None") | |
try: | |
result = client.predict( | |
prompt=prompt, | |
seed=123, | |
randomize_seed=False, | |
width=1024, | |
height=576, | |
guidance_scale=5, | |
num_inference_steps=28, | |
api_name="/infer_t2i" | |
) | |
logging.debug(f"API call result: {result}") | |
if 'path' in result: | |
image_path = result['path'] | |
logging.debug(f"Image successfully retrieved from {image_path}") | |
return image_path | |
else: | |
raise RuntimeError("μ΄λ―Έμ§ μμ± μ€ν¨: κ²°κ³Όκ° λΉμ΄ μκ±°λ κ²½λ‘κ° ν¬ν¨λμ§ μμμ΅λλ€.") | |
except Exception as e: | |
logging.error(f'μ΄λ―Έμ§ μμ± λμ€ μμΈ λ°μ: {e}') | |
raise RuntimeError(f"μ΄λ―Έμ§ μμ± λμ€ μμΈ λ°μ: {str(e)}") | |
async def initiate_conversation(prompt, image_path, message): | |
logging.debug(f'λν μμ μ€: {prompt}') | |
description = "μμ±λ μ΄λ―Έμ§μ λλ€." | |
logging.debug(f'μ΄λ―Έμ§ μ€λͺ : {description}') | |
await message.channel.send(f"μ΄λ―Έμ§ μ€λͺ : {description}") | |
await continue_conversation(prompt, message) | |
async def continue_conversation(prompt, message): | |
logging.debug(f'λν μ§μ μ€: {prompt}') | |
# κ³μ λν λ΄μ©μ λ°μ μνΈμμ©νλλ‘ κ΅¬ν | |
# λμ€μ½λ ν ν° λ° λ΄ μ€ν | |
if __name__ == "__main__": | |
discord_token = os.getenv('DISCORD_TOKEN') | |
discord_client = MyClient(intents=intents) | |
discord_client.run(discord_token) | |
# Gradio μΈν°νμ΄μ€ μ€μ | |
iface = gr.Interface(fn=my_model_function, inputs="text", outputs="image") | |
# μΈν°νμ΄μ€ μ€ν, μ€λ₯ 보μ¬μ£Όλ μ΅μ νμ±ν | |
iface.launch(show_error=True) |