amitbhatt6075 commited on
Commit
43c61c9
Β·
1 Parent(s): e806ed2

Fix NameError for get_lazy_llm and enable async trend decoding

Browse files
Files changed (1) hide show
  1. api/main.py +26 -10
api/main.py CHANGED
@@ -54,11 +54,11 @@ EMBEDDING_MODEL_PATH = os.path.join(ROOT_DIR, 'embedding_model')
54
  DB_PATH = os.path.join(os.environ.get("WRITABLE_DIR", "/tmp"), "vector_db_persistent")
55
 
56
  # --- Global Instances ---
57
- _llm_instance: Optional[Llama] = None
58
- _vector_store: Optional[Any] = None
59
- _ai_strategist: Optional[AIStrategist] = None
60
- _creative_director: CreativeDirector | None = None
61
- _support_agent: Optional[SupportAgent] = None
62
  _budget_predictor = None
63
  _influencer_matcher = None
64
  _performance_predictor = None
@@ -69,11 +69,31 @@ _likes_predictor = None
69
  _comments_predictor = None
70
  _revenue_forecaster = None
71
  _performance_scorer = None
72
- _community_brain: Optional[CommunityBrain] = None
73
 
74
  def to_snake(name: str) -> str:
75
  return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # ==============================================================
78
  # 🎯 FIX 1: DEFINE NESTED CLASSES FIRST
79
  # These MUST come before they are used in ForecastResponse.
@@ -1770,14 +1790,10 @@ async def decode_trend_endpoint(req: TrendAnalysisRequest):
1770
  # 2. Process the request
1771
  from core.thunderbird_engine import decode_market_trend
1772
 
1773
- # We don't need to await this because the llm_instance call itself is synchronous
1774
- # The benefit of async on the endpoint is that FastAPI can handle other requests
1775
- # while this one is waiting for the AI.
1776
  result = decode_market_trend(req.topic, ai_brain)
1777
 
1778
  return result
1779
 
1780
  except Exception as e:
1781
  print(f"❌ AI Decoding Error in Endpoint: {e}")
1782
- # traceback.print_exc()
1783
  raise HTTPException(status_code=500, detail="An internal error occurred in the AI.")
 
54
  DB_PATH = os.path.join(os.environ.get("WRITABLE_DIR", "/tmp"), "vector_db_persistent")
55
 
56
  # --- Global Instances ---
57
+ _llm_instance = None
58
+ _vector_store = None
59
+ _ai_strategist = None
60
+ _creative_director = None
61
+ _support_agent = None
62
  _budget_predictor = None
63
  _influencer_matcher = None
64
  _performance_predictor = None
 
69
  _comments_predictor = None
70
  _revenue_forecaster = None
71
  _performance_scorer = None
72
+ _community_brain = None
73
 
74
  def to_snake(name: str) -> str:
75
  return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
76
 
77
+ def get_lazy_llm():
78
+ """Wakes up the AI model only when it's needed."""
79
+ global _llm_instance
80
+ if _llm_instance:
81
+ return _llm_instance
82
+
83
+ print("⏳ Awakening AI Brain (Loading LLM on-demand)...")
84
+ try:
85
+ from llama_cpp import Llama
86
+ if not os.path.exists(LLAMA_MODEL_PATH):
87
+ print(" - Downloading model (first-time only)...")
88
+ hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME, local_dir=MODEL_SAVE_DIRECTORY)
89
+
90
+ _llm_instance = Llama(model_path=LLAMA_MODEL_PATH, n_ctx=1024, n_threads=2, verbose=False)
91
+ print("βœ… AI Brain is Active.")
92
+ return _llm_instance
93
+ except Exception as e:
94
+ print(f"❌ Failed to load AI: {e}")
95
+ return None
96
+
97
  # ==============================================================
98
  # 🎯 FIX 1: DEFINE NESTED CLASSES FIRST
99
  # These MUST come before they are used in ForecastResponse.
 
1790
  # 2. Process the request
1791
  from core.thunderbird_engine import decode_market_trend
1792
 
 
 
 
1793
  result = decode_market_trend(req.topic, ai_brain)
1794
 
1795
  return result
1796
 
1797
  except Exception as e:
1798
  print(f"❌ AI Decoding Error in Endpoint: {e}")
 
1799
  raise HTTPException(status_code=500, detail="An internal error occurred in the AI.")