Fixed and added all the stats_service.py methods

This commit is contained in:
bnair123
2025-12-25 17:48:41 +04:00
parent d63a05fb72
commit 508d001d7e
4 changed files with 580 additions and 245 deletions

View File

@@ -18,43 +18,35 @@ class NarrativeService:
return {"error": "Missing API Key"}
prompt = f"""
You are analyzing a user's Spotify listening data. Below is a JSON summary of metrics I've computed. Your job is to:
You are a witty, insightful, and slightly snarky music critic analyzing a user's listening history.
Below is a JSON summary of their listening data.
1. Write a narrative "Vibe Check" (2-3 paragraphs) describing their overall listening personality this period.
2. Identify 3-5 notable patterns or anomalies.
3. Provide a "Musical Persona" label (e.g., "Late-Night Binge Listener", "Genre Chameleon", "Album Purist").
4. Write a brief, playful "roast" (1-2 sentences) based on the data.
Your goal is to generate a report that feels like a 'Spotify Wrapped' but deeper and more honest.
Guidelines:
- Do NOT recalculate any numbers.
- Use specific metrics to support observations (e.g., "Your whiplash score of 18.3 BPM suggests...").
- Keep tone conversational but insightful.
- Avoid mental health claims; stick to behavioral descriptors.
- Highlight both positive patterns and quirks.
Please output your response in strict JSON format with the following keys:
1. "vibe_check": (String) 2-3 paragraphs describing their overall listening personality.
2. "patterns": (List of Strings) 3-5 specific observations based on the data (e.g., "You listen to sad music on Tuesdays", "Your Whiplash Score is high").
3. "persona": (String) A creative label for the user (e.g., "The Genre Chameleon", "Nostalgic Dad-Rocker", "Algorithm Victim").
4. "roast": (String) A playful, harmlessly mean roast about their taste (1-2 sentences).
5. "era_insight": (String) A specific comment on their 'Musical Age' and 'Nostalgia Gap'.
Data:
GUIDELINES:
- **Use the Metrics:** Do not just say "You like pop." Say "Your Mainstream Score of 85% suggests you live on the Top 40."
- **Whiplash Score:** If 'whiplash' > 20, comment on their chaotic transitions.
- **Hipster Score:** If 'hipster_score' > 50, call them pretentious; if < 10, call them basic.
- **Comparison:** Use the 'comparison' block to mention if they are listening more/less or if their mood (valence/energy) has shifted.
- **Tone:** Conversational, fun, slightly judgmental but good-natured.
DATA:
{json.dumps(stats_json, indent=2)}
Output Format (return valid JSON):
{{
"vibe_check": "...",
"patterns": ["...", "..."],
"persona": "...",
"roast": "..."
}}
OUTPUT (JSON):
"""
try:
# Handle full model path if passed or default short name
# The library often accepts 'gemini-2.5-flash' but list_models returns 'models/gemini-2.5-flash'
model_id = self.model_name
if not model_id.startswith("models/") and "/" not in model_id:
# Try simple name, if it fails user might need to pass 'models/...'
pass
model = genai.GenerativeModel(model_id)
model = genai.GenerativeModel(self.model_name)
response = model.generate_content(prompt)
# Clean up response to ensure valid JSON (sometimes LLMs add markdown blocks)
# Clean up response to ensure valid JSON
text = response.text.strip()
if text.startswith("```json"):
text = text.replace("```json", "").replace("```", "")
@@ -64,4 +56,4 @@ Output Format (return valid JSON):
return json.loads(text)
except Exception as e:
return {"error": str(e), "raw_response": response.text if 'response' in locals() else "No response"}
return {"error": str(e), "raw_response": "Error generating narrative."}