File size: 4,944 Bytes
60c56d7 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# Setup Guide for Colorize API
## Prerequisites
1. Python 3.10+
2. Docker (for containerized deployment)
3. Firebase Admin SDK credentials file
4. Hugging Face account with access to the Colorize Space
## Local Development Setup
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Firebase Credentials
Copy your Firebase Admin SDK JSON file to the project root:
- Source: `C:\Colorize\colorize-662df-firebase-adminsdk-fbsvc-e080668793.json`
- Destination: `colorize-662df-firebase-adminsdk-fbsvc-e080668793.json`
Or set the path in environment variable:
```bash
export FIREBASE_CREDENTIALS_PATH=/path/to/your/firebase-credentials.json
```
### 3. Run the API
```bash
uvicorn app.main:app --reload --port 7860
```
The API will be available at `http://localhost:7860`
## Docker Setup
### 1. Build the Docker Image
```bash
docker build -t colorize-api .
```
### 2. Run the Container
```bash
docker run -p 7860:7860 \
-v $(pwd)/colorize-662df-firebase-adminsdk-fbsvc-e080668793.json:/app/colorize-662df-firebase-adminsdk-fbsvc-e080668793.json \
-e BASE_URL=http://localhost:7860 \
colorize-api
```
## Hugging Face Spaces Deployment
### 1. Prepare Your Repository
```bash
# Initialize git repository
git init
git add .
git commit -m "Initial commit: Colorize API with Firebase App Check"
```
### 2. Set Up Hugging Face Space
1. Go to https://huggingface.co/spaces/LogicGoInfotechSpaces/Colorize
2. If the space doesn't exist, create a new Docker space
### 3. Add Firebase Credentials as Secret
In Hugging Face Space settings:
1. Go to Settings → Secrets
2. Add a new secret:
- Name: `FIREBASE_CREDENTIALS`
- Value: Contents of your Firebase Admin SDK JSON file
### 4. Update Dockerfile for Secrets (if needed)
The Dockerfile will automatically use the credentials file if it exists. For Hugging Face Spaces, you may need to create the file from secrets:
```dockerfile
# Add this to Dockerfile if using HF secrets
RUN echo "$FIREBASE_CREDENTIALS" > colorize-662df-firebase-adminsdk-fbsvc-e080668793.json || true
```
### 5. Push to Hugging Face
```bash
git remote add origin https://huggingface.co/spaces/LogicGoInfotechSpaces/Colorize
git push -u origin main
```
## Testing the API
### Health Check
```bash
curl http://localhost:7860/health
```
### Upload Image (with App Check token)
```bash
curl -X POST http://localhost:7860/upload \
-H "X-Firebase-AppCheck: YOUR_APP_CHECK_TOKEN" \
-F "file=@path/to/image.jpg"
```
### Colorize Image (with App Check token)
```bash
curl -X POST http://localhost:7860/colorize \
-H "X-Firebase-AppCheck: YOUR_APP_CHECK_TOKEN" \
-F "file=@path/to/grayscale_image.jpg"
```
## Frontend Integration
### Initialize Firebase App Check
```javascript
import { initializeApp } from "firebase/app";
import { initializeAppCheck, ReCaptchaEnterpriseProvider } from "firebase/app-check";
const firebaseConfig = {
apiKey: "AIzaSyBIB6rcfyyqy5niERTXWvVD714Ter4Vx68",
authDomain: "colorize-662df.firebaseapp.com",
projectId: "colorize-662df",
storageBucket: "colorize-662df.firebasestorage.app",
messagingSenderId: "69166278311",
appId: "1:69166278311:web:0e8c50b8dd8627aaeadd82",
measurementId: "G-58CC2J8XKX"
};
const app = initializeApp(firebaseConfig);
// Initialize App Check
const appCheck = initializeAppCheck(app, {
provider: new ReCaptchaEnterpriseProvider('YOUR_RECAPTCHA_SITE_KEY'),
isTokenAutoRefreshEnabled: true
});
// Get token and make API call
async function colorizeImage(imageFile) {
const token = await appCheck.getToken();
const formData = new FormData();
formData.append('file', imageFile);
const response = await fetch('https://your-space.hf.space/colorize', {
method: 'POST',
headers: {
'X-Firebase-AppCheck': token.token
},
body: formData
});
const result = await response.json();
console.log('Colorized image URL:', result.download_url);
return result;
}
```
## Troubleshooting
### Model Loading Issues
If the ColorizeNet model fails to load:
1. Check your internet connection (model downloads from Hugging Face)
2. Verify you have sufficient disk space
3. Check logs for specific error messages
### Firebase App Check Issues
If App Check verification fails:
1. Verify your Firebase credentials file is correct
2. Check that App Check is enabled in Firebase Console
3. Ensure the token is being sent in the `X-Firebase-AppCheck` header
### Port Issues
Hugging Face Spaces uses port 7860 by default. Make sure your Dockerfile exposes this port.
## Environment Variables
- `FIREBASE_CREDENTIALS_PATH`: Path to Firebase Admin SDK JSON file
- `ENABLE_APP_CHECK`: Enable/disable Firebase App Check (true/false)
- `BASE_URL`: Base URL for generating download URLs
- `PORT`: Port to run the API on (default: 7860)
- `MODEL_ID`: Hugging Face model ID (default: rsortino/ColorizeNet)
- `NUM_INFERENCE_STEPS`: Number of inference steps (default: 20)
|