Spaces:
Paused
Paused
| # π Deployment Guide β Medicine Safety Agent on Hugging Face Spaces | |
| --- | |
| ## 1. Prerequisites | |
| | Tool | Install | | |
| |------|---------| | |
| | Python 3.10+ | https://python.org | | |
| | Git | https://git-scm.com | | |
| | Git-LFS | `git lfs install` | | |
| | Hugging Face account | https://huggingface.co/join | | |
| | `huggingface_hub` CLI | `pip install huggingface_hub` | | |
| --- | |
| ## 2. Run Locally First | |
| ```bash | |
| # 1 β Clone / enter your project folder | |
| cd med-agent | |
| # 2 β Create a virtual environment | |
| python -m venv .venv | |
| source .venv/bin/activate # Windows: .venv\Scripts\activate | |
| # 3 β Install dependencies | |
| pip install -r requirements.txt | |
| # 4 β Set up your API key | |
| cp .env .env | |
| # Edit .env β set LLM_PROVIDER and the matching API key | |
| # 5 β Launch | |
| python app.py | |
| # Open http://localhost:7860 | |
| ``` | |
| --- | |
| ## 3. Create a Hugging Face Space | |
| ### Option A β via the Web UI (easiest) | |
| 1. Go to https://huggingface.co/new-space | |
| 2. Fill in: | |
| - **Owner**: your HF username / org | |
| - **Space name**: `medicine-safety-agent` (or any name) | |
| - **License**: MIT | |
| - **SDK**: **Gradio** | |
| - **SDK version**: 4.27.0 | |
| 3. Click **Create Space**. | |
| ### Option B β via CLI | |
| ```bash | |
| huggingface-cli login # paste your HF token | |
| python - <<'EOF' | |
| from huggingface_hub import HfApi | |
| api = HfApi() | |
| api.create_repo( | |
| repo_id="YOUR_USERNAME/medicine-safety-agent", | |
| repo_type="space", | |
| space_sdk="gradio", | |
| private=False, # set True for a private Space | |
| ) | |
| EOF | |
| ``` | |
| --- | |
| ## 4. Push Code to the Space | |
| ```bash | |
| # Inside your project folder | |
| # 4-a Initialise git (skip if already a git repo) | |
| git init | |
| git add . | |
| git commit -m "initial commit: Medicine Safety Agent" | |
| # 4-b Add the HF Space as a remote | |
| git remote add space https://huggingface.co/spaces/YOUR_USERNAME/medicine-safety-agent | |
| # 4-c Push | |
| git push space main | |
| ``` | |
| > π‘ Replace `YOUR_USERNAME` with your actual Hugging Face username. | |
| --- | |
| ## 5. Set Secret API Keys in the Space | |
| **Never commit your `.env` file.** | |
| Add secrets via the Space Settings UI: | |
| 1. Open your Space β **Settings** tab β **Repository secrets**. | |
| 2. Add each secret: | |
| | Name | Value | | |
| |------|-------| | |
| | `LLM_PROVIDER` | `openai` (or `gemini` / `claude`) | | |
| | `LLM_MODEL` | *(optional)* e.g. `gpt-4o` | | |
| | `OPENAI_API_KEY` | `sk-...` | | |
| | `GEMINI_API_KEY` | `AIza...` | | |
| | `ANTHROPIC_API_KEY` | `sk-ant-...` | | |
| Hugging Face injects these as environment variables at runtime β exactly what `python-dotenv` / `os.getenv` reads. | |
| --- | |
| ## 6. Verify the Deployment | |
| 1. Visit `https://huggingface.co/spaces/YOUR_USERNAME/medicine-safety-agent` | |
| 2. Wait for the **Building** badge to turn **Running** (β 2β3 minutes on first build). | |
| 3. Enter a medicine name and click **Analyse Medicine**. | |
| --- | |
| ## 7. Switching LLM Providers | |
| Update the `LLM_PROVIDER` secret in Space Settings (and the matching key) β no code change needed. | |
| | Provider | `LLM_PROVIDER` | Key secret | | |
| |----------|---------------|------------| | |
| | OpenAI | `openai` | `OPENAI_API_KEY` | | |
| | Google | `gemini` | `GEMINI_API_KEY` | | |
| | Anthropic| `claude` | `ANTHROPIC_API_KEY` | | |
| --- | |
| ## 8. Updating the App | |
| ```bash | |
| # Make your code changes, then: | |
| git add . | |
| git commit -m "feat: your change description" | |
| git push space main | |
| ``` | |
| The Space auto-rebuilds on every push. | |
| --- | |
| ## 9. Project Structure Reference | |
| ``` | |
| med-agent/ | |
| βββ app.py β Gradio UI entry point | |
| βββ requirements.txt β Python dependencies | |
| βββ README.md β HF Space card metadata + docs | |
| βββ .env.example β API key template (never commit .env) | |
| β | |
| βββ agents/ | |
| β βββ __init__.py | |
| β βββ medicine_agent.py β Orchestrates the 3 AI tasks | |
| β | |
| βββ llm/ | |
| β βββ __init__.py | |
| β βββ connector.py β Generic multi-provider LLM interface | |
| β | |
| βββ utils/ | |
| βββ __init__.py | |
| βββ search.py β DuckDuckGo web search utility | |
| ``` | |
| --- | |
| ## 10. Troubleshooting | |
| | Symptom | Fix | | |
| |---------|-----| | |
| | `ModuleNotFoundError` | Ensure all packages are in `requirements.txt` | | |
| | `AuthenticationError` | Check your API key secret in Space Settings | | |
| | Space stuck at "Building" | Check the **Logs** tab for the error | | |
| | Empty search results | DuckDuckGo may rate-limit; add a short `time.sleep(1)` in `search.py` | | |
| | JSON parse error from LLM | The model returned markdown fences β the strip logic in `medicine_agent.py` handles this, but try a different model if it persists | | |