Create tokens.md
Browse files
tokens.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Author : Georgios Ioannou
|
| 2 |
+
#
|
| 3 |
+
# Copyright © 2024 by Georgios Ioannou
|
| 4 |
+
|
| 5 |
+
# Setting Up API Tokens for Hugging Face & OpenAI in Hugging Face Space
|
| 6 |
+
|
| 7 |
+
## 1. Create Required Tokens
|
| 8 |
+
1. **Hugging Face Token**:
|
| 9 |
+
- Log in to [Hugging Face](https://huggingface.co)
|
| 10 |
+
- Go to Settings → Access Tokens
|
| 11 |
+
- Click "New token"
|
| 12 |
+
- Name your token and select permissions
|
| 13 |
+
- Copy the token
|
| 14 |
+
|
| 15 |
+
2. **OpenAI API Key**:
|
| 16 |
+
- Log in to [OpenAI](https://platform.openai.com)
|
| 17 |
+
- Go to API settings
|
| 18 |
+
- Create new API key
|
| 19 |
+
- Copy the key
|
| 20 |
+
|
| 21 |
+
## 2. Add Tokens to Space Settings
|
| 22 |
+
1. Go to your Hugging Face Space settings (⚙️ icon)
|
| 23 |
+
2. Find "Variables and secrets" section
|
| 24 |
+
3. Add both tokens as "New secret" one by one:
|
| 25 |
+
```toml
|
| 26 |
+
MONGO_URI = "your_mongo_uri"
|
| 27 |
+
HUGGINGFACEHUB_API_TOKEN = "your_huggingface_token_here"
|
| 28 |
+
OPENAI_API_KEY = "your_openai_key_here"
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## 3. Access Tokens in app.py
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
import os
|
| 35 |
+
|
| 36 |
+
# Load environment variable(s)
|
| 37 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 38 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## Alternative Methods
|
| 42 |
+
### Using Streamlit secrets.toml
|
| 43 |
+
```toml
|
| 44 |
+
# .streamlit/secrets.toml
|
| 45 |
+
HUGGINGFACEHUB_API_TOKEN = "your_token_here"
|
| 46 |
+
OPENAI_API_KEY = "your_key_here"
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
## Security Best Practices
|
| 50 |
+
- Never commit tokens to version control
|
| 51 |
+
- Add `.env` or `secrets.toml` to `.gitignore`
|
| 52 |
+
- Use read-only tokens when possible
|
| 53 |
+
- Regularly reresh your API keys
|
| 54 |
+
- Set appropriate token permissions
|