File size: 8,716 Bytes
fd77f04
 
 
 
5a061ee
 
 
 
 
 
 
 
 
 
 
 
 
fd77f04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Troubleshooting Guide

## Common Issues and Solutions

### πŸ”„ HuggingFace API Update (November 2025)

**Important:** HuggingFace has updated their Inference API endpoint.

**If you see 404 errors:**
- βœ… Make sure you're using the latest version of `llm_backend.py`
- βœ… The new endpoint is: `https://router.huggingface.co/hf-inference/`
- βœ… Old endpoint (`api-inference.huggingface.co`) is deprecated

**Already updated:** This version uses the new Inference Providers API automatically.

---

### ❌ "LLM backend not configured" Error

**Symptom:** App shows warning banner, features don't work

**Cause:** No LLM provider credentials found

**Solutions:**

#### On HuggingFace Spaces

**Option 1: Make Space Public (Easiest)**
1. Go to your Space Settings
2. Change Visibility: Private β†’ **Public**
3. Refresh/Restart the Space
4. HF_TOKEN will automatically be available



**Option 2: Add Token Manually (for Private Spaces)**

1. Get your token: https://huggingface.co/settings/tokens

2. Create token with "Read" permission (if you don't have one)

3. Go to Space Settings β†’ Variables

4. Add variable:

   - Name: `HUGGINGFACE_API_KEY`

   - Value: your_token_here

5. Restart the Space



**Option 3: Use Premium Provider**

1. Get API key from OpenAI or Anthropic

2. Go to Space Settings β†’ Variables

3. Add:

   - `LLM_PROVIDER=openai`
   - `OPENAI_API_KEY=sk-your-key`
4. Restart Space

#### Running Locally

**Check your environment:**
```bash

python check_env.py

```

**Set credentials:**
```bash

# For OpenAI (recommended)

export OPENAI_API_KEY="sk-your-key-here"



# OR for Anthropic

export ANTHROPIC_API_KEY="your-key-here"



# OR for HuggingFace

export HUGGINGFACE_API_KEY="your-token-here"



# Then run

python app.py

```

---

### ⏱️ Generation Takes Too Long

**Symptom:** Waiting 30+ seconds for survey generation

**Cause:** HuggingFace free tier can be slow, especially during high usage

**Solutions:**

1. **Be patient on first request** - Models "cold start", can take 60+ seconds
2. **Try again** - You might hit a faster server
3. **Simplify requests:**
   - Use shorter outlines (2-3 sentences)
   - Request fewer questions (5-10)
   - Translate one language at a time
4. **Upgrade to paid provider:**
   ```bash

   LLM_PROVIDER=openai

   OPENAI_API_KEY=sk-your-key

   ```
   Much faster, costs ~$0.01-0.05 per survey

---

### πŸ”΄ "AttributeError: 'NoneType' object has no attribute..."

**Symptom:** App crashes on startup

**Cause:** Interface trying to use backend before it's initialized

**Solution:** Update to latest version - this has been fixed

**If still happening:**
1. Check `app.py` has the latest code
2. Verify `get_language_choices()` doesn't depend on `survey_trans` instance
3. Make sure all functions check `if survey_gen:` before using it

---

### πŸ“Š Analysis Returns Empty/Poor Results

**Symptom:** Analysis doesn't find themes or gives generic insights

**Causes & Solutions:**

1. **Too few responses:**
   - Need 10-20+ responses for meaningful analysis
   - Add more sample data

2. **Low-quality model:**
   - Free HF models may struggle with complex analysis
   - Upgrade to OpenAI GPT-4 or Claude for better analysis

3. **Responses too short:**
   - Analysis works best with detailed, paragraph-length responses
   - Encourage respondents to elaborate

4. **Wrong format:**
   - Make sure responses are properly formatted JSON
   - Use the "Load Example" button to see correct format

---

### 🌍 Translation Failed

**Symptom:** Translation returns error or garbled text

**Causes & Solutions:**

1. **No survey generated:**
   - Generate a survey first before translating
   - Or upload a valid survey JSON

2. **Model limitations:**
   - Free HF models may struggle with some languages
   - Use OpenAI or Anthropic for better translations
   - Try more common languages (Spanish, French) first

3. **Rate limit:**
   - Wait a few minutes
   - Translate fewer languages at once

---

### πŸ’Ύ Downloads Don't Work

**Symptom:** Can't download survey/analysis files

**Cause:** Browser/Gradio issues

**Solutions:**

1. **Check browser console** for errors (F12)
2. **Try different browser** (Chrome, Firefox)
3. **Copy-paste data** manually from the output box
4. **Check Gradio version** - update to latest:
   ```bash

   pip install --upgrade gradio

   ```

---

### πŸ”’ Private Space Issues

**Symptom:** Works in public Space but not private

**Cause:** HF_TOKEN not available in private Spaces



**Solution:**



Add token manually:

1. https://huggingface.co/settings/tokens

2. Copy token with "Read" permission

3. Space Settings β†’ Variables β†’ Add `HUGGINGFACE_API_KEY`

4. Restart



---



### 🚫 Rate Limit Errors



**Symptom:** "Rate limit exceeded" or 429 errors



**HuggingFace Free Tier:**

- Wait 5-10 minutes

- Reduce request frequency

- Upgrade to Pro: https://huggingface.co/pricing



**OpenAI:**

- Check usage: https://platform.openai.com/usage

- Add credits or wait for reset

- Use GPT-3.5 instead of GPT-4 (cheaper)



**Anthropic:**

- Check console: https://console.anthropic.com

- Add credits or upgrade plan



---



### πŸ“± Mobile/Responsive Issues



**Symptom:** UI doesn't work well on mobile



**Cause:** Gradio has some mobile limitations



**Solutions:**

- Use landscape orientation

- Use tablet or desktop for best experience

- Some features may be limited on mobile



---



## Debugging Steps



### 1. Check Environment

```bash

python check_env.py
```



Look for:

- βœ… At least one API key is SET

- βœ… All dependencies installed

- βœ… Provider will be detected



### 2. Check Logs



**On HuggingFace Spaces:**

- Go to "Logs" tab

- Look for "=== LLM Backend Initialization ==="

- Check which credentials are found



**Running locally:**

- Check terminal output

- Look for initialization messages

- Check for errors/warnings



### 3. Test Backend

```bash

python test_hf_backend.py

```

Should show:
- βœ… Backend initialized
- βœ… Generation successful
- βœ… ALL TESTS PASSED

### 4. Start Simple

If issues persist:
1. **Test with OpenAI first** (most reliable)
2. **Use example data** from the UI
3. **Try one feature at a time** (generation only)
4. **Check network connectivity**

---

## Getting Help

### Before Asking for Help

1. βœ… Run `python check_env.py`
2. βœ… Check logs for error messages
3. βœ… Try with example data
4. βœ… Update to latest code
5. βœ… Read this troubleshooting guide

### Where to Get Help

1. **Check documentation:**
   - `README.md` - Overview
   - `USAGE_GUIDE.md` - How to use
   - `DEPLOYMENT.md` - Setup instructions
   - `QUICK_START_HF_SPACES.md` - Fast deployment

2. **Common resources:**
   - Gradio docs: https://gradio.app/docs
   - HuggingFace docs: https://huggingface.co/docs
   - OpenAI docs: https://platform.openai.com/docs

3. **Report issues:**
   - Include output from `check_env.py`
   - Include relevant logs
   - Describe what you tried

---

## Performance Tips

### For Best Results

**Model Selection:**
- **Best Quality:** OpenAI GPT-4 or Anthropic Claude 3.5 Sonnet
- **Best Value:** OpenAI GPT-4o-mini
- **Free:** HuggingFace (slower, lower quality)

**Request Optimization:**
- Keep outlines concise (2-4 sentences)
- Request 10-15 questions (not 25)
- Translate common languages first
- Provide 20+ responses for analysis

**Cost Control:**
- Use GPT-4o-mini instead of GPT-4 ($0.15 vs $5 per 1M tokens)
- Cache common surveys
- Batch operations when possible
- Monitor usage dashboards

---

## Emergency Fixes

### App Won't Start At All

```bash

# 1. Clean install

rm -rf venv

python -m venv venv

source venv/bin/activate  # or venv\Scripts\activate on Windows

pip install -r requirements.txt



# 2. Check environment

python check_env.py



# 3. Try with minimal config

export OPENAI_API_KEY="sk-your-key"

python app.py

```

### App Starts But Nothing Works

```bash

# 1. Verify backend

python test_hf_backend.py



# 2. Check imports

python -c "from llm_backend import LLMBackend; print('OK')"

python -c "from survey_generator import SurveyGenerator; print('OK')"



# 3. Test manually

python

>>> from llm_backend import LLMBackend, LLMProvider

>>> backend = LLMBackend(provider=LLMProvider.OPENAI)

>>> backend.generate([{"role": "user", "content": "Hello"}], max_tokens=10)

```

---

**Still stuck?** Make sure you're using the latest version of all files and have followed the setup instructions carefully.