update for streamlit deploy
Browse files1357 unmodified lines13581359136013611362136313641365136613671368136913701371137213731374137513761357 unmodified lines# Save code to a temporary fileif sdk == "static":file_name = "index.html"else:file_name = "app.py"with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:f.write(code)temp_path = f.name# Upload the filetry:api.upload_file(path_or_fileobj=temp_path,path_in_repo=file_name,repo_id=repo_id,repo_type="space")space_url = f"https://huggingface.co/spaces/{repo_id}"return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)except Exception as e:1357 unmodified lines135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311357 unmodified lines# Save code to a temporary fileif sdk == "static":file_name = "index.html"with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:f.write(code)temp_path = f.namefiles_to_upload = [(temp_path, file_name)]elif sdk == "docker": # Streamlit (Python)import shutilimport osimport tempfile# Create a temp dir for the structuretemp_dir = tempfile.mkdtemp()# 1. Write requirements.txtreqs = "altair\npandas\nstreamlit\n"req_path = os.path.join(temp_dir, "requirements.txt")with open(req_path, "w") as f:f.write(reqs)# 2. Write Dockerfiledockerfile_content = '''FROM python:3.9-slimWORKDIR /appRUN apt-get update && apt-get install -y \build-essential \curl \software-properties-common \git \&& rm -rf /var/lib/apt/lists/*COPY requirements.txt ./COPY src/ ./src/RUN pip3 install -r requirements.txtEXPOSE 8501HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/healthENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]'''dockerfile_path = os.path.join(temp_dir, "Dockerfile")with open(dockerfile_path, "w") as f:f.write(dockerfile_content)# 3. Write src/streamlit_app.pysrc_dir = os.path.join(temp_dir, "src")os.makedirs(src_dir, exist_ok=True)app_py_path = os.path.join(src_dir, "streamlit_app.py")with open(app_py_path, "w") as f:f.write(code)# Prepare files to uploadfiles_to_upload = [(req_path, "requirements.txt"),(dockerfile_path, "Dockerfile"),(app_py_path, "src/streamlit_app.py"),]else:file_name = "app.py"with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)temp_path = f.namefiles_to_upload = [(temp_path, file_name)]# Upload all filestry:for local_path, repo_path in files_to_upload:api.upload_file(path_or_fileobj=local_path,path_in_repo=repo_path,repo_id=repo_id,repo_type="space")space_url = f"https://huggingface.co/spaces/{repo_id}"return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)except Exception as e:
app.py+65-10