| import gradio as gr |
| import replicate |
| import os |
| import requests |
| import tempfile |
| import logging |
| import base64 |
|
|
| logging.basicConfig( |
| level=logging.DEBUG, |
| format='%(asctime)s - %(levelname)s - %(message)s' |
| ) |
| logger = logging.getLogger(__name__) |
|
|
| def process_image(password, input_image): |
| |
| correct_password = os.getenv("APP_PASSWORD") |
| if not correct_password: |
| logger.error("APP_PASSWORD ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") |
| raise ValueError("์๋ฒ ์ค์ ์ค๋ฅ์
๋๋ค.") |
| |
| |
| if password != correct_password: |
| raise ValueError("์๋ชป๋ ๋น๋ฐ๋ฒํธ์
๋๋ค.") |
| |
| |
| if not os.getenv("REPLICATE_API_TOKEN"): |
| logger.error("REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค.") |
| return None, None |
| |
| |
| model_name = os.getenv("REPLICATE_MODEL") |
| if not model_name: |
| logger.error("REPLICATE_MODEL ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") |
| return None, None |
| |
| if input_image is None: |
| logger.error("์
๋ ฅ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค.") |
| return None, None |
| |
| try: |
| |
| with open(input_image, "rb") as f: |
| data = base64.b64encode(f.read()).decode() |
| |
| image_uri = f"data:image/png;base64,{data}" |
| |
| |
| output = replicate.run( |
| model_name, |
| input={"image": image_uri} |
| ) |
| |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp: |
| if hasattr(output, "read"): |
| tmp.write(output.read()) |
| elif isinstance(output, (list, tuple)) and output: |
| resp = requests.get(output[0]) |
| resp.raise_for_status() |
| tmp.write(resp.content) |
| |
| out_path = tmp.name |
| |
| return out_path, out_path |
| |
| except replicate.exceptions.ReplicateError as re: |
| logger.error(f"API ์ค๋ฅ: {re}") |
| return None, None |
| except Exception as e: |
| logger.error(f"์์์น ๋ชปํ ์ค๋ฅ: {e}") |
| return None, None |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| with gr.Row(): |
| with gr.Column(): |
| password_input = gr.Textbox( |
| label="๋น๋ฐ๋ฒํธ", |
| type="password", |
| placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์" |
| ) |
| input_image = gr.Image( |
| type="filepath", |
| label="์
๋ ฅ ์ด๋ฏธ์ง", |
| interactive=True |
| ) |
| process_btn = gr.Button("์คํ", variant="primary") |
| |
| with gr.Column(): |
| output_image = gr.Image( |
| type="filepath", |
| label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง", |
| interactive=False |
| ) |
| download_btn = gr.DownloadButton( |
| label="์ด๋ฏธ์ง ๋ค์ด๋ก๋" |
| ) |
| |
| |
| process_btn.click( |
| fn=process_image, |
| inputs=[password_input, input_image], |
| outputs=[output_image, download_btn] |
| ) |
| |
| gr.Markdown("---") |
|
|
| if __name__ == "__main__": |
| demo.launch() |