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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# Whisper Malayalam Fine-tuning Code
# Extracted from Colab session

# Cell 1
# ============================================================
# PUSH TRAINED MODEL TO HF HUB - FIXED!
# ============================================================

from huggingface_hub import notebook_login
from transformers import WhisperForConditionalGeneration, WhisperProcessor

# 1) Login
notebook_login()

# 2) Your details
YOUR_USERNAME = "kasimali"
MODEL_NAME = "whisper-small-malayalam"

# 3) Load from LOCAL folder (not repo!)
print("πŸ“¦ Loading model from local folder...")
model = WhisperForConditionalGeneration.from_pretrained("./whisper-small-ml-final", local_files_only=True)
processor = WhisperProcessor.from_pretrained("./whisper-small-ml-final", local_files_only=True)

print("βœ… Model loaded!")

# 4) Push to HF Hub
print(f"πŸ“€ Pushing to {YOUR_USERNAME}/{MODEL_NAME}...")

model.push_to_hub(f"{YOUR_USERNAME}/{MODEL_NAME}", use_auth_token=True)
processor.push_to_hub(f"{YOUR_USERNAME}/{MODEL_NAME}", use_auth_token=True)

print(f"βœ… DONE!")
print(f"πŸ”— Your model: https://huggingface.co/{YOUR_USERNAME}/{MODEL_NAME}")


# Cell 2
# ============================================================
# UPLOAD MODEL FOLDER DIRECTLY TO HF HUB
# ============================================================

from huggingface_hub import HfApi, create_repo, notebook_login
import os

# 1) Login
notebook_login()

# 2) Config
YOUR_USERNAME = "kasimali"
MODEL_NAME = "whisper-small-malayalam"
LOCAL_FOLDER = "whisper-small-ml-final"  # Without ./

# 3) Create repo on HF Hub
api = HfApi()

print(f"πŸ“¦ Creating repo: {YOUR_USERNAME}/{MODEL_NAME}...")

try:
    create_repo(
        repo_id=f"{YOUR_USERNAME}/{MODEL_NAME}",
        repo_type="model",
        private=False
    )
    print("βœ… Repo created!")
except Exception as e:
    print(f"⚠️ Repo might already exist: {e}")

# 4) Upload entire folder
print(f"πŸ“€ Uploading {LOCAL_FOLDER} to Hub...")

api.upload_folder(
    folder_path=LOCAL_FOLDER,
    repo_id=f"{YOUR_USERNAME}/{MODEL_NAME}",
    repo_type="model",
)

print(f"\nβœ… SUCCESS!")
print(f"πŸ”— Your model: https://huggingface.co/{YOUR_USERNAME}/{MODEL_NAME}")


# Cell 3
# Check what folders/files exist
import os

print("πŸ“ Files in /content:")
for item in os.listdir("/content"):
    print(f"  - {item}")

print("\nπŸ“ Looking for model folders...")
# Check common save locations
possible_folders = [
    "whisper-small-ml",
    "whisper-small-ml-final", 
    "./whisper-small-ml",
    "./whisper-small-ml-final"
]

for folder in possible_folders:
    if os.path.exists(folder):
        print(f"βœ… Found: {folder}")
        print(f"   Contents: {os.listdir(folder)[:5]}")  # Show first 5 files
    else:
        print(f"❌ Not found: {folder}")


# Cell 4
from huggingface_hub import HfApi, create_repo, notebook_login

notebook_login()

YOUR_USERNAME = "kasimali"
SPACE_NAME = "malayalam-whisper-finetuning"

# Create Space
create_repo(
    repo_id=f"{YOUR_USERNAME}/{SPACE_NAME}",
    repo_type="space",
    space_sdk="gradio",
    private=False
)

# Create app.py that displays notebook
app_code = '''import gradio as gr

# Simple viewer
def show_notebook():
    return "Upload finetunning.ipynb to this Space to share your training code!"

demo = gr.Interface(
    fn=show_notebook,
    inputs=None,
    outputs=gr.Textbox(),
    title="Malayalam Whisper Fine-tuning Notebook"
)

demo.launch()
'''

# Write and upload
with open("app.py", "w") as f:
    f.write(app_code)

api = HfApi()
api.upload_file(
    path_or_fileobj="app.py",
    path_in_repo="app.py",
    repo_id=f"{YOUR_USERNAME}/{SPACE_NAME}",
    repo_type="space"
)

# Upload your notebook
from google.colab import files
files.download("finetunning.ipynb")

api.upload_file(
    path_or_fileobj="finetunning.ipynb",
    path_in_repo="finetunning.ipynb",
    repo_id=f"{YOUR_USERNAME}/{SPACE_NAME}",
    repo_type="space"
)

print(f"βœ… Space: https://huggingface.co/spaces/{YOUR_USERNAME}/{SPACE_NAME}")


# Cell 5
from huggingface_hub import HfApi, create_repo, notebook_login

# Login
notebook_login()

# Configuration
USERNAME = "kasimali"
SPACE_NAME = "whisper-malayalam-finetuning"
SPACE_ID = f"{USERNAME}/{SPACE_NAME}"

