akhaliq HF Staff commited on
Commit
0d4ff7c
·
1 Parent(s): 425a6d5

update for react

Browse files
Files changed (1) hide show
  1. app.py +85 -12
app.py CHANGED
@@ -1893,7 +1893,7 @@ Output format (CRITICAL):
1893
  - Do NOT wrap files in Markdown code fences or use === markers inside file content
1894
 
1895
  CRITICAL Requirements:
1896
- 1. Always include a Dockerfile configured for Node.js deployment
1897
  2. Use Next.js with TypeScript/JSX (.jsx files for components)
1898
  3. Include Tailwind CSS for styling (in postcss.config.js and tailwind.config.js)
1899
  4. Create necessary components in the components/ directory
@@ -1905,13 +1905,75 @@ CRITICAL Requirements:
1905
  10. Make the application fully responsive
1906
  11. Include proper error handling and loading states
1907
  12. Follow accessibility best practices
 
1908
 
1909
- Dockerfile Requirements:
1910
- - Use Node.js 18+ base image
1911
- - Install dependencies with npm install
1912
- - Run "npm run build" to build Next.js app
1913
- - Expose port 3000
1914
- - Start with "npm start"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1915
 
1916
  IMPORTANT: Always include "Built with anycoder" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder
1917
  """
@@ -7004,8 +7066,14 @@ Generate the exact search/replace blocks needed to make these changes."""
7004
 
7005
  # Deploy to Spaces logic
7006
 
7007
- def add_anycoder_tag_to_readme(api, repo_id):
7008
- """Download existing README, add anycoder tag, and upload back."""
 
 
 
 
 
 
7009
  try:
7010
  import tempfile
7011
  import re
@@ -7038,6 +7106,10 @@ def add_anycoder_tag_to_readme(api, repo_id):
7038
  # Add tags section with anycoder
7039
  frontmatter += '\ntags:\n- anycoder'
7040
 
 
 
 
 
7041
  # Reconstruct the README
7042
  new_content = f"---\n{frontmatter}\n---{body}"
7043
  else:
@@ -7045,7 +7117,8 @@ def add_anycoder_tag_to_readme(api, repo_id):
7045
  new_content = content.replace('---', '---\ntags:\n- anycoder\n---', 1)
7046
  else:
7047
  # No frontmatter, add it at the beginning
7048
- new_content = f"---\ntags:\n- anycoder\n---\n\n{content}"
 
7049
 
7050
  # Upload the modified README
7051
  with tempfile.NamedTemporaryFile("w", suffix=".md", delete=False, encoding='utf-8') as f:
