File size: 10,541 Bytes
c7297b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Hugging Face Spaces Secrets Setup Guide

This guide explains how to configure and deploy your Telugu Dialect Map to Hugging Face Spaces with secure secrets management.

## Overview

Your application requires two secret files:
- **`config.json`**: Configuration for Google Sheets sync and automation
- **`credentials.json`**: Google Cloud service account credentials

These files contain sensitive information and should NEVER be committed to git. Instead, we'll use Hugging Face Spaces **secrets** (environment variables) to store them securely.

---

## Step 1: Obtain Google Service Account Credentials

### 1.1 Create a Google Cloud Project

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Note your project ID

### 1.2 Enable Google Sheets API

1. In your project, go to **APIs & Services** β†’ **Library**
2. Search for "Google Sheets API"
3. Click **Enable**

### 1.3 Create a Service Account

1. Go to **APIs & Services** β†’ **Credentials**
2. Click **Create Credentials** β†’ **Service Account**
3. Fill in the details:
   - **Service account name**: `dialect-map-automation`
   - **Service account ID**: (auto-generated)
   - **Description**: "Service account for dialect map Google Sheets automation"
4. Click **Create and Continue**
5. Skip the optional steps (roles and user access)
6. Click **Done**

### 1.4 Create and Download Service Account Key

1. Click on the service account you just created
2. Go to the **Keys** tab
3. Click **Add Key** β†’ **Create new key**
4. Select **JSON** format
5. Click **Create**
6. A JSON file will be downloaded - this is your `credentials.json`
7. **Keep this file secure!** It provides full access to your Google Sheets

### 1.5 Share Your Google Sheets with the Service Account

1. Open your `credentials.json` file
2. Find the `client_email` field (e.g., `dialect-map-automation@your-project.iam.gserviceaccount.com`)
3. Copy this email address
4. Open each Google Sheet you want to sync
5. Click **Share** button
6. Paste the service account email
7. Give it **Editor** or **Viewer** access (Editor if you want to write data back)
8. Click **Send**

---

## Step 2: Configure config.json

Create your `config.json` file with your specific settings:

```json
{
  "google_sheets": {
    "enabled": true,
    "sync_interval_minutes": 5,
    "credentials_file": "credentials.json",
    "spreadsheets": [
      {
        "id": "YOUR_ACTUAL_SPREADSHEET_ID_HERE",
        "sheet_name": "processed_dialects",
        "output_file": "sheets_output/processed_dialects.csv"
      },
      {
        "id": "YOUR_ACTUAL_SPREADSHEET_ID_HERE",
        "sheet_name": "digiwords_grouped",
        "output_file": "sheets_output/digiwords_grouped.csv"
      }
    ]
  },
  "file_watcher": {
    "enabled": true,
    "watch_directory": "sheets_output",
    "file_patterns": ["*.csv"]
  },
  "output": {
    "json_directory": "data/processed"
  }
}
```

### Finding Your Spreadsheet ID

Your Google Sheets URL looks like:
```
https://docs.google.com/spreadsheets/d/1AbC123XyZ456_Example_ID/edit#gid=0
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^
                                    This is your spreadsheet ID
```

Copy the ID from your URL and replace `YOUR_ACTUAL_SPREADSHEET_ID_HERE` in the config.

---

## Step 3: Deploy to Hugging Face Spaces

### 3.1 Create a New Space

