Spaces:
Sleeping
Sleeping
|
Download LOGIN_GUIDE.md from refaelz/legacy-code-challenge-v2: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/refaelz/legacy-code-challenge-v2/resolve/main/LOGIN_GUIDE.md
- Command line
-
hf download hf://spaces/refaelz/legacy-code-challenge-v2/LOGIN_GUIDE.md
-
curl -L -o LOGIN_GUIDE.md https://huggingface.co/spaces/refaelz/legacy-code-challenge-v2/resolve/main/LOGIN_GUIDE.md
4.18 kB
Login & Logout System β Usage Guide
π― Overview
The system now supports two modes:
1. Local Mode (Development with .env)
- API key in
.envfile - No login page β goes straight to setup
- Logout button hidden
- For developers/instructors
2. Hosted Mode (Render/Hugging Face)
- No
.envfile (or emptyOPENAI_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):
# Make sure .env has OPENAI_API_KEY
python challenge.py
# or
python app.py
Without API Key (Test Login Page):
# 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 for full guide.
Quick steps:
- Push code to GitHub
- Connect Render to your repo
- Render auto-detects
render.yaml - 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
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β Addedvalidate_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
.envfile 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:
# 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:
# 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 β How to deploy to Render
- add_login.md β Detailed implementation notes
- README.md β Main project documentation