Download app.py from ssboost/B_G: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/spaces/ssboost/B_G/resolve/main/app.py
- Command line
-
hf download hf://spaces/ssboost/B_G/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ssboost/B_G/resolve/main/app.py
3.44 kB
| 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("์๋ชป๋ ๋น๋ฐ๋ฒํธ์ ๋๋ค.") | |
| # Replicate API ํ ํฐ ํ์ธ | |
| 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: | |
| # base64๋ก ๋ณํ | |
| 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 | |
| # Gradio ์ธํฐํ์ด์ค ์์ฑ | |
| 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="์ด๋ฏธ์ง ๋ค์ด๋ก๋" | |
| ) | |
| # click ์ (ํ์์ฉ, ๋ค์ด๋ก๋์ฉ) ๋ ๊ฐ ๋ฆฌํด | |
| process_btn.click( | |
| fn=process_image, | |
| inputs=[password_input, input_image], | |
| outputs=[output_image, download_btn] | |
| ) | |
| gr.Markdown("---") | |
| if __name__ == "__main__": | |
| demo.launch() |