Spaces:
Running
Running
details.wes commited on
Commit ·
7f62453
1
Parent(s): 3d60a43
solved the problems in hf token
Browse files- .env.example +19 -0
- .gitignore +5 -0
- README.md +1 -1
- __pycache__/crew.cpython-313.pyc +0 -0
- __pycache__/service.cpython-313.pyc +0 -0
- crew.py +48 -10
- requirements.txt +4 -3
- service.py +5 -1
.env.example
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copy to ".env" in this same folder (Automatic-post-agents/) — service.py loads it on startup.
|
| 2 |
+
# https://huggingface.co/settings/tokens
|
| 3 |
+
|
| 4 |
+
# Required
|
| 5 |
+
HF_TOKEN=hf_replace_with_your_token
|
| 6 |
+
# LiteLLM Hugging Face form: huggingface/<org>/<model> (see Hub Inference / provider badges).
|
| 7 |
+
# WebWorld is NOT on Inference Providers unless you use AGENTS_LLM_BASE_URL to your own endpoint.
|
| 8 |
+
AGENTS_LLM_MODEL=your-model
|
| 9 |
+
|
| 10 |
+
# Optional
|
| 11 |
+
AGENTS_LLM_TEMPERATURE=0.72
|
| 12 |
+
# Only if you use a dedicated OpenAI-compatible endpoint (e.g. HF Inference Endpoint URL):
|
| 13 |
+
# AGENTS_LLM_BASE_URL=https://xxxx.region.aws.endpoints.huggingface.cloud
|
| 14 |
+
|
| 15 |
+
# Optional: return full tracebacks from POST /generate (do not use in production)
|
| 16 |
+
# AGENTS_DEBUG=1
|
| 17 |
+
|
| 18 |
+
# Optional: uvicorn reads PORT when you use python service.py; you can also pass --port on the CLI
|
| 19 |
+
# PORT=9000
|
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
.venv/
|
| 5 |
+
venv/
|
README.md
CHANGED
|
@@ -15,7 +15,7 @@ FastAPI app that exposes:
|
|
| 15 |
- `GET /health` — liveness check
|
| 16 |
- `POST /generate` — body: `topic`, optional `feedback`, `memory_context`, `tone_instruction`; returns `{"post": "..."}`
|
| 17 |
|
| 18 |
-
Set **`HF_TOKEN`** (or **`HUGGINGFACE_HUB_TOKEN`**) as a [Space secret](https://huggingface.co/docs/hub/spaces-overview#managing-secrets) (required).
|
| 19 |
|
| 20 |
**Port:** the container listens on `$PORT` when the platform sets it, otherwise **7860** (Hugging Face Spaces default).
|
| 21 |
|
|
|
|
| 15 |
- `GET /health` — liveness check
|
| 16 |
- `POST /generate` — body: `topic`, optional `feedback`, `memory_context`, `tone_instruction`; returns `{"post": "..."}`
|
| 17 |
|
| 18 |
+
Set **`HF_TOKEN`** (or **`HUGGINGFACE_HUB_TOKEN`**) as a [Space secret](https://huggingface.co/docs/hub/spaces-overview#managing-secrets) (required). Set **`AGENTS_LLM_MODEL`** as a Space **variable** (required — e.g. `huggingface/Qwen/Qwen2.5-7B-Instruct`; see `.env.example`). Optionally set **`AGENTS_LLM_TEMPERATURE`** or **`AGENTS_LLM_BASE_URL`** for an OpenAI-compatible endpoint (see `crew.py`). Do **not** use WebWorld with the public HF router only — it is not supported there unless **`AGENTS_LLM_BASE_URL`** points at your own endpoint.
|
| 19 |
|
| 20 |
**Port:** the container listens on `$PORT` when the platform sets it, otherwise **7860** (Hugging Face Spaces default).
|
| 21 |
|
__pycache__/crew.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/crew.cpython-313.pyc and b/__pycache__/crew.cpython-313.pyc differ
|
|
|
__pycache__/service.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/service.cpython-313.pyc and b/__pycache__/service.cpython-313.pyc differ
|
|
|
crew.py
CHANGED
|
@@ -1,18 +1,26 @@
|
|
| 1 |
"""
|
| 2 |
Crew definition for the agents service.
|
| 3 |
|
| 4 |
-
LLM is provided via LiteLLM
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
provider
|
| 9 |
-
huggingface/<provider>/Qwen/WebWorld-32B
|
| 10 |
-
Example shape (valid only when listed on the model card): huggingface/together/Qwen/WebWorld-32B
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
AGENTS_LLM_TEMPERATURE float, default 0.5
|
| 15 |
-
AGENTS_LLM_BASE_URL
|
| 16 |
"""
|
| 17 |
from __future__ import annotations
|
| 18 |
|
|
@@ -26,6 +34,8 @@ from crewai.project import CrewBase, agent, crew, task
|
|
| 26 |
|
| 27 |
def _resolve_hf_token() -> str:
|
| 28 |
token = (os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN") or "").strip()
|
|
|
|
|
|
|
| 29 |
if not token:
|
| 30 |
raise RuntimeError(
|
| 31 |
"Missing Hugging Face token. Set HF_TOKEN (or HUGGINGFACE_HUB_TOKEN) in the "
|
|
@@ -34,14 +44,42 @@ def _resolve_hf_token() -> str:
|
|
| 34 |
return token
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def _build_llm() -> LLM:
|
| 38 |
-
model = (
|
| 39 |
temperature = float(os.getenv("AGENTS_LLM_TEMPERATURE", "0.5"))
|
| 40 |
|
| 41 |
hf_token = _resolve_hf_token()
|
| 42 |
os.environ["HF_TOKEN"] = hf_token
|
|
|
|
| 43 |
|
| 44 |
base_url = (os.getenv("AGENTS_LLM_BASE_URL") or "").strip().rstrip("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if base_url:
|
| 46 |
return LLM(
|
| 47 |
model=model,
|
|
|
|
| 1 |
"""
|
| 2 |
Crew definition for the agents service.
|
| 3 |
|
| 4 |
+
LLM is provided via LiteLLM against Hugging Face. Set the model id in the environment — see
|
| 5 |
+
AGENTS_LLM_MODEL in .env.example (LiteLLM form: huggingface/<org>/<model>).
|
| 6 |
|
| 7 |
+
Pick a model that your Hugging Face account can run via Inference Providers (check the model's Hub page
|
| 8 |
+
for Inference / provider badges), or set AGENTS_LLM_BASE_URL to your own OpenAI-compatible endpoint.
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
Qwen/WebWorld-32B is not served on the public Inference Providers router; use it only with
|
| 11 |
+
AGENTS_LLM_BASE_URL (e.g. your own HF Inference Endpoint or vLLM).
|
| 12 |
+
|
| 13 |
+
If a plain huggingface/<org>/<model> call fails, LiteLLM supports:
|
| 14 |
+
huggingface/<provider>/<org>/<model>
|
| 15 |
+
only when that provider is listed on the model card for this model (do not guess the provider).
|
| 16 |
+
|
| 17 |
+
Required environment:
|
| 18 |
+
HF_TOKEN (or HUGGINGFACE_HUB_TOKEN)
|
| 19 |
+
AGENTS_LLM_MODEL
|
| 20 |
+
|
| 21 |
+
Optional:
|
| 22 |
AGENTS_LLM_TEMPERATURE float, default 0.5
|
| 23 |
+
AGENTS_LLM_BASE_URL OpenAI-compatible base URL (e.g. HF Inference Endpoint)
|
| 24 |
"""
|
| 25 |
from __future__ import annotations
|
| 26 |
|
|
|
|
| 34 |
|
| 35 |
def _resolve_hf_token() -> str:
|
| 36 |
token = (os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN") or "").strip()
|
| 37 |
+
if (token.startswith('"') and token.endswith('"')) or (token.startswith("'") and token.endswith("'")):
|
| 38 |
+
token = token[1:-1].strip()
|
| 39 |
if not token:
|
| 40 |
raise RuntimeError(
|
| 41 |
"Missing Hugging Face token. Set HF_TOKEN (or HUGGINGFACE_HUB_TOKEN) in the "
|
|
|
|
| 44 |
return token
|
| 45 |
|
| 46 |
|
| 47 |
+
def _strip_optional_env_quotes(value: str) -> str:
|
| 48 |
+
v = value.strip()
|
| 49 |
+
if (v.startswith('"') and v.endswith('"')) or (v.startswith("'") and v.endswith("'")):
|
| 50 |
+
v = v[1:-1].strip()
|
| 51 |
+
return v
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _resolve_llm_model() -> str:
|
| 55 |
+
raw = (os.getenv("AGENTS_LLM_MODEL") or "").strip()
|
| 56 |
+
raw = _strip_optional_env_quotes(raw)
|
| 57 |
+
if not raw:
|
| 58 |
+
raise RuntimeError(
|
| 59 |
+
"Missing AGENTS_LLM_MODEL. Set it in the environment (see .env.example), e.g. "
|
| 60 |
+
"huggingface/Qwen/Qwen2.5-7B-Instruct"
|
| 61 |
+
)
|
| 62 |
+
return raw
|
| 63 |
+
|
| 64 |
+
|
| 65 |
def _build_llm() -> LLM:
|
| 66 |
+
model = _resolve_llm_model()
|
| 67 |
temperature = float(os.getenv("AGENTS_LLM_TEMPERATURE", "0.5"))
|
| 68 |
|
| 69 |
hf_token = _resolve_hf_token()
|
| 70 |
os.environ["HF_TOKEN"] = hf_token
|
| 71 |
+
os.environ["HUGGINGFACE_HUB_TOKEN"] = hf_token
|
| 72 |
|
| 73 |
base_url = (os.getenv("AGENTS_LLM_BASE_URL") or "").strip().rstrip("/")
|
| 74 |
+
base_url = _strip_optional_env_quotes(base_url) if base_url else ""
|
| 75 |
+
|
| 76 |
+
if "webworld" in model.lower() and not base_url:
|
| 77 |
+
raise RuntimeError(
|
| 78 |
+
"AGENTS_LLM_MODEL is set to a WebWorld model, which Hugging Face Inference Providers "
|
| 79 |
+
"does not host. Set AGENTS_LLM_MODEL to a routed instruct model, or run WebWorld on your "
|
| 80 |
+
"own endpoint and set AGENTS_LLM_BASE_URL to that OpenAI-compatible base URL."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
if base_url:
|
| 84 |
return LLM(
|
| 85 |
model=model,
|
requirements.txt
CHANGED
|
@@ -2,9 +2,10 @@ fastapi
|
|
| 2 |
uvicorn[standard]
|
| 3 |
python-dotenv
|
| 4 |
|
| 5 |
-
# LLM: set HF_TOKEN (or HUGGINGFACE_HUB_TOKEN)
|
| 6 |
-
#
|
| 7 |
-
#
|
|
|
|
| 8 |
crewai
|
| 9 |
crewai-tools
|
| 10 |
litellm
|
|
|
|
| 2 |
uvicorn[standard]
|
| 3 |
python-dotenv
|
| 4 |
|
| 5 |
+
# LLM: set HF_TOKEN (or HUGGINGFACE_HUB_TOKEN) and AGENTS_LLM_MODEL (required — no default in code).
|
| 6 |
+
# LiteLLM model ids: huggingface/<org>/<model>. See crew.py and .env.example.
|
| 7 |
+
# If plain huggingface/<org>/<model> fails, try huggingface/<provider>/<org>/<model> only when the Hub
|
| 8 |
+
# model page lists that inference provider (do not guess the provider).
|
| 9 |
crewai
|
| 10 |
crewai-tools
|
| 11 |
litellm
|
service.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
@@ -17,6 +18,8 @@ _backend_env = Path(__file__).resolve().parents[1] / "backend" / ".env"
|
|
| 17 |
load_dotenv(_agents_env, override=False)
|
| 18 |
load_dotenv(_backend_env, override=False)
|
| 19 |
|
|
|
|
|
|
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
|
|
@@ -54,8 +57,9 @@ def generate(req: GenerateRequest):
|
|
| 54 |
result = crew_instance.kickoff(inputs={"topic": prompt})
|
| 55 |
return {"post": str(result)}
|
| 56 |
except Exception as exc:
|
| 57 |
-
# Don't leak internal stack traces
|
| 58 |
msg = str(exc) or "Agent generation failed"
|
|
|
|
| 59 |
if os.getenv("AGENTS_DEBUG", "").strip() in ("1", "true", "yes"):
|
| 60 |
raise
|
| 61 |
raise HTTPException(500, msg[:500])
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
import logging
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
|
|
| 18 |
load_dotenv(_agents_env, override=False)
|
| 19 |
load_dotenv(_backend_env, override=False)
|
| 20 |
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
app = FastAPI()
|
| 24 |
|
| 25 |
|
|
|
|
| 57 |
result = crew_instance.kickoff(inputs={"topic": prompt})
|
| 58 |
return {"post": str(result)}
|
| 59 |
except Exception as exc:
|
| 60 |
+
# Don't leak internal stack traces in the HTTP response by default.
|
| 61 |
msg = str(exc) or "Agent generation failed"
|
| 62 |
+
logger.warning("POST /generate failed: %s", msg, exc_info=True)
|
| 63 |
if os.getenv("AGENTS_DEBUG", "").strip() in ("1", "true", "yes"):
|
| 64 |
raise
|
| 65 |
raise HTTPException(500, msg[:500])
|