untitled4 / app.py
kasimali's picture
Upload folder using huggingface_hub
18c3501 verified
# Untitled4
from huggingface_hub import HfApi, create_repo, login
import os
import json
import glob
# FORCE FRESH LOGIN
print("=" * 70)
print("IMPORTANT: You need a token with WRITE permission!")
print("Get one from: https://huggingface.co/settings/tokens")
print("=" * 70)
# Clear any cached tokens
if os.path.exists(os.path.expanduser("~/.huggingface/token")):
os.remove(os.path.expanduser("~/.huggingface/token"))
# Login with prompt
# Verify login works
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
# Configuration
USERNAME = user_info['name']
# Mount Google Drive
print("Mounting Google Drive...")
from google.colab import drive
# Find all notebooks
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:
# Read notebook
with open(nb_path, 'r', encoding='utf-8') as f:
notebook_data = json.load(f)
# Extract code
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)
# Clean space name (fix -- issue)
space_name = notebook_name.lower().strip()
space_name = space_name.replace(" ", "-").replace("_", "-")
# Remove multiple consecutive dashes
while "--" in space_name:
space_name = space_name.replace("--", "-")
# Remove leading/trailing dashes
space_name = space_name.strip("-")
# Keep only valid chars
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 Space
create_repo(
repo_id=space_id,
repo_type="space",
space_sdk=sdk,
exist_ok=True,
token=True
)
print("Space created")
# Create app.py
app_content = f"# {notebook_name}\n\n{full_code}"
with open("app.py", "w", encoding='utf-8') as f:
f.write(app_content)
# Upload files
api.upload_file(
path_or_fileobj="app.py",
path_in_repo="app.py",
repo_id=space_id,
repo_type="space"
)
# Requirements
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
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
# Summary
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")
# CELL 1: Delete broken/recent spaces
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")
# Show all for selection
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
# Login
USERNAME = "kasimali"
# Mount Drive
from google.colab import drive
# Find all notebooks
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()
# Skip pip install
if stripped.startswith('!pip') or stripped.startswith('!apt'):
continue
# Skip all magic commands
if stripped.startswith('!') or stripped.startswith('%'):
continue
# Skip authentication
continue
# Skip drive mounting
continue
# Skip file downloads/uploads
if 'files.download' in line or 'files.upload' in line:
continue
# Skip cell markers
if stripped.startswith('# Cell'):
continue
# Keep the line
cleaned.append(line)
return '\n'.join(cleaned)
def extract_requirements_from_code(code):
"""Extract library requirements from code"""
libs = set()
# Library mappings
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',
}
# Check imports
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)
# Also check for version-specific installs from pip commands
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:
# Read notebook
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
# Extract all code cells
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
# Combine code
full_code = '\n\n'.join(all_code)
# Check for Gradio
if not has_gradio_interface(full_code):
print("⚠️ No Gradio interface found, skipping")
skipped_spaces.append(notebook_name)
continue
print("βœ… Found Gradio app!")
# Clean the code
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
# Extract requirements
requirements = extract_requirements_from_code(full_code)
print(f"πŸ“¦ Detected libraries: {', '.join(requirements)}")
# Create space name
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 Space
create_repo(
repo_id=space_id,
repo_type="space",
space_sdk="gradio",
exist_ok=True
)
# Create app.py
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)
# Upload app.py
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")
# Create requirements.txt
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")
# Create README
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
# Final Summary
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}")