amitbhatt6075 commited on
Commit
6c28c47
·
1 Parent(s): 0a19c36

feat(analytics): Replace unreliable LLM with robust Python-based summary generator

Browse files
Files changed (1) hide show
  1. core/strategist.py +31 -48
core/strategist.py CHANGED
@@ -274,60 +274,43 @@ You are an expert Campaign Analyst...
274
 
275
  def generate_influencer_analytics_summary(self, kpis: Dict[str, Any]) -> str:
276
  """
277
- [BULLETPROOF "FILL-IN-THE-BLANKS" VERSION] This gives the AI model a strict template
278
- to complete, preventing hallucinations and ensuring a consistent, encouraging tone.
279
  """
280
- if not self.llm: return "AI Analyst is currently calibrating. Please check back shortly."
281
-
282
- print(f"--- Strategist Skill (Influencer Analytics): Received KPIs for analysis.")
283
-
284
- # === THE "FILL-IN-THE-BLANKS" PROMPT ===
285
- # Model ko ab sirf खाली jagah bharni hai.
286
- prompt = f"""You are an AI Analyst. Your job is to complete the 2-sentence summary below using the provided data.
287
- DO NOT change the template. Only fill in the [adjective] and [actionable tip] placeholders.
288
-
289
- [DATA]
290
- - Engagement Rate: {kpis.get('avgEngagementRate', 0.0):.2f}%
291
- - Total Reach: {kpis.get('totalReach', 0)}
292
 
293
- [TEMPLATE TO COMPLETE]
294
- "Your engagement rate is looking [adjective], which is great for building your community! To boost your reach even further, try to [actionable tip] on your next post."
295
-
296
- [YOUR COMPLETED SUMMARY]
297
- """
298
-
299
  try:
300
- print(" - Calling LLM with Fill-in-the-Blanks prompt...")
301
- response = self.llm(
302
- prompt,
303
- max_tokens=150,
304
- temperature=0.8, # Thodi creativity allow karte hain adjective ke liye
305
- stop=["[INST]", "[DATA]", "[TEMPLATE TO COMPLETE]", "\n\n"],
306
- echo=False
307
- )
308
- raw_text = response['choices'][0]['text'].strip()
309
-
310
- print(f" - Raw AI Response: '{raw_text}'")
311
-
312
- # --- PYTHON CLEANUP (FINAL VERSION) ---
313
- summary_text = raw_text.replace("Answer:", "").strip()
314
-
315
- # Agar AI ne phir bhi quotes add kiye, to unhein hata do
316
- if summary_text.startswith('"') and summary_text.endswith('"'):
317
- summary_text = summary_text[1:-1]
 
 
 
 
 
318
 
319
- # Agar AI ne galti se template repeat kiya, toh fallback message do
320
- if "[adjective]" in summary_text or "[actionable tip]" in summary_text:
321
- print(" - ⚠️ AI failed to fill the template. Falling back to a default message.")
322
- return "Your performance analytics are looking solid! Keep creating great content to grow your reach."
323
-
324
- print(f" - ✅ LLM generated and CLEANED summary successfully: '{summary_text}'")
325
- return summary_text
326
 
327
  except Exception as e:
328
- print(f"--- Strategist Skill (Influencer Analytics) ERROR: {e}")
329
- return "AI summary could not be generated at this time."
330
-
331
 
332
  def generate_influencer_growth_plan(self, influencer_data: Dict[str, Any]) -> List[str]:
333
  """
 
274
 
275
  def generate_influencer_analytics_summary(self, kpis: Dict[str, Any]) -> str:
276
  """
277
+ [BULLETPROOF "NON-AI" VERSION] This function no longer calls the LLM.
278
+ It generates a high-quality, dynamic summary using Python logic for 100% reliability.
279
  """
280
+ print(f"--- Strategist Skill (Influencer Analytics): Generating summary with RELIABLE PYTHON logic.")
 
 
 
 
 
 
 
 
 
 
 
281
 
 
 
 
 
 
 
282
  try:
283
+ engagement_rate = kpis.get('avgEngagementRate', 0.0)
284
+ reach = kpis.get('totalReach', 0)
285
+
286
+ # --- DYNAMIC SENTENCE GENERATION LOGIC ---
287
+
288
+ # Part 1: Adjective for engagement
289
+ if engagement_rate > 5.0:
290
+ adjective = "excellent"
291
+ elif engagement_rate > 2.5:
292
+ adjective = "solid"
293
+ else:
294
+ adjective = "good"
295
+
296
+ # Part 2: Actionable tip based on the most impactful metric
297
+ if reach < 1000 and engagement_rate < 3.0:
298
+ actionable_tip = "try collaborating with another creator to cross-promote and grow your audience."
299
+ elif engagement_rate < 2.0:
300
+ actionable_tip = "try asking a question in your next caption to boost comments and engagement."
301
+ else:
302
+ actionable_tip = "try using a trending audio or format on your next Reel to expand your reach."
303
+
304
+ # Part 3: Combine everything into a perfect summary
305
+ summary = f"Your engagement rate is looking {adjective}, which is great for building community! To grow even further, {actionable_tip}"
306
 
307
+ print(f" - Python-generated summary: '{summary}'")
308
+ return summary
 
 
 
 
 
309
 
310
  except Exception as e:
311
+ print(f"--- Strategist Skill (Influencer Analytics) PYTHON ERROR: {e}")
312
+ return "Analytics summary is currently unavailable due to a system error."
313
+
314
 
315
  def generate_influencer_growth_plan(self, influencer_data: Dict[str, Any]) -> List[str]:
316
  """