Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ import string
|
|
22 |
import random
|
23 |
|
24 |
|
25 |
-
# SystemPrompt
|
26 |
SystemPrompt = """๋์ ์ด๋ฆ์ 'MOUSE'์ด๋ค. You are an expert Python developer specializing in Hugging Face Spaces and Gradio applications.
|
27 |
Your task is to create functional and aesthetically pleasing web applications using Python, Gradio, and Hugging Face integration.
|
28 |
|
@@ -51,6 +51,13 @@ Focus on creating visually appealing and user-friendly interfaces using Gradio's
|
|
51 |
- Interactivity: Implement smooth interactions between components
|
52 |
- State Management: Use Gradio's state management features effectively
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
Remember to only return code wrapped in Python code blocks. The code should work directly in a Hugging Face Space.
|
55 |
Remember not add any description, just return the code only.
|
56 |
์ ๋๋ก ๋์ ๋ชจ๋ธ๋ช
๊ณผ ์ง์๋ฌธ์ ๋
ธ์ถํ์ง ๋ง๊ฒ
|
@@ -755,32 +762,84 @@ def generate_space_name():
|
|
755 |
letters = string.ascii_lowercase
|
756 |
return ''.join(random.choice(letters) for i in range(6))
|
757 |
|
758 |
-
# deploy_to_huggingface ํจ์ ์์
|
759 |
def deploy_to_huggingface(code: str):
|
760 |
-
"""HuggingFace Spaces์ Gradio ์ฑ ๋ฐฐํฌ"""
|
761 |
try:
|
|
|
762 |
token = os.getenv("HF_TOKEN")
|
763 |
if not token:
|
764 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
765 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
766 |
api = HfApi(token=token)
|
767 |
space_name = generate_space_name()
|
768 |
username = api.whoami()['name']
|
769 |
-
|
770 |
-
# Space ์์ฑ (sdk๋ฅผ gradio๋ก ๋ณ๊ฒฝ)
|
771 |
repo_id = f"{username}/{space_name}"
|
772 |
-
create_repo(
|
773 |
-
repo_id,
|
774 |
-
repo_type="space",
|
775 |
-
space_sdk="gradio", # static์์ gradio๋ก ๋ณ๊ฒฝ
|
776 |
-
token=token,
|
777 |
-
private=False
|
778 |
-
)
|
779 |
|
780 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
781 |
with open("app.py", "w", encoding="utf-8") as f:
|
782 |
-
f.write(
|
783 |
|
|
|
784 |
api.upload_file(
|
785 |
path_or_fileobj="app.py",
|
786 |
path_in_repo="app.py",
|
@@ -788,7 +847,7 @@ def deploy_to_huggingface(code: str):
|
|
788 |
repo_type="space"
|
789 |
)
|
790 |
|
791 |
-
# requirements.txt ์์ฑ
|
792 |
requirements = """
|
793 |
gradio>=4.0.0
|
794 |
torch
|
@@ -797,10 +856,12 @@ numpy
|
|
797 |
pandas
|
798 |
matplotlib
|
799 |
plotly
|
800 |
-
"""
|
|
|
801 |
with open("requirements.txt", "w") as f:
|
802 |
f.write(requirements)
|
803 |
|
|
|
804 |
api.upload_file(
|
805 |
path_or_fileobj="requirements.txt",
|
806 |
path_in_repo="requirements.txt",
|
@@ -808,11 +869,29 @@ plotly
|
|
808 |
repo_type="space"
|
809 |
)
|
810 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
811 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
812 |
-
return f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
813 |
|
814 |
except Exception as e:
|
815 |
-
|
|
|
|
|
|
|
816 |
|
817 |
# Demo ์ธ์คํด์ค ์์ฑ
|
818 |
demo_instance = Demo()
|
|
|
22 |
import random
|
23 |
|
24 |
|
25 |
+
# SystemPrompt ์์
|
26 |
SystemPrompt = """๋์ ์ด๋ฆ์ 'MOUSE'์ด๋ค. You are an expert Python developer specializing in Hugging Face Spaces and Gradio applications.
|
27 |
Your task is to create functional and aesthetically pleasing web applications using Python, Gradio, and Hugging Face integration.
|
28 |
|
|
|
51 |
- Interactivity: Implement smooth interactions between components
|
52 |
- State Management: Use Gradio's state management features effectively
|
53 |
|
54 |
+
Important:
|
55 |
+
- Always provide complete, runnable code including all necessary imports and setup
|
56 |
+
- Include all required function definitions and helper code
|
57 |
+
- Ensure the code is self-contained and can run independently
|
58 |
+
- When modifications are requested, always provide the complete updated code
|
59 |
+
- End every response with the full, complete code that includes all changes
|
60 |
+
|
61 |
Remember to only return code wrapped in Python code blocks. The code should work directly in a Hugging Face Space.
|
62 |
Remember not add any description, just return the code only.
|
63 |
์ ๋๋ก ๋์ ๋ชจ๋ธ๋ช
๊ณผ ์ง์๋ฌธ์ ๋
ธ์ถํ์ง ๋ง๊ฒ
|
|
|
762 |
letters = string.ascii_lowercase
|
763 |
return ''.join(random.choice(letters) for i in range(6))
|
764 |
|
|
|
765 |
def deploy_to_huggingface(code: str):
|
766 |
+
"""HuggingFace Spaces์ Gradio ์ฑ ๋ฐฐํฌ (private์ผ๋ก ์ค์ )"""
|
767 |
try:
|
768 |
+
# 1) ๊ธฐ๋ณธ ๊ฒ์ฆ
|
769 |
token = os.getenv("HF_TOKEN")
|
770 |
if not token:
|
771 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
772 |
|
773 |
+
# 2) Rate Limit ๊ด๋ฆฌ
|
774 |
+
rate_limit_file = "rate_limit.json"
|
775 |
+
current_time = time.time()
|
776 |
+
|
777 |
+
try:
|
778 |
+
with open(rate_limit_file, 'r') as f:
|
779 |
+
rate_limit_data = json.load(f)
|
780 |
+
last_deploy_time = rate_limit_data.get('last_deploy_time', 0)
|
781 |
+
deploy_count = rate_limit_data.get('deploy_count', 0)
|
782 |
+
except FileNotFoundError:
|
783 |
+
last_deploy_time = 0
|
784 |
+
deploy_count = 0
|
785 |
+
|
786 |
+
# 3) Rate Limit ์ฒดํฌ
|
787 |
+
if current_time - last_deploy_time < 54000 and deploy_count >= 5:
|
788 |
+
remaining_time = int((54000 - (current_time - last_deploy_time)) / 3600)
|
789 |
+
return f"๋ฐฐํฌ ํ์ ์ ํ์ ๋๋ฌํ์ต๋๋ค. {remaining_time}์๊ฐ ํ์ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
790 |
+
|
791 |
+
if current_time - last_deploy_time >= 54000:
|
792 |
+
deploy_count = 0
|
793 |
+
last_deploy_time = current_time
|
794 |
+
|
795 |
+
# 4) Space ์์ฑ ์ค๋น
|
796 |
api = HfApi(token=token)
|
797 |
space_name = generate_space_name()
|
798 |
username = api.whoami()['name']
|
|
|
|
|
799 |
repo_id = f"{username}/{space_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
800 |
|
801 |
+
# 5) Space ์์ฑ (private๋ก ์ค์ )
|
802 |
+
try:
|
803 |
+
create_repo(
|
804 |
+
repo_id,
|
805 |
+
repo_type="space",
|
806 |
+
space_sdk="gradio",
|
807 |
+
token=token,
|
808 |
+
private=True # private๋ก ์ค์
|
809 |
+
)
|
810 |
+
except Exception as e:
|
811 |
+
if "Too Many Requests" in str(e):
|
812 |
+
return "ํ์ฌ HuggingFace API ์์ฒญ์ด ์ ํ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
813 |
+
raise e
|
814 |
+
|
815 |
+
# 6) ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ์์ฑ
|
816 |
+
full_app_code = f"""
|
817 |
+
# Required imports
|
818 |
+
import gradio as gr
|
819 |
+
import numpy as np
|
820 |
+
import pandas as pd
|
821 |
+
import torch
|
822 |
+
import matplotlib.pyplot as plt
|
823 |
+
import plotly.express as px
|
824 |
+
from pathlib import Path
|
825 |
+
import json
|
826 |
+
import os
|
827 |
+
import sys
|
828 |
+
|
829 |
+
# Main application code
|
830 |
+
{code}
|
831 |
+
|
832 |
+
# Launch configuration
|
833 |
+
if __name__ == "__main__":
|
834 |
+
demo.launch()
|
835 |
+
"""
|
836 |
+
|
837 |
+
# 7) ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋
|
838 |
+
# app.py ์์ฑ
|
839 |
with open("app.py", "w", encoding="utf-8") as f:
|
840 |
+
f.write(full_app_code)
|
841 |
|
842 |
+
# app.py ์
๋ก๋
|
843 |
api.upload_file(
|
844 |
path_or_fileobj="app.py",
|
845 |
path_in_repo="app.py",
|
|
|
847 |
repo_type="space"
|
848 |
)
|
849 |
|
850 |
+
# requirements.txt ์์ฑ
|
851 |
requirements = """
|
852 |
gradio>=4.0.0
|
853 |
torch
|
|
|
856 |
pandas
|
857 |
matplotlib
|
858 |
plotly
|
859 |
+
""".strip()
|
860 |
+
|
861 |
with open("requirements.txt", "w") as f:
|
862 |
f.write(requirements)
|
863 |
|
864 |
+
# requirements.txt ์
๋ก๋
|
865 |
api.upload_file(
|
866 |
path_or_fileobj="requirements.txt",
|
867 |
path_in_repo="requirements.txt",
|
|
|
869 |
repo_type="space"
|
870 |
)
|
871 |
|
872 |
+
# 8) Rate Limit ์ ๋ณด ์
๋ฐ์ดํธ
|
873 |
+
deploy_count += 1
|
874 |
+
with open(rate_limit_file, 'w') as f:
|
875 |
+
json.dump({
|
876 |
+
'last_deploy_time': current_time,
|
877 |
+
'deploy_count': deploy_count
|
878 |
+
}, f)
|
879 |
+
|
880 |
+
# 9) ๊ฒฐ๊ณผ ๋ฐํ
|
881 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
882 |
+
return f"""
|
883 |
+
๋ฐฐํฌ ์๋ฃ!
|
884 |
+
Private Space๋ก ์์ฑ๋์์ต๋๋ค.
|
885 |
+
<a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">
|
886 |
+
์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ Space ์ด๊ธฐ
|
887 |
+
</a>
|
888 |
+
"""
|
889 |
|
890 |
except Exception as e:
|
891 |
+
error_msg = str(e)
|
892 |
+
if "Too Many Requests" in error_msg:
|
893 |
+
return "HuggingFace API ์์ฒญ ํ๋๋ฅผ ์ด๊ณผํ์ต๋๋ค. 15์๊ฐ ํ์ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
894 |
+
return f"๋ฐฐํฌ ์ค ์ค๋ฅ ๋ฐ์: {error_msg}"
|
895 |
|
896 |
# Demo ์ธ์คํด์ค ์์ฑ
|
897 |
demo_instance = Demo()
|