amitbhatt6075 commited on
Commit
14f0730
·
1 Parent(s): e10f2f3

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

Browse files
Files changed (1) hide show
  1. core/strategist.py +34 -34
core/strategist.py CHANGED
@@ -173,49 +173,49 @@ Your Answer:
173
 
174
  def generate_analytics_insights(self, analytics_data: dict) -> str:
175
  """
176
- [BULLETPROOF VERSION] Takes campaign analytics data and generates 3 actionable insights
177
- using a very simple, direct prompt for small LLMs.
178
  """
179
- if not self.llm:
180
- return "AI Analyst is currently unavailable."
181
-
182
- print(f"--- Strategist Skill (Analytics Insights): Received analytics data.")
 
 
183
 
184
- # === THE "IDIOT-PROOF" SIMPLIFIED PROMPT ===
185
- # Hum AI ko sirf 3 bullet points poora karne ko bol rahe hain.
186
- prompt = f"""You are an expert campaign analyst.
187
- Analyze the data below and provide ONLY 3 short, actionable bullet points about the campaign's performance.
188
 
189
- [CAMPAIGN DATA]
190
- - Total Reach: {analytics_data.get('totalReach', 0)}
191
- - Average Engagement Rate: {analytics_data.get('averageEngagementRate', 0.0):.2f}%
192
- - Top Performing Influencer: {analytics_data.get('topPerformingInfluencer', 'N/A')}
 
 
 
193
 
194
- [YOUR 3 BULLET POINTS]
195
- - The total reach of {analytics_data.get('totalReach', 0)} is a good start.
196
- - """ # Hum pehla point khud shuru kar denge taaki AI ko hint mil jaaye.
197
-
198
- print("--- Strategist Skill (Analytics Insights): Sending simplified prompt to LLM...")
199
- try:
200
- response = self.llm(
201
- prompt,
202
- max_tokens=250,
203
- temperature=0.7,
204
- stop=["[SYSTEM]", "[CAMPAIGN DATA]", "User:", "\n\n-"], # Stop at new bullet points
205
- echo=False
206
- )
207
- raw_text = response['choices'][0]['text'].strip()
208
 
209
- # Final response ko format karein
210
- final_insights = "- The total reach of {:,} is a good start.\n- {}".format(analytics_data.get('totalReach', 0), raw_text)
 
 
 
211
 
212
- print("--- Strategist Skill (Analytics Insights): Successfully received and processed response.")
213
- return final_insights
214
 
 
 
215
  except Exception as e:
216
- print(f"--- Strategist Skill (Analytics Insights) FATAL ERROR: {e}")
217
  traceback.print_exc()
218
- return "- AI insights could not be generated due to an internal model error."
219
 
220
 
221
  def get_caption_assistance(self, caption: str, action: str, guidelines: str = None) -> str:
 
173
 
174
  def generate_analytics_insights(self, analytics_data: dict) -> str:
175
  """
176
+ [BULLETPROOF "NON-AI" VERSION] This function no longer calls the LLM.
177
+ It generates a high-quality, dynamic summary using Python logic for 100% reliability.
178
  """
179
+ print(f"--- Strategist Skill (Analytics Insights): Generating summary with RELIABLE PYTHON logic.")
180
+
181
+ try:
182
+ reach = analytics_data.get('totalReach', 0)
183
+ engagement_rate = analytics_data.get('averageEngagementRate', 0.0)
184
+ top_influencer = analytics_data.get('topPerformingInfluencer', 'N/A')
185
 
186
+ insights = []
 
 
 
187
 
188
+ # Insight 1: Analyze Reach
189
+ if reach > 50000:
190
+ insights.append(f"- The campaign reached an impressive {reach:,} people, indicating strong initial visibility.")
191
+ elif reach > 0:
192
+ insights.append(f"- With a total reach of {reach:,}, the campaign is building a good foundation. Let's focus on amplifying this.")
193
+ else:
194
+ insights.append("- Campaign data is still being gathered. Initial reach numbers will appear here soon.")
195
 
196
+ # Insight 2: Analyze Engagement
197
+ if engagement_rate > 3.5:
198
+ insights.append(f"- The engagement rate is excellent at {engagement_rate:.2f}%. The content is clearly resonating with the audience.")
199
+ elif engagement_rate > 1.5:
200
+ insights.append(f"- The engagement rate of {engagement_rate:.2f}% is solid. A great next step is to encourage more comments and shares.")
201
+ else:
202
+ insights.append("- To boost the engagement rate, try creating more interactive content like polls or Q&A sessions.")
 
 
 
 
 
 
 
203
 
204
+ # Insight 3: Analyze Top Performer
205
+ if top_influencer and top_influencer != "N/A":
206
+ insights.append(f"- Analyze the content from '{top_influencer}'. Their successful strategy should be replicated with other creators.")
207
+ else:
208
+ insights.append("- It is crucial to identify top-performing influencers early. This helps in doubling down on successful strategies.")
209
 
210
+ final_summary = "\n".join(insights)
211
+ print(f" - ✅ Python-generated insights:\n{final_summary}")
212
 
213
+ return final_summary
214
+
215
  except Exception as e:
216
+ print(f"--- Strategist Skill (Analytics Insights) PYTHON ERROR: {e}")
217
  traceback.print_exc()
218
+ return "- Analytics summary is currently unavailable due to a system error."
219
 
220
 
221
  def get_caption_assistance(self, caption: str, action: str, guidelines: str = None) -> str: