-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhf.ps1
More file actions
278 lines (242 loc) · 9 KB
/
hf.ps1
File metadata and controls
278 lines (242 loc) · 9 KB
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
param(
[Parameter(Mandatory = $true, HelpMessage = "The podcast folder name to process.")]
[string]$PodcastFolder
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Continue"
$CurrentYear = 2026
$SuccessColor = "Green"
$WarningColor = "Yellow"
$ErrorColor = "Red"
$InfoColor = "Cyan"
function Write-ColoredOutput
{
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Invoke-HfCommand
{
param(
[string[]]$Arguments
)
Write-ColoredOutput " -> hf $($Arguments -join ' ')" -Color $InfoColor
$output = & hf @Arguments 2>&1
$exitCode = $LASTEXITCODE
$output | ForEach-Object { Write-Host $_ }
return [PSCustomObject]@{
ExitCode = $exitCode
Output = @($output)
}
}
$CurrentPath = (Get-Location).ProviderPath
$PodcastPath = Join-Path -Path $CurrentPath -ChildPath $PodcastFolder
if (-not (Test-Path -Path $PodcastPath -PathType Container))
{
Write-ColoredOutput "ERROR: Podcast folder '$PodcastFolder' not found at '$PodcastPath'" -Color $ErrorColor
exit 1
}
Write-ColoredOutput "`n========================================" -Color $SuccessColor
Write-ColoredOutput "HUGGING FACE DATASET PUBLISH SCRIPT" -Color $SuccessColor
Write-ColoredOutput "========================================`n" -Color $SuccessColor
Write-ColoredOutput "Podcast: $PodcastFolder" -Color $InfoColor
Write-ColoredOutput "Location: $PodcastPath" -Color $InfoColor
Write-ColoredOutput "Current year (will skip): $CurrentYear`n" -Color $InfoColor
Write-ColoredOutput "`nScanning year folders in '$PodcastFolder'..." -Color $InfoColor
$YearFolders = @()
Get-ChildItem -Path $PodcastPath -Directory | ForEach-Object {
if ($_.Name -match '^\d{4}$')
{
$yearNum = [int]$_.Name
if ($yearNum -ne $CurrentYear)
{
$YearFolders += $_.Name
}
}
}
if ($YearFolders.Count -eq 0)
{
Write-ColoredOutput "No year folders found (excluding $CurrentYear)" -Color $WarningColor
Write-ColoredOutput "Exiting." -Color $WarningColor
exit 0
}
$YearFolders = @($YearFolders | Sort-Object)
Write-ColoredOutput "Found year folders: $($YearFolders -join ', ')`n" -Color $SuccessColor
$CreatedRepos = 0
$ExistingRepos = 0
$UploadedYears = 0
$SkippedNoFiles = 0
$ErrorCount = 0
foreach ($Year in $YearFolders)
{
$YearPath = Join-Path -Path $PodcastPath -ChildPath $Year
$RepoId = "$Year-Shell-Game-Transcripts"
$PrettyName = "$Year Shell Game Transcripts"
$ReadmeTempPath = $null
$YearHadErrors = $false
$UploadedFileCount = 0
Write-ColoredOutput "`n-------------------------------------------" -Color $InfoColor
Write-ColoredOutput "Processing year: $Year" -Color $InfoColor
Write-ColoredOutput "Dataset repo: $RepoId" -Color $InfoColor
Write-ColoredOutput "-------------------------------------------" -Color $InfoColor
try
{
$createResult = Invoke-HfCommand -Arguments @("repo", "create", $RepoId, "--repo-type", "dataset", "--exist-ok")
if ($createResult.ExitCode -ne 0)
{
Write-ColoredOutput " [ERROR] Failed to ensure dataset repo exists." -Color $ErrorColor
$YearHadErrors = $true
continue
}
$createOutputText = ($createResult.Output | Out-String)
if ($createOutputText -match '(?i)already exists')
{
Write-ColoredOutput " [SKIP] Dataset repo already exists" -Color $WarningColor
$ExistingRepos++
} else
{
Write-ColoredOutput " [OK] Dataset repo created" -Color $SuccessColor
$CreatedRepos++
}
$ReadmeContent = @"
---
license: mit
task_categories:
- summarization
language:
- en
tags:
- transcript
- summary
- podcast
- show
pretty_name: $PrettyName
---
# $PrettyName
Complete transcripts from the $Year episodes of the Shell Game podcast.
Generated from [this GitHub repository](https://github.com/willtheorangeguy/Shell-Game-Transcripts).
"@
$ReadmeTempPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "$RepoId-README.md"
Set-Content -Path $ReadmeTempPath -Value $ReadmeContent -Encoding UTF8
$readmeUploadResult = Invoke-HfCommand -Arguments @(
"upload",
$RepoId,
$ReadmeTempPath,
"README.md",
"--repo-type",
"dataset",
"--commit-message",
"docs: update README",
"--quiet"
)
if ($readmeUploadResult.ExitCode -ne 0)
{
Write-ColoredOutput " [ERROR] Failed to upload README.md" -Color $ErrorColor
$YearHadErrors = $true
continue
}
Write-ColoredOutput " [OK] Uploaded README.md" -Color $SuccessColor
$TranscriptFiles = @(Get-ChildItem -Path $YearPath -File -Filter "*_transcript.txt" | Sort-Object Name)
$SummaryFiles = @(Get-ChildItem -Path $YearPath -File -Filter "*_summary.txt" | Sort-Object Name)
if (($TranscriptFiles.Count + $SummaryFiles.Count) -eq 0)
{
Write-ColoredOutput " [SKIP] No *_transcript.txt or *_summary.txt files found" -Color $WarningColor
$SkippedNoFiles++
if (-not $YearHadErrors)
{
$UploadedYears++
}
continue
}
if ($TranscriptFiles.Count -gt 0)
{
Write-ColoredOutput " Uploading $($TranscriptFiles.Count) transcript files in one commit..." -Color $InfoColor
$transcriptUploadResult = Invoke-HfCommand -Arguments @(
"upload",
$RepoId,
$YearPath,
".",
"--repo-type",
"dataset",
"--include",
"*_transcript.txt",
"--commit-message",
"add all $Year transcripts",
"--quiet"
)
if ($transcriptUploadResult.ExitCode -ne 0)
{
Write-ColoredOutput " [ERROR] Failed to upload transcript files" -Color $ErrorColor
$YearHadErrors = $true
} else
{
$UploadedFileCount += $TranscriptFiles.Count
Write-ColoredOutput " [OK] Uploaded transcript files with commit: add all $Year transcripts" -Color $SuccessColor
}
}
if (-not $YearHadErrors -and $SummaryFiles.Count -gt 0)
{
Write-ColoredOutput " Uploading $($SummaryFiles.Count) summary files in one commit..." -Color $InfoColor
$summaryUploadResult = Invoke-HfCommand -Arguments @(
"upload",
$RepoId,
$YearPath,
".",
"--repo-type",
"dataset",
"--include",
"*_summary.txt",
"--commit-message",
"add all $Year summaries",
"--quiet"
)
if ($summaryUploadResult.ExitCode -ne 0)
{
Write-ColoredOutput " [ERROR] Failed to upload summary files" -Color $ErrorColor
$YearHadErrors = $true
} else
{
$UploadedFileCount += $SummaryFiles.Count
Write-ColoredOutput " [OK] Uploaded summary files with commit: add all $Year summaries" -Color $SuccessColor
}
}
if (-not $YearHadErrors -and $SummaryFiles.Count -eq 0)
{
Write-ColoredOutput " [SKIP] No *_summary.txt files found for summary commit" -Color $WarningColor
}
if (-not $YearHadErrors -and $TranscriptFiles.Count -eq 0)
{
Write-ColoredOutput " [SKIP] No *_transcript.txt files found for transcript commit" -Color $WarningColor
}
if (-not $YearHadErrors)
{
Write-ColoredOutput " [OK] Year $Year complete ($UploadedFileCount files uploaded)" -Color $SuccessColor
$UploadedYears++
}
} catch
{
Write-ColoredOutput " [ERROR] $($_.Exception.Message)" -Color $ErrorColor
$YearHadErrors = $true
} finally
{
if ($ReadmeTempPath -and (Test-Path -Path $ReadmeTempPath -PathType Leaf))
{
Remove-Item -Path $ReadmeTempPath -Force -ErrorAction SilentlyContinue
}
}
if ($YearHadErrors)
{
$ErrorCount++
}
}
Write-ColoredOutput "`n========================================" -Color $SuccessColor
Write-ColoredOutput "SUMMARY" -Color $SuccessColor
Write-ColoredOutput "========================================" -Color $SuccessColor
Write-ColoredOutput "Repos created: $CreatedRepos" -Color $SuccessColor
Write-ColoredOutput "Repos already existing: $ExistingRepos" -Color $InfoColor
Write-ColoredOutput "Years processed successfully: $UploadedYears" -Color $SuccessColor
Write-ColoredOutput "Years skipped (no matching files): $SkippedNoFiles" -Color $WarningColor
Write-ColoredOutput "Years with errors: $ErrorCount" -Color $ErrorColor
Write-ColoredOutput "`n[OK] Script completed.`n" -Color $SuccessColor