File size: 4,872 Bytes
74dd3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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