File size: 2,931 Bytes
a6e0d48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Phase 1: Prepare the Backend for Hugging Face

  Since you have a custom FastAPI server (main.py) rather than a standard Gradio or Streamlit app, you should use the Docker SDK on Hugging Face Spaces.

   1. Configure the Space:
       * Go to your Hugging Face Space settings.
       * Ensure the "Space SDK" is set to Docker.

   2. Create a `Dockerfile`:
       * You will need to create a file named Dockerfile in your backend/ folder.
       * This file tells Hugging Face how to build your server. It effectively says: "Start with Python, install my requirements.txt, copy my files, and run main.py."
       * Note: Hugging Face Spaces expects the application to run on port 7860, not 8000.

   3. Handle Large Files (Git LFS):
       * Your finetuned_model.pt is likely a binary file. You must use Git LFS (Large File Storage) to upload it, or the upload might fail/corrupt.

  Phase 2: Upload to Hugging Face

  You will treat your Hugging Face Space just like a GitHub repository.

   1. Clone the Repository:
       * On your Space page, look for the instructions to "Clone repository."
       * Run that command locally to get a folder for your Space.

   2. Move Files:
       * Copy all files from your local backend/ folder (including main.py, requirements.txt, finetuned_model.pt, meta.pkl, and the new Dockerfile) into this new cloned folder.

   3. Push Changes:
       * Use standard git commands to upload:
   1         git add .
   2         git commit -m "Initial backend deploy"
   3         git push
       * *Note: If `finetuned_model.pt` is large, you might need to run `git lfs track "*.pt"` before adding.*

   4. Wait for Build:
       * Go back to your Space in the browser. You will see a "Building" status. Once it says "Running," your backend is live!
       * Copy the "Direct URL" (it usually looks like https://username-spacename.hf.space).

  Phase 3: Connect the Frontend

  Now you need to tell your React website to stop looking at localhost:8000 and look at the new Hugging Face URL.

   1. Update Environment Variables:
       * In your websitev1 folder, open your .env file.
       * Find the VITE_API_URL variable.
       * Change it from http://localhost:8000 to your new Hugging Face URL (e.g., https://chenhsu-chenbot.hf.space).
       * Important: Ensure there is no trailing slash `/` at the end of the URL if your code appends `/chat` manually.

   2. Rebuild and Deploy:
       * Since React is a build-time framework, you must rebuild your project for the new URL to take effect.
       * Run npm run build.
       * Deploy the new dist/ folder to your hosting provider (GitHub Pages, Vercel, Netlify, etc.).

  Summary Checklist
   * [ ] Space SDK: Set to Docker.
   * [ ] Dockerfile: Created to run app on port 7860.
   * [ ] Git LFS: Used for the .pt model file.
   * [ ] Frontend Env: VITE_API_URL updated to the HF Space URL.
   * [ ] Rebuild: Frontend rebuilt and redeployed.