mirror of
https://github.com/bnair123/MusicAnalyser.git
synced 2026-02-25 11:46:07 +00:00
feat: Initial backend setup for Music Analyser
- Created FastAPI backend structure. - Implemented Spotify Recently Played ingestion logic. - Set up SQLite database with SQLAlchemy models. - Added AI Service using Google Gemini. - Created helper scripts for auth and background worker. - Added Dockerfile and GitHub Actions workflow.
This commit is contained in:
40
backend/app/services/ai_service.py
Normal file
40
backend/app/services/ai_service.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
import google.generativeai as genai
|
||||
from typing import List
|
||||
from ..models import PlayHistory, Track
|
||||
|
||||
class AIService:
|
||||
def __init__(self, api_key: str):
|
||||
genai.configure(api_key=api_key)
|
||||
self.model = genai.GenerativeModel('models/gemini-2.0-flash')
|
||||
|
||||
def generate_analysis(self, plays: List[PlayHistory]) -> str:
|
||||
"""
|
||||
Generates a summary analysis of the provided play history.
|
||||
"""
|
||||
if not plays:
|
||||
return "No listening history available to analyze."
|
||||
|
||||
# Prepare a simple text representation of the history
|
||||
history_text = "Here is my recent listening history:\n"
|
||||
for play in plays:
|
||||
history_text += f"- {play.track.name} by {play.track.artist} (Played at {play.played_at})\n"
|
||||
|
||||
prompt = f"""
|
||||
You are a music taste analyst.
|
||||
Analyze the following listening history and provide a short, fun, and insightful summary.
|
||||
Identify the vibe, top artists, and any interesting patterns (e.g. "You started with high energy and chilled out").
|
||||
Keep it under 200 words.
|
||||
|
||||
{history_text}
|
||||
"""
|
||||
|
||||
try:
|
||||
response = self.model.generate_content(prompt)
|
||||
return response.text
|
||||
except Exception as e:
|
||||
return f"AI Analysis failed: {str(e)}"
|
||||
|
||||
# Singleton accessor
|
||||
def get_ai_service():
|
||||
return AIService(api_key=os.getenv("GEMINI_API_KEY"))
|
||||
Reference in New Issue
Block a user