File size: 6,371 Bytes
196c707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c327bd5
196c707
c327bd5
196c707
c327bd5
 
 
 
 
196c707
 
 
 
 
 
 
 
 
 
 
 
 
c327bd5
 
 
 
 
 
196c707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Deployment Guide

## Deploying to HuggingFace Spaces

### Prerequisites
- HuggingFace account
- API token from your LLM provider (or use HF Inference API)

### Step-by-Step Deployment

#### 1. Create a New Space

1. Go to https://huggingface.co/spaces
2. Click "Create new Space"
3. Choose a name (e.g., "conversai-research-assistant")
4. Select SDK: **Gradio**
5. Choose visibility (Public or Private)
6. Click "Create Space"

#### 2. Upload Files

Upload these files to your Space:

**Required Files:**
- `app.py` - Main application
- `llm_backend.py` - LLM interface
- `survey_generator.py` - Survey generation
- `survey_translator.py` - Translation module
- `data_analyzer.py` - Analysis module
- `export_utils.py` - Export utilities
- `requirements.txt` - Dependencies
- `README.md` - Space description

**Optional Files:**
- `.env.example` - Configuration template
- `USAGE_GUIDE.md` - User guide
- `test_app.py` - Testing script

#### 3. Configure Environment Variables (Optional)

**Default Configuration (Recommended for Quick Start):**

No configuration needed! The app automatically uses HuggingFace Inference API with the built-in `HF_TOKEN`.

**Optional: Use Premium Providers**

For better performance, you can add these environment variables in Space Settings:

**For OpenAI:**
```

LLM_PROVIDER=openai

OPENAI_API_KEY=sk-your-key-here

```

**For Anthropic:**
```

LLM_PROVIDER=anthropic

ANTHROPIC_API_KEY=your-key-here

```

**For Custom HuggingFace Model:**
```

LLM_MODEL=mistralai/Mistral-7B-Instruct-v0.2

# LLM_PROVIDER defaults to huggingface

```

#### 4. Space Will Auto-Deploy

- HuggingFace will automatically build and deploy
- Check the "Logs" tab for build status
- First build may take 2-3 minutes

#### 5. Test Your Deployment

1. Wait for "Running" status
2. Open the Space URL
3. Test survey generation
4. Test translation
5. Test analysis with example data

### Using HuggingFace Inference API

The easiest option for deployment is to use HuggingFace's free Inference API:

**Pros:**
- No API key needed (uses HF_TOKEN automatically)

- Free tier available

- Easy setup



**Cons:**

- May have rate limits on free tier

- Slower than paid providers

- May queue during high usage



**Configuration:**

Just set `LLM_PROVIDER=huggingface` in your environment variables.

### Using Other Providers

#### OpenAI (Recommended for Production)

**Pros:**
- Fast and reliable
- High quality outputs
- Good API documentation

**Cons:**
- Requires paid API key
- Usage costs

**Cost Estimate:**
- Survey generation: ~$0.01-0.05 per survey
- Translation: ~$0.01-0.03 per language
- Analysis: ~$0.05-0.15 per batch

#### Anthropic Claude

**Pros:**
- Excellent for nuanced text
- Strong reasoning capabilities
- Good safety features

**Cons:**
- Requires API key
- Usage costs

**Cost Estimate:**
Similar to OpenAI pricing

## Deploying Locally

### For Development

```bash

# 1. Clone/download repository

git clone <your-repo-url>

cd ConversAI



# 2. Create virtual environment

python -m venv venv

source venv/bin/activate  # On Windows: venv\Scripts\activate



# 3. Install dependencies

pip install -r requirements.txt



# 4. Set environment variables

export LLM_PROVIDER="openai"

export OPENAI_API_KEY="your-key"



# 5. Run

python app.py

```

Access at `http://localhost:7860`

### For Production (Self-Hosted)

Use Docker for production deployment:

**Create Dockerfile:**
```dockerfile

FROM python:3.10-slim



WORKDIR /app



COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt



COPY *.py .

COPY *.md .



ENV GRADIO_SERVER_NAME="0.0.0.0"

ENV GRADIO_SERVER_PORT=7860



EXPOSE 7860



CMD ["python", "app.py"]

```

**Build and run:**
```bash

docker build -t conversai .

docker run -p 7860:7860 \

  -e LLM_PROVIDER=openai \

  -e OPENAI_API_KEY=your-key \

  conversai

```

## Post-Deployment Checklist

- [ ] App loads without errors
- [ ] Can generate a survey
- [ ] Can translate a survey
- [ ] Can analyze sample data
- [ ] Downloads work correctly
- [ ] Error messages are clear
- [ ] All tabs are accessible
- [ ] Mobile view works (if public)

## Monitoring and Maintenance

### Check Usage

Monitor your LLM API usage:
- OpenAI: https://platform.openai.com/usage
- Anthropic: Check your console
- HuggingFace: Monitor rate limits

### Update Dependencies

Regularly update to get security fixes:
```bash

pip install --upgrade gradio requests pandas

```

### Backup

Regularly backup:
- Generated surveys
- Analysis results
- User feedback
- Configuration

## Troubleshooting Deployment

### Space Build Fails

**Check:**
- `requirements.txt` is valid
- `README.md` has correct frontmatter
- No syntax errors in Python files

### Space Runs But Errors

**Check:**
- Environment variables are set
- API keys are valid
- Provider quotas aren't exceeded

### Slow Performance

**Solutions:**
- Upgrade to paid LLM tier
- Use faster models (e.g., GPT-4o-mini)
- Add caching for common requests
- Optimize prompts for shorter responses

## Scaling Considerations

### For Heavy Usage

1. **Use faster models**: GPT-4o-mini instead of GPT-4
2. **Implement caching**: Cache common survey patterns
3. **Add rate limiting**: Prevent abuse
4. **Load balancing**: Use multiple API keys
5. **Queue system**: Handle concurrent requests

### Cost Optimization

1. **Optimize prompts**: Shorter prompts = lower costs
2. **Batch operations**: Process multiple items together
3. **Use cheaper models**: For simpler tasks
4. **Set token limits**: Prevent runaway costs
5. **Monitor usage**: Set up alerts

## Security Best Practices

1. **Never commit API keys** to version control
2. **Use environment variables** for secrets
3. **Rotate keys regularly**
4. **Set spending limits** with providers
5. **Monitor for unusual activity**
6. **Use private Spaces** for sensitive research

## Support and Resources

- **HuggingFace Docs**: https://huggingface.co/docs/hub/spaces
- **Gradio Docs**: https://gradio.app/docs
- **OpenAI API**: https://platform.openai.com/docs
- **Anthropic API**: https://docs.anthropic.com

---

Need help? Check the USAGE_GUIDE.md or open an issue!