Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -3,18 +3,22 @@ import logging | |
| 3 | 
             
            import os
         | 
| 4 | 
             
            from huggingface_hub import InferenceClient
         | 
| 5 | 
             
            import asyncio
         | 
| 6 | 
            -
            import subprocess | 
|  | |
|  | |
| 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 | 
             
            intents.messages = True
         | 
|  | |
|  | |
| 15 |  | 
| 16 | 
             
            # ์ถ๋ก  API ํด๋ผ์ด์ธํธ ์ค์ 
         | 
| 17 | 
            -
            hf_client = InferenceClient(" | 
| 18 |  | 
| 19 | 
             
            # ํน์  ์ฑ๋ ID
         | 
| 20 | 
             
            SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
         | 
| @@ -28,13 +32,12 @@ class MyClient(discord.Client): | |
| 28 | 
             
                    self.is_processing = False
         | 
| 29 |  | 
| 30 | 
             
                async def on_ready(self):
         | 
| 31 | 
            -
                    logging.info(f'{self.user} | 
| 32 | 
            -
                    # web.py | 
| 33 | 
            -
                    subprocess.Popen(["python", "web.py"])
         | 
| 34 | 
             
                    logging.info("Web.py server has been started.")
         | 
| 35 |  | 
| 36 | 
             
                async def on_message(self, message):
         | 
| 37 | 
            -
                    if message.author == self.user:
         | 
| 38 | 
             
                        return
         | 
| 39 | 
             
                    if message.channel.id != SPECIFIC_CHANNEL_ID:
         | 
| 40 | 
             
                        return
         | 
| @@ -42,43 +45,24 @@ class MyClient(discord.Client): | |
| 42 | 
             
                        return
         | 
| 43 | 
             
                    self.is_processing = True
         | 
| 44 | 
             
                    try:
         | 
| 45 | 
            -
                         | 
| 46 | 
            -
                        await message.channel | 
| 47 | 
             
                    finally:
         | 
| 48 | 
             
                        self.is_processing = False
         | 
| 49 |  | 
| 50 | 
            -
            async def  | 
| 51 | 
            -
                 | 
| 52 | 
            -
                 | 
| 53 | 
            -
                 | 
| 54 | 
            -
                 | 
| 55 | 
            -
                 | 
| 56 | 
            -
                 | 
| 57 | 
            -
                 | 
| 58 | 
            -
                ๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค.
         | 
| 59 | 
            -
                """
         | 
| 60 | 
            -
                global conversation_history
         | 
| 61 | 
            -
                conversation_history.append({"role": "user", "content": user_input})
         | 
| 62 | 
            -
                logging.debug(f'Conversation history updated: {conversation_history}')
         | 
| 63 |  | 
| 64 | 
            -
             | 
| 65 | 
            -
                 | 
| 66 | 
            -
             | 
| 67 | 
            -
                 | 
| 68 | 
            -
                response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
         | 
| 69 | 
            -
                    messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
         | 
| 70 | 
            -
             | 
| 71 | 
            -
                full_response = []
         | 
| 72 | 
            -
                for part in response:
         | 
| 73 | 
            -
                    logging.debug(f'Part received from stream: {part}')  # ์คํธ๋ฆฌ๋ฐ ์๋ต์ ๊ฐ ํํธ ๋ก๊น
         | 
| 74 | 
            -
                    if part.choices and part.choices[0].delta and part.choices[0].delta.content:
         | 
| 75 | 
            -
                        full_response.append(part.choices[0].delta.content)
         | 
| 76 | 
            -
             | 
| 77 | 
            -
                full_response_text = ''.join(full_response)
         | 
| 78 | 
            -
                logging.debug(f'Full model response: {full_response_text}')
         | 
| 79 | 
            -
             | 
| 80 | 
            -
                conversation_history.append({"role": "assistant", "content": full_response_text})
         | 
| 81 | 
            -
                return full_response_text
         | 
| 82 |  | 
| 83 | 
             
            if __name__ == "__main__":
         | 
| 84 | 
             
                discord_client = MyClient(intents=intents)
         | 
|  | |
| 3 | 
             
            import os
         | 
| 4 | 
             
            from huggingface_hub import InferenceClient
         | 
| 5 | 
             
            import asyncio
         | 
| 6 | 
            +
            import subprocess
         | 
| 7 | 
            +
            from PIL import Image
         | 
| 8 | 
            +
            import io
         | 
| 9 |  | 
| 10 | 
             
            # ๋ก๊น
 ์ค์ 
         | 
| 11 | 
             
            logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
         | 
| 12 |  | 
| 13 | 
             
            # ์ธํ
ํธ ์ค์ 
         | 
| 14 | 
             
            intents = discord.Intents.default()
         | 
| 15 | 
            +
            intents.message_content = True
         | 
| 16 | 
             
            intents.messages = True
         | 
| 17 | 
            +
            intents.guilds = True
         | 
| 18 | 
            +
            intents.guild_messages = True
         | 
| 19 |  | 
| 20 | 
             
            # ์ถ๋ก  API ํด๋ผ์ด์ธํธ ์ค์ 
         | 
| 21 | 
            +
            hf_client = InferenceClient("stabilityai/stable-diffusion-3-medium")
         | 
| 22 |  | 
| 23 | 
             
            # ํน์  ์ฑ๋ ID
         | 
| 24 | 
             
            SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
         | 
|  | |
| 32 | 
             
                    self.is_processing = False
         | 
| 33 |  | 
| 34 | 
             
                async def on_ready(self):
         | 
| 35 | 
            +
                    logging.info(f'{self.user} has logged in!')
         | 
| 36 | 
            +
                    subprocess.Popen(["python", "web.py"])  # Start the web.py server as a separate process
         | 
|  | |
| 37 | 
             
                    logging.info("Web.py server has been started.")
         | 
| 38 |  | 
| 39 | 
             
                async def on_message(self, message):
         | 
| 40 | 
            +
                    if message.author == self.user or not message.content:
         | 
| 41 | 
             
                        return
         | 
| 42 | 
             
                    if message.channel.id != SPECIFIC_CHANNEL_ID:
         | 
| 43 | 
             
                        return
         | 
|  | |
| 45 | 
             
                        return
         | 
| 46 | 
             
                    self.is_processing = True
         | 
| 47 | 
             
                    try:
         | 
| 48 | 
            +
                        image_path = await generate_image(message.content)
         | 
| 49 | 
            +
                        await send_image(message.channel, image_path)
         | 
| 50 | 
             
                    finally:
         | 
| 51 | 
             
                        self.is_processing = False
         | 
| 52 |  | 
| 53 | 
            +
            async def generate_image(prompt):
         | 
| 54 | 
            +
                """Generate an image using the Stable Diffusion model."""
         | 
| 55 | 
            +
                response = hf_client(text=prompt)
         | 
| 56 | 
            +
                image_data = response['image'][0]  # Assuming the response contains image data
         | 
| 57 | 
            +
                image_bytes = io.BytesIO(image_data)
         | 
| 58 | 
            +
                image = Image.open(image_bytes)
         | 
| 59 | 
            +
                image.save("output.png")
         | 
| 60 | 
            +
                return "output.png"
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 61 |  | 
| 62 | 
            +
            async def send_image(channel, image_path):
         | 
| 63 | 
            +
                """Send an image to the specified Discord channel."""
         | 
| 64 | 
            +
                file = discord.File(image_path)
         | 
| 65 | 
            +
                await channel.send(file=file)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 66 |  | 
| 67 | 
             
            if __name__ == "__main__":
         | 
| 68 | 
             
                discord_client = MyClient(intents=intents)
         | 
 
			
