File size: 4,179 Bytes
532e19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Login & Logout System β€” Usage Guide

## 🎯 Overview

The system now supports two modes:

### 1. **Local Mode** (Development with .env)
- API key in `.env` file
- **No login page** β€” goes straight to setup
- Logout button **hidden**
- For developers/instructors

### 2. **Hosted Mode** (Render/Hugging Face)
- No `.env` file (or empty `OPENAI_API_KEY`)
- **Login page shown** β€” users provide name + API key
- Logout button **visible** in top-right corner
- Each user pays for their own OpenAI usage

---

## πŸš€ Running Locally

### With API Key (Skip Login):
```bash
# Make sure .env has OPENAI_API_KEY
python challenge.py
# or
python app.py
```

### Without API Key (Test Login Page):
```bash
# Temporarily hide .env
mv .env .env.backup

# Run the app
python app.py

# You'll see the login page!
# Enter name + API key to continue

# Restore .env when done
mv .env.backup .env
```

---

## 🌐 Deploying to Render

See [RENDER_DEPLOYMENT.md](RENDER_DEPLOYMENT.md) for full guide.

**Quick steps:**
1. Push code to GitHub
2. Connect Render to your repo
3. Render auto-detects `render.yaml`
4. Deploy! πŸš€

**Don't add `OPENAI_API_KEY` to Render** β€” let users provide their own!

---

## πŸ” How Login Works

### Login Page Components:
- **Name field**: Student's name (shown in header)
- **API Key field**: OpenAI API key (sk-proj-...)
- **Continue button**: Validates key and proceeds

### Validation:
- Checks format (must start with `sk-`)
- Makes test call to OpenAI API
- Shows error if invalid/no quota
- Sets key in environment on success

### Logout:
- **Button location**: Top-right corner (after login)
- **Action**: Clears API key, returns to login page
- **Note**: Refresh won't logout β€” must click button explicitly

---

## πŸ”„ Auto-Detection Logic

```python
has_env_key = bool(os.getenv("OPENAI_API_KEY"))

if has_env_key:
    # Show setup page directly
    # Hide login page
    # Hide logout button
else:
    # Show login page
    # Hide setup page
    # Show logout button after login
```

---

## πŸ“ Files Modified

- βœ… `student_interface.py` β€” Added `validate_openai_key()`, login page, logout button
- βœ… `challenge.py` β€” Made API key optional for GUI mode
- βœ… `app.py` β€” New entry point for hosting (uses port from env)
- βœ… `render.yaml` β€” Render configuration
- βœ… `RENDER_DEPLOYMENT.md` β€” Deployment guide

---

## ⚠️ Important Notes

### For Local Users:
- Keep your `.env` file **private** (in `.gitignore`)
- Never commit API keys to git!

### For Hosted Users:
- Each student needs their own OpenAI API key
- Keys are **session-only** (not stored)
- Logout clears the key from memory

### Cost Management:
- **Hosted mode**: Each user pays for their own usage
- **Local mode**: You pay (instructor/developer)
- **Recommendation**: Use hosted mode for courses/public access

---

## πŸ§ͺ Testing

### Test Login Flow:
```bash
# 1. Hide .env to force login page
mv .env .env.backup

# 2. Run app
python app.py

# 3. Try login with:
#    - Empty fields β†’ error
#    - Invalid key β†’ error  
#    - Valid key β†’ success!

# 4. Test logout button
#    - Click logout β†’ returns to login page
#    - Login again β†’ works!

# 5. Restore .env
mv .env.backup .env
```

### Test Local Mode:
```bash
# With .env present
python challenge.py

# Should skip login page entirely
# Go straight to setup page
# No logout button visible
```

---

## πŸ› Troubleshooting

**Q: Login page not showing?**  
A: Check that `OPENAI_API_KEY` is not in `.env` or environment

**Q: Logout button not visible?**  
A: It only appears after successful login (hosted mode)

**Q: "API key valid but no quota"?**  
A: User needs to add billing/credits at platform.openai.com/billing

**Q: Refresh goes back to setup page?**  
A: Known Gradio limitation β€” use explicit logout button instead

**Q: Can I force login page even with .env?**  
A: Yes! Temporarily comment out `OPENAI_API_KEY` in `.env`

---

## πŸ“š Related Documentation

- [RENDER_DEPLOYMENT.md](RENDER_DEPLOYMENT.md) β€” How to deploy to Render
- [add_login.md](add_login.md) β€” Detailed implementation notes
- [README.md](README.md) β€” Main project documentation