File size: 7,188 Bytes
93c19dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# πŸš€ Domify Academy Super Bot - Hugging Face Setup Guide

## Download Backend Code

**Download the complete backend code package:**

πŸ“¦ **[Download backend-code.tar.gz](https://files.manuscdn.com/user_upload_by_module/session_file/310519663512731124/PptOqCfNfXiULWvx.gz)**

---

## What's Included

The archive contains all backend files:

```
backend-code/
β”œβ”€β”€ server/
β”‚   β”œβ”€β”€ llm.ts                 # LLM engine with NVIDIA integration
β”‚   β”œβ”€β”€ search.ts              # DuckDuckGo search
β”‚   β”œβ”€β”€ rateLimit.ts           # Rate limiting middleware
β”‚   β”œβ”€β”€ db.ts                  # Database helpers
β”‚   β”œβ”€β”€ googleSheets.ts        # Google Sheets logging
β”‚   β”œβ”€β”€ middleware.ts          # Industrial middleware
β”‚   β”œβ”€β”€ routers.ts             # tRPC procedures
β”‚   β”œβ”€β”€ storage.ts             # S3 storage helpers
β”‚   └── auth.logout.test.ts    # Test example
β”œβ”€β”€ drizzle/
β”‚   └── schema.ts              # Database schema
β”œβ”€β”€ Dockerfile                 # Production Docker image
β”œβ”€β”€ DEPLOYMENT.md              # Complete deployment guide
β”œβ”€β”€ BACKEND_README.md          # Backend documentation
β”œβ”€β”€ QUICKSTART.md              # 5-minute quick start
└── .dockerignore              # Docker build optimization
```

---

## Step-by-Step Setup

### Step 1: Extract Files

```bash
# Extract the archive
tar -xzf backend-code.tar.gz

# You now have all the files
ls -la
```

### Step 2: Create Hugging Face Space

1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
2. Click **"Create new Space"**
3. Fill in:
   - **Space name**: `domify-academy-bot`
   - **SDK**: Select **"Docker"**
   - **License**: Apache 2.0
   - **Visibility**: Public
4. Click **"Create Space"**

### Step 3: Get Your Repository URL

After creating, you'll see:
```
https://huggingface.co/spaces/YOUR_USERNAME/domify-academy-bot
```

### Step 4: Copy Files to Hugging Face

```bash
# Initialize git in your local directory
cd /path/to/extracted/files
git init

# Add all files
git add .

# Commit
git commit -m "Domify Academy Bot - Backend"

# Add Hugging Face remote
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/domify-academy-bot

# Push to Hugging Face
git push -u origin main
```

### Step 5: Set Environment Variables

In Hugging Face Space **Settings** β†’ **Repository secrets**, add:

| Variable | Value | Example |
|----------|-------|---------|
| `DATABASE_URL` | MySQL connection | `mysql://user:pass@host/db` |
| `NVIDIA_API_KEY` | Your NVIDIA key | `nvapi-xxxxx` |
| `JWT_SECRET` | Random secret | `openssl rand -base64 32` |

**Optional:**
- `GOOGLE_SHEETS_API_KEY` - For feedback logging
- `GOOGLE_SHEETS_ID` - Google Sheet ID

### Step 6: Wait for Build

Hugging Face will:
1. Detect the `Dockerfile`
2. Build the image (5-10 minutes)
3. Deploy automatically
4. Assign a public URL

**Monitor in the "Build" tab**

### Step 7: Test

Once deployed:

```bash
# Test health endpoint
curl https://YOUR_SPACE_URL/api/health

# Expected response:
{
  "status": "healthy",
  "uptime": 123.45,
  "database": "connected"
}
```

---

## File Descriptions

### `server/llm.ts`
LLM engine with NVIDIA API integration. Handles:
- Llama-3 70B as primary model
- Automatic fallback to alternate models
- DeepSeek-style reasoning generation
- Image generation via SDXL/Flux

### `server/search.ts`
DuckDuckGo search integration for "Search Online" mode.

### `server/rateLimit.ts`
Token bucket rate limiting to prevent API abuse.

### `server/db.ts`
Database helper functions for:
- User management
- Conversation history
- Message storage
- Image management
- Feedback logging

### `server/googleSheets.ts`
Google Sheets integration for feedback analytics.

### `server/middleware.ts`
Industrial-standard middleware:
- Request logging
- Response caching
- Error handling
- Performance monitoring
- Security headers

### `server/routers.ts`
tRPC procedure definitions:
- `chat.send` - Text generation
- `imagine.generate` - Image generation
- `search.online` - Web search

### `drizzle/schema.ts`
Database schema with tables:
- `users` - User accounts
- `conversations` - Chat conversations
- `messages` - Individual messages
- `images` - Generated images
- `feedback` - User feedback

### `Dockerfile`
Production-ready Docker image for Hugging Face Spaces.

### `DEPLOYMENT.md`
Complete deployment guide with troubleshooting.

### `BACKEND_README.md`
Backend API documentation and reference.

### `QUICKSTART.md`
5-minute quick start guide.

---

## Environment Variables Reference

### Required

**`DATABASE_URL`**
- Format: `mysql://user:password@host:port/database`
- Example: `mysql://admin:secret@db.example.com:3306/domify_bot`
- Get from: Your database provider

**`NVIDIA_API_KEY`**
- Get from: [NVIDIA Build Portal](https://build.nvidia.com/)
- Used for: Llama-3 70B, SDXL, Flux models

**`JWT_SECRET`**
- Generate: `openssl rand -base64 32`
- Used for: Session token signing

### Optional

**`GOOGLE_SHEETS_API_KEY`**
- Get from: Google Cloud Console
- Used for: Feedback logging to Google Sheets

**`GOOGLE_SHEETS_ID`**
- Get from: Google Sheet URL
- Used with: `GOOGLE_SHEETS_API_KEY`

---

## Troubleshooting

### Build Fails

**Check logs in "Build" tab for:**
- Missing environment variables
- Database connection error
- Invalid NVIDIA API key

**Solution:**
1. Verify all required variables are set
2. Test database connection
3. Check NVIDIA API key validity

### Application Crashes

**Check logs in "Logs" tab:**
- Look for error messages
- Restart the Space if needed

### Slow Responses

**Possible causes:**
- Database too slow
- NVIDIA API busy
- Rate limiting triggered

**Solution:**
- Upgrade Space compute resources
- Check database performance
- Increase rate limit if needed

---

## What's Next?

After backend is deployed:

1. **Build the Frontend** (React + Tailwind)
   - Dark glassmorphism UI
   - Ask | Imagine mode switcher
   - Advanced prompt input box
   - Reasoning panel
   - Rich response formatting

2. **Connect Frontend to Backend**
   - Update API endpoint URLs
   - Configure tRPC client

3. **Test All Features**
   - Ask mode (text generation)
   - Imagine mode (image generation)
   - Search online
   - Think longer (reasoning)

4. **Deploy Frontend**
   - Same Hugging Face Space or separate URL
   - Configure custom domain

---

## Support

- **Deployment issues**: See `DEPLOYMENT.md`
- **Backend details**: See `BACKEND_README.md`
- **Quick setup**: See `QUICKSTART.md`
- **Architecture**: See `ARCHITECTURE.md`

---

## Key Features Deployed

βœ… NVIDIA API integration (Llama-3 70B + fallbacks)
βœ… DeepSeek-style reasoning
βœ… Rate limiting (30 req/min per user)
βœ… DuckDuckGo search integration
βœ… Database with conversation history
βœ… Google Sheets feedback logging
βœ… Industrial-standard middleware
βœ… Production-ready Docker image
βœ… Complete documentation
βœ… Health check endpoints

---

## Ready to Deploy? πŸš€

1. Download the code
2. Create Hugging Face Space
3. Push code to Space
4. Set environment variables
5. Wait for build
6. Test the health endpoint
7. You're live!

Good luck! πŸŽ‰