add tag to readme
Browse files2115 unmodified lines211621172118211921202121715 unmodified lines283728382839284028412842101 unmodified lines2944294529462947294829492950295129523 unmodified lines29562957295829592960296188 unmodified lines3050305130523053305430559 unmodified lines30653066306730683069307052 unmodified lines3123312431253126312731282115 unmodified lines# Deploy to Spaces logicdef extract_import_statements(code):"""Extract import statements from generated code."""import ast715 unmodified linesif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Upload the user's code to src/streamlit_app.py (for both new and existing spaces)with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)101 unmodified linesrepo_id=repo_id,repo_type="space")space_url = f"https://huggingface.co/spaces/{repo_id}"action_text = "Updated" if is_update else "Deployed"return gr.update(value=f"✅ {action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)except Exception as e:error_msg = str(e)if "403 Forbidden" in error_msg and "write token" in error_msg:3 unmodified linesfinally:import osos.unlink(temp_path)except Exception as e:# Handle potential RepoUrl object errors88 unmodified linesimport osos.unlink(temp_path)# Success - all files uploadedspace_url = f"https://huggingface.co/spaces/{actual_repo_id}"action_text = "Updated" if is_update else "Deployed"9 unmodified linesif sdk == "static":import timefile_name = "index.html"# Wait and retry logic after repo creationmax_attempts = 3for attempt in range(max_attempts):52 unmodified linesif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Now upload the main app.py filefile_name = "app.py"with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:2115 unmodified lines2116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182715 unmodified lines289828992900290129022903290429052906101 unmodified lines3008300930103011301230133 unmodified lines301730183019302030213022302330243025302630273028302988 unmodified lines3118311931203121312231233124312531269 unmodified lines313631373138313931403141314231433144314552 unmodified lines3198319932003201320232033204320532062115 unmodified lines# Deploy to Spaces logicdef add_anycoder_tag_to_readme(api, repo_id):"""Download existing README, add anycoder tag, and upload back."""try:import tempfileimport re# Download the existing READMEreadme_path = api.hf_hub_download(repo_id=repo_id,filename="README.md",repo_type="space")# Read the existing README contentwith open(readme_path, 'r', encoding='utf-8') as f:content = f.read()# Parse frontmatter and contentif content.startswith('---'):# Split frontmatter and bodyparts = content.split('---', 2)if len(parts) >= 3:frontmatter = parts[1].strip()body = parts[2] if len(parts) > 2 else ""# Check if tags already existif 'tags:' in frontmatter:# Add anycoder to existing tags if not presentif '- anycoder' not in frontmatter:frontmatter = re.sub(r'(tags:\s*\n(?:\s*-\s*[^\n]+\n)*)', r'\1- anycoder\n', frontmatter)else:# Add tags section with anycoderfrontmatter += '\ntags:\n- anycoder'# Reconstruct the READMEnew_content = f"---\n{frontmatter}\n---{body}"else:# Malformed frontmatter, just add tags at the end of frontmatternew_content = content.replace('---', '---\ntags:\n- anycoder\n---', 1)else:# No frontmatter, add it at the beginningnew_content = f"---\ntags:\n- anycoder\n---\n\n{content}"# Upload the modified READMEwith tempfile.NamedTemporaryFile("w", suffix=".md", delete=False, encoding='utf-8') as f:f.write(new_content)temp_path = f.nameapi.upload_file(path_or_fileobj=temp_path,path_in_repo="README.md",repo_id=repo_id,repo_type="space")import osos.unlink(temp_path)except Exception as e:print(f"Warning: Could not modify README.md to add anycoder tag: {e}")def extract_import_statements(code):"""Extract import statements from generated code."""import ast715 unmodified linesif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Add anycoder tag to existing READMEadd_anycoder_tag_to_readme(api, repo_id)# Upload the user's code to src/streamlit_app.py (for both new and existing spaces)with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:f.write(code)101 unmodified linesrepo_id=repo_id,repo_type="space")except Exception as e:error_msg = str(e)if "403 Forbidden" in error_msg and "write token" in error_msg:3 unmodified linesfinally:import osos.unlink(temp_path)# Add anycoder tag to existing READMEadd_anycoder_tag_to_readme(api, repo_id)space_url = f"https://huggingface.co/spaces/{repo_id}"action_text = "Updated" if is_update else "Deployed"return gr.update(value=f"✅ {action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)except Exception as e:# Handle potential RepoUrl object errors88 unmodified linesimport osos.unlink(temp_path)# Add anycoder tag to existing READMEadd_anycoder_tag_to_readme(api, actual_repo_id)# Success - all files uploadedspace_url = f"https://huggingface.co/spaces/{actual_repo_id}"action_text = "Updated" if is_update else "Deployed"9 unmodified linesif sdk == "static":import timefile_name = "index.html"# Add anycoder tag to existing README (after repo creation)add_anycoder_tag_to_readme(api, repo_id)# Wait and retry logic after repo creationmax_attempts = 3for attempt in range(max_attempts):52 unmodified linesif 'requirements_temp_path' in locals():os.unlink(requirements_temp_path)# Add anycoder tag to existing READMEadd_anycoder_tag_to_readme(api, repo_id)# Now upload the main app.py filefile_name = "app.py"with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
app.py+81-3