Spaces:
Sleeping
Sleeping
fix: expose dependencies to system PATH and add robust diagnostics for LLM proxy
Browse files- Dockerfile +1 -1
- backend/llm_proxy.py +4 -0
- debug_env.py +26 -0
- inference.py +26 -40
Dockerfile
CHANGED
|
@@ -18,7 +18,7 @@ RUN apt-get update && apt-get install -y \
|
|
| 18 |
# Install Python dependencies
|
| 19 |
# Install Python dependencies using uv for reproducible builds
|
| 20 |
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 21 |
-
ENV PATH="/root/.local/bin:${PATH}"
|
| 22 |
|
| 23 |
# Copy all metadata and manifests
|
| 24 |
COPY pyproject.toml uv.lock openenv.yaml README.md ./
|
|
|
|
| 18 |
# Install Python dependencies
|
| 19 |
# Install Python dependencies using uv for reproducible builds
|
| 20 |
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 21 |
+
ENV PATH="/root/.local/bin:/app/.venv/bin:${PATH}"
|
| 22 |
|
| 23 |
# Copy all metadata and manifests
|
| 24 |
COPY pyproject.toml uv.lock openenv.yaml README.md ./
|
backend/llm_proxy.py
CHANGED
|
@@ -24,6 +24,8 @@ class LLMProxy:
|
|
| 24 |
self.mode = "none"
|
| 25 |
|
| 26 |
# 1. Prioritize Grader Proxy (OpenAI-compatible)
|
|
|
|
|
|
|
| 27 |
if self.api_base_url and self.api_key:
|
| 28 |
try:
|
| 29 |
self.client = OpenAI(
|
|
@@ -46,6 +48,8 @@ class LLMProxy:
|
|
| 46 |
print("[LLM] google-genai not installed for fallback.")
|
| 47 |
except Exception as e:
|
| 48 |
print(f"[LLM] Gemini init failed: {e}")
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def generate_content(self, prompt: str, image_bytes: bytes = None) -> str:
|
| 51 |
"""
|
|
|
|
| 24 |
self.mode = "none"
|
| 25 |
|
| 26 |
# 1. Prioritize Grader Proxy (OpenAI-compatible)
|
| 27 |
+
print(f"[LLM] Check: base_url={self.api_base_url}, api_key={'[SET]' if self.api_key else '[MISSING]'}")
|
| 28 |
+
|
| 29 |
if self.api_base_url and self.api_key:
|
| 30 |
try:
|
| 31 |
self.client = OpenAI(
|
|
|
|
| 48 |
print("[LLM] google-genai not installed for fallback.")
|
| 49 |
except Exception as e:
|
| 50 |
print(f"[LLM] Gemini init failed: {e}")
|
| 51 |
+
else:
|
| 52 |
+
print("[LLM] WARNING: No API credentials found. AI analysis will be disabled.")
|
| 53 |
|
| 54 |
def generate_content(self, prompt: str, image_bytes: bytes = None) -> str:
|
| 55 |
"""
|
debug_env.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Add backend to path
|
| 6 |
+
_BACKEND = os.path.join(os.getcwd(), "backend")
|
| 7 |
+
if _BACKEND not in sys.path:
|
| 8 |
+
sys.path.insert(0, _BACKEND)
|
| 9 |
+
|
| 10 |
+
print("--- OpenEnv Debug ---")
|
| 11 |
+
print(f"API_BASE_URL: {os.getenv('API_BASE_URL')}")
|
| 12 |
+
print(f"API_KEY: {'[PRESENT]' if os.getenv('API_KEY') else '[MISSING]'}")
|
| 13 |
+
print(f"HF_TOKEN: {'[PRESENT]' if os.getenv('HF_TOKEN') else '[MISSING]'}")
|
| 14 |
+
print(f"MODEL_NAME: {os.getenv('MODEL_NAME')}")
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from llm_proxy import get_llm_client
|
| 18 |
+
client = get_llm_client()
|
| 19 |
+
print(f"LLM Mode: {client.mode}")
|
| 20 |
+
if client.mode == "proxy":
|
| 21 |
+
print(f"Proxy URL: {client.api_base_url}")
|
| 22 |
+
print(f"Model: {client.model_name}")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error initializing proxy: {e}")
|
| 25 |
+
|
| 26 |
+
print("--- End Debug ---")
|
inference.py
CHANGED
|
@@ -83,39 +83,42 @@ def run_inference(ticker: str, sentiment_score: float = 0.5) -> dict:
|
|
| 83 |
sentiment_score: Pre-computed sentiment in [-0.99, 0.99]. Defaults to 0.5 (neutral).
|
| 84 |
"""
|
| 85 |
try:
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
from sentiment import analyze_sentiment
|
| 88 |
from technical_signals import score_technical_signals
|
| 89 |
from llm_analysis import analyze_with_llm
|
| 90 |
|
| 91 |
# 1. Fetch & prepare data
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# 2. Sentiment analysis
|
| 97 |
sent = analyze_sentiment(ticker)
|
| 98 |
raw_sent_score = float(sent.get("overall_score", 0.5))
|
| 99 |
-
# Clamp to (-0.99, 0.99)
|
| 100 |
sent_score = max(-0.99, min(0.99, raw_sent_score))
|
| 101 |
sent_label = sent.get("overall_sentiment", "neutral")
|
| 102 |
|
| 103 |
# 3. Technical signals
|
| 104 |
tech = score_technical_signals(df)
|
| 105 |
tech_score = max(-0.99, min(0.99, float(tech.get("score", 0.5))))
|
| 106 |
-
tech_dir = tech.get("direction", "neutral")
|
| 107 |
|
| 108 |
# 4. LLM market reasoning
|
| 109 |
indicators = {
|
| 110 |
-
"RSI": round(float(latest
|
| 111 |
-
"MACD": round(float(latest
|
| 112 |
-
"
|
| 113 |
-
"
|
| 114 |
-
"SMA_50": round(float(latest["SMA_50"]), 2),
|
| 115 |
-
"BB_Width": round(float(latest["BB_Width"]), 4),
|
| 116 |
-
"ATR": round(float(latest["ATR"]), 2),
|
| 117 |
}
|
| 118 |
recent_prices = df["Close"].values[-10:].tolist()
|
|
|
|
| 119 |
llm = analyze_with_llm(
|
| 120 |
ticker=ticker,
|
| 121 |
current_price=float(latest["Close"]),
|
|
@@ -125,47 +128,30 @@ def run_inference(ticker: str, sentiment_score: float = 0.5) -> dict:
|
|
| 125 |
recent_prices=recent_prices,
|
| 126 |
)
|
| 127 |
llm_score = max(-0.99, min(0.99, float(llm.get("score", 0.5))))
|
| 128 |
-
llm_dir = llm.get("direction", "neutral")
|
| 129 |
|
| 130 |
# 5. Composite directional score (weighted blend)
|
| 131 |
-
|
| 132 |
-
W_TECH = 0.30
|
| 133 |
-
W_LLM = 0.45
|
| 134 |
-
composite = W_SENT * sent_score + W_TECH * tech_score + W_LLM * llm_score
|
| 135 |
-
# Clamp composite to (-0.99, 0.99) before converting
|
| 136 |
composite = max(-0.99, min(0.99, composite))
|
| 137 |
|
| 138 |
# 6. Convert to strict open interval task score
|
| 139 |
task_score = _sentiment_to_task_score(composite)
|
| 140 |
-
score_100 = round(task_score * 100, 1)
|
| 141 |
-
|
| 142 |
-
# 7. Derive direction from composite
|
| 143 |
-
if composite > 0.1:
|
| 144 |
-
direction = "bullish"
|
| 145 |
-
elif composite < -0.1:
|
| 146 |
-
direction = "bearish"
|
| 147 |
-
else:
|
| 148 |
-
direction = "neutral"
|
| 149 |
|
| 150 |
return {
|
| 151 |
"ticker": ticker,
|
| 152 |
-
"score": round(task_score, 6),
|
| 153 |
-
"
|
| 154 |
-
"
|
| 155 |
-
"reasoning": llm.get("reasoning", f"Composite score: {composite:+.3f}"),
|
| 156 |
"source": llm.get("source", "engine"),
|
| 157 |
-
"components": {
|
| 158 |
-
"sentiment": round(sent_score, 4),
|
| 159 |
-
"technical": round(tech_score, 4),
|
| 160 |
-
"llm": round(llm_score, 4),
|
| 161 |
-
"composite": round(composite, 4),
|
| 162 |
-
},
|
| 163 |
}
|
| 164 |
|
| 165 |
except ImportError as e:
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
except Exception as e:
|
| 168 |
-
|
|
|
|
|
|
|
| 169 |
|
| 170 |
|
| 171 |
def run_batch_inference(tickers: list[str]) -> list[dict]:
|
|
|
|
| 83 |
sentiment_score: Pre-computed sentiment in [-0.99, 0.99]. Defaults to 0.5 (neutral).
|
| 84 |
"""
|
| 85 |
try:
|
| 86 |
+
# Logging environment at startup for grader visibility
|
| 87 |
+
print(f"[StockSense] ENV Check: API_URL={API_BASE_URL}, MODEL={MODEL_NAME}, HF_TOKEN={'[SET]' if HF_TOKEN else '[MISSING]'}", flush=True)
|
| 88 |
+
|
| 89 |
+
from stock_data import fetch_stock_data, add_technical_indicators
|
| 90 |
from sentiment import analyze_sentiment
|
| 91 |
from technical_signals import score_technical_signals
|
| 92 |
from llm_analysis import analyze_with_llm
|
| 93 |
|
| 94 |
# 1. Fetch & prepare data
|
| 95 |
+
try:
|
| 96 |
+
df = fetch_stock_data(ticker)
|
| 97 |
+
df = add_technical_indicators(df)
|
| 98 |
+
latest = df.iloc[-1]
|
| 99 |
+
except Exception as data_err:
|
| 100 |
+
print(f"[StockSense] Data fetch failed for {ticker}: {data_err}. Using fallback mechanisms.", flush=True)
|
| 101 |
+
return {**_fallback(f"Data error: {str(data_err)[:80]}"), "ticker": ticker}
|
| 102 |
|
| 103 |
# 2. Sentiment analysis
|
| 104 |
sent = analyze_sentiment(ticker)
|
| 105 |
raw_sent_score = float(sent.get("overall_score", 0.5))
|
|
|
|
| 106 |
sent_score = max(-0.99, min(0.99, raw_sent_score))
|
| 107 |
sent_label = sent.get("overall_sentiment", "neutral")
|
| 108 |
|
| 109 |
# 3. Technical signals
|
| 110 |
tech = score_technical_signals(df)
|
| 111 |
tech_score = max(-0.99, min(0.99, float(tech.get("score", 0.5))))
|
|
|
|
| 112 |
|
| 113 |
# 4. LLM market reasoning
|
| 114 |
indicators = {
|
| 115 |
+
"RSI": round(float(latest.get("RSI", 50)), 2),
|
| 116 |
+
"MACD": round(float(latest.get("MACD", 0)), 4),
|
| 117 |
+
"SMA_20": round(float(latest.get("SMA_20", latest["Close"])), 2),
|
| 118 |
+
"ATR": round(float(latest.get("ATR", 0)), 2),
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
recent_prices = df["Close"].values[-10:].tolist()
|
| 121 |
+
|
| 122 |
llm = analyze_with_llm(
|
| 123 |
ticker=ticker,
|
| 124 |
current_price=float(latest["Close"]),
|
|
|
|
| 128 |
recent_prices=recent_prices,
|
| 129 |
)
|
| 130 |
llm_score = max(-0.99, min(0.99, float(llm.get("score", 0.5))))
|
|
|
|
| 131 |
|
| 132 |
# 5. Composite directional score (weighted blend)
|
| 133 |
+
composite = 0.25 * sent_score + 0.30 * tech_score + 0.45 * llm_score
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
composite = max(-0.99, min(0.99, composite))
|
| 135 |
|
| 136 |
# 6. Convert to strict open interval task score
|
| 137 |
task_score = _sentiment_to_task_score(composite)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
return {
|
| 140 |
"ticker": ticker,
|
| 141 |
+
"score": round(task_score, 6),
|
| 142 |
+
"direction": llm.get("direction", "neutral"),
|
| 143 |
+
"reasoning": llm.get("reasoning", "Analysis complete."),
|
|
|
|
| 144 |
"source": llm.get("source", "engine"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
}
|
| 146 |
|
| 147 |
except ImportError as e:
|
| 148 |
+
msg = f"CRITICAL: Missing dependency: {e}. Check Docker PATH and virtualenv."
|
| 149 |
+
print(f"[StockSense] {msg}", flush=True)
|
| 150 |
+
return {**_fallback(msg), "ticker": ticker}
|
| 151 |
except Exception as e:
|
| 152 |
+
msg = f"CRITICAL: Unexpected engine error: {str(e)[:150]}"
|
| 153 |
+
print(f"[StockSense] {msg}", flush=True)
|
| 154 |
+
return {**_fallback(msg), "ticker": ticker}
|
| 155 |
|
| 156 |
|
| 157 |
def run_batch_inference(tickers: list[str]) -> list[dict]:
|