| | |
| |
|
| | from huggingface_hub import HfApi, create_repo, login |
| | import os |
| | import json |
| | import glob |
| |
|
| | |
| | print("=" * 70) |
| | print("IMPORTANT: You need a token with WRITE permission!") |
| | print("Get one from: https://huggingface.co/settings/tokens") |
| | print("=" * 70) |
| |
|
| | |
| | if os.path.exists(os.path.expanduser("~/.huggingface/token")): |
| | os.remove(os.path.expanduser("~/.huggingface/token")) |
| |
|
| | |
| |
|
| | |
| | try: |
| | api = HfApi() |
| | user_info = api.whoami() |
| | print(f"\nLogged in as: {user_info['name']}") |
| | print("Token verified!\n") |
| | except Exception as e: |
| | print(f"\nLogin failed: {e}") |
| | print("Please check your token has WRITE permission!") |
| | raise |
| |
|
| | |
| | USERNAME = user_info['name'] |
| |
|
| | |
| | print("Mounting Google Drive...") |
| | from google.colab import drive |
| |
|
| | |
| | print("\nSearching for notebooks...") |
| | notebook_paths = glob.glob("/content/drive/**/*.ipynb", recursive=True) |
| | print(f"Found {len(notebook_paths)} notebooks\n") |
| |
|
| | if len(notebook_paths) == 0: |
| | print("No notebooks found!") |
| | else: |
| | created_spaces = [] |
| |
|
| | for nb_path in notebook_paths: |
| | notebook_name = os.path.basename(nb_path).replace(".ipynb", "") |
| | print(f"\n{'='*70}") |
| | print(f"Processing: {notebook_name}") |
| |
|
| | try: |
| | |
| | with open(nb_path, 'r', encoding='utf-8') as f: |
| | notebook_data = json.load(f) |
| |
|
| | |
| | code_cells = [] |
| | has_gradio = False |
| |
|
| | if 'cells' in notebook_data: |
| | for i, cell in enumerate(notebook_data['cells']): |
| | if cell.get('cell_type') == 'code': |
| | cell_source = ''.join(cell.get('source', [])) |
| | code_cells.append(f"# Cell {i+1}\n{cell_source}") |
| |
|
| | if 'gradio' in cell_source.lower() or 'gr.' in cell_source: |
| | has_gradio = True |
| |
|
| | if not code_cells: |
| | print("No code cells, skipping") |
| | continue |
| |
|
| | full_code = "\n\n".join(code_cells) |
| |
|
| | |
| | space_name = notebook_name.lower().strip() |
| | space_name = space_name.replace(" ", "-").replace("_", "-") |
| | |
| | while "--" in space_name: |
| | space_name = space_name.replace("--", "-") |
| | |
| | space_name = space_name.strip("-") |
| | |
| | space_name = ''.join(c for c in space_name if c.isalnum() or c == '-') |
| |
|
| | if has_gradio: |
| | space_name = f"{space_name}-gradio-app" |
| | sdk = "gradio" |
| | print("Type: Gradio app") |
| | else: |
| | space_name = f"{space_name}-code" |
| | sdk = "static" |
| | print("Type: Python code") |
| |
|
| | space_id = f"{USERNAME}/{space_name}" |
| | print(f"Space: {space_id}") |
| |
|
| | |
| | create_repo( |
| | repo_id=space_id, |
| | repo_type="space", |
| | space_sdk=sdk, |
| | exist_ok=True, |
| | token=True |
| | ) |
| | print("Space created") |
| |
|
| | |
| | app_content = f"# {notebook_name}\n\n{full_code}" |
| |
|
| | with open("app.py", "w", encoding='utf-8') as f: |
| | f.write(app_content) |
| |
|
| | |
| | api.upload_file( |
| | path_or_fileobj="app.py", |
| | path_in_repo="app.py", |
| | repo_id=space_id, |
| | repo_type="space" |
| | ) |
| |
|
| | |
| | if has_gradio: |
| | reqs = "gradio\ntransformers\ntorch\n" |
| | else: |
| | reqs = "transformers\ntorch\ndatasets\n" |
| |
|
| | with open("requirements.txt", "w") as f: |
| | f.write(reqs) |
| |
|
| | api.upload_file( |
| | path_or_fileobj="requirements.txt", |
| | path_in_repo="requirements.txt", |
| | repo_id=space_id, |
| | repo_type="space" |
| | ) |
| |
|
| | |
| | readme = f"---\ntitle: {notebook_name}\nsdk: {sdk}\n---\n\n# {notebook_name}\n" |
| |
|
| | 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("SUCCESS") |
| |
|
| | created_spaces.append({ |
| | 'name': notebook_name, |
| | 'type': 'Gradio' if has_gradio else 'Code', |
| | 'url': f"https://huggingface.co/spaces/{space_id}" |
| | }) |
| |
|
| | except Exception as e: |
| | print(f"ERROR: {e}") |
| | continue |
| |
|
| | |
| | print(f"\n{'='*70}") |
| | print(f"COMPLETE: {len(created_spaces)}/{len(notebook_paths)} Spaces created") |
| | print(f"{'='*70}\n") |
| |
|
| | for space in created_spaces: |
| | print(f"{space['name']} ({space['type']})") |
| | print(f" {space['url']}\n") |
| |
|
| |
|
| | |
| | from huggingface_hub import HfApi, login |
| | from datetime import datetime, timedelta |
| |
|
| |
|
| | api = HfApi() |
| | user_info = api.whoami() |
| | USERNAME = user_info['name'] |
| |
|
| | all_spaces = list(api.list_spaces(author=USERNAME)) |
| | print(f"Total Spaces: {len(all_spaces)}\n") |
| |
|
| | |
| | for i, space in enumerate(all_spaces): |
| | print(f"{i+1}. {space.id}") |
| |
|
| | selection = input("\nEnter numbers to delete (e.g., 1,2,5) or 'all': ") |
| |
|
| | if selection.lower() == 'all': |
| | to_delete = all_spaces |
| | else: |
| | indices = [int(x.strip()) for x in selection.split(',')] |
| | to_delete = [all_spaces[i-1] for i in indices if 0 < i <= len(all_spaces)] |
| |
|
| | if to_delete: |
| | confirm = input(f"\nDelete {len(to_delete)} spaces? Type 'yes': ") |
| | if confirm.lower() == 'yes': |
| | for space in to_delete: |
| | try: |
| | api.delete_repo(repo_id=space.id, repo_type="space") |
| | print(f"β
Deleted: {space.id}") |
| | except Exception as e: |
| | print(f"β Failed: {space.id}") |
| | print(f"\nβ
Done!") |
| |
|
| |
|
| | from huggingface_hub import HfApi, create_repo, login |
| | import os |
| | import json |
| | import glob |
| | import re |
| |
|
| | |
| |
|
| | USERNAME = "kasimali" |
| |
|
| | |
| | from google.colab import drive |
| |
|
| | |
| | notebook_paths = glob.glob("/content/drive/**/*.ipynb", recursive=True) |
| | print(f"Found {len(notebook_paths)} notebooks\n") |
| |
|
| | def clean_code_for_spaces(code): |
| | """Remove Colab-specific code and keep only working code""" |
| | lines = code.split('\n') |
| | cleaned = [] |
| | skip_until_next = False |
| |
|
| | for i, line in enumerate(lines): |
| | stripped = line.strip() |
| |
|
| | |
| | if stripped.startswith('!pip') or stripped.startswith('!apt'): |
| | continue |
| |
|
| | |
| | if stripped.startswith('!') or stripped.startswith('%'): |
| | continue |
| |
|
| | |
| | continue |
| |
|
| | |
| | continue |
| |
|
| | |
| | if 'files.download' in line or 'files.upload' in line: |
| | continue |
| |
|
| | |
| | if stripped.startswith('# Cell'): |
| | continue |
| |
|
| | |
| | cleaned.append(line) |
| |
|
| | return '\n'.join(cleaned) |
| |
|
| | def extract_requirements_from_code(code): |
| | """Extract library requirements from code""" |
| | libs = set() |
| |
|
| | |
| | import_to_package = { |
| | 'transformers': 'transformers', |
| | 'torch': 'torch', |
| | 'gradio': 'gradio', |
| | 'gr': 'gradio', |
| | 'datasets': 'datasets', |
| | 'evaluate': 'evaluate', |
| | 'jiwer': 'jiwer', |
| | 'sentencepiece': 'sentencepiece', |
| | 'sacremoses': 'sacremoses', |
| | 'indic_transliteration': 'indic-transliteration', |
| | 'IndicTransToolkit': 'IndicTransToolkit', |
| | 'librosa': 'librosa', |
| | 'soundfile': 'soundfile', |
| | 'torchaudio': 'torchaudio', |
| | 'pandas': 'pandas', |
| | 'numpy': 'numpy', |
| | 'scipy': 'scipy', |
| | } |
| |
|
| | |
| | for line in code.split('\n'): |
| | stripped = line.strip() |
| | if stripped.startswith('import ') or stripped.startswith('from '): |
| | for import_name, package_name in import_to_package.items(): |
| | if import_name in line: |
| | libs.add(package_name) |
| |
|
| | |
| | pip_pattern = r'!pip install.*?([\w-]+)==?[\d\.]+' |
| | for match in re.finditer(pip_pattern, code): |
| | package = match.group(1) |
| | if package in import_to_package.values(): |
| | libs.add(package) |
| |
|
| | return sorted(libs) |
| |
|
| | def has_gradio_interface(code): |
| | """Check if code contains Gradio interface""" |
| | gradio_indicators = [ |
| | 'gr.Interface', |
| | 'gr.Blocks', |
| | 'gradio.Interface', |
| | 'gradio.Blocks', |
| | '.launch()', |
| | 'demo.launch', |
| | ] |
| |
|
| | return any(indicator in code for indicator in gradio_indicators) |
| |
|
| | api = HfApi() |
| | created_spaces = [] |
| | skipped_spaces = [] |
| |
|
| | for nb_path in notebook_paths: |
| | notebook_name = os.path.basename(nb_path).replace(".ipynb", "") |
| |
|
| | print(f"\n{'='*70}") |
| | print(f"Processing: {notebook_name}") |
| | print(f"Path: {nb_path}") |
| |
|
| | try: |
| | |
| | with open(nb_path, 'r', encoding='utf-8') as f: |
| | notebook_data = json.load(f) |
| |
|
| | if 'cells' not in notebook_data: |
| | print("β Invalid notebook format") |
| | skipped_spaces.append(notebook_name) |
| | continue |
| |
|
| | |
| | all_code = [] |
| |
|
| | for cell in notebook_data['cells']: |
| | if cell.get('cell_type') == 'code': |
| | cell_source = ''.join(cell.get('source', [])) |
| | if cell_source.strip(): |
| | all_code.append(cell_source) |
| |
|
| | if not all_code: |
| | print("β No code cells found") |
| | skipped_spaces.append(notebook_name) |
| | continue |
| |
|
| | |
| | full_code = '\n\n'.join(all_code) |
| |
|
| | |
| | if not has_gradio_interface(full_code): |
| | print("β οΈ No Gradio interface found, skipping") |
| | skipped_spaces.append(notebook_name) |
| | continue |
| |
|
| | print("β
Found Gradio app!") |
| |
|
| | |
| | cleaned_code = clean_code_for_spaces(full_code) |
| |
|
| | if len(cleaned_code.strip()) < 50: |
| | print("β Not enough code after cleaning") |
| | skipped_spaces.append(notebook_name) |
| | continue |
| |
|
| | |
| | requirements = extract_requirements_from_code(full_code) |
| | print(f"π¦ Detected libraries: {', '.join(requirements)}") |
| |
|
| | |
| | space_name = notebook_name.lower().strip() |
| | space_name = re.sub(r'[^a-z0-9\-]', '-', space_name) |
| | while '--' in space_name: |
| | space_name = space_name.replace('--', '-') |
| | space_name = space_name.strip('-') |
| |
|
| | if not space_name: |
| | space_name = 'gradio-app' |
| |
|
| | space_id = f"{USERNAME}/{space_name}" |
| | print(f"π Creating Space: {space_id}") |
| |
|
| | |
| | create_repo( |
| | repo_id=space_id, |
| | repo_type="space", |
| | space_sdk="gradio", |
| | exist_ok=True |
| | ) |
| |
|
| | |
| | app_content = f"# {notebook_name}\n# Gradio application\n\n{cleaned_code}" |
| |
|
| | with open("app.py", "w", encoding='utf-8') as f: |
| | f.write(app_content) |
| |
|
| | |
| | api.upload_file( |
| | path_or_fileobj="app.py", |
| | path_in_repo="app.py", |
| | repo_id=space_id, |
| | repo_type="space", |
| | commit_message=f"Upload {notebook_name}" |
| | ) |
| | print("β
Uploaded app.py") |
| |
|
| | |
| | if requirements: |
| | with open("requirements.txt", "w") as f: |
| | f.write('\n'.join(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" |
| | ) |
| | print("β
Uploaded requirements.txt") |
| |
|
| | |
| | readme = f"""--- |
| | title: {notebook_name} |
| | emoji: π |
| | colorFrom: blue |
| | colorTo: green |
| | sdk: gradio |
| | pinned: false |
| | --- |
| | |
| | # {notebook_name} |
| | |
| | Gradio application for {notebook_name}. |
| | |
| | ## About |
| | |
| | This Space contains an interactive Gradio interface. |
| | """ |
| |
|
| | 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", |
| | commit_message="Add README" |
| | ) |
| | print("β
Uploaded README.md") |
| |
|
| | print("π SUCCESS!") |
| |
|
| | created_spaces.append({ |
| | 'name': notebook_name, |
| | 'url': f"https://huggingface.co/spaces/{space_id}" |
| | }) |
| |
|
| | except Exception as e: |
| | print(f"β ERROR: {e}") |
| | skipped_spaces.append(notebook_name) |
| | continue |
| |
|
| | |
| | print(f"\n{'='*70}") |
| | print(f"π SUMMARY") |
| | print(f"{'='*70}") |
| | print(f"β
Created: {len(created_spaces)} Gradio Spaces") |
| | print(f"β οΈ Skipped: {len(skipped_spaces)} notebooks") |
| | print(f"{'='*70}\n") |
| |
|
| | if created_spaces: |
| | print("Created Spaces:") |
| | for space in created_spaces: |
| | print(f" π {space['name']}") |
| | print(f" {space['url']}\n") |
| |
|
| | if skipped_spaces: |
| | print("\nSkipped notebooks (no Gradio or errors):") |
| | for name in skipped_spaces: |
| | print(f" β οΈ {name}") |
| |
|