1. Go to [Hugging Face](https://huggingface.co/)
2. Click your profile β†’ **New Space**
3. Fill in the details:
   - **Space name**: `telugu-dialect-map` (or your choice)
   - **License**: Choose appropriate license
   - **Space SDK**: Select **Static** (we'll use a custom Python app)
   - **Visibility**: Public or Private
4. Click **Create Space**

### 3.2 Push Your Code to the Space

You can either:

**Option A: Use Git**
```bash
# Clone the Space repository
git clone https://huggingface.co/spaces/YOUR_USERNAME/telugu-dialect-map
cd telugu-dialect-map

# Copy your project files (excluding secrets!)
cp -r /path/to/dialect-map/* .

# Make sure .gitignore is in place
cat .gitignore  # Should include config.json and credentials.json

# Commit and push
git add .
git commit -m "Initial commit"
git push
```

**Option B: Upload via Web Interface**
1. In your Space, click **Files** tab
2. Click **Add file** β†’ **Upload files**
3. Select all your project files (EXCEPT `config.json` and `credentials.json`)
4. Click **Commit changes**

### 3.3 Add Secrets to Your Space

This is the **critical step** - we'll add your sensitive credentials as secrets.

1. In your Space, click the **Settings** tab
2. Scroll down to **Repository secrets**
3. Add the following secrets:

#### Secret 1: HF_CONFIG_JSON

- **Name**: `HF_CONFIG_JSON`
- **Value**: Paste the **entire contents** of your `config.json` file
  
  Example (all on one line):
  ```
  {"google_sheets":{"enabled":true,"sync_interval_minutes":5,"credentials_file":"credentials.json","spreadsheets":[{"id":"1AbC123XyZ456_Example","sheet_name":"processed_dialects","output_file":"sheets_output/processed_dialects.csv"}]},"file_watcher":{"enabled":true,"watch_directory":"sheets_output","file_patterns":["*.csv"]},"output":{"json_directory":"data/processed"}}
  ```

#### Secret 2: HF_CREDENTIALS_JSON

- **Name**: `HF_CREDENTIALS_JSON`
- **Value**: Paste the **entire contents** of your `credentials.json` file

  Example (all on one line, with escaped newlines in private key):
  ```
  {"type":"service_account","project_id":"your-project","private_key_id":"abc123...","private_key":"-----BEGIN PRIVATE KEY-----\\nMIIEvQIB...\\n-----END PRIVATE KEY-----\\n","client_email":"name@project.iam.gserviceaccount.com",...}
  ```

4. Click **Add secret** for each one

**Important Notes:**
- The entire JSON must be on one line (no newlines except in the `private_key` field where `\n` should be `\\n`)
- Make sure to escape special characters if needed
- You can use a JSON minifier tool to compact your JSON

### 3.4 Rebuild Your Space

After adding secrets:
1. Your Space should automatically rebuild
2. Watch the **Logs** tab for any errors
3. Once built, click **App** tab to view your running application

---

## Step 4: Verify Deployment

### 4.1 Check Space Logs

1. Go to your Space's **Logs** tab
2. You should see:
   ```
   πŸ” Loading secrets from environment variables...
   βœ… Created config.json from HF_CONFIG_JSON secret
   βœ… Created credentials.json from HF_CREDENTIALS_JSON secret
   πŸš€ Starting automation runner...
   βœ… Automation runner started
   🌐 Starting web server on port 7860...
   ```

3. If you see errors, check:
   - JSON formatting in your secrets
   - Spreadsheet IDs are correct
   - Service account has access to sheets

### 4.2 Access Your Application

1. Click the **App** tab
2. You should see your Telugu Dialect Map interface
3. The map should load with data from your Google Sheets

### 4.3 Verify Automation

1. Edit your Google Sheet (add/modify some dialect data)
2. Wait 5 minutes (or your configured interval)
3. Check the Space logs - you should see sync messages
4. Refresh your app - changes should appear

---

## Troubleshooting

### "HF_CONFIG_JSON not found in environment"

**Problem**: The secret wasn't added or has the wrong name.

**Solution**:
1. Go to Space Settings β†’ Repository secrets
2. Verify the secret name is exactly `HF_CONFIG_JSON` (case-sensitive)
3. Add it if missing
4. Rebuild the Space

### "Error parsing HF_CONFIG_JSON"

**Problem**: The JSON is malformed.

**Solution**:
1. Copy your `config.json` content
2. Use a JSON validator (e.g., [jsonlint.com](https://jsonlint.com/))
3. Make sure it's valid JSON
4. Remove all newlines (except `\n` in strings should become `\\n`)
5. Update the secret with the corrected value

### "Connection refused" or "Credentials invalid"

**Problem**: Google service account credentials are wrong or not shared.

**Solution**:
1. Verify `credentials.json` content is correct
2. Check that you shared your Google Sheets with the service account email
3. Verify the Sheets API is enabled in Google Cloud Console
4. Regenerate service account key if needed

### "Automation not syncing"

**Problem**: Automation runner isn't working.

**Solution**:
1. Check Space logs for error messages
2. Verify `spreadsheets.id` values in config.json match your actual sheet IDs
3. Verify `sheet_name` values match the tab names in your spreadsheets
4. Check that service account has Editor access (not just Viewer)

### Space keeps crashing or restarting

**Problem**: HF Spaces free tier may have resource limits.

**Solution**:
1. Consider upgrading to a paid Space for guaranteed uptime
2. Reduce sync interval (e.g., 15-30 minutes instead of 5)
3. Check logs for memory/CPU issues

---

## Local Testing (Before Deploying)

Before deploying to HF Spaces, test locally:

### 1. Create actual config files

```bash
cd /home/kashikuldeep/Desktop/dialect-map

# Copy examples and fill in real values
cp config.json.example config.json
cp credentials.json.example credentials.json

# Edit with your actual values
nano config.json
nano credentials.json
```

### 2. Test the app

```bash
# Run the app locally
python app.py

# Should see:
# βœ… Created config.json from HF_CONFIG_JSON secret (if using .env)
# πŸš€ Starting automation runner...
# 🌐 Starting web server on port 7860...

# Open browser to http://localhost:7860
```

### 3. Test with environment variables (simulating HF Spaces)

```bash
# Create .env file
cp .env.example .env

# Edit .env with your actual JSON (minified)
nano .env

# Load environment and run
python -c "from dotenv import load_dotenv; load_dotenv()" && python app.py
```

---

## Security Best Practices

1. **Never commit secrets to git**
   - Keep `config.json` and `credentials.json` in `.gitignore`
   - Double-check before pushing code

2. **Rotate credentials regularly**
   - Generate new service account keys periodically
   - Update HF Spaces secrets

3. **Use minimal permissions**
   - Service account should only have access to necessary sheets
   - Use Viewer access if you don't need to write back

4. **Monitor usage**
   - Check Google Cloud Console for API usage
   - Set up billing alerts
   - Review Space logs regularly

---

## Need Help?

- **Google Cloud Issues**: [Google Cloud Support](https://cloud.google.com/support)
- **Hugging Face Spaces**: [HF Documentation](https://huggingface.co/docs/hub/spaces)
- **Project Issues**: Check the Space logs first, then review this guide

---

**Happy Mapping! πŸ—ΊοΈ**