@@ -9536,8 +9609,8 @@ with gr.Blocks(
9536
  if not success:
9537
  return gr.update(value=f"Error uploading {file_name}: {last_error}", visible=True)
9538
 
9539
- # Add anycoder tag to existing README
9540
- add_anycoder_tag_to_readme(api, repo_id)
9541
 
9542
  space_url = f"https://huggingface.co/spaces/{repo_id}"
9543
  action_text = "Updated" if is_update else "Deployed"
 
1893
  - Do NOT wrap files in Markdown code fences or use === markers inside file content
1894
 
1895
  CRITICAL Requirements:
1896
+ 1. Always include a Dockerfile configured for Node.js deployment (see Dockerfile Requirements below)
1897
  2. Use Next.js with TypeScript/JSX (.jsx files for components)
1898
  3. Include Tailwind CSS for styling (in postcss.config.js and tailwind.config.js)
1899
  4. Create necessary components in the components/ directory
 
1905
  10. Make the application fully responsive
1906
  11. Include proper error handling and loading states
1907
  12. Follow accessibility best practices
1908
+ 13. Configure next.config.js properly for HuggingFace Spaces deployment
1909
 
1910
+ next.config.js Requirements:
1911
+ - Must be configured to work on any host (0.0.0.0)
1912
+ - Should not have hardcoded localhost references
1913
+ - Example minimal configuration:
1914
+ ```javascript
1915
+ /** @type {import('next').NextConfig} */
1916
+ const nextConfig = {
1917
+ reactStrictMode: true,
1918
+ // Allow the app to work on HuggingFace Spaces
1919
+ output: 'standalone',
1920
+ }
1921
+
1922
+ module.exports = nextConfig
1923
+ ```
1924
+
1925
+ Dockerfile Requirements (CRITICAL for HuggingFace Spaces):
1926
+ - Use Node.js 18+ base image (e.g., FROM node:18-slim)
1927
+ - Set up a user with ID 1000 for proper permissions:
1928
+ ```
1929
+ RUN useradd -m -u 1000 user
1930
+ USER user
1931
+ ENV HOME=/home/user \\
1932
+ PATH=/home/user/.local/bin:$PATH
1933
+ WORKDIR $HOME/app
1934
+ ```
1935
+ - ALWAYS use --chown=user with COPY and ADD commands:
1936
+ ```
1937
+ COPY --chown=user package*.json ./
1938
+ COPY --chown=user . .
1939
+ ```
1940
+ - Install dependencies: RUN npm install
1941
+ - Build the app: RUN npm run build
1942
+ - Expose port 7860 (HuggingFace Spaces default): EXPOSE 7860
1943
+ - Start with: CMD ["npm", "start", "--", "-p", "7860"]
1944
+ - If using a different port, make sure to set app_port in the README.md YAML frontmatter
1945
+
1946
+ Example Dockerfile structure:
1947
+ ```dockerfile
1948
+ FROM node:18-slim
1949
+
1950
+ # Set up user with ID 1000
1951
+ RUN useradd -m -u 1000 user
1952
+ USER user
1953
+ ENV HOME=/home/user \\
1954
+ PATH=/home/user/.local/bin:$PATH
1955
+
1956
+ # Set working directory
1957
+ WORKDIR $HOME/app
1958
+
1959
+ # Copy package files with proper ownership
1960
+ COPY --chown=user package*.json ./
1961
+
1962
+ # Install dependencies
1963
+ RUN npm install
1964
+
1965
+ # Copy rest of the application with proper ownership
1966
+ COPY --chown=user . .
1967
+
1968
+ # Build the Next.js app
1969
+ RUN npm run build
1970
+
1971
+ # Expose port 7860
1972
+ EXPOSE 7860
1973
+
1974
+ # Start the application on port 7860
1975
+ CMD ["npm", "start", "--", "-p", "7860"]
1976
+ ```
1977
 
1978
  IMPORTANT: Always include "Built with anycoder" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder
1979
  """
 
7066
 
7067
  # Deploy to Spaces logic
7068
 
7069
+ def add_anycoder_tag_to_readme(api, repo_id, app_port=None):
7070
+ """Download existing README, add anycoder tag and app_port if needed, and upload back.
7071
+
7072
+ Args:
7073
+ api: HuggingFace API client
7074
+ repo_id: Repository ID
7075
+ app_port: Optional port number to set for Docker spaces (e.g., 7860 for React apps)
7076
+ """
7077
  try:
7078
  import tempfile
7079
  import re
 
7106
  # Add tags section with anycoder
7107
  frontmatter += '\ntags:\n- anycoder'
7108
 
7109
+ # Add app_port if specified and not already present
7110
+ if app_port is not None and 'app_port:' not in frontmatter:
7111
+ frontmatter += f'\napp_port: {app_port}'
7112
+
7113
  # Reconstruct the README
7114
  new_content = f"---\n{frontmatter}\n---{body}"
7115
  else:
 
7117
  new_content = content.replace('---', '---\ntags:\n- anycoder\n---', 1)
7118
  else:
7119
  # No frontmatter, add it at the beginning
7120
+ app_port_line = f'\napp_port: {app_port}' if app_port else ''
7121
+ new_content = f"---\ntags:\n- anycoder{app_port_line}\n---\n\n{content}"
7122
 
7123
  # Upload the modified README
7124
  with tempfile.NamedTemporaryFile("w", suffix=".md", delete=False, encoding='utf-8') as f:
 
9609
  if not success:
9610
  return gr.update(value=f"Error uploading {file_name}: {last_error}", visible=True)
9611
 
9612
+ # Add anycoder tag and app_port to existing README
9613
+ add_anycoder_tag_to_readme(api, repo_id, app_port=7860)
9614
 
9615
  space_url = f"https://huggingface.co/spaces/{repo_id}"
9616
  action_text = "Updated" if is_update else "Deployed"