Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import sys | |
| from pathlib import Path | |
| # repo_to_space_auto ๋ชจ๋ ๋์ ์ํฌํธ | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| from repo_to_space_auto import deploy | |
| # OAuth ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ | |
| OAUTH_ENABLED = os.getenv("OAUTH_CLIENT_ID") is not None | |
| def launch_deploy_oauth(repo_url, private, profile: gr.OAuthProfile | None): | |
| """OAuth ์ธ์ฆ ๋ฒ์ """ | |
| if not profile: | |
| return """### โ ๋ก๊ทธ์ธ ํ์ | |
| ์๋จ์ 'Sign in with Hugging Face' ๋ฒํผ์ ํด๋ฆญํด ๋ก๊ทธ์ธํด์ฃผ์ธ์.""" | |
| hf_token = profile.token | |
| # ํ ํฐ ์ ๋ฆฌ (OAuth์์๋ ์์ ํ๊ฒ) | |
| if hf_token: | |
| hf_token = hf_token.strip() | |
| return _deploy_space(repo_url, hf_token, private) | |
| def launch_deploy_manual(repo_url, hf_token, private): | |
| """์๋ ํ ํฐ ์ ๋ ฅ ๋ฒ์ """ | |
| if not hf_token: | |
| return """### โ ํ ํฐ ํ์ | |
| Hugging Face Write ํ ํฐ์ ์ ๋ ฅํด์ฃผ์ธ์.""" | |
| # ํ ํฐ ์ ๋ฆฌ (๊ณต๋ฐฑ, ์ค๋ฐ๊ฟ ์ ๊ฑฐ) | |
| hf_token = hf_token.strip() | |
| # ํ ํฐ ํ์ ๊ฒ์ฆ | |
| if not hf_token.startswith("hf_"): | |
| return """### โ ์๋ชป๋ ํ ํฐ ํ์ | |
| ํ ํฐ์ 'hf_'๋ก ์์ํด์ผ ํฉ๋๋ค. | |
| https://huggingface.co/settings/tokens ์์ Write ๊ถํ ํ ํฐ์ ์์ฑํด์ฃผ์ธ์.""" | |
| return _deploy_space(repo_url, hf_token, private) | |
| def _deploy_space(repo_url, hf_token, private): | |
| """๊ณตํต ๋ฐฐํฌ ๋ก์ง""" | |
| # URL ์ ๋ฆฌ | |
| repo_url = repo_url.strip() | |
| # URL ๊ฒ์ฆ | |
| if not repo_url.startswith(("https://github.com/", "http://github.com/", "git@github.com:")): | |
| return """### โ ์๋ชป๋ Repository URL | |
| GitHub URL ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. | |
| ์์: https://github.com/username/repository""" | |
| # ํ๊ฒฝ๋ณ์ ๋์ ์ง์ ํ ํฐ ์ ๋ฌ | |
| try: | |
| link = deploy(repo_url, hf_token, private=private) | |
| return f"""### โ Space ์์ฑ ์๋ฃ | |
| ๐ ์ฑ๊ณต์ ์ผ๋ก ๋ฐฐํฌ๋์์ต๋๋ค! | |
| **Space URL:** [{link}]({link}) | |
| --- | |
| *๋ฐฐํฌ๋ Space๊ฐ ์ ๋๋ก ์๋ํ๋์ง ํ์ธํด๋ณด์ธ์.*""" | |
| except Exception as e: | |
| error_msg = str(e) | |
| # ํน์ ์ค๋ฅ์ ๋ํ ์์ธ ์๋ด | |
| if "BAPI_TOKEN" in error_msg: | |
| return """### โ Brave Search API ํ ํฐ ํ์ | |
| Space Settings > Variables and secrets์์ `BAPI_TOKEN`์ ์ค์ ํด์ฃผ์ธ์. | |
| [Brave Search API](https://brave.com/search/api/) ์์ ๋ฌด๋ฃ ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ ์์ต๋๋ค.""" | |
| elif "FRIENDLI_TOKEN" in error_msg: | |
| return """### โ Friendli AI ํ ํฐ ํ์ | |
| Space Settings > Variables and secrets์์ `FRIENDLI_TOKEN`์ ์ค์ ํด์ฃผ์ธ์. | |
| [Friendli AI](https://friendli.ai/) ์์ API ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ ์์ต๋๋ค.""" | |
| elif "Invalid header value" in error_msg: | |
| return """### โ ํ ํฐ ์ค๋ฅ | |
| ํ ํฐ์ ์๋ชป๋ ๋ฌธ์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. | |
| ํ ํฐ์ ๋ค์ ๋ณต์ฌํ์ฌ ๊ณต๋ฐฑ์ด๋ ์ค๋ฐ๊ฟ ์์ด ๋ถ์ฌ๋ฃ์ด์ฃผ์ธ์.""" | |
| elif "401" in error_msg or "403" in error_msg: | |
| return """### โ ์ธ์ฆ ์คํจ | |
| ํ ํฐ์ด ์ ํจํ์ง ์๊ฑฐ๋ Write ๊ถํ์ด ์์ต๋๋ค. | |
| https://huggingface.co/settings/tokens ์์ Write ๊ถํ์ด ์๋ ํ ํฐ์ธ์ง ํ์ธํด์ฃผ์ธ์.""" | |
| else: | |
| return f"""### โ ๋ฐฐํฌ ์ค๋ฅ | |
| ์ค๋ฅ ๋ด์ฉ: `{error_msg}` | |
| **๊ฐ๋ฅํ ์์ธ:** | |
| - ์๋ชป๋ Repository URL | |
| - ๊ถํ ๋ถ์กฑ (write ๊ถํ ํ์) | |
| - ๋คํธ์ํฌ ๋ฌธ์ """ | |
| # Gradio ์ฑ ๊ตฌ์ฑ | |
| with gr.Blocks( | |
| title="HF Space Auto-Deployer", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .main-container { max-width: 800px; margin: 0 auto; } | |
| .gr-button-primary { background-color: #2563eb !important; } | |
| .status-box { padding: 20px; border-radius: 8px; margin-top: 20px; } | |
| """ | |
| ) as demo: | |
| gr.Markdown(""" | |
| # ๐ Git Repository โ Hugging Face Space ์๋ ๋ฐฐํฌ | |
| ๊ณต๊ฐ Git ์ ์ฅ์๋ฅผ Hugging Face Gradio Space๋ก ์๋ ๋ณํํ๊ณ ๋ฐฐํฌํฉ๋๋ค. | |
| ## ์ฌ์ฉ ๋ฐฉ๋ฒ | |
| 1. **์ธ์ฆ**: OAuth ๋ก๊ทธ์ธ ๋๋ ํ ํฐ ์ ๋ ฅ | |
| 2. **Repository URL ์ ๋ ฅ** | |
| 3. **Deploy ๋ฒํผ ํด๋ฆญ** | |
| --- | |
| """) | |
| with gr.Column(elem_classes="main-container"): | |
| if OAUTH_ENABLED: | |
| # OAuth ๋ฒ์ | |
| gr.LoginButton( | |
| value="Sign in with Hugging Face ๐ค", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| gr.Markdown("### ๐ ๋ฐฐํฌ ์ค์ ") | |
| repo_input = gr.Textbox( | |
| label="Git Repository URL", | |
| placeholder="https://github.com/username/repository", | |
| info="๊ณต๊ฐ ์ ์ฅ์ URL์ ์ ๋ ฅํ์ธ์" | |
| ) | |
| private_checkbox = gr.Checkbox( | |
| label="๐ Private Space๋ก ์์ฑ", | |
| value=False, | |
| info="์ฒดํฌํ๋ฉด ๋น๊ณต๊ฐ Space๋ก ์์ฑ๋ฉ๋๋ค" | |
| ) | |
| deploy_btn = gr.Button( | |
| "๐ Deploy to Space", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| output_status = gr.Markdown( | |
| elem_classes="status-box", | |
| visible=False | |
| ) | |
| # OAuth ์ด๋ฒคํธ ํธ๋ค๋ฌ | |
| deploy_btn.click( | |
| fn=launch_deploy_oauth, | |
| inputs=[repo_input, private_checkbox], | |
| outputs=output_status | |
| ).then( | |
| fn=lambda: gr.update(visible=True), | |
| outputs=output_status | |
| ) | |
| else: | |
| # ์๋ ํ ํฐ ์ ๋ ฅ ๋ฒ์ | |
| gr.Markdown(""" | |
| ### ๐ ์ธ์ฆ ์ ๋ณด | |
| > **์ฐธ๊ณ **: OAuth๊ฐ ์ค์ ๋์ง ์์ Space์ ๋๋ค. | |
| > Space ์ค์ ์์ `hf_oauth: true`๋ฅผ ์ถ๊ฐํ๋ฉด ์๋ ๋ก๊ทธ์ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. | |
| """) | |
| token_input = gr.Textbox( | |
| label="Hugging Face Write Token", | |
| type="password", | |
| placeholder="hf_xxxxxxxxxxxxxxxxxxxxx", | |
| info="https://huggingface.co/settings/tokens ์์ Write ๊ถํ ํ ํฐ ์์ฑ" | |
| ) | |
| gr.Markdown("### ๐ ๋ฐฐํฌ ์ค์ ") | |
| repo_input = gr.Textbox( | |
| label="Git Repository URL", | |
| placeholder="https://github.com/username/repository", | |
| info="๊ณต๊ฐ ์ ์ฅ์ URL์ ์ ๋ ฅํ์ธ์" | |
| ) | |
| private_checkbox = gr.Checkbox( | |
| label="๐ Private Space๋ก ์์ฑ", | |
| value=False, | |
| info="์ฒดํฌํ๋ฉด ๋น๊ณต๊ฐ Space๋ก ์์ฑ๋ฉ๋๋ค" | |
| ) | |
| deploy_btn = gr.Button( | |
| "๐ Deploy to Space", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| output_status = gr.Markdown( | |
| elem_classes="status-box", | |
| visible=False | |
| ) | |
| # ์๋ ์ ๋ ฅ ์ด๋ฒคํธ ํธ๋ค๋ฌ | |
| deploy_btn.click( | |
| fn=launch_deploy_manual, | |
| inputs=[repo_input, token_input, private_checkbox], | |
| outputs=output_status | |
| ).then( | |
| fn=lambda: gr.update(visible=True), | |
| outputs=output_status | |
| ) | |
| # ๊ณตํต ์์ ๋ฐ ๋์๋ง | |
| gr.Markdown(""" | |
| ### ๐ก ์์ | |
| - `https://github.com/gradio-app/gradio` | |
| - `https://github.com/huggingface/transformers` | |
| ### โ ๏ธ ์ฃผ์์ฌํญ | |
| - **ํ๊ฒฝ๋ณ์ ํ์**: `BAPI_TOKEN` (Brave Search API), `FRIENDLI_TOKEN` (Friendli AI) | |
| - Space ์ด๋ฆ์ repository ์ด๋ฆ์ ๊ธฐ๋ฐ์ผ๋ก ์๋ ์์ฑ๋ฉ๋๋ค | |
| - GPU๊ฐ ํ์ํ ๊ฒฝ์ฐ Space ์์ฑ ํ ์๋ ์ค์ ์ด ํ์ํฉ๋๋ค | |
| """) | |
| if __name__ == "__main__": | |
| # ์์ ์ ํ๊ฒฝ ์ฒดํฌ | |
| print("\n" + "="*50) | |
| print("๐ HF Space Auto-Deployer ์์") | |
| print("="*50) | |
| # ํ์ ํ๊ฒฝ๋ณ์ ์ฒดํฌ | |
| missing_vars = [] | |
| if not os.getenv("BAPI_TOKEN"): | |
| missing_vars.append("BAPI_TOKEN (Brave Search API)") | |
| if not os.getenv("FRIENDLI_TOKEN"): | |
| missing_vars.append("FRIENDLI_TOKEN (Friendli AI)") | |
| if missing_vars: | |
| print("\nโ ๏ธ ๋ค์ ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค:") | |
| for var in missing_vars: | |
| print(f" - {var}") | |
| print("\nSpace Settings > Variables and secrets์์ ์ค์ ํด์ฃผ์ธ์.") | |
| else: | |
| print("โ ๋ชจ๋ ํ์ ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์์ต๋๋ค.") | |
| # OAuth ์ํ ํ์ธ | |
| if OAUTH_ENABLED: | |
| print("โ OAuth๊ฐ ํ์ฑํ๋์์ต๋๋ค.") | |
| else: | |
| print("โน๏ธ OAuth๊ฐ ๋นํ์ฑํ๋์์ต๋๋ค. (์๋ ํ ํฐ ์ ๋ ฅ ๋ชจ๋)") | |
| print("="*50 + "\n") | |
| demo.launch( | |
| ssr_mode=False, # SSR ๋นํ์ฑํ๋ก ๊ฒฝ๊ณ ์ ๊ฑฐ | |
| show_error=True, | |
| quiet=True # Gradio ๊ธฐ๋ณธ ์ถ๋ ฅ ์ต์ํ | |
| ) |