[dyad] commited on
Commit
8239d81
·
1 Parent(s): 7905940

[dyad] Fixing Hugging Face secret handling - wrote 2 file(s)

Browse files
Files changed (2) hide show
  1. Dockerfile +13 -23
  2. src/pages/Index.tsx +6 -2
Dockerfile CHANGED
@@ -1,12 +1,7 @@
1
- # Stage 1: Build the application
2
  # Use a specific Node.js version for consistency
3
- FROM node:20-alpine AS builder
4
-
5
- # Explicitly declare the build argument we expect from Hugging Face secrets
6
- ARG VITE_GEMINI_API_KEY
7
- # Set it as an environment variable for the build process
8
- ENV VITE_GEMINI_API_KEY=$VITE_GEMINI_API_KEY
9
 
 
10
  WORKDIR /app
11
 
12
  # Copy package files and install dependencies
@@ -16,28 +11,23 @@ RUN npm install
16
  # Copy the rest of the application source code
17
  COPY . .
18
 
19
- # Build the application.
20
- # Vite will now replace import.meta.env.VITE_GEMINI_API_KEY with the value
21
- # of the environment variable we just set.
22
- RUN npm run build
23
-
24
- # Stage 2: Serve the application
25
- # Use a slim Node.js image for a smaller final image size
26
- FROM node:20-alpine
27
- WORKDIR /app
28
 
29
- # Copy built assets from the builder stage
30
- COPY --from=builder /app/dist ./dist
 
31
 
32
- # Copy package.json to install 'serve'
33
- COPY package.json ./
34
 
35
- # Install only the 'serve' dependency for production
36
  RUN npm install --production serve
37
 
38
  # Expose the port that Hugging Face Spaces uses by default
39
  EXPOSE 7860
40
 
41
- # Command to start the server
42
- # This serves the 'dist' folder and listens on port 7860
43
  CMD ["npx", "serve", "-s", "dist", "-l", "7860"]
 
 
1
  # Use a specific Node.js version for consistency
2
+ FROM node:20-alpine
 
 
 
 
 
3
 
4
+ # Set the working directory inside the container
5
  WORKDIR /app
6
 
7
  # Copy package files and install dependencies
 
11
  # Copy the rest of the application source code
12
  COPY . .
13
 
14
+ # Hugging Face automatically provides secrets as build arguments.
15
+ # We need to ensure Vite can access it during the build.
16
+ # The ARG instruction makes the secret available during the build.
17
+ ARG VITE_GEMINI_API_KEY
 
 
 
 
 
18
 
19
+ # This is the key part: we must explicitly set it as an environment variable
20
+ # so Vite's build process can replace import.meta.env.VITE_GEMINI_API_KEY
21
+ ENV VITE_GEMINI_API_KEY=${VITE_GEMINI_API_KEY}
22
 
23
+ # Build the application
24
+ RUN npm run build
25
 
26
+ # Install 'serve' to run the built application
27
  RUN npm install --production serve
28
 
29
  # Expose the port that Hugging Face Spaces uses by default
30
  EXPOSE 7860
31
 
32
+ # Command to start the server, serving the 'dist' folder
 
33
  CMD ["npx", "serve", "-s", "dist", "-l", "7860"]
src/pages/Index.tsx CHANGED
@@ -49,14 +49,18 @@ const Index = () => {
49
  const iframeRef = useRef<HTMLIFrameElement>(null);
50
 
51
  useEffect(() => {
52
- if (import.meta.env.VITE_GEMINI_API_KEY) {
 
 
 
53
  setIsAIReady(true);
54
  } else {
55
  setIsAIReady(false);
56
  toast({
57
  title: "AI Not Ready",
58
- description: "Gemini API key is not configured. Please set it in your deployment environment secrets.",
59
  variant: "destructive",
 
60
  });
61
  }
62
  }, [toast]);
 
49
  const iframeRef = useRef<HTMLIFrameElement>(null);
50
 
51
  useEffect(() => {
52
+ // Explicitly check for the environment variable
53
+ const apiKey = import.meta.env.VITE_GEMINI_API_KEY;
54
+
55
+ if (apiKey && apiKey !== "undefined") {
56
  setIsAIReady(true);
57
  } else {
58
  setIsAIReady(false);
59
  toast({
60
  title: "AI Not Ready",
61
+ description: "Gemini API key is not configured. Please set VITE_GEMINI_API_KEY in your Hugging Face Space secrets.",
62
  variant: "destructive",
63
+ duration: 10000 // Show for longer to ensure visibility
64
  });
65
  }
66
  }, [toast]);