Fix HF Inference API: correct router URL + Qwen2.5 model
Browse files- URL: api-inference.huggingface.co β router.huggingface.co/v1/chat/completions
(old URL no longer works; new router is the current HF standard)
- Model: mistralai/Mistral-7B-Instruct-v0.2 β Qwen/Qwen2.5-7B-Instruct-1M
(Mistral-7B removed from free tier; Qwen2.5-7B is in current free tier)
- Add status code logging so failures are visible in HF Spaces logs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- ai_analyst/ai_analyst.py +10 -5
- docker-compose.yml +1 -1
ai_analyst/ai_analyst.py
CHANGED
|
@@ -11,7 +11,7 @@ from shared.kafka_utils import create_producer, create_consumer
|
|
| 11 |
OLLAMA_HOST = os.getenv("OLLAMA_HOST", "") # e.g. http://host.docker.internal:11434
|
| 12 |
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3.1:8b")
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 14 |
-
HF_MODEL = os.getenv("HF_MODEL", "
|
| 15 |
ANALYSIS_INTERVAL = int(os.getenv("ANALYSIS_INTERVAL", "1800")) # 30 min default
|
| 16 |
|
| 17 |
# ββ Rolling market data buffers ββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -49,14 +49,18 @@ def call_llm(prompt: str) -> str | None:
|
|
| 49 |
except Exception as e:
|
| 50 |
print(f"[AI-Analyst] Ollama unreachable: {e}")
|
| 51 |
|
| 52 |
-
# 2. HuggingFace Inference API
|
| 53 |
if HF_TOKEN:
|
| 54 |
-
url =
|
|
|
|
| 55 |
for attempt in range(3):
|
| 56 |
try:
|
| 57 |
resp = requests.post(
|
| 58 |
url,
|
| 59 |
-
headers={
|
|
|
|
|
|
|
|
|
|
| 60 |
json={
|
| 61 |
"model": HF_MODEL,
|
| 62 |
"messages": [{"role": "user", "content": prompt}],
|
|
@@ -65,6 +69,7 @@ def call_llm(prompt: str) -> str | None:
|
|
| 65 |
},
|
| 66 |
timeout=60,
|
| 67 |
)
|
|
|
|
| 68 |
if resp.status_code == 200:
|
| 69 |
text = resp.json()["choices"][0]["message"]["content"].strip()
|
| 70 |
if text:
|
|
@@ -76,7 +81,7 @@ def call_llm(prompt: str) -> str | None:
|
|
| 76 |
print(f"[AI-Analyst] HF model loading, waiting {wait:.0f}s (attempt {attempt+1}/3)")
|
| 77 |
time.sleep(min(float(wait), 30))
|
| 78 |
else:
|
| 79 |
-
print(f"[AI-Analyst] HF HTTP {resp.status_code}: {resp.text[:
|
| 80 |
break
|
| 81 |
except Exception as e:
|
| 82 |
print(f"[AI-Analyst] HF API error (attempt {attempt+1}/3): {e}")
|
|
|
|
| 11 |
OLLAMA_HOST = os.getenv("OLLAMA_HOST", "") # e.g. http://host.docker.internal:11434
|
| 12 |
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3.1:8b")
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 14 |
+
HF_MODEL = os.getenv("HF_MODEL", "Qwen/Qwen2.5-7B-Instruct-1M")
|
| 15 |
ANALYSIS_INTERVAL = int(os.getenv("ANALYSIS_INTERVAL", "1800")) # 30 min default
|
| 16 |
|
| 17 |
# ββ Rolling market data buffers ββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
print(f"[AI-Analyst] Ollama unreachable: {e}")
|
| 51 |
|
| 52 |
+
# 2. HuggingFace Inference API β router.huggingface.co (OpenAI-compatible)
|
| 53 |
if HF_TOKEN:
|
| 54 |
+
url = "https://router.huggingface.co/v1/chat/completions"
|
| 55 |
+
print(f"[AI-Analyst] Calling HF router: model={HF_MODEL}")
|
| 56 |
for attempt in range(3):
|
| 57 |
try:
|
| 58 |
resp = requests.post(
|
| 59 |
url,
|
| 60 |
+
headers={
|
| 61 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 62 |
+
"Content-Type": "application/json",
|
| 63 |
+
},
|
| 64 |
json={
|
| 65 |
"model": HF_MODEL,
|
| 66 |
"messages": [{"role": "user", "content": prompt}],
|
|
|
|
| 69 |
},
|
| 70 |
timeout=60,
|
| 71 |
)
|
| 72 |
+
print(f"[AI-Analyst] HF response status: {resp.status_code}")
|
| 73 |
if resp.status_code == 200:
|
| 74 |
text = resp.json()["choices"][0]["message"]["content"].strip()
|
| 75 |
if text:
|
|
|
|
| 81 |
print(f"[AI-Analyst] HF model loading, waiting {wait:.0f}s (attempt {attempt+1}/3)")
|
| 82 |
time.sleep(min(float(wait), 30))
|
| 83 |
else:
|
| 84 |
+
print(f"[AI-Analyst] HF HTTP {resp.status_code}: {resp.text[:400]}")
|
| 85 |
break
|
| 86 |
except Exception as e:
|
| 87 |
print(f"[AI-Analyst] HF API error (attempt {attempt+1}/3): {e}")
|
docker-compose.yml
CHANGED
|
@@ -158,7 +158,7 @@ services:
|
|
| 158 |
- OLLAMA_HOST=http://host.docker.internal:11434
|
| 159 |
- OLLAMA_MODEL=llama3.1:8b
|
| 160 |
- HF_TOKEN=${HF_TOKEN:-}
|
| 161 |
-
- HF_MODEL=${HF_MODEL:-
|
| 162 |
- ANALYSIS_INTERVAL=1800
|
| 163 |
extra_hosts:
|
| 164 |
- "host.docker.internal:host-gateway"
|
|
|
|
| 158 |
- OLLAMA_HOST=http://host.docker.internal:11434
|
| 159 |
- OLLAMA_MODEL=llama3.1:8b
|
| 160 |
- HF_TOKEN=${HF_TOKEN:-}
|
| 161 |
+
- HF_MODEL=${HF_MODEL:-Qwen/Qwen2.5-7B-Instruct-1M}
|
| 162 |
- ANALYSIS_INTERVAL=1800
|
| 163 |
extra_hosts:
|
| 164 |
- "host.docker.internal:host-gateway"
|