Spaces:
Sleeping
Sleeping
adding readme with intructions
Browse files
README.md
CHANGED
|
@@ -11,4 +11,70 @@ license: mit
|
|
| 11 |
short_description: using-external-apis
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
short_description: using-external-apis
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Using external APIs (OpenAI) on Hugging Face Spaces
|
| 15 |
+
|
| 16 |
+
This Space runs a small [Gradio](https://www.gradio.app/) app with **two tabs** that both call **OpenAI Chat Completions** the same way a student would in a notebook—once with plain HTTP (`requests`) and once with Hugging Face’s client (`huggingface_hub.InferenceClient`).
|
| 17 |
+
|
| 18 |
+
Official Space configuration reference: [Spaces config reference](https://huggingface.co/docs/hub/spaces-config-reference).
|
| 19 |
+
|
| 20 |
+
## How to use the app
|
| 21 |
+
|
| 22 |
+
1. **Set your API key** (see [Secrets](#secrets-openai_api_key) below). Without `OPENAI_API_KEY`, the app will raise when it reads the environment variable.
|
| 23 |
+
2. Open the Space and pick a tab:
|
| 24 |
+
- **`requests`** — builds a `POST` to `https://api.openai.com/v1/chat/completions` with JSON `model` + `messages` and a `Bearer` token header. Useful when you are teaching REST, curl, or reading OpenAI’s HTTP docs side by side with the code.
|
| 25 |
+
- **`huggingface_hub`** — creates an `InferenceClient` aimed at OpenAI’s `/v1` base URL and calls `chat.completions.create(...)`. The request shape matches the “OpenAI-style” chat API described in the [huggingface_hub inference guide](https://huggingface.co/docs/huggingface_hub/guides/inference).
|
| 26 |
+
3. Type a question, submit, and read the answer in the output box. Both tabs use the same model name (`MODEL` in [`app.py`](app.py)).
|
| 27 |
+
|
| 28 |
+
## Secrets: `OPENAI_API_KEY`
|
| 29 |
+
|
| 30 |
+
- In the Space: **Settings → Variables and secrets** (or **Repository secrets** depending on UI), add a **secret** named exactly **`OPENAI_API_KEY`** with your OpenAI API key as the value.
|
| 31 |
+
- Hugging Face exposes repository secrets to the running app as **environment variables** with the same name. The code uses `os.environ["OPENAI_API_KEY"]`—no key files, no pasting keys into the README or `app.py`.
|
| 32 |
+
- **Do not** commit keys to git or put them in this README’s YAML frontmatter. If a key ever leaks, rotate it in the OpenAI dashboard.
|
| 33 |
+
|
| 34 |
+
### Gotchas with secrets
|
| 35 |
+
|
| 36 |
+
- **Name must match**: `OPENAI_API_KEY` (case-sensitive) must match what [`app.py`](app.py) expects.
|
| 37 |
+
- **Restart**: After adding or changing a secret, **restart the Space** (or trigger a new build) if the app still behaves as if the variable were missing.
|
| 38 |
+
- **Local runs**: On your laptop, export the variable yourself, for example:
|
| 39 |
+
- macOS/Linux: `export OPENAI_API_KEY="sk-..."` then run the app.
|
| 40 |
+
- Never check a `.env` file with real keys into version control.
|
| 41 |
+
|
| 42 |
+
## `requirements.txt` on Hugging Face Spaces
|
| 43 |
+
|
| 44 |
+
This repo’s [`requirements.txt`](requirements.txt) lists **one Python package per line**. Spaces use it to `pip install` dependencies before starting the app.
|
| 45 |
+
|
| 46 |
+
- **Blank lines** are usually fine; avoid stray text that is not a valid `pip` requirement.
|
| 47 |
+
- **Pinning** (e.g. `gradio==6.14.0`) improves reproducibility so a future `gradio` major release does not surprise your class. The README frontmatter’s `sdk_version` should stay compatible with the Gradio version you install (see below).
|
| 48 |
+
- **Order** does not matter for `pip`, but keeping standard library–heavy teachers first (`requests`, `huggingface_hub`) can make the file easier to read.
|
| 49 |
+
|
| 50 |
+
## README formatting for Hugging Face Spaces
|
| 51 |
+
|
| 52 |
+
Spaces treat the top of this file specially:
|
| 53 |
+
|
| 54 |
+
- The block between the **first** `---` and the **second** `---` is **YAML frontmatter**. Hugging Face reads fields like `sdk`, `sdk_version`, `app_file`, and `title` from it.
|
| 55 |
+
- **Do not remove** that block unless you know how you will configure the Space elsewhere.
|
| 56 |
+
- **Common gotchas**:
|
| 57 |
+
- **`app_file`** must point to the file that calls `launch()` (here: `app.py`).
|
| 58 |
+
- **`sdk_version`** should match the Gradio major/minor you rely on; a mismatch with `requirements.txt` can produce confusing import or layout errors.
|
| 59 |
+
- **Indentation** in YAML matters; keep values simple strings without unquoted colons that break parsing.
|
| 60 |
+
- **Content below the closing `---`** is normal Markdown for humans (this section). You can edit it freely without changing how the Space boots, as long as the frontmatter stays valid.
|
| 61 |
+
|
| 62 |
+
## Other setup gotchas
|
| 63 |
+
|
| 64 |
+
- **Model name**: `MODEL` in [`app.py`](app.py) must be a model your OpenAI **account** and **billing** can access. If OpenAI returns an error, the simplified app does not catch it—you will see the raw exception in the Space logs, which is fine for teaching debugging.
|
| 65 |
+
- **Network**: The Space runtime must reach `api.openai.com`. Corporate or restricted networks are rare on HF but worth knowing for local copies.
|
| 66 |
+
- **`huggingface_hub` vs Hub login**: This app sends your **OpenAI** key to **OpenAI** (`base_url="https://api.openai.com/v1"`). You do **not** need a Hugging Face user token for these two tabs unless you change the code to call HF Inference Providers or the HF router instead.
|
| 67 |
+
|
| 68 |
+
## Local quick start (optional)
|
| 69 |
+
|
| 70 |
+
From this directory, with Python 3 and a virtual environment:
|
| 71 |
+
|
| 72 |
+
```bash
|
| 73 |
+
python -m venv .venv
|
| 74 |
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 75 |
+
pip install -r requirements.txt
|
| 76 |
+
export OPENAI_API_KEY="sk-..." # use your real key; do not commit it
|
| 77 |
+
python app.py
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
Then open the URL Gradio prints (often `http://127.0.0.1:7860`).
|