seawolf2357 commited on
Commit
f2ced47
·
verified ·
1 Parent(s): 5e3e01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import discord
2
  import logging
3
  import os
4
- import subprocess
5
  from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor
6
  import torch
7
  import re
8
  import requests
9
  from PIL import Image
10
  import io
11
- import asyncio
 
12
 
13
  # 로깅 설정
14
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -55,6 +55,45 @@ async def create_captions_rich(image: Image.Image) -> str:
55
  modified_caption = modify_caption(decoded)
56
  return modified_caption
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # 특정 채널 ID 설정
59
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")) # 환경 변수 또는 직접 설정
60
 
@@ -66,11 +105,8 @@ class MyClient(discord.Client):
66
 
67
  async def on_ready(self):
68
  logging.info(f'{self.user}로 로그인되었습니다!')
69
- asyncio.create_task(self.start_gradio_server())
70
- logging.info("Web.py 서버가 시작되었습니다.")
71
-
72
- async def start_gradio_server(self):
73
- subprocess.run(["python", "web.py"], check=True)
74
 
75
  async def on_message(self, message):
76
  if message.author == self.user:
@@ -106,3 +142,6 @@ async def download_image(url):
106
  if __name__ == "__main__":
107
  discord_client = MyClient(intents=intents)
108
  discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
 
 
1
  import discord
2
  import logging
3
  import os
 
4
  from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor
5
  import torch
6
  import re
7
  import requests
8
  from PIL import Image
9
  import io
10
+ import gradio as gr
11
+ import threading
12
 
13
  # 로깅 설정
14
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
55
  modified_caption = modify_caption(decoded)
56
  return modified_caption
57
 
58
+ # Gradio 인터페이스 설정
59
+ def create_captions_rich_sync(image):
60
+ return asyncio.run(create_captions_rich(image))
61
+
62
+ css = """
63
+ #mkd {
64
+ height: 500px;
65
+ overflow: auto;
66
+ border: 1px solid #ccc;
67
+ }
68
+ """
69
+
70
+ with gr.Blocks(css=css) as demo:
71
+ gr.HTML("<h1><center>PaliGemma Fine-tuned for Long Captioning<center><h1>")
72
+ with gr.Tab(label="PaliGemma Long Captioner"):
73
+ with gr.Row():
74
+ with gr.Column():
75
+ input_img = gr.Image(label="Input Picture")
76
+ submit_btn = gr.Button(value="Submit")
77
+ output = gr.Text(label="Caption")
78
+
79
+ gr.Examples(
80
+ [["image1.jpg"], ["image2.jpg"], ["image3.png"], ["image4.jpg"], ["image5.jpg"], ["image6.PNG"]],
81
+ inputs=[input_img],
82
+ outputs=[output],
83
+ fn=create_captions_rich_sync,
84
+ label='Try captioning on examples'
85
+ )
86
+
87
+ submit_btn.click(create_captions_rich_sync, [input_img], [output])
88
+
89
+ # Gradio 서버를 비동기적으로 실행
90
+ def run_gradio():
91
+ demo.launch(
92
+ server_name="0.0.0.0",
93
+ server_port=int(os.getenv("GRADIO_SERVER_PORT", 7861)),
94
+ inbrowser=True
95
+ )
96
+
97
  # 특정 채널 ID 설정
98
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID", "123456789012345678")) # 환경 변수 또는 직접 설정
99
 
 
105
 
106
  async def on_ready(self):
107
  logging.info(f'{self.user}로 로그인되었습니다!')
108
+ threading.Thread(target=run_gradio, daemon=True).start()
109
+ logging.info("Gradio 서버가 시작되었습니다.")
 
 
 
110
 
111
  async def on_message(self, message):
112
  if message.author == self.user:
 
142
  if __name__ == "__main__":
143
  discord_client = MyClient(intents=intents)
144
  discord_client.run(os.getenv('DISCORD_TOKEN'))
145
+
146
+ discord_client = MyClient(intents=intents)
147
+ discord_client.run(os.getenv('DISCORD_TOKEN'))