chen-bot-api-v1 / random_notes.txt
chenhsu92's picture
Deploying backend with Dockerfile and Model
a6e0d48
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.