import streamlit as st import time def create_repository_form(): """Form for creating a new model repository""" st.subheader("Create a New Model Repository") with st.form("create_repo_form"): # Repository name input st.markdown("#### Repository Name") repo_name = st.text_input( "Enter a name for your repository", help="This will be part of the URL: huggingface.co/username/repository-name", placeholder="my-awesome-model", key="repo_name_input", ) # Repository visibility st.markdown("#### Repository Visibility") is_private = st.checkbox( "Make repository private", value=False, help="Private repositories are only visible to you and collaborators", ) # Repository type st.markdown("#### Repository Type") repo_type = st.selectbox( "Select repository type", options=["model", "dataset", "space"], index=0, help="The type of content you'll be storing in this repository", ) # Model tags st.markdown("#### Model Tags") if "client" in st.session_state: available_tags = st.session_state.client.get_model_tags() selected_tags = st.multiselect( "Select tags for your model", options=available_tags, help="Tags help others discover your model", ) # Model description st.markdown("#### Description") description = st.text_area( "Provide a brief description of your model", placeholder="This model is designed for...", help="This will appear on your model card and help others understand your model's purpose", ) # Submit button submitted = st.form_submit_button("Create Repository", use_container_width=True) if submitted: if not repo_name: st.error("Repository name is required") return False, None # Validate repository name (alphanumeric with hyphens only) if not all(c.isalnum() or c == "-" for c in repo_name): st.error( "Repository name can only contain letters, numbers, and hyphens" ) return False, None # Create the repository with st.spinner("Creating repository..."): try: # Format the repo_id with username username = st.session_state.username repo_id = f"{username}/{repo_name}" # Create the repository success, response = st.session_state.client.create_model_repository( repo_name=repo_id, is_private=is_private, exist_ok=False, repo_type=repo_type, ) if success: # Create a basic model card with description and tags model_card_content = f"""--- tags: {chr(10).join(['- ' + tag for tag in selected_tags])} --- # {repo_name} {description} ## Model description Add more details about your model here. ## Intended uses & limitations Describe the intended uses of your model and any limitations. ## Training and evaluation data Describe the data you used to train and evaluate your model. ## Training procedure Describe the training procedure. ## Evaluation results Provide evaluation results. """ # Update the model card card_success, _ = st.session_state.client.update_model_card( repo_id, model_card_content ) if card_success: st.success(f"Repository '{repo_id}' created successfully!") # Update the models list time.sleep(1) # Wait briefly for the API to update st.session_state.models = ( st.session_state.client.get_user_models() ) return True, repo_id else: st.warning( "Repository created but failed to update model card." ) return True, repo_id else: st.error(f"Failed to create repository: {response}") return False, None except Exception as e: st.error(f"Error creating repository: {str(e)}") return False, None return False, None