zpsajst commited on
Commit
761248a
Β·
1 Parent(s): 64bb768

Add HuggingFace Spaces deployment (16GB RAM free tier)

Browse files
Files changed (4) hide show
  1. DEPLOY_HUGGINGFACE.md +277 -0
  2. DEPLOY_TO_HF.ps1 +39 -0
  3. Dockerfile +33 -0
  4. app.py +6 -0
DEPLOY_HUGGINGFACE.md ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸš€ Deploy LinkScout Backend on Hugging Face Spaces (FREE)
2
+
3
+ ## Why Hugging Face Spaces?
4
+
5
+ - βœ… **16GB RAM FREE** (vs Render's 512MB)
6
+ - βœ… **Perfect for ML models** (built for this)
7
+ - βœ… **Free GPU option** available
8
+ - βœ… **Persistent storage** for models
9
+ - βœ… **No credit card** required
10
+ - βœ… **Always on** (no sleep like Render)
11
+
12
+ ---
13
+
14
+ ## πŸ“¦ Step-by-Step Deployment
15
+
16
+ ### Step 1: Create Hugging Face Account
17
+
18
+ 1. Go to https://huggingface.co/join
19
+ 2. Sign up (free, no credit card)
20
+ 3. Verify your email
21
+
22
+ ### Step 2: Create a New Space
23
+
24
+ 1. Go to https://huggingface.co/new-space
25
+ 2. Fill in details:
26
+ - **Owner**: Your username
27
+ - **Space name**: `linkscout-backend`
28
+ - **License**: MIT
29
+ - **Select SDK**: **Gradio** (we'll use custom Docker)
30
+ - **Space hardware**: **CPU basic (free)** - 16GB RAM!
31
+ - **Visibility**: Public
32
+
33
+ 3. Click **"Create Space"**
34
+
35
+ ### Step 3: Prepare Files for HuggingFace
36
+
37
+ We need to create a few HuggingFace-specific files:
38
+
39
+ #### 3.1 Create `app.py` (Entry point for HuggingFace)
40
+
41
+ Create `D:\LinkScout\app.py`:
42
+
43
+ ```python
44
+ # This is a wrapper for HuggingFace Spaces
45
+ # It imports and runs the Flask server from combined_server.py
46
+
47
+ if __name__ == '__main__':
48
+ import combined_server
49
+ # Server will start automatically when combined_server is imported
50
+ ```
51
+
52
+ #### 3.2 Create `Dockerfile` (for HuggingFace Spaces)
53
+
54
+ Create `D:\LinkScout\Dockerfile`:
55
+
56
+ ```dockerfile
57
+ FROM python:3.11-slim
58
+
59
+ # Set working directory
60
+ WORKDIR /app
61
+
62
+ # Install system dependencies
63
+ RUN apt-get update && apt-get install -y \
64
+ build-essential \
65
+ curl \
66
+ git \
67
+ && rm -rf /var/lib/apt/lists/*
68
+
69
+ # Copy requirements
70
+ COPY requirements.txt .
71
+
72
+ # Install Python dependencies
73
+ RUN pip install --no-cache-dir -r requirements.txt
74
+
75
+ # Copy application code
76
+ COPY . .
77
+
78
+ # Create cache directory for models
79
+ RUN mkdir -p ./models_cache
80
+
81
+ # Expose port (HuggingFace uses port 7860 by default)
82
+ EXPOSE 7860
83
+
84
+ # Set environment variables
85
+ ENV PORT=7860
86
+ ENV PYTHONUNBUFFERED=1
87
+
88
+ # Run the application
89
+ CMD ["python", "combined_server.py"]
90
+ ```
91
+
92
+ #### 3.3 Create `README.md` for HuggingFace Space
93
+
94
+ Create `D:\LinkScout\README_SPACE.md`:
95
+
96
+ ```markdown
97
+ ---
98
+ title: LinkScout Backend
99
+ emoji: πŸ”
100
+ colorFrom: orange
101
+ colorTo: yellow
102
+ sdk: docker
103
+ pinned: false
104
+ ---
105
+
106
+ # LinkScout AI-Powered Misinformation Detection Backend
107
+
108
+ This is the backend API for LinkScout, featuring:
109
+
110
+ - πŸ€– 8 Pre-trained ML Models
111
+ - πŸ”¬ 8-Phase Revolutionary Detection
112
+ - 🧠 Groq AI Integration
113
+ - 🌐 Real-time Fact Checking
114
+
115
+ ## API Endpoints
116
+
117
+ - `POST /analyze` - Analyze text for misinformation
118
+ - `GET /health` - Health check
119
+ - `POST /feedback` - Submit RL feedback
120
+
121
+ ## Environment Variables Required
122
+
123
+ Set these in Space Settings β†’ Variables:
124
+
125
+ - `GROQ_API_KEY` - Your Groq API key
126
+ - `GOOGLE_API_KEY` - (Optional) Google Search API key
127
+ - `GOOGLE_CSE_ID` - (Optional) Google Custom Search Engine ID
128
+ ```
129
+
130
+ ### Step 4: Update Port for HuggingFace
131
+
132
+ HuggingFace Spaces use port **7860** by default. Update `combined_server.py`:
133
+
134
+ Find the port configuration section and update:
135
+
136
+ ```python
137
+ if __name__ == '__main__':
138
+ import os
139
+ # HuggingFace Spaces uses port 7860, Render uses env PORT
140
+ port = int(os.environ.get('PORT', 7860))
141
+ print(f" 🌐 Port: {port}")
142
+ app.run(host='0.0.0.0', port=port, debug=False, threaded=True, use_reloader=False)
143
+ ```
144
+
145
+ ### Step 5: Push to HuggingFace
146
+
147
+ #### Option A: Using Git (Recommended)
148
+
149
+ ```powershell
150
+ # Add HuggingFace as a remote
151
+ cd D:\LinkScout
152
+ git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/linkscout-backend
153
+
154
+ # Push to HuggingFace
155
+ git push hf main
156
+ ```
157
+
158
+ **Replace `YOUR_USERNAME`** with your HuggingFace username!
159
+
160
+ #### Option B: Upload Files Manually
161
+
162
+ 1. Go to your Space: `https://huggingface.co/spaces/YOUR_USERNAME/linkscout-backend`
163
+ 2. Click **"Files"** tab
164
+ 3. Click **"Add file"** β†’ **"Upload files"**
165
+ 4. Upload all your files (drag & drop the entire `LinkScout` folder)
166
+
167
+ ### Step 6: Set Environment Variables
168
+
169
+ 1. Go to your Space settings
170
+ 2. Click **"Settings"** tab
171
+ 3. Scroll to **"Repository secrets"**
172
+ 4. Add secrets:
173
+ - Name: `GROQ_API_KEY`, Value: `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`
174
+ - Name: `GOOGLE_API_KEY`, Value: `AIzaSyA9yKthZUnPAHFnmsnCZoikpvfUteJiX0s`
175
+ - Name: `GOOGLE_CSE_ID`, Value: `11cbd494597034810`
176
+
177
+ ### Step 7: Wait for Build
178
+
179
+ The Space will automatically build when you push files:
180
+
181
+ 1. Go to **"Logs"** tab to watch build progress
182
+ 2. First build takes **10-15 minutes** (downloads models)
183
+ 3. Look for:
184
+ ```
185
+ βœ… RoBERTa loaded
186
+ βœ… Emotion model loaded
187
+ ...
188
+ πŸš€ Starting LinkScout server on port 7860...
189
+ Running on http://0.0.0.0:7860
190
+ ```
191
+
192
+ ### Step 8: Get Your API URL
193
+
194
+ Once deployed, your backend will be at:
195
+
196
+ ```
197
+ https://YOUR_USERNAME-linkscout-backend.hf.space
198
+ ```
199
+
200
+ **Example endpoints:**
201
+ - Health check: `https://YOUR_USERNAME-linkscout-backend.hf.space/health`
202
+ - Analyze: `https://YOUR_USERNAME-linkscout-backend.hf.space/analyze`
203
+
204
+ ---
205
+
206
+ ## 🎯 Advantages of HuggingFace Spaces
207
+
208
+ | Feature | Render Free | HuggingFace Free |
209
+ |---------|-------------|------------------|
210
+ | **RAM** | 512MB ❌ | 16GB βœ… |
211
+ | **ML Models** | Can't load ❌ | Perfect βœ… |
212
+ | **Sleep** | After 15 min ⚠️ | Always on βœ… |
213
+ | **Build Time** | Fast | Fast |
214
+ | **Custom Domain** | Yes βœ… | Yes βœ… |
215
+ | **For Your App** | Won't work ❌ | Perfect! βœ… |
216
+
217
+ ---
218
+
219
+ ## πŸ”§ Troubleshooting
220
+
221
+ ### Build Fails
222
+ - Check logs for errors
223
+ - Verify all files uploaded
224
+ - Check Dockerfile syntax
225
+
226
+ ### Models Won't Load
227
+ - Check you have 16GB RAM selected (CPU basic)
228
+ - Verify requirements.txt has all dependencies
229
+ - Check logs for download errors
230
+
231
+ ### API Not Responding
232
+ - Check Space is "Running" (not building)
233
+ - Verify port 7860 is used
234
+ - Test health endpoint first
235
+
236
+ ### Environment Variables Not Working
237
+ - Make sure they're set as "Repository secrets"
238
+ - Restart the Space after adding secrets
239
+
240
+ ---
241
+
242
+ ## πŸš€ Next Steps After Deployment
243
+
244
+ 1. **Test your backend:**
245
+ ```bash
246
+ curl https://YOUR_USERNAME-linkscout-backend.hf.space/health
247
+ ```
248
+
249
+ 2. **Update your frontend** to use new URL:
250
+ - Replace `http://localhost:5000` with your HF Space URL
251
+ - Update in `app/search/page.tsx`
252
+
253
+ 3. **Update extension** to use new URL:
254
+ - Update `extension/popup.js` API_URL
255
+
256
+ 4. **Deploy frontend on Vercel** (separate from backend)
257
+
258
+ ---
259
+
260
+ ## πŸ’‘ Cost Comparison
261
+
262
+ - **Render Free**: Can't run your app (512MB limit)
263
+ - **Railway Free**: $5/month credit, will run out
264
+ - **HuggingFace Free**: Unlimited, perfect for ML
265
+ - **Paid Options**: $7-20/month for more RAM
266
+
267
+ **HuggingFace Spaces is the ONLY truly free option that will work for your ML-heavy backend!**
268
+
269
+ ---
270
+
271
+ ## πŸ“š Resources
272
+
273
+ - HuggingFace Spaces Docs: https://huggingface.co/docs/hub/spaces
274
+ - Docker SDK Guide: https://huggingface.co/docs/hub/spaces-sdks-docker
275
+ - HuggingFace Git: https://huggingface.co/docs/hub/repositories-getting-started
276
+
277
+ Good luck! πŸš€
DEPLOY_TO_HF.ps1 ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Quick Deployment to HuggingFace Spaces
2
+
3
+ Write-Host "πŸš€ LinkScout - HuggingFace Spaces Deployment" -ForegroundColor Cyan
4
+ Write-Host "=" * 60
5
+
6
+ # Get HuggingFace username
7
+ $username = Read-Host "Enter your HuggingFace username"
8
+
9
+ Write-Host "`nπŸ“ Setting up HuggingFace remote..." -ForegroundColor Yellow
10
+ git remote remove hf 2>$null # Remove if exists
11
+ git remote add hf "https://huggingface.co/spaces/$username/linkscout-backend"
12
+
13
+ Write-Host "`nβœ… Remote added: https://huggingface.co/spaces/$username/linkscout-backend" -ForegroundColor Green
14
+
15
+ Write-Host "`nπŸ“¦ Committing HuggingFace files..." -ForegroundColor Yellow
16
+ git add app.py Dockerfile DEPLOY_HUGGINGFACE.md
17
+ git commit -m "Add HuggingFace Spaces deployment files" 2>$null
18
+
19
+ Write-Host "`nπŸš€ Pushing to HuggingFace..." -ForegroundColor Yellow
20
+ Write-Host "You may be asked for your HuggingFace credentials:" -ForegroundColor Gray
21
+ Write-Host " Username: $username" -ForegroundColor Gray
22
+ Write-Host " Password: Use your HuggingFace ACCESS TOKEN (not password!)" -ForegroundColor Gray
23
+ Write-Host " Get token from: https://huggingface.co/settings/tokens" -ForegroundColor Gray
24
+ Write-Host ""
25
+
26
+ git push hf main
27
+
28
+ Write-Host "`nβœ… Deployment initiated!" -ForegroundColor Green
29
+ Write-Host "`nπŸ“Š Next steps:" -ForegroundColor Cyan
30
+ Write-Host " 1. Go to: https://huggingface.co/spaces/$username/linkscout-backend" -ForegroundColor White
31
+ Write-Host " 2. Click 'Settings' tab" -ForegroundColor White
32
+ Write-Host " 3. Add Repository secrets:" -ForegroundColor White
33
+ Write-Host " - GROQ_API_KEY = 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" -ForegroundColor Gray
34
+ Write-Host " - GOOGLE_API_KEY = AIzaSyA9yKthZUnPAHFnmsnCZoikpvfUteJiX0s" -ForegroundColor Gray
35
+ Write-Host " - GOOGLE_CSE_ID = 11cbd494597034810" -ForegroundColor Gray
36
+ Write-Host " 4. Wait 10-15 minutes for build to complete" -ForegroundColor White
37
+ Write-Host " 5. Your API will be at: https://$username-linkscout-backend.hf.space" -ForegroundColor White
38
+
39
+ Write-Host "`nπŸŽ‰ Done!" -ForegroundColor Green
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ # Set working directory
4
+ WORKDIR /app
5
+
6
+ # Install system dependencies
7
+ RUN apt-get update && apt-get install -y \
8
+ build-essential \
9
+ curl \
10
+ git \
11
+ && rm -rf /var/lib/apt/lists/*
12
+
13
+ # Copy requirements
14
+ COPY requirements.txt .
15
+
16
+ # Install Python dependencies
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # Copy application code
20
+ COPY . .
21
+
22
+ # Create cache directory for models
23
+ RUN mkdir -p ./models_cache
24
+
25
+ # Expose port (HuggingFace uses port 7860 by default)
26
+ EXPOSE 7860
27
+
28
+ # Set environment variables
29
+ ENV PORT=7860
30
+ ENV PYTHONUNBUFFERED=1
31
+
32
+ # Run the application
33
+ CMD ["python", "combined_server.py"]
app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # This is a wrapper for HuggingFace Spaces
2
+ # It imports and runs the Flask server from combined_server.py
3
+
4
+ if __name__ == '__main__':
5
+ import combined_server
6
+ # Server will start automatically when combined_server is imported