akhaliq HF Staff commited on
Commit
cc7302a
·
1 Parent(s): e512265

add tag to readme

Browse files
Files changed (1) hide show
  1. app.py +81 -3
app.py CHANGED
@@ -2116,6 +2116,67 @@ This will help me create a better design for you."""
2116
 
2117
  # Deploy to Spaces logic
2118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2119
  def extract_import_statements(code):
2120
  """Extract import statements from generated code."""
2121
  import ast
@@ -2837,6 +2898,9 @@ with gr.Blocks(
2837
  if 'requirements_temp_path' in locals():
2838
  os.unlink(requirements_temp_path)
2839
 
 
 
 
2840
  # Upload the user's code to src/streamlit_app.py (for both new and existing spaces)
2841
  with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
2842
  f.write(code)
@@ -2944,9 +3008,6 @@ with gr.Blocks(
2944
  repo_id=repo_id,
2945
  repo_type="space"
2946
  )
2947
- space_url = f"https://huggingface.co/spaces/{repo_id}"
2948
- action_text = "Updated" if is_update else "Deployed"
2949
- return gr.update(value=f"✅ {action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)
2950
  except Exception as e:
2951
  error_msg = str(e)
2952
  if "403 Forbidden" in error_msg and "write token" in error_msg:
@@ -2956,6 +3017,13 @@ with gr.Blocks(
2956
  finally:
2957
  import os
2958
  os.unlink(temp_path)
 
 
 
 
 
 
 
2959
 
2960
  except Exception as e:
2961
  # Handle potential RepoUrl object errors
@@ -3050,6 +3118,9 @@ with gr.Blocks(
3050
  import os
3051
  os.unlink(temp_path)
3052
 
 
 
 
3053
  # Success - all files uploaded
3054
  space_url = f"https://huggingface.co/spaces/{actual_repo_id}"
3055
  action_text = "Updated" if is_update else "Deployed"
@@ -3065,6 +3136,10 @@ with gr.Blocks(
3065
  if sdk == "static":
3066
  import time
3067
  file_name = "index.html"
 
 
 
 
3068
  # Wait and retry logic after repo creation
3069
  max_attempts = 3
3070
  for attempt in range(max_attempts):
@@ -3123,6 +3198,9 @@ with gr.Blocks(
3123
  if 'requirements_temp_path' in locals():
3124
  os.unlink(requirements_temp_path)
3125
 
 
 
 
3126
  # Now upload the main app.py file
3127
  file_name = "app.py"
3128
  with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
 
2116
 
2117
  # Deploy to Spaces logic
2118
 
2119
+ def add_anycoder_tag_to_readme(api, repo_id):
2120
+ """Download existing README, add anycoder tag, and upload back."""
2121
+ try:
2122
+ import tempfile
2123
+ import re
2124
+
2125
+ # Download the existing README
2126
+ readme_path = api.hf_hub_download(
2127
+ repo_id=repo_id,
2128
+ filename="README.md",
2129
+ repo_type="space"
2130
+ )
2131
+
2132
+ # Read the existing README content
2133
+ with open(readme_path, 'r', encoding='utf-8') as f:
2134
+ content = f.read()
2135
+
2136
+ # Parse frontmatter and content
2137
+ if content.startswith('---'):
2138
+ # Split frontmatter and body
2139
+ parts = content.split('---', 2)
2140
+ if len(parts) >= 3:
2141
+ frontmatter = parts[1].strip()
2142
+ body = parts[2] if len(parts) > 2 else ""
2143
+
2144
+ # Check if tags already exist
2145
+ if 'tags:' in frontmatter:
2146
+ # Add anycoder to existing tags if not present
2147
+ if '- anycoder' not in frontmatter:
2148
+ frontmatter = re.sub(r'(tags:\s*\n(?:\s*-\s*[^\n]+\n)*)', r'\1- anycoder\n', frontmatter)
2149
+ else:
2150
+ # Add tags section with anycoder
2151
+ frontmatter += '\ntags:\n- anycoder'
2152
+
2153
+ # Reconstruct the README
2154
+ new_content = f"---\n{frontmatter}\n---{body}"
2155
+ else:
2156
+ # Malformed frontmatter, just add tags at the end of frontmatter
2157
+ new_content = content.replace('---', '---\ntags:\n- anycoder\n---', 1)
2158
+ else:
2159
+ # No frontmatter, add it at the beginning
2160
+ new_content = f"---\ntags:\n- anycoder\n---\n\n{content}"
2161
+
2162
+ # Upload the modified README
2163
+ with tempfile.NamedTemporaryFile("w", suffix=".md", delete=False, encoding='utf-8') as f:
2164
+ f.write(new_content)
2165
+ temp_path = f.name
2166
+
2167
+ api.upload_file(
2168
+ path_or_fileobj=temp_path,
2169
+ path_in_repo="README.md",
2170
+ repo_id=repo_id,
2171
+ repo_type="space"
2172
+ )
2173
+
2174
+ import os
2175
+ os.unlink(temp_path)
2176
+
2177
+ except Exception as e:
2178
+ print(f"Warning: Could not modify README.md to add anycoder tag: {e}")
2179
+
2180
  def extract_import_statements(code):
2181
  """Extract import statements from generated code."""
2182
  import ast
 
2898
  if 'requirements_temp_path' in locals():
2899
  os.unlink(requirements_temp_path)
2900
 
2901
+ # Add anycoder tag to existing README
2902
+ add_anycoder_tag_to_readme(api, repo_id)
2903
+
2904
  # Upload the user's code to src/streamlit_app.py (for both new and existing spaces)
2905
  with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
2906
  f.write(code)
 
3008
  repo_id=repo_id,
3009
  repo_type="space"
3010
  )
 
 
 
3011
  except Exception as e:
3012
  error_msg = str(e)
3013
  if "403 Forbidden" in error_msg and "write token" in error_msg:
 
3017
  finally:
3018
  import os
3019
  os.unlink(temp_path)
3020
+
3021
+ # Add anycoder tag to existing README
3022
+ add_anycoder_tag_to_readme(api, repo_id)
3023
+
3024
+ space_url = f"https://huggingface.co/spaces/{repo_id}"
3025
+ action_text = "Updated" if is_update else "Deployed"
3026
+ return gr.update(value=f"✅ {action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)
3027
 
3028
  except Exception as e:
3029
  # Handle potential RepoUrl object errors
 
3118
  import os
3119
  os.unlink(temp_path)
3120
 
3121
+ # Add anycoder tag to existing README
3122
+ add_anycoder_tag_to_readme(api, actual_repo_id)
3123
+
3124
  # Success - all files uploaded
3125
  space_url = f"https://huggingface.co/spaces/{actual_repo_id}"
3126
  action_text = "Updated" if is_update else "Deployed"
 
3136
  if sdk == "static":
3137
  import time
3138
  file_name = "index.html"
3139
+
3140
+ # Add anycoder tag to existing README (after repo creation)
3141
+ add_anycoder_tag_to_readme(api, repo_id)
3142
+
3143
  # Wait and retry logic after repo creation
3144
  max_attempts = 3
3145
  for attempt in range(max_attempts):
 
3198
  if 'requirements_temp_path' in locals():
3199
  os.unlink(requirements_temp_path)
3200
 
3201
+ # Add anycoder tag to existing README
3202
+ add_anycoder_tag_to_readme(api, repo_id)
3203
+
3204
  # Now upload the main app.py file
3205
  file_name = "app.py"
3206
  with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f: