File size: 13,430 Bytes
f2100d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
"""
Google Gemini API client for text analysis and content generation
"""
import os
import json
import logging
from typing import Dict, List, Optional, Any
import google.generativeai as genai
from config import Config
logger = logging.getLogger(__name__)
class GeminiClient:
"""Client for Google Gemini API integration"""
def __init__(self):
"""Initialize Gemini client"""
self.api_key = Config.GEMINI_API_KEY
if not self.api_key:
logger.warning("Gemini API key not found. Some features may be limited.")
self.client = None
else:
try:
genai.configure(api_key=self.api_key)
self.model = genai.GenerativeModel(Config.GEMINI_MODEL)
self.client = self.model
logger.info("Gemini client initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize Gemini client: {e}")
self.client = None
def analyze_content(self, text_analysis: Dict) -> Dict:
"""
Analyze content using Gemini API
Args:
text_analysis: Text analysis from TextProcessor
Returns:
Enhanced analysis with AI insights
"""
if not self.client:
return self._fallback_analysis(text_analysis)
try:
prompt = self._create_analysis_prompt(text_analysis)
response = self.client.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
temperature=Config.GEMINI_TEMPERATURE,
max_output_tokens=Config.GEMINI_MAX_TOKENS,
)
)
ai_analysis = self._parse_gemini_response(response.text)
# Combine with original analysis
enhanced_analysis = {
**text_analysis,
'ai_insights': ai_analysis,
'suggested_template': ai_analysis.get('template', 'Modern'),
'visual_elements': ai_analysis.get('visual_elements', []),
'content_structure': ai_analysis.get('structure', {}),
'design_recommendations': ai_analysis.get('design', {})
}
return enhanced_analysis
except Exception as e:
logger.error(f"Gemini analysis failed: {e}")
return self._fallback_analysis(text_analysis)
def generate_title(self, text: str) -> str:
"""Generate an engaging title for the content"""
if not self.client:
return self._extract_title_fallback(text)
try:
prompt = f"""
Generate a compelling, concise title for this content that would work well in an infographic:
Content: {text[:500]}...
Requirements:
- Maximum 8 words
- Engaging and clear
- Suitable for visual presentation
- Return only the title, nothing else
"""
response = self.client.generate_content(prompt)
title = response.text.strip().strip('"').strip("'")
return title if len(title.split()) <= 8 else self._extract_title_fallback(text)
except Exception as e:
logger.error(f"Title generation failed: {e}")
return self._extract_title_fallback(text)
def suggest_visual_elements(self, content: Dict) -> List[Dict]:
"""Suggest visual elements for the infographic"""
if not self.client:
return self._fallback_visual_elements(content)
try:
prompt = f"""
Based on this content analysis, suggest 5-8 visual elements for an infographic:
Keywords: {', '.join(content.get('keywords', [])[:10])}
Key Points: {content.get('key_points', [])[:3]}
Content Type: {content.get('sentiment', 'neutral')}
Has Data: {content.get('data_elements', {}).get('has_data', False)}
For each visual element, provide:
- type (icon, chart, illustration, diagram)
- description
- placement_suggestion (header, body, footer)
- importance (1-10)
Return as JSON array with these fields.
"""
response = self.client.generate_content(prompt)
visual_elements = self._parse_json_response(response.text)
return visual_elements if isinstance(visual_elements, list) else self._fallback_visual_elements(content)
except Exception as e:
logger.error(f"Visual element suggestion failed: {e}")
return self._fallback_visual_elements(content)
def optimize_content_for_visual(self, sections: List[Dict]) -> List[Dict]:
"""Optimize content sections for visual presentation"""
if not self.client:
return self._fallback_optimize_content(sections)
try:
content_summary = "\n".join([f"Section {s['id']}: {s['content'][:100]}..." for s in sections[:5]])
prompt = f"""
Optimize these content sections for infographic presentation:
{content_summary}
For each section, provide:
- condensed_text (max 15 words)
- visual_treatment (bullet, number, highlight, quote)
- priority (1-10)
- color_suggestion (primary, secondary, accent)
Return as JSON array maintaining the same section IDs.
"""
response = self.client.generate_content(prompt)
optimized = self._parse_json_response(response.text)
# Merge with original sections
if isinstance(optimized, list):
for i, section in enumerate(sections):
if i < len(optimized):
section.update(optimized[i])
return sections
except Exception as e:
logger.error(f"Content optimization failed: {e}")
return self._fallback_optimize_content(sections)
def _create_analysis_prompt(self, analysis: Dict) -> str:
"""Create comprehensive analysis prompt for Gemini"""
return f"""
Analyze this content for infographic design and provide recommendations:
Content Statistics:
- Word count: {analysis['statistics']['word_count']}
- Sentiment: {analysis['sentiment']}
- Key points: {len(analysis['key_points'])}
- Has data elements: {analysis['data_elements']['has_data']}
Content Structure:
- Paragraphs: {analysis['structure']['paragraph_count']}
- Has headers: {analysis['structure']['has_headers']}
- Has lists: {analysis['structure']['has_lists']}
- Has numbers: {analysis['structure']['has_numbers']}
Top Keywords: {', '.join(analysis['keywords'][:8])}
Based on this analysis, provide:
1. Best template style (Modern, Corporate, Creative, Minimalist, Academic)
2. Recommended layout (Vertical, Horizontal, Grid, Flow)
3. Color scheme suggestions (primary mood)
4. Visual element recommendations
5. Content hierarchy suggestions
Return response as JSON with these keys:
- template
- layout
- color_mood
- visual_elements (array)
- structure (object)
- design (object)
"""
def _parse_gemini_response(self, response_text: str) -> Dict:
"""Parse Gemini response into structured data"""
try:
# Try to extract JSON from response
json_match = response_text.find('{')
if json_match != -1:
json_end = response_text.rfind('}') + 1
json_str = response_text[json_match:json_end]
return json.loads(json_str)
except:
pass
# Fallback parsing
return {
'template': self._extract_template_from_text(response_text),
'layout': self._extract_layout_from_text(response_text),
'color_mood': self._extract_color_mood_from_text(response_text),
'visual_elements': [],
'structure': {'hierarchy': 'standard'},
'design': {'emphasis': 'balanced'}
}
def _parse_json_response(self, response_text: str) -> Any:
"""Parse JSON from Gemini response"""
try:
json_match = response_text.find('[')
if json_match != -1:
json_end = response_text.rfind(']') + 1
json_str = response_text[json_match:json_end]
return json.loads(json_str)
json_match = response_text.find('{')
if json_match != -1:
json_end = response_text.rfind('}') + 1
json_str = response_text[json_match:json_end]
return json.loads(json_str)
except:
pass
return []
def _extract_template_from_text(self, text: str) -> str:
"""Extract template suggestion from text"""
templates = ['Modern', 'Corporate', 'Creative', 'Minimalist', 'Academic']
for template in templates:
if template.lower() in text.lower():
return template
return 'Modern'
def _extract_layout_from_text(self, text: str) -> str:
"""Extract layout suggestion from text"""
layouts = ['Vertical', 'Horizontal', 'Grid', 'Flow']
for layout in layouts:
if layout.lower() in text.lower():
return layout
return 'Vertical'
def _extract_color_mood_from_text(self, text: str) -> str:
"""Extract color mood from text"""
if any(word in text.lower() for word in ['professional', 'business', 'corporate']):
return 'professional'
elif any(word in text.lower() for word in ['creative', 'vibrant', 'colorful']):
return 'creative'
elif any(word in text.lower() for word in ['minimal', 'clean', 'simple']):
return 'minimal'
else:
return 'balanced'
def _extract_title_fallback(self, text: str) -> str:
"""Fallback title extraction"""
first_line = text.split('\n')[0].strip()
if len(first_line.split()) <= 8:
return first_line
# Extract first sentence
first_sentence = text.split('.')[0].strip()
if len(first_sentence.split()) <= 8:
return first_sentence
# Create title from keywords
words = text.split()[:8]
return ' '.join(words).title()
def _fallback_analysis(self, text_analysis: Dict) -> Dict:
"""Fallback analysis when Gemini is not available"""
structure = text_analysis.get('structure', {})
sentiment = text_analysis.get('sentiment', 'neutral')
# Rule-based template suggestion
template = 'Modern'
if sentiment == 'positive' and structure.get('has_data'):
template = 'Corporate'
elif len(text_analysis.get('keywords', [])) > 10:
template = 'Creative'
elif structure.get('paragraph_count', 0) > 5:
template = 'Academic'
return {
**text_analysis,
'ai_insights': {
'template': template,
'layout': structure.get('suggested_layout', 'Vertical'),
'color_mood': 'balanced'
},
'suggested_template': template,
'visual_elements': self._fallback_visual_elements(text_analysis),
'content_structure': {'hierarchy': 'standard'},
'design_recommendations': {'emphasis': 'balanced'}
}
def _fallback_visual_elements(self, content: Dict) -> List[Dict]:
"""Fallback visual elements"""
elements = []
if content.get('data_elements', {}).get('has_data'):
elements.append({
'type': 'chart',
'description': 'Data visualization',
'placement': 'body',
'importance': 8
})
keywords = content.get('keywords', [])[:3]
for i, keyword in enumerate(keywords):
elements.append({
'type': 'icon',
'description': f'Icon for {keyword}',
'placement': 'body',
'importance': 6 - i
})
return elements
def _fallback_optimize_content(self, sections: List[Dict]) -> List[Dict]:
"""Fallback content optimization"""
for section in sections:
content = section.get('content', '')
words = content.split()[:15]
section['condensed_text'] = ' '.join(words)
section['visual_treatment'] = 'bullet' if len(words) < 10 else 'highlight'
section['priority'] = section.get('priority', 5)
section['color_suggestion'] = 'primary'
return sections |