File size: 10,648 Bytes
11e4904
 
 
 
 
58e6c10
11e4904
58e6c10
11e4904
 
 
 
 
 
 
 
 
58e6c10
11e4904
58e6c10
11e4904
 
 
 
58e6c10
7d762a5
11e4904
 
 
58e6c10
11e4904
7d762a5
 
58e6c10
11e4904
 
 
58e6c10
11e4904
58e6c10
11e4904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58e6c10
11e4904
 
 
 
 
 
 
 
 
 
 
58e6c10
11e4904
 
 
 
 
 
 
58e6c10
11e4904
 
 
 
 
 
 
 
 
 
 
7d762a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54760e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d762a5
 
54760e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d762a5
7fb2842
54760e3
 
 
 
 
 
 
 
 
 
 
 
 
7fb2842
 
54760e3
 
7fb2842
54760e3
 
 
 
 
 
7fb2842
 
54760e3
7fb2842
54760e3
 
 
 
7fb2842
 
54760e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fb2842
 
54760e3
 
 
 
 
 
 
7fb2842
 
54760e3
 
 
 
 
 
7fb2842
 
54760e3
 
 
7fb2842
 
54760e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fb2842
7d762a5
 
 
11e4904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58e6c10
7d762a5
58e6c10
11e4904
 
 
 
 
 
 
 
7d762a5
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import csv
import os
from datetime import datetime
from typing import Optional

import gradio as gr
from huggingface_hub import HfApi, Repository

from optimum_neuron_export import convert
from gradio_huggingfacehub_search import HuggingfaceHubSearch
from apscheduler.schedulers.background import BackgroundScheduler

DATASET_REPO_URL = "https://huggingface.co/datasets/optimum/neuron-exports"
DATA_FILENAME = "exports.csv"
DATA_FILE = os.path.join("data", DATA_FILENAME)

HF_TOKEN = os.environ.get("HF_WRITE_TOKEN")

DATADIR = "neuron_exports_data"

repo: Optional[Repository] = None
# Uncomment if you want to push to dataset repo with token
# if HF_TOKEN:
#     repo = Repository(local_dir=DATADIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN)


def neuron_export(model_id: str, task: str) -> str:
    if not model_id:
        return f"### Invalid input 🐞 Please specify a model name, got {model_id}"

    try:
        api = HfApi(token=HF_TOKEN)  # Use HF_TOKEN if available, else anonymous
        token = HF_TOKEN  # Pass token to convert only if available

        error, commit_info = convert(api=api, model_id=model_id, task=task, token=token)
        if error != "0":
            return error

        print("[commit_info]", commit_info)

        # Save in a private dataset if repo initialized
        if repo is not None:
            repo.git_pull(rebase=True)
            with open(os.path.join(DATADIR, DATA_FILE), "a") as csvfile:
                writer = csv.DictWriter(
                    csvfile, fieldnames=["model_id", "pr_url", "time"]
                )
                writer.writerow(
                    {
                        "model_id": model_id,
                        "pr_url": commit_info.pr_url,
                        "time": str(datetime.now()),
                    }
                )
            commit_url = repo.push_to_hub()
            print("[dataset]", commit_url)

        pr_revision = commit_info.pr_revision.replace("/", "%2F")
        return f"#### Success πŸ”₯ This model was successfully exported and a PR was opened: [{commit_info.pr_url}]({commit_info.pr_url}). To use the model before the PR is approved, go to https://huggingface.co/{model_id}/tree/{pr_revision}"

    except Exception as e:
        return f"#### Error: {e}"


TITLE_IMAGE = """
<div style="display: block; margin-left: auto; margin-right: auto; width: 50%;">
<img src="https://huggingface.co/spaces/optimum/neuron-export/resolve/main/huggingfaceXneuron.png"/>
</div>
"""

TITLE = """
<div style="display: inline-flex; align-items: center; text-align: center; max-width: 1400px; gap: 0.8rem; font-size: 2.2rem;">
<h1 style="font-weight: 900; margin-bottom: 10px; margin-top: 10px;">
    πŸ€— Optimum Neuron Model Exporter
</h1>
</div>
"""

DESCRIPTION = """
Export πŸ€— Transformers models hosted on the Hugging Face Hub to AWS Neuron-optimized format for Inferentia/Trainium acceleration.
*Features:*
- Automatically opens PR with Neuron-optimized model
- Preserves original model weights
- Adds proper tags to model card
*Note:*
- PR creation requires the Space owner to have a valid write token set via HF_WRITE_TOKEN
"""

# Custom CSS to fix dark mode compatibility and transparency issues
CUSTOM_CSS = """
/* Fix for HuggingfaceHubSearch component visibility in both light and dark modes */
.gradio-container .gr-form {
    background: var(--background-fill-primary) !important;
    border: 1px solid var(--border-color-primary) !important;
}

/* Ensure text is visible in both modes */
.gradio-container input[type="text"], 
.gradio-container textarea,
.gradio-container .gr-textbox input {
    color: var(--body-text-color) !important;
    background: var(--input-background-fill) !important;
    border: 1px solid var(--border-color-primary) !important;
}

/* Fix dropdown/search results visibility */
.gradio-container .gr-dropdown,
.gradio-container .gr-dropdown .gr-box,
.gradio-container [data-testid="textbox"] {
    background: var(--background-fill-primary) !important;
    color: var(--body-text-color) !important;
    border: 1px solid var(--border-color-primary) !important;
}

/* Fix for search component specifically */
.gradio-container .gr-form > div,
.gradio-container .gr-form input {
    background: var(--input-background-fill) !important;
    color: var(--body-text-color) !important;
}

/* Ensure proper contrast for placeholder text */
.gradio-container input::placeholder {
    color: var(--body-text-color-subdued) !important;
    opacity: 0.7;
}

/* Fix any remaining transparent backgrounds */
.gradio-container .gr-box,
.gradio-container .gr-panel {
    background: var(--background-fill-primary) !important;
}

/* AGGRESSIVE FIXES FOR DROPDOWN VISIBILITY - Target all possible elements */

/* Universal dropdown item fixes - cast wide net */
.gradio-container div[class*="dropdown"] div,
.gradio-container div[class*="dropdown"] span,
.gradio-container div[class*="dropdown"] p,
.gradio-container div[class*="dropdown"] li,
.gradio-container ul[class*="dropdown"] li,
.gradio-container [role="listbox"] div,
.gradio-container [role="listbox"] span,
.gradio-container [role="option"],
.gradio-container [role="option"] span,
.gradio-container [role="option"] div {
    color: #1f2937 !important;  /* Dark text for light mode */
    background: white !important;
}

/* Dark mode specific overrides */
@media (prefers-color-scheme: dark) {
    .gradio-container div[class*="dropdown"] div,
    .gradio-container div[class*="dropdown"] span,
    .gradio-container div[class*="dropdown"] p,
    .gradio-container div[class*="dropdown"] li,
    .gradio-container ul[class*="dropdown"] li,
    .gradio-container [role="listbox"] div,
    .gradio-container [role="listbox"] span,
    .gradio-container [role="option"],
    .gradio-container [role="option"] span,
    .gradio-container [role="option"] div {
        color: #f9fafb !important;  /* Light text for dark mode */
        background: #374151 !important;
    }
}

/* Force text visibility for Gradio dark theme */
.dark .gradio-container div[class*="dropdown"] div,
.dark .gradio-container div[class*="dropdown"] span,
.dark .gradio-container div[class*="dropdown"] p,
.dark .gradio-container div[class*="dropdown"] li,
.dark .gradio-container ul[class*="dropdown"] li,
.dark .gradio-container [role="listbox"] div,
.dark .gradio-container [role="listbox"] span,
.dark .gradio-container [role="option"],
.dark .gradio-container [role="option"] span,
.dark .gradio-container [role="option"] div {
    color: #f9fafb !important;
    background: #374151 !important;
}

/* Additional specific targeting for HuggingfaceHubSearch */
.gradio-container .gr-dropdown-item,
.gradio-container .gr-dropdown .gr-dropdown-item,
.gradio-container .gr-dropdown li,
.gradio-container .gr-dropdown div[role="option"] {
    background: white !important;
    color: #1f2937 !important;
    border-bottom: 1px solid #e5e7eb !important;
    padding: 8px 12px !important;
}

.gradio-container .gr-dropdown-item:hover,
.gradio-container .gr-dropdown .gr-dropdown-item:hover,
.gradio-container .gr-dropdown li:hover,
.gradio-container .gr-dropdown div[role="option"]:hover {
    background: #f3f4f6 !important;
    color: #1f2937 !important;
}

/* Dark mode for specific dropdown items */
@media (prefers-color-scheme: dark) {
    .gradio-container .gr-dropdown-item,
    .gradio-container .gr-dropdown .gr-dropdown-item,
    .gradio-container .gr-dropdown li,
    .gradio-container .gr-dropdown div[role="option"] {
        background: #374151 !important;
        color: #f9fafb !important;
        border-bottom: 1px solid #4b5563 !important;
    }
    
    .gradio-container .gr-dropdown-item:hover,
    .gradio-container .gr-dropdown .gr-dropdown-item:hover,
    .gradio-container .gr-dropdown li:hover,
    .gradio-container .gr-dropdown div[role="option"]:hover {
        background: #4b5563 !important;
        color: #f9fafb !important;
    }
}

.dark .gradio-container .gr-dropdown-item,
.dark .gradio-container .gr-dropdown .gr-dropdown-item,
.dark .gradio-container .gr-dropdown li,
.dark .gradio-container .gr-dropdown div[role="option"] {
    background: #374151 !important;
    color: #f9fafb !important;
    border-bottom: 1px solid #4b5563 !important;
}

.dark .gradio-container .gr-dropdown-item:hover,
.dark .gradio-container .gr-dropdown .gr-dropdown-item:hover,
.dark .gradio-container .gr-dropdown li:hover,
.dark .gradio-container .gr-dropdown div[role="option"]:hover {
    background: #4b5563 !important;
    color: #f9fafb !important;
}

/* Nuclear option - force all text elements to be visible */
.gradio-container * {
    text-shadow: none !important;
}

.gradio-container [class*="dropdown"] * {
    color: inherit !important;
}

/* Fix dropdown container backgrounds */
.gradio-container .gr-dropdown,
.gradio-container [role="listbox"],
.gradio-container div[class*="dropdown"] {
    background: white !important;
    border: 1px solid #d1d5db !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}

@media (prefers-color-scheme: dark) {
    .gradio-container .gr-dropdown,
    .gradio-container [role="listbox"],
    .gradio-container div[class*="dropdown"] {
        background: #374151 !important;
        border: 1px solid #4b5563 !important;
    }
}

.dark .gradio-container .gr-dropdown,
.dark .gradio-container [role="listbox"],
.dark .gradio-container div[class*="dropdown"] {
    background: #374151 !important;
    border: 1px solid #4b5563 !important;
}
"""

with gr.Blocks(css=CUSTOM_CSS) as demo:
    gr.HTML(TITLE_IMAGE)
    gr.HTML(TITLE)

    with gr.Row():
        with gr.Column(scale=50):
            gr.Markdown(DESCRIPTION)

        with gr.Column(scale=50):
            input_model = HuggingfaceHubSearch(
                label="Hub model ID",
                placeholder="Search for model ID on the hub",
                search_type="model",
            )
            input_task = gr.Textbox(
                value="auto",
                max_lines=1,
                label='Task (can be left to "auto", will be automatically inferred)',
            )
            btn = gr.Button("Export to Neuron")
            output = gr.Markdown(label="Output")

    btn.click(
        fn=neuron_export,
        inputs=[input_model, input_task],
        outputs=output,
    )


if __name__ == "__main__":
    def restart_space():
        if HF_TOKEN:
            HfApi().restart_space(repo_id="optimum/neuron-export", token=HF_TOKEN, factory_reboot=True)

    scheduler = BackgroundScheduler()
    scheduler.add_job(restart_space, "interval", seconds=21600)
    scheduler.start()

    demo.launch()