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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Dict, List
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class MarketAnalyzer:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
# Initialize Gemini
|
| 13 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 14 |
+
if not api_key:
|
| 15 |
+
raise ValueError("GOOGLE_API_KEY not found")
|
| 16 |
+
|
| 17 |
+
genai.configure(api_key=api_key)
|
| 18 |
+
self.model = genai.GenerativeModel('gemini-pro')
|
| 19 |
+
|
| 20 |
+
def analyze_commercial_viability(self, coverage: str, creative_analysis: str) -> str:
|
| 21 |
+
"""Analyze commercial potential based on coverage and creative analysis"""
|
| 22 |
+
prompt = f"""As an experienced film industry analyst, evaluate the commercial viability
|
| 23 |
+
of this screenplay based on the following coverage and analysis. Consider:
|
| 24 |
+
- Box office potential
|
| 25 |
+
- Genre marketability
|
| 26 |
+
- International appeal
|
| 27 |
+
- Franchise potential
|
| 28 |
+
- Current market trends
|
| 29 |
+
- Production challenges and risks
|
| 30 |
+
|
| 31 |
+
Coverage:
|
| 32 |
+
{coverage}
|
| 33 |
+
|
| 34 |
+
Creative Analysis:
|
| 35 |
+
{creative_analysis}
|
| 36 |
+
|
| 37 |
+
Provide a detailed commercial viability analysis with specific examples and market comparisons."""
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
response = self.model.generate_content(prompt)
|
| 41 |
+
return response.text
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Error analyzing commercial viability: {str(e)}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
def identify_target_audiences(self, coverage: str, viability_analysis: str) -> str:
|
| 47 |
+
"""Identify and analyze potential target audiences"""
|
| 48 |
+
prompt = f"""Based on the screenplay coverage and commercial viability analysis,
|
| 49 |
+
identify and analyze key target audiences. For each audience segment, consider:
|
| 50 |
+
- Demographics and psychographics
|
| 51 |
+
- Viewing habits and preferences
|
| 52 |
+
- Marketing channels to reach them
|
| 53 |
+
- Similar films they've supported
|
| 54 |
+
- Potential audience size and engagement level
|
| 55 |
+
|
| 56 |
+
Coverage:
|
| 57 |
+
{coverage}
|
| 58 |
+
|
| 59 |
+
Viability Analysis:
|
| 60 |
+
{viability_analysis}
|
| 61 |
+
|
| 62 |
+
Provide a detailed audience analysis with specific segments and supporting data."""
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
response = self.model.generate_content(prompt)
|
| 66 |
+
return response.text
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logger.error(f"Error identifying target audiences: {str(e)}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def analyze_comparables(self, coverage: str, target_audience: str) -> str:
|
| 72 |
+
"""Analyze comparable films and their performance"""
|
| 73 |
+
prompt = f"""Using the screenplay coverage and target audience analysis,
|
| 74 |
+
identify and analyze comparable films. For each comparable:
|
| 75 |
+
- Box office performance
|
| 76 |
+
- Budget range
|
| 77 |
+
- Marketing approach
|
| 78 |
+
- Release strategy
|
| 79 |
+
- Audience reception
|
| 80 |
+
- Critical response
|
| 81 |
+
- What worked/didn't work
|
| 82 |
+
|
| 83 |
+
Coverage:
|
| 84 |
+
{coverage}
|
| 85 |
+
|
| 86 |
+
Target Audience Analysis:
|
| 87 |
+
{target_audience}
|
| 88 |
+
|
| 89 |
+
Provide detailed analysis of 3-5 most relevant comparable films."""
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
response = self.model.generate_content(prompt)
|
| 93 |
+
return response.text
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logger.error(f"Error analyzing comparables: {str(e)}")
|
| 96 |
+
return None
|
| 97 |
+
|
| 98 |
+
def assess_budget_range(self, coverage: str, comparables: str) -> str:
|
| 99 |
+
"""Assess potential budget range based on content and comparables"""
|
| 100 |
+
prompt = f"""Based on the screenplay coverage and comparable films analysis,
|
| 101 |
+
evaluate appropriate budget ranges. Consider:
|
| 102 |
+
- Production requirements (VFX, locations, stunts)
|
| 103 |
+
- Cast level needed
|
| 104 |
+
- Technical requirements
|
| 105 |
+
- Marketing spend implications
|
| 106 |
+
- ROI potential at different budget levels
|
| 107 |
+
|
| 108 |
+
Coverage:
|
| 109 |
+
{coverage}
|
| 110 |
+
|
| 111 |
+
Comparables Analysis:
|
| 112 |
+
{comparables}
|
| 113 |
+
|
| 114 |
+
Provide detailed budget analysis with ranges and justification."""
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
response = self.model.generate_content(prompt)
|
| 118 |
+
return response.text
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"Error assessing budget: {str(e)}")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
def evaluate_marketing_angles(self, all_analyses: Dict[str, str]) -> str:
|
| 124 |
+
"""Evaluate potential marketing angles based on all previous analyses"""
|
| 125 |
+
analyses_text = "\n\n".join([f"{k}:\n{v}" for k, v in all_analyses.items()])
|
| 126 |
+
|
| 127 |
+
prompt = f"""Based on all previous analyses, identify and evaluate key marketing angles. Consider:
|
| 128 |
+
- Unique selling propositions
|
| 129 |
+
- Genre hooks
|
| 130 |
+
- Cast/talent opportunities
|
| 131 |
+
- Visual marketing potential
|
| 132 |
+
- Social media angles
|
| 133 |
+
- PR opportunities
|
| 134 |
+
- Promotional partnerships
|
| 135 |
+
- Release timing strategies
|
| 136 |
+
|
| 137 |
+
Previous Analyses:
|
| 138 |
+
{analyses_text}
|
| 139 |
+
|
| 140 |
+
Provide comprehensive marketing strategy recommendations with specific angles and approaches."""
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
response = self.model.generate_content(prompt)
|
| 144 |
+
return response.text
|
| 145 |
+
except Exception as e:
|
| 146 |
+
logger.error(f"Error evaluating marketing angles: {str(e)}")
|
| 147 |
+
return None
|
| 148 |
+
|
| 149 |
+
def generate_market_analysis(self, coverage_path: Path, creative_analysis_path: Path) -> bool:
|
| 150 |
+
"""Main method to generate complete market analysis"""
|
| 151 |
+
try:
|
| 152 |
+
# Read input files
|
| 153 |
+
with open(coverage_path, 'r', encoding='utf-8') as f:
|
| 154 |
+
coverage = f.read()
|
| 155 |
+
with open(creative_analysis_path, 'r', encoding='utf-8') as f:
|
| 156 |
+
creative_analysis = f.read()
|
| 157 |
+
|
| 158 |
+
# Generate analyses
|
| 159 |
+
analyses = {}
|
| 160 |
+
|
| 161 |
+
logger.info("Analyzing commercial viability...")
|
| 162 |
+
analyses['commercial_viability'] = self.analyze_commercial_viability(
|
| 163 |
+
coverage, creative_analysis)
|
| 164 |
+
|
| 165 |
+
logger.info("Identifying target audiences...")
|
| 166 |
+
analyses['target_audiences'] = self.identify_target_audiences(
|
| 167 |
+
coverage, analyses['commercial_viability'])
|
| 168 |
+
|
| 169 |
+
logger.info("Analyzing comparable films...")
|
| 170 |
+
analyses['comparables'] = self.analyze_comparables(
|
| 171 |
+
coverage, analyses['target_audiences'])
|
| 172 |
+
|
| 173 |
+
logger.info("Assessing budget ranges...")
|
| 174 |
+
analyses['budget_assessment'] = self.assess_budget_range(
|
| 175 |
+
coverage, analyses['comparables'])
|
| 176 |
+
|
| 177 |
+
logger.info("Evaluating marketing angles...")
|
| 178 |
+
analyses['marketing_strategy'] = self.evaluate_marketing_angles(analyses)
|
| 179 |
+
|
| 180 |
+
# Save complete analysis
|
| 181 |
+
output_path = coverage_path.parent / "market_analysis.txt"
|
| 182 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 183 |
+
f.write("SCREENPLAY MARKET ANALYSIS\n\n")
|
| 184 |
+
|
| 185 |
+
for title, content in analyses.items():
|
| 186 |
+
formatted_title = title.replace('_', ' ').upper()
|
| 187 |
+
f.write(f"### {formatted_title} ###\n\n")
|
| 188 |
+
f.write(content)
|
| 189 |
+
f.write("\n\n")
|
| 190 |
+
|
| 191 |
+
logger.info(f"Market analysis saved to: {output_path}")
|
| 192 |
+
return True
|
| 193 |
+
|
| 194 |
+
except Exception as e:
|
| 195 |
+
logger.error(f"Error in market analysis: {str(e)}")
|
| 196 |
+
return False
|