File size: 7,536 Bytes
ad3c52d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Persona Feedback Simulator – Integration & Deployment

## Table of Contents
1. [Overview](#overview)  
2. [System Architecture](#system-architecture)  
3. [Prerequisites](#prerequisites)  
4. [Installation and Setup](#installation-and-setup)  
5. [Configuration](#configuration)  
6. [Database / Data Storage](#database--data-storage)  
7. [API Specifications](#api-specifications)  
8. [Deployment Procedures](#deployment-procedures)  
9. [Monitoring and Metrics](#monitoring-and-metrics)  
10. [Maintenance and Update Procedures](#maintenance-and-update-procedures)  
11. [Troubleshooting](#troubleshooting)  
12. [Backup and Recovery](#backup-and-recovery)  

---

## 1. Overview
The **Persona Feedback Simulator** is a Streamlit-based application that allows users to simulate conversations among virtual personas regarding product features. The system is designed to support:

- Multiple personas stored in `personas.json`
- AI-driven responses via OpenAI models (`gpt-4o-mini` recommended)
- Automatic backup and recovery of persona data
- Monitoring and observability via Prometheus metrics
- Scalable deployment with load balancing

---

## 2. System Architecture
**Components:**
1. **Streamlit App** (`app.py`)  
   - Handles UI, persona selection, question submission, AI integration, and persona responses.
2. **Prometheus Metrics** (`Counter`, `Histogram`)  
   - Exposes metrics on `/metrics` for request counts, response times, and errors.
3. **Load Balancer** (NGINX)  
   - Balances traffic between multiple containerized app instances.
4. **Persistent Storage** (`personas.json` + backup `personas_backup.json`)  
   - Stores persona definitions and allows safe recovery.
5. **Optional Cloud Integration**  
   - Can use S3, GCS, or a database backend for production durability.

**Diagram:**

```
[User Browser] --> [NGINX Load Balancer] --> [Streamlit App Containers]
                                         \--> [Prometheus Metrics]
                                         \--> [Persistent Storage / Backup]
```

---

## 3. Prerequisites
- **Python 3.11+** (tested with 3.13)  
- **Docker & Docker Compose** for containerized deployment  
- **OpenAI API key** for AI responses  
- **Prometheus** for metrics collection (optional, but recommended)  

**Python packages** (in `requirements.txt`):
```
streamlit
openai
prometheus_client
```

---

## 4. Installation and Setup

1. **Clone the repository:**
```bash
git clone https://github.com/<your-username>/persona-feedback-simulator.git
cd persona-feedback-simulator
```

2. **Set OpenAI API Key:**
```bash
export OPENAI_API_KEY="sk-..."
```

3. **Install dependencies locally (optional):**
```bash
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

4. **Run the app locally:**
```bash
streamlit run app.py
```

---

## 5. Configuration

### Environment Variables
- `OPENAI_API_KEY` – OpenAI authentication
- `PROMETHEUS_PORT` – Optional, defaults to 8000
- `STREAMLIT_PORT` – Optional, defaults to 8501

### Configuration Files
- `personas.json` – Primary persona definitions
- `personas_backup.json` – Auto-backup file
- `prometheus.yml` – Prometheus scrape configuration
- `nginx.conf` – Load balancer configuration

---

## 6. Database / Data Storage
Currently uses **JSON files**:

### `personas.json`
| Field | Type | Description |
|-------|------|-------------|
| id | integer | Unique persona ID |
| name | string | Persona full name |
| occupation | string | Job title |
| location | string | Geographical location |
| tech_proficiency | string | "Low", "Medium", or "High" |
| behavioral_traits | array[string] | List of persona traits |

### Backup Strategy
- Automatic backup to `personas_backup.json` whenever personas are added or modified.
- Restore automatically if `personas.json` is missing or corrupted.

---

## 7. API Specifications

### 7.1 Internal App Functions
| Function | Endpoint/Usage | Description |
|----------|----------------|-------------|
| `generate_response` | Internal | Sends feature input + persona data to OpenAI and returns conversation |
| `generate_feedback_report` | Internal | Summarizes conversation with actionable insights |
| `instrumented` | Decorator | Wraps functions for Prometheus metrics (`REQUEST_COUNTER`, `RESPONSE_TIME`) |

### 7.2 Prometheus Metrics
Exposed at `/metrics` (default port 8000):

| Metric | Type | Labels | Description |
|--------|------|--------|-------------|
| `app_requests_total` | Counter | `endpoint`, `status` | Counts total requests and success/error outcomes |
| `app_response_time_seconds` | Histogram | `endpoint` | Measures response duration per endpoint |
| `app_heartbeat` | Gauge | – | Optional: timestamp for health checks |

### 7.3 Health Endpoint
Optional `/healthz` endpoint for load balancer or Kubernetes:

```python
@app.route("/healthz")
def health():
    return "OK", 200
```

---

## 8. Deployment Procedures

### 8.1 Docker Deployment
**Dockerfile**
```dockerfile
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8501 8000
CMD ["streamlit", "run", "app.py", "--server.port=8501"]
```

**Docker Compose**
```yaml
version: '3'
services:
  app1:
    build: .
    ports:
      - "8501:8501"
    environment:
      - OPENAI_API_KEY=${OPENAI_API_KEY}
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
```

---

## 9. Monitoring and Metrics

1. **Prometheus Scraping**
```yaml
scrape_configs:
  - job_name: 'persona_app'
    static_configs:
      - targets: ['app1:8000', 'app2:8000', 'app3:8000']
```

2. **Metrics to Watch**
- Request counts per endpoint
- Error rate
- Response latency
- Heartbeat (optional)

3. **Optional Visualization**
- Connect Prometheus to Grafana dashboards
- Create alerts for:
  - High error rate
  - Service unavailability
  - Slow response times

---

## 10. Maintenance and Update Procedures

- **Update Python packages:** `pip install -r requirements.txt --upgrade`
- **Update app code:** Pull latest from GitHub and rebuild Docker containers
- **Persona updates:** Add through UI, then backup occurs automatically
- **Rolling restart:** Use Docker Compose `up -d --force-recreate` or Kubernetes Deployment rolling update
- **Configuration changes:** Update `nginx.conf`, `prometheus.yml`, or `.env` variables and reload respective services

---

## 11. Troubleshooting

| Issue | Solution |
|-------|---------|
| `personas.json` not found | Ensure it exists next to `app.py` or restore from backup |
| Duplicated Prometheus metrics | Use custom registry or `st.cache_resource` (see metrics setup) |
| OpenAI errors | Verify `OPENAI_API_KEY` and network connectivity |
| Streamlit reload issues | Clear browser cache or restart app container |

---

## 12. Backup and Recovery

1. **Automatic Backup:** Occurs whenever personas are added or modified → saved to `personas_backup.json`.
2. **Restore Backup:** If `personas.json` missing or corrupted, app restores from backup on startup.
3. **Manual Backup Trigger:** Call `backup_personas()` in code after persona updates.
4. **Optional Cloud Backup:** Sync `personas_backup.json` to S3/GCS for off-site durability.

---

### Notes for Developers
- Always maintain a **clean `personas.json`** in version control for initial deployments.
- Use the **backup/restore helpers** to prevent accidental persona data loss.
- Keep **metrics endpoints open** only for internal monitoring (don’t expose publicly without authentication).