fantos commited on
Commit
c80bb75
·
verified ·
1 Parent(s): 444fc48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # img_bot.py
2
  import discord, os, io, re, random, asyncio, logging, requests, replicate, subprocess
3
  from transformers import pipeline as transformers_pipeline
4
- from gradio_client import Client
5
 
6
  # ── 환경 변수 ────────────────────────────────────────────────
7
  TOKEN = os.getenv("DISCORD_TOKEN")
@@ -14,14 +14,14 @@ if not TOKEN or not CHANNEL_ID:
14
  if not REPL_TOKEN:
15
  raise RuntimeError("OPENAI_API_KEY 에 Replicate Personal Access Token 값을 넣어주세요.")
16
 
17
- os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # 구조 유지
18
 
19
  # ── Gradio 서버 ─────────────────────────────────────────────
20
  GRADIO_URL = "http://211.233.58.201:7896"
21
  GRADIO_API = "/generate_image"
22
  DUMMY_IMG = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
23
 
24
- # ── 번역 파이프라인(마리안, CPU) ────────────────────────────
25
  translator = transformers_pipeline(
26
  "translation",
27
  model="Helsinki-NLP/opus-mt-ko-en",
@@ -35,11 +35,10 @@ async def ko2en_async(text: str) -> str:
35
  return text
36
  loop = asyncio.get_running_loop()
37
  try:
38
- result = await loop.run_in_executor(
39
  None,
40
  lambda: translator(text, max_length=256, num_beams=1)[0]["translation_text"].strip()
41
  )
42
- return result
43
  except Exception as e:
44
  logging.warning(f"번역 실패, 원문 사용: {e}")
45
  return text
@@ -69,19 +68,21 @@ class ImageBot(discord.Client):
69
  if not prompt_raw:
70
  return
71
 
72
- prompt_en = await ko2en_async(prompt_raw) # ✔️ 비동기 번역
73
  await message.channel.typing()
74
 
75
- # ── Gradio 호출 (블로킹 → executor) ─────────────────
76
  def generate_image():
77
  client = Client(GRADIO_URL)
78
  return client.predict(
79
  prompt=prompt_en,
80
- width=768, height=768,
81
- guidance=3.5, inference_steps=30,
 
 
82
  seed=random.randint(0, 2**32 - 1),
83
  do_img2img=False,
84
- init_image={"url": DUMMY_IMG},
85
  image2image_strength=0.8,
86
  resize_img=True,
87
  api_name=GRADIO_API
@@ -106,8 +107,10 @@ class ImageBot(discord.Client):
106
  except Exception as e:
107
  logging.warning(f"이미지 처리 실패: {e}")
108
 
109
- await message.reply(files=files if files else None,
110
- content=None if files else "⚠️ 이미지를 전송할 수 없습니다.")
 
 
111
 
112
  # ── 실행 ────────────────────────────────────────────────────
113
  if __name__ == "__main__":
 
1
  # img_bot.py
2
  import discord, os, io, re, random, asyncio, logging, requests, replicate, subprocess
3
  from transformers import pipeline as transformers_pipeline
4
+ from gradio_client import Client, handle_file # ⬅️ handle_file 추가
5
 
6
  # ── 환경 변수 ────────────────────────────────────────────────
7
  TOKEN = os.getenv("DISCORD_TOKEN")
 
14
  if not REPL_TOKEN:
15
  raise RuntimeError("OPENAI_API_KEY 에 Replicate Personal Access Token 값을 넣어주세요.")
16
 
17
+ os.environ["REPLICATE_API_TOKEN"] = REPL_TOKEN # 구조 유지
18
 
19
  # ── Gradio 서버 ─────────────────────────────────────────────
20
  GRADIO_URL = "http://211.233.58.201:7896"
21
  GRADIO_API = "/generate_image"
22
  DUMMY_IMG = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
23
 
24
+ # ── 번역 파이프라인 (CPU) ───────────────────────────────────
25
  translator = transformers_pipeline(
26
  "translation",
27
  model="Helsinki-NLP/opus-mt-ko-en",
 
35
  return text
36
  loop = asyncio.get_running_loop()
37
  try:
38
+ return await loop.run_in_executor(
39
  None,
40
  lambda: translator(text, max_length=256, num_beams=1)[0]["translation_text"].strip()
41
  )
 
42
  except Exception as e:
43
  logging.warning(f"번역 실패, 원문 사용: {e}")
44
  return text
 
68
  if not prompt_raw:
69
  return
70
 
71
+ prompt_en = await ko2en_async(prompt_raw)
72
  await message.channel.typing()
73
 
74
+ # ── Gradio 호출 ────────────────────────────────────
75
  def generate_image():
76
  client = Client(GRADIO_URL)
77
  return client.predict(
78
  prompt=prompt_en,
79
+ width=768,
80
+ height=768,
81
+ guidance=3.5,
82
+ inference_steps=30,
83
  seed=random.randint(0, 2**32 - 1),
84
  do_img2img=False,
85
+ init_image=handle_file(DUMMY_IMG), # ⬅️ path 포함 dict
86
  image2image_strength=0.8,
87
  resize_img=True,
88
  api_name=GRADIO_API
 
107
  except Exception as e:
108
  logging.warning(f"이미지 처리 실패: {e}")
109
 
110
+ await message.reply(
111
+ files=files if files else None,
112
+ content=None if files else "⚠️ 이미지를 전송할 수 없습니다."
113
+ )
114
 
115
  # ── 실행 ────────────────────────────────────────────────────
116
  if __name__ == "__main__":