fix(music-lab): market API 타입 강화·ANTHROPIC_API_KEY call-time·HTTP 레이어 테스트 추가

This commit is contained in:
2026-05-01 12:32:24 +09:00
parent 26b9eea0dc
commit 355667cf9c
3 changed files with 69 additions and 8 deletions

View File

@@ -2,8 +2,8 @@ import json
import os
import shutil
import uuid
from typing import List, Optional
from fastapi import FastAPI, HTTPException, BackgroundTasks
from typing import Any, Dict, List, Optional
from fastapi import FastAPI, HTTPException, BackgroundTasks, Query
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
@@ -819,7 +819,7 @@ def remove_revenue(record_id: int):
# ── 시장 조사 API ─────────────────────────────────────────────────────────────
class MarketIngestRequest(BaseModel):
trends: list
trends: List[Dict[str, Any]]
report_date: str = ""
@@ -837,7 +837,7 @@ def list_market_trends(
country: Optional[str] = None,
genre: Optional[str] = None,
source: Optional[str] = None,
days: int = 7,
days: int = Query(7, ge=1),
):
return {"trends": _get_market_trends(country, genre, source, days)}

View File

@@ -8,8 +8,6 @@ from .db import (
insert_market_trends, upsert_trend_report,
)
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "")
GENRE_PROMPTS: Dict[str, str] = {
"lo-fi": "lo-fi hip hop, chill, relaxing beats, study music, 85 BPM, jazzy chords",
"phonk": "dark phonk, aggressive 808 bass, Memphis trap, distorted synths, 140 BPM",
@@ -71,14 +69,15 @@ def _build_report(trends: List[Dict[str, Any]], report_date: str) -> Dict[str, A
def _generate_insights(top_genres: list, top_keywords: list) -> str:
api_key = os.getenv("ANTHROPIC_API_KEY", "")
if not top_genres:
return "아직 수집된 트렌드 데이터가 없습니다."
if not ANTHROPIC_API_KEY:
if not api_key:
names = ", ".join(g["genre"] for g in top_genres[:3])
return f"이번 주 인기 장르: {names}. 해당 장르 중심 제작을 추천합니다."
import anthropic
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
client = anthropic.Anthropic(api_key=api_key)
genre_str = ", ".join(f"{g['genre']}({g['score']:.1f})" for g in top_genres[:5])
kw_str = ", ".join(top_keywords[:10])
try: