Spaces:
Paused
Paused
A newer version of the Gradio SDK is available: 6.20.0
π 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
# 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)
- Go to https://huggingface.co/new-space
- Fill in:
- Owner: your HF username / org
- Space name:
medicine-safety-agent(or any name) - License: MIT
- SDK: Gradio
- SDK version: 4.27.0
- Click Create Space.
Option B β via CLI
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
# 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_USERNAMEwith 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:
- Open your Space β Settings tab β Repository secrets.
- 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
- Visit
https://huggingface.co/spaces/YOUR_USERNAME/medicine-safety-agent - Wait for the Building badge to turn Running (β 2β3 minutes on first build).
- 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 |
gemini |
GEMINI_API_KEY |
|
| Anthropic | claude |
ANTHROPIC_API_KEY |
8. Updating the App
# 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 |