| """ | |
| HuggingFace integration for notes – upload as Dataset or Space. | |
| Supports: free public hosting, sharing via link, and sync. | |
| """ | |
| import json | |
| import logging | |
| import os | |
| import tempfile | |
| from django.conf import settings | |
| from django.utils import timezone | |
| from huggingface_hub import HfApi, create_repo, upload_folder, whoami | |
| logger = logging.getLogger(__name__) | |
| HF_TOKEN = getattr(settings, "HUGGINGFACE_TOKEN", None) | |
| HF_USERNAME = getattr(settings, "HUGGINGFACE_USERNAME", None) | |
| def _get_api() -> HfApi: | |
| """Get authenticated HF API instance.""" | |
| token = HF_TOKEN | |
| if not token: | |
| raise ValueError( | |
| "HUGGINGFACE_TOKEN not set. Set HUGGINGFACE_TOKEN or run `huggingface-cli login`." | |
| ) | |
| return HfApi(token=token) | |
| def upload_note_to_hub(note, repo_type: str = "dataset", make_public: bool = True): | |
| """Upload a note to HuggingFace Hub as a Dataset.""" | |
| api = _get_api() | |
| username = HF_USERNAME or whoami(token=HF_TOKEN)["name"] | |
| repo_id = f"{username}/neuralops-note-{note.id}" | |
| try: | |
| create_repo( | |
| repo_id, | |
| token=HF_TOKEN, | |
| repo_type=repo_type, | |
| exist_ok=True, | |
| private=not make_public, | |
| ) | |
| except Exception: | |
| pass | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| readme_path = os.path.join(tmpdir, "README.md") | |
| with open(readme_path, "w") as f: | |
| f.write(_note_to_markdown(note)) | |
| meta_path = os.path.join(tmpdir, "metadata.json") | |
| with open(meta_path, "w") as f: | |
| json.dump( | |
| { | |
| "note_id": str(note.id), | |
| "title": note.title, | |
| "author": note.author.username, | |
| "note_type": note.note_type, | |
| "tags": note.tags, | |
| "created_at": note.created_at.isoformat(), | |
| "version": note.version, | |
| }, | |
| f, | |
| indent=2, | |
| ) | |
| upload_folder( | |
| folder_path=tmpdir, repo_id=repo_id, repo_type=repo_type, token=HF_TOKEN | |
| ) | |
| if make_public: | |
| try: | |
| api.update_repo_visibility( | |
| repo_id=repo_id, repo_type=repo_type, private=False, token=HF_TOKEN | |
| ) | |
| except Exception: | |
| pass | |
| return { | |
| "status": "uploaded", | |
| "url": f"https://huggingface.co/{repo_type}s/{repo_id}", | |
| "repo_id": repo_id, | |
| "repo_type": repo_type, | |
| } | |
| def upload_note_as_space(note): | |
| """Upload a note as a static HTML Space on HuggingFace (free hosting).""" | |
| api = _get_api() | |
| username = HF_USERNAME or whoami(token=HF_TOKEN)["name"] | |
| repo_id = f"{username}/neuralops-note-{note.id}" | |
| try: | |
| create_repo( | |
| repo_id, | |
| token=HF_TOKEN, | |
| repo_type="space", | |
| exist_ok=True, | |
| private=False, | |
| space_sdk="static", | |
| ) | |
| except Exception: | |
| pass | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| html = _note_to_html(note) | |
| with open(os.path.join(tmpdir, "index.html"), "w") as f: | |
| f.write(html) | |
| upload_folder( | |
| folder_path=tmpdir, repo_id=repo_id, repo_type="space", token=HF_TOKEN | |
| ) | |
| return { | |
| "status": "uploaded", | |
| "url": f"https://huggingface.co/spaces/{repo_id}", | |
| "repo_id": repo_id, | |
| "repo_type": "space", | |
| } | |
| def sync_note_to_hf(note): | |
| """Sync a note to HuggingFace. Re‑uploads if a previous URL exists.""" | |
| existing_url = (note.metadata or {}).get("hf_url", "") | |
| if existing_url: | |
| try: | |
| return upload_note_to_hub(note) | |
| except Exception as e: | |
| logger.warning(f"HF sync failed: {e}") | |
| return {"status": "error", "error": str(e)} | |
| return {"status": "skipped", "reason": "no existing HF upload"} | |
| def _note_to_markdown(note) -> str: | |
| """Convert a ResearcherNote to a rich Markdown string.""" | |
| return f"""--- | |
| title: "{note.title}" | |
| author: "{note.author.username}" | |
| type: "{note.note_type}" | |
| tags: {json.dumps(note.tags)} | |
| created: "{note.created_at.isoformat()}" | |
| version: {note.version} | |
| --- | |
| # {note.title or 'Untitled Note'} | |
| **Author:** {note.author.username} | |
| **Type:** {note.note_type} | |
| **Created:** {note.created_at.strftime('%Y-%m-%d %H:%M UTC')} | |
| **Tags:** {', '.join(note.tags) if note.tags else 'None'} | |
| --- | |
| {note.content} | |
| --- | |
| *Generated by NeuralOps — {timezone.now().strftime('%Y-%m-%d %H:%M UTC')}* | |
| """ | |
| def _note_to_html(note) -> str: | |
| """Convert note to a styled static HTML page for HF Spaces.""" | |
| import html as html_mod | |
| try: | |
| import markdown as md_lib | |
| content_html = md_lib.markdown( | |
| note.content or "", extensions=["fenced_code", "tables"] | |
| ) | |
| except ImportError: | |
| content_html = f"<pre>{html_mod.escape(note.content or '')}</pre>" | |
| return f"""<!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>{html_mod.escape(note.title or 'Untitled')}</title> | |
| <style> | |
| body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; | |
| max-width: 800px; margin: 0 auto; padding: 2rem; line-height: 1.6; color: #1a1a2e; }} | |
| h1 {{ border-bottom: 2px solid #667eea; padding-bottom: 0.5rem; }} | |
| .meta {{ color: #666; font-size: 0.9rem; margin-bottom: 2rem; }} | |
| .tag {{ display: inline-block; background: #667eea; color: white; padding: 0.2rem 0.6rem; | |
| border-radius: 12px; font-size: 0.8rem; margin-right: 0.3rem; }} | |
| pre {{ background: #f4f4f4; padding: 1rem; border-radius: 8px; overflow-x: auto; }} | |
| code {{ font-family: 'Fira Code', monospace; }} | |
| .footer {{ margin-top: 3rem; padding-top: 1rem; border-top: 1px solid #ddd; font-size: 0.8rem; color: #999; }} | |
| </style> | |
| </head> | |
| <body> | |
| <h1>{html_mod.escape(note.title or 'Untitled Note')}</h1> | |
| <div class="meta"> | |
| <strong>Author:</strong> {html_mod.escape(note.author.username)} · | |
| <strong>Type:</strong> {note.note_type} · | |
| <strong>Version:</strong> {note.version} | |
| </div> | |
| <div class="meta"> | |
| {''.join(f'<span class="tag">{html_mod.escape(t)}</span>' for t in (note.tags or []))} | |
| </div> | |
| <hr> | |
| {content_html} | |
| <div class="footer"> | |
| Generated by <strong>NeuralOps</strong> — {timezone.now().strftime('%Y-%m-%d %H:%M UTC')} | |
| </div> | |
| </body> | |
| </html>""" | |
Xet Storage Details
- Size:
- 6.57 kB
- Xet hash:
- f907dd141bfe3599ecf005cafb5722a05e4952c1284deb2e5b5dee0d87bb8dfd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.