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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Spaces Deployment Guide
|
| 2 |
+
|
| 3 |
+
This guide will walk you through deploying your FastAPI Search Engine to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## Prerequisites
|
| 6 |
+
|
| 7 |
+
1. A Hugging Face account (sign up at https://huggingface.co/join)
|
| 8 |
+
2. Git installed on your computer
|
| 9 |
+
3. Your application files ready
|
| 10 |
+
|
| 11 |
+
## Deployment Steps
|
| 12 |
+
|
| 13 |
+
### Option 1: Deploy via Web Interface (Easiest)
|
| 14 |
+
|
| 15 |
+
#### Step 1: Create a New Space
|
| 16 |
+
|
| 17 |
+
1. Go to https://huggingface.co/spaces
|
| 18 |
+
2. Click on **"Create new Space"** button
|
| 19 |
+
3. Fill in the details:
|
| 20 |
+
- **Space name**: `simple-search-engine` (or your preferred name)
|
| 21 |
+
- **License**: Choose appropriate license (e.g., MIT, Apache 2.0)
|
| 22 |
+
- **Select the SDK**: Choose **Docker**
|
| 23 |
+
- **Space hardware**: Start with **CPU basic** (free tier)
|
| 24 |
+
- **Visibility**: Public or Private (your choice)
|
| 25 |
+
4. Click **"Create Space"**
|
| 26 |
+
|
| 27 |
+
#### Step 2: Upload Files
|
| 28 |
+
|
| 29 |
+
After creating the Space, you'll see options to upload files:
|
| 30 |
+
|
| 31 |
+
1. **Upload the following files in this exact order:**
|
| 32 |
+
- `README.md` (the one provided)
|
| 33 |
+
- `Dockerfile` (the one provided)
|
| 34 |
+
- `requirements.txt` (your existing file)
|
| 35 |
+
- `main.py` (your existing file)
|
| 36 |
+
- `.gitignore` (optional, the one provided)
|
| 37 |
+
|
| 38 |
+
2. Click **"Commit to main"** after uploading each file
|
| 39 |
+
|
| 40 |
+
#### Step 3: Wait for Build
|
| 41 |
+
|
| 42 |
+
1. After uploading all files, Hugging Face will automatically start building your Space
|
| 43 |
+
2. You'll see build logs in the interface
|
| 44 |
+
3. The build process typically takes 3-5 minutes
|
| 45 |
+
4. Once complete, your app will be live at: `https://huggingface.co/spaces/YOUR-USERNAME/simple-search-engine`
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
### Option 2: Deploy via Git (Recommended for developers)
|
| 50 |
+
|
| 51 |
+
#### Step 1: Create Space on Hugging Face
|
| 52 |
+
|
| 53 |
+
1. Go to https://huggingface.co/spaces
|
| 54 |
+
2. Click **"Create new Space"**
|
| 55 |
+
3. Configure as described in Option 1, Step 1
|
| 56 |
+
4. After creation, copy the Git clone URL (shown on the Space page)
|
| 57 |
+
|
| 58 |
+
#### Step 2: Clone the Repository
|
| 59 |
+
|
| 60 |
+
Open your terminal and run:
|
| 61 |
+
|
| 62 |
+
```bash
|
| 63 |
+
# Clone the Space repository
|
| 64 |
+
git clone https://huggingface.co/spaces/YOUR-USERNAME/simple-search-engine
|
| 65 |
+
cd simple-search-engine
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
#### Step 3: Add Your Files
|
| 69 |
+
|
| 70 |
+
```bash
|
| 71 |
+
# Copy your files to the cloned directory
|
| 72 |
+
# Assuming you're in the simple-search-engine directory
|
| 73 |
+
|
| 74 |
+
# Copy files
|
| 75 |
+
cp /path/to/your/main.py .
|
| 76 |
+
cp /path/to/your/requirements.txt .
|
| 77 |
+
cp /path/to/provided/Dockerfile .
|
| 78 |
+
cp /path/to/provided/README.md .
|
| 79 |
+
cp /path/to/provided/.gitignore .
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
#### Step 4: Git Add, Commit, and Push
|
| 83 |
+
|
| 84 |
+
```bash
|
| 85 |
+
# Configure git LFS (if not already done)
|
| 86 |
+
git lfs install
|
| 87 |
+
|
| 88 |
+
# Add all files
|
| 89 |
+
git add .
|
| 90 |
+
|
| 91 |
+
# Commit changes
|
| 92 |
+
git commit -m "Initial deployment of search engine"
|
| 93 |
+
|
| 94 |
+
# Push to Hugging Face
|
| 95 |
+
git push
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
#### Step 5: Monitor Deployment
|
| 99 |
+
|
| 100 |
+
1. Go to your Space URL: `https://huggingface.co/spaces/YOUR-USERNAME/simple-search-engine`
|
| 101 |
+
2. Watch the build logs
|
| 102 |
+
3. Once built, your app will be live!
|
| 103 |
+
|
| 104 |
+
---
|
| 105 |
+
|
| 106 |
+
## Post-Deployment
|
| 107 |
+
|
| 108 |
+
### Testing Your Deployed App
|
| 109 |
+
|
| 110 |
+
1. Navigate to your Space URL
|
| 111 |
+
2. You should see the purple gradient search interface
|
| 112 |
+
3. Try sample queries like:
|
| 113 |
+
- "machine learning AI"
|
| 114 |
+
- "cloud services AWS"
|
| 115 |
+
- "financial revenue"
|
| 116 |
+
|
| 117 |
+
### Monitoring
|
| 118 |
+
|
| 119 |
+
- Check the **Logs** tab in your Space to see application logs
|
| 120 |
+
- Monitor build status and errors
|
| 121 |
+
- View usage statistics
|
| 122 |
+
|
| 123 |
+
### Updating Your App
|
| 124 |
+
|
| 125 |
+
When you need to update:
|
| 126 |
+
|
| 127 |
+
**Via Web Interface:**
|
| 128 |
+
1. Go to your Space β Files tab
|
| 129 |
+
2. Click on the file you want to edit
|
| 130 |
+
3. Make changes
|
| 131 |
+
4. Commit changes
|
| 132 |
+
5. Space will rebuild automatically
|
| 133 |
+
|
| 134 |
+
**Via Git:**
|
| 135 |
+
```bash
|
| 136 |
+
# Make changes to your files
|
| 137 |
+
git add .
|
| 138 |
+
git commit -m "Description of changes"
|
| 139 |
+
git push
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
---
|
| 143 |
+
|
| 144 |
+
## Troubleshooting
|
| 145 |
+
|
| 146 |
+
### Build Fails
|
| 147 |
+
|
| 148 |
+
**Issue**: Docker build fails
|
| 149 |
+
- **Solution**: Check the build logs for specific errors
|
| 150 |
+
- Common causes:
|
| 151 |
+
- Missing dependencies in requirements.txt
|
| 152 |
+
- Syntax errors in code
|
| 153 |
+
- Port configuration issues
|
| 154 |
+
|
| 155 |
+
### App Not Loading
|
| 156 |
+
|
| 157 |
+
**Issue**: Space built successfully but app doesn't load
|
| 158 |
+
- **Solution**:
|
| 159 |
+
- Ensure port 7860 is used (required by HF Spaces)
|
| 160 |
+
- Check application logs for runtime errors
|
| 161 |
+
- Verify NLTK downloads completed successfully
|
| 162 |
+
|
| 163 |
+
### Slow Model Loading
|
| 164 |
+
|
| 165 |
+
**Issue**: First request takes a long time
|
| 166 |
+
- **Solution**: This is normal - the SBERT model loads on first request
|
| 167 |
+
- The model stays in memory for subsequent requests
|
| 168 |
+
- Consider upgrading to better hardware for faster cold starts
|
| 169 |
+
|
| 170 |
+
### Out of Memory
|
| 171 |
+
|
| 172 |
+
**Issue**: App crashes with memory errors
|
| 173 |
+
- **Solution**:
|
| 174 |
+
- Upgrade to a better hardware tier (CPU upgrade or small GPU)
|
| 175 |
+
- Go to Settings β Change hardware
|
| 176 |
+
- Note: GPU tiers are paid
|
| 177 |
+
|
| 178 |
+
---
|
| 179 |
+
|
| 180 |
+
## Upgrading Hardware
|
| 181 |
+
|
| 182 |
+
If you need better performance:
|
| 183 |
+
|
| 184 |
+
1. Go to your Space β **Settings**
|
| 185 |
+
2. Scroll to **"Space hardware"**
|
| 186 |
+
3. Choose from available options:
|
| 187 |
+
- **CPU basic** (Free)
|
| 188 |
+
- **CPU upgrade** (Paid)
|
| 189 |
+
- **T4 small** (GPU, Paid)
|
| 190 |
+
- **T4 medium** (GPU, Paid)
|
| 191 |
+
4. Click **"Save"**
|
| 192 |
+
|
| 193 |
+
---
|
| 194 |
+
|
| 195 |
+
## Making Your Space Private
|
| 196 |
+
|
| 197 |
+
1. Go to Space β **Settings**
|
| 198 |
+
2. Find **"Visibility"** section
|
| 199 |
+
3. Change from "Public" to "Private"
|
| 200 |
+
4. Only you can access the Space when private
|
| 201 |
+
|
| 202 |
+
---
|
| 203 |
+
|
| 204 |
+
## Additional Resources
|
| 205 |
+
|
| 206 |
+
- [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces)
|
| 207 |
+
- [Docker Spaces Guide](https://huggingface.co/docs/hub/spaces-sdks-docker)
|
| 208 |
+
- [Sentence Transformers Documentation](https://www.sbert.net/)
|
| 209 |
+
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
|
| 210 |
+
|
| 211 |
+
---
|
| 212 |
+
|
| 213 |
+
## Quick Reference: File Structure
|
| 214 |
+
|
| 215 |
+
Your final file structure should look like:
|
| 216 |
+
|
| 217 |
+
```
|
| 218 |
+
simple-search-engine/
|
| 219 |
+
βββ .gitignore
|
| 220 |
+
βββ Dockerfile
|
| 221 |
+
βββ README.md
|
| 222 |
+
βββ main.py
|
| 223 |
+
βββ requirements.txt
|
| 224 |
+
```
|
| 225 |
+
|
| 226 |
+
---
|
| 227 |
+
|
| 228 |
+
## Support
|
| 229 |
+
|
| 230 |
+
If you encounter issues:
|
| 231 |
+
1. Check the build logs in your Space
|
| 232 |
+
2. Review the troubleshooting section above
|
| 233 |
+
3. Ask in Hugging Face Discord or Forums
|
| 234 |
+
4. Open an issue on Hugging Face Spaces GitHub
|
| 235 |
+
|
| 236 |
+
Good luck with your deployment! π
|
Dockerfile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.11 slim image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install system dependencies
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
build-essential \
|
| 10 |
+
curl \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Copy requirements file
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
|
| 16 |
+
# Install Python dependencies
|
| 17 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Download NLTK data
|
| 20 |
+
RUN python -c "import nltk; nltk.download('punkt'); nltk.download('punkt_tab')"
|
| 21 |
+
|
| 22 |
+
# Copy application code
|
| 23 |
+
COPY main.py .
|
| 24 |
+
|
| 25 |
+
# Expose port 7860 (required by Hugging Face Spaces)
|
| 26 |
+
EXPOSE 7860
|
| 27 |
+
|
| 28 |
+
# Run the application
|
| 29 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|