update streamlit
Browse files1345 unmodified lines1346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471345 unmodified lines}sdk = sdk_map.get(sdk_name, "gradio")api = HfApi(token=token.token)# Create the Space if it doesn't existtry:api.create_repo(repo_id=repo_id, # e.g. username/space_namerepo_type="space",space_sdk=sdk, # Use selected SDKexist_ok=True # Don't error if it already exists)except Exception as e:return gr.update(value=f"Error creating Space: {e}", visible=True)# Streamlit/docker logicif sdk == "docker":import tempfile, ostemp_dir = tempfile.mkdtemp()try:# 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 src/streamlit_app.pysrc_dir = os.path.join(temp_dir, "src")os.makedirs(src_dir, exist_ok=True)app_path = os.path.join(src_dir, "streamlit_app.py")with open(app_path, "w") as f:f.write(code)# 3. 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)# 4. Write README.md in the required formatreadme_content = f'''---title: {space_name.strip()}emoji: ๐colorFrom: redcolorTo: redsdk: dockerapp_port: 8501tags:- streamlitpinned: falseshort_description: Streamlit template space---# Welcome to Streamlit!Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community\nforums](https://discuss.streamlit.io).'''readme_path = os.path.join(temp_dir, "README.md")with open(readme_path, "w") as f:f.write(readme_content)# Upload all four filesapi.upload_file(path_or_fileobj=req_path,path_in_repo="requirements.txt",repo_id=repo_id,repo_type="space")api.upload_file(path_or_fileobj=app_path,path_in_repo="src/streamlit_app.py",repo_id=repo_id,repo_type="space")api.upload_file(path_or_fileobj=dockerfile_path,path_in_repo="Dockerfile",repo_id=repo_id,repo_type="space")api.upload_file(path_or_fileobj=readme_path,path_in_repo="README.md",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:return gr.update(value=f"Error uploading Streamlit files: {e}", visible=True)finally:shutil.rmtree(temp_dir, ignore_errors=True)# Other SDKs (existing logic)if sdk == "static":file_name = "index.html"1345 unmodified lines1346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991345 unmodified lines}sdk = sdk_map.get(sdk_name, "gradio")api = HfApi(token=token.token)# Create the Space if it doesn't exist (only for non-Streamlit spaces)if sdk != "docker":try:api.create_repo(repo_id=repo_id, # e.g. username/space_namerepo_type="space",space_sdk=sdk, # Use selected SDKexist_ok=True # Don't error if it already exists)except Exception as e:return gr.update(value=f"Error creating Space: {e}", visible=True)# Streamlit/docker logicif sdk == "docker":try:# Use duplicate_space to create a Streamlit template spacefrom huggingface_hub import duplicate_space# Duplicate the streamlit template spaceduplicated_repo = duplicate_space(from_id="streamlit/streamlit-template-space",to_id=space_name.strip(),token=token.token,exist_ok=True)# Upload the user's code to the duplicated spaceimport tempfilewith tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)temp_path = f.nametry:api.upload_file(path_or_fileobj=temp_path,path_in_repo="src/streamlit_app.py",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:return gr.update(value=f"Error uploading Streamlit app: {e}", visible=True)finally:import osos.unlink(temp_path)except Exception as e:return gr.update(value=f"Error duplicating Streamlit space: {e}", visible=True)# Other SDKs (existing logic)if sdk == "static":file_name = "index.html"
app.py+43-91