Rename ㅁ to app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import replicate
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import tempfile
|
| 6 |
+
import logging
|
| 7 |
+
import base64
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(
|
| 10 |
+
level=logging.DEBUG,
|
| 11 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 12 |
+
)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
def process_image(password, input_image):
|
| 16 |
+
# 환경변수에서 비밀번호 가져오기
|
| 17 |
+
correct_password = os.getenv("APP_PASSWORD")
|
| 18 |
+
if not correct_password:
|
| 19 |
+
logger.error("APP_PASSWORD 환경변수가 설정되지 않았습니다.")
|
| 20 |
+
raise ValueError("서버 설정 오류입니다.")
|
| 21 |
+
|
| 22 |
+
# 비밀번호 검증
|
| 23 |
+
if password != correct_password:
|
| 24 |
+
raise ValueError("잘못된 비밀번호입니다.")
|
| 25 |
+
|
| 26 |
+
# Replicate API 토큰 확인
|
| 27 |
+
if not os.getenv("REPLICATE_API_TOKEN"):
|
| 28 |
+
logger.error("REPLICATE_API_TOKEN이 설정되지 않았습니다.")
|
| 29 |
+
return None, None
|
| 30 |
+
|
| 31 |
+
# 환경변수에서 모델 이름 가져오기
|
| 32 |
+
model_name = os.getenv("REPLICATE_MODEL")
|
| 33 |
+
if not model_name:
|
| 34 |
+
logger.error("REPLICATE_MODEL 환경변수가 설정되지 않았습니다.")
|
| 35 |
+
return None, None
|
| 36 |
+
|
| 37 |
+
if input_image is None:
|
| 38 |
+
logger.error("입력 이미지가 없습니다.")
|
| 39 |
+
return None, None
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# base64로 변환
|
| 43 |
+
with open(input_image, "rb") as f:
|
| 44 |
+
data = base64.b64encode(f.read()).decode()
|
| 45 |
+
|
| 46 |
+
image_uri = f"data:image/png;base64,{data}"
|
| 47 |
+
|
| 48 |
+
# 환경변수에서 가져온 모델로 실행
|
| 49 |
+
output = replicate.run(
|
| 50 |
+
model_name,
|
| 51 |
+
input={"image": image_uri}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 결과 저장
|
| 55 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 56 |
+
if hasattr(output, "read"):
|
| 57 |
+
tmp.write(output.read())
|
| 58 |
+
elif isinstance(output, (list, tuple)) and output:
|
| 59 |
+
resp = requests.get(output[0])
|
| 60 |
+
resp.raise_for_status()
|
| 61 |
+
tmp.write(resp.content)
|
| 62 |
+
|
| 63 |
+
out_path = tmp.name
|
| 64 |
+
|
| 65 |
+
return out_path, out_path
|
| 66 |
+
|
| 67 |
+
except replicate.exceptions.ReplicateError as re:
|
| 68 |
+
logger.error(f"API 오류: {re}")
|
| 69 |
+
return None, None
|
| 70 |
+
except Exception as e:
|
| 71 |
+
logger.error(f"예상치 못한 오류: {e}")
|
| 72 |
+
return None, None
|
| 73 |
+
|
| 74 |
+
# Gradio 인터페이스 생성
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column():
|
| 78 |
+
password_input = gr.Textbox(
|
| 79 |
+
label="비밀번호",
|
| 80 |
+
type="password",
|
| 81 |
+
placeholder="비밀번호를 입력하세요"
|
| 82 |
+
)
|
| 83 |
+
input_image = gr.Image(
|
| 84 |
+
type="filepath",
|
| 85 |
+
label="입력 이미지",
|
| 86 |
+
interactive=True
|
| 87 |
+
)
|
| 88 |
+
process_btn = gr.Button("실행", variant="primary")
|
| 89 |
+
|
| 90 |
+
with gr.Column():
|
| 91 |
+
output_image = gr.Image(
|
| 92 |
+
type="filepath",
|
| 93 |
+
label="결과 이미지",
|
| 94 |
+
interactive=False
|
| 95 |
+
)
|
| 96 |
+
download_btn = gr.DownloadButton(
|
| 97 |
+
label="이미지 다운로드"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# click 시 (표시용, 다운로드용) 두 개 리턴
|
| 101 |
+
process_btn.click(
|
| 102 |
+
fn=process_image,
|
| 103 |
+
inputs=[password_input, input_image],
|
| 104 |
+
outputs=[output_image, download_btn]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
gr.Markdown("---")
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|
ㅁ
DELETED
|
File without changes
|