# Create Space
try:
    create_repo(repo_id=SPACE_ID, repo_type="space", space_sdk="static")
    print(f"Created Space: {SPACE_ID}")
except:
    print("Space already exists")

# Get notebook from Colab
import json
from google.colab import _message
notebook = _message.blocking_request('get_ipynb', timeout_sec=10)
notebook_content = json.dumps(notebook, indent=2)

# Save notebook locally
with open("FINETUNINNG.ipynb", "w") as f:
    f.write(notebook_content)

# Upload to Space
api = HfApi()
api.upload_file(
    path_or_fileobj="FINETUNINNG.ipynb",
    path_in_repo="FINETUNINNG.ipynb",
    repo_id=SPACE_ID,
    repo_type="space"
)

# Create README
readme = f"""---
title: Whisper Malayalam Fine-tuning
emoji: 🎀
colorFrom: blue
colorTo: green
sdk: static
---

# Whisper Malayalam Fine-tuning

This Space contains the training notebook for fine-tuning Whisper on Malayalam language.

## Files
- FINETUNINNG.ipynb: Training notebook

## Usage
Download the notebook and run it in Google Colab or Jupyter.
"""

with open("README.md", "w") as f:
    f.write(readme)

api.upload_file(
    path_or_fileobj="README.md",
    path_in_repo="README.md",
    repo_id=SPACE_ID,
    repo_type="space"
)

print(f"Done. Visit: https://huggingface.co/spaces/{SPACE_ID}")


# Cell 6
from huggingface_hub import HfApi, create_repo, notebook_login
import os
import glob

# Login once
notebook_login()

# Configuration
USERNAME = "kasimali"

# Find all notebook files
print("Searching for notebook files...")
notebook_files = []

# Search in common locations
search_paths = [
    "/content/*.ipynb",
    "/content/**/*.ipynb",
]

for pattern in search_paths:
    notebook_files.extend(glob.glob(pattern, recursive=True))

# Remove duplicates
notebook_files = list(set(notebook_files))

print(f"\nFound {len(notebook_files)} notebook(s):")
for i, nb in enumerate(notebook_files):
    print(f"{i+1}. {nb}")

if len(notebook_files) == 0:
    print("\nNo notebooks found! Make sure you have .ipynb files in /content/")
else:
    print(f"\nCreating {len(notebook_files)} separate Spaces...")
    
    api = HfApi()
    created_spaces = []
    
    for notebook_path in notebook_files:
        # Get notebook name without extension
        notebook_name = os.path.basename(notebook_path).replace(".ipynb", "")
        
        # Clean name for Space (remove special chars, lowercase)
        space_name = notebook_name.lower().replace(" ", "-").replace("_", "-")
        space_id = f"{USERNAME}/{space_name}"
        
        print(f"\n{'='*60}")
        print(f"Processing: {notebook_name}")
        print(f"Space ID: {space_id}")
        
        try:
            # Create Space
            create_repo(
                repo_id=space_id,
                repo_type="space",
                space_sdk="static",
                exist_ok=True
            )
            print(f"βœ… Space created/exists")
            
            # Upload notebook
            api.upload_file(
                path_or_fileobj=notebook_path,
                path_in_repo=os.path.basename(notebook_path),
                repo_id=space_id,
                repo_type="space",
                commit_message=f"Upload {notebook_name}"
            )
            print(f"βœ… Notebook uploaded")
            
            # Create README
            readme = f"""---
title: {notebook_name}
emoji: πŸ““
colorFrom: blue
colorTo: green
sdk: static
---

# {notebook_name}

This Space contains the notebook: **{os.path.basename(notebook_path)}**

## Usage
Download the notebook and run it in Google Colab or Jupyter.

## Files
- {os.path.basename(notebook_path)}
"""
            
            # Save and upload README
            readme_path = f"/tmp/README_{space_name}.md"
            with open(readme_path, "w") as f:
                f.write(readme)
            
            api.upload_file(
                path_or_fileobj=readme_path,
                path_in_repo="README.md",
                repo_id=space_id,
                repo_type="space",
                commit_message="Add README"
            )
            print(f"βœ… README added")
            
            created_spaces.append({
                'name': notebook_name,
                'url': f"https://huggingface.co/spaces/{space_id}"
            })
            
        except Exception as e:
            print(f"❌ Error: {e}")
    
    # Summary
    print(f"\n{'='*60}")
    print(f"SUMMARY: Created {len(created_spaces)} Spaces")
    print(f"{'='*60}")
    
    for space in created_spaces:
        print(f"\nπŸ““ {space['name']}")
        print(f"   πŸ”— {space['url']}")
    
    print(f"\nβœ… All done!")


# Cell 7
from huggingface_hub import HfApi, create_repo, notebook_login
import json

# Login
notebook_login()

# Configuration
USERNAME = "kasimali"
SPACE_NAME = "whisper-malayalam-code"
SPACE_ID = f"{USERNAME}/{SPACE_NAME}"

# Get all code from current session
print("Extracting code from Colab session...")

# Get execution history
from IPython import get_ipython
ipython = get_ipython()

# Get all executed code
all_code = []
for i, cell in enumerate(ipython.user_ns.get('In', [])):
    if cell and cell.strip():
        all_code.append(f"# Cell {i}\n{cell}\n")

# Combine all code
full_code = "\n\n".join(all_code)

# Save as Python file
print("Creating app.py...")
app_content = f"""# Whisper Malayalam Fine-tuning Code
# Extracted from Colab session

{full_code}
"""

with open("app.py", "w") as f:
    f.write(app_content)

print(f"βœ… Created app.py ({len(full_code)} characters)")

# Create Space
print(f"\nCreating Space: {SPACE_ID}...")
try:
    create_repo(repo_id=SPACE_ID, repo_type="space", space_sdk="static", exist_ok=True)
    print("βœ… Space created")
except Exception as e:
    print(f"Space exists: {e}")

# Upload
api = HfApi()

print("\nUploading app.py...")
api.upload_file(
    path_or_fileobj="app.py",
    path_in_repo="app.py",
    repo_id=SPACE_ID,
    repo_type="space",
    commit_message="Upload code from Colab"
)

# Create requirements.txt
requirements = """datasets==3.1.0
transformers
accelerate
evaluate
jiwer
torch
"""

with open("requirements.txt", "w") as f:
    f.write(requirements)

api.upload_file(
    path_or_fileobj="requirements.txt",
    path_in_repo="requirements.txt",
    repo_id=SPACE_ID,
    repo_type="space",
    commit_message="Add requirements"
)

# Create README
readme = f"""---
title: Whisper Malayalam Fine-tuning Code
emoji: 🎀
colorFrom: blue
colorTo: green
sdk: static
---

# Whisper Malayalam Fine-tuning Code

This Space contains the Python code for fine-tuning Whisper on Malayalam.

## Files
- `app.py`: Main training code (extracted from Colab)
- `requirements.txt`: Python dependencies

## Usage


# Cell 8
from huggingface_hub import HfApi, create_repo, notebook_login

# Login
notebook_login()

# Configuration
USERNAME = "kasimali"
SPACE_NAME = "whisper-malayalam-code"
SPACE_ID = f"{USERNAME}/{SPACE_NAME}"

# Get all code from current session
print("Extracting code from Colab session...")

from IPython import get_ipython
ipython = get_ipython()

# Get all executed code
all_code = []
for i, cell in enumerate(ipython.user_ns.get('In', [])):
    if cell and cell.strip():
        all_code.append(f"# Cell {i}\n{cell}\n")

# Combine all code
full_code = "\n\n".join(all_code)

# Save as Python file
print("Creating app.py...")
app_content = "# Whisper Malayalam Fine-tuning Code\n"
app_content += "# Extracted from Colab session\n\n"
app_content += full_code

with open("app.py", "w") as f:
    f.write(app_content)

print(f"Created app.py ({len(full_code)} characters)")

# Create Space
print(f"\nCreating Space: {SPACE_ID}...")
try:
    create_repo(repo_id=SPACE_ID, repo_type="space", space_sdk="static", exist_ok=True)
    print("Space created")
except:
    print("Space already exists")

# Upload
api = HfApi()

print("\nUploading app.py...")
api.upload_file(
    path_or_fileobj="app.py",
    path_in_repo="app.py",
    repo_id=SPACE_ID,
    repo_type="space",
    commit_message="Upload code from Colab"
)

# Create requirements.txt
requirements = "datasets==3.1.0\ntransformers\naccelerate\nevaluate\njiwer\ntorch\n"

with open("requirements.txt", "w") as f:
    f.write(requirements)

api.upload_file(
    path_or_fileobj="requirements.txt",
    path_in_repo="requirements.txt",
    repo_id=SPACE_ID,
    repo_type="space",
    commit_message="Add requirements"
)

# Create README
readme_content = "---\n"
readme_content += "title: Whisper Malayalam Code\n"
readme_content += "emoji: 🎀\n"
readme_content += "colorFrom: blue\n"
readme_content += "colorTo: green\n"
readme_content += "sdk: static\n"
readme_content += "---\n\n"
readme_content += "# Whisper Malayalam Fine-tuning Code\n\n"
readme_content += "Python code for fine-tuning Whisper on Malayalam.\n\n"
readme_content += "## Files\n"
readme_content += "- app.py: Main training code\n"
readme_content += "- requirements.txt: Dependencies\n\n"
readme_content += "## Usage\n"
readme_content += "pip install -r requirements.txt\n"
readme_content += "python app.py\n"

with open("README.md", "w") as f:
    f.write(readme_content)

api.upload_file(
    path_or_fileobj="README.md",
    path_in_repo="README.md",
    repo_id=SPACE_ID,
    repo_type="space",
    commit_message="Add README"
)

print("\nSUCCESS!")
print(f"Uploaded: app.py, requirements.txt, README.md")
print(f"\nView at: https://huggingface.co/spaces/{SPACE_ID}")