import json import re import io import os from datetime import datetime from openai import OpenAI from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import inch from reportlab.lib.enums import TA_CENTER client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # validate_startup_idea def validate_startup_idea(idea, category=None, subcategory=None): category_text = f"Category: {category}" if category else "" subcategory_text = f"Subcategory: {subcategory}" if subcategory else "" prompt = f""" You are a business analysis assistant. Analyze the following startup idea: "{idea}" {category_text} {subcategory_text} Return your analysis strictly in the following JSON format without extra text: {{ "swot": {{ "strengths": ["bullet point", "bullet point", "bullet point", "bullet point", "bullet point"], "weaknesses": ["bullet point", "bullet point", "bullet point", "bullet point", "bullet point"], "opportunities": ["bullet point", "bullet point", "bullet point", "bullet point", "bullet point"], "threats": ["bullet point", "bullet point", "bullet point", "bullet point", "bullet point"] }}, "audience": [ "bullet point audience segment", "bullet point audience segment", "bullet point audience segment", "bullet point audience segment", "bullet point audience segment" ], "competitors": [ {{"name": "Exact Competitor Name", "description": "Short description of why they're a competitor", "link": "https://competitor-website.com"}}, {{"name": "Exact Competitor Name", "description": "Short description", "link": "https://competitor-website.com"}}, {{"name": "Exact Competitor Name", "description": "Short description", "link": "https://competitor-website.com"}} ], "tools_and_tips": [ {{"name": "Tool or Platform Name", "description": "What it helps with", "link": "https://link-to-tool.com"}}, {{"name": "Tool or Platform Name", "description": "What it helps with", "link": "https://link-to-tool.com"}}, {{"name": "Tool or Platform Name", "description": "What it helps with", "link": "https://link-to-tool.com"}} ], "tools_and_resources": [ {{"name": "Resource Name", "description": "Why it’s useful", "link": "https://link-to-resource.com"}}, {{"name": "Resource Name", "description": "Why it’s useful", "link": "https://link-to-resource.com"}}, {{"name": "Resource Name", "description": "Why it’s useful", "link": "https://link-to-resource.com"}}, {{"name": "Resource Name", "description": "Why it’s useful", "link": "https://link-to-resource.com"}}, {{"name": "Resource Name", "description": "Why it’s useful", "link": "https://link-to-resource.com"}} ] }} Rules: - Always include **at least 5 bullet points** for "audience" and "tools_and_resources". - All tools, platforms, competitors, and resources must include clickable **links**. - Be very specific and practical. Avoid generic placeholders like "Competitor A" or "use social media". - In "tools_and_tips", name the platform (e.g., "Instagram") and the tool (e.g., "Buffer") and explain why. """ response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a business strategy expert."}, {"role": "user", "content": prompt} ], temperature=0.7 ) content = response.choices[0].message.content try: data = json.loads(content) except json.JSONDecodeError: raise ValueError(f"Invalid JSON format returned from AI: {content}") swot = data.get("swot", {}) swot_scores = {} for key in ["strengths", "weaknesses", "opportunities", "threats"]: items = swot.get(key, []) swot_scores[key] = len(items) data["swot_scores"] = swot_scores return data # Removed generate_brand_name function completely # generate_pdf (Removed brand_names section) def generate_pdf(data, user_name=None, logo_path=None): buffer = io.BytesIO() doc = SimpleDocTemplate(buffer, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18) styles = getSampleStyleSheet() story = [] title_style = ParagraphStyle( name='TitleStyle', fontSize=18, leading=22, alignment=TA_CENTER, spaceAfter=14 ) if logo_path and os.path.exists(logo_path): img = Image(logo_path, width=2*inch, height=2*inch) img.hAlign = 'CENTER' story.append(img) story.append(Spacer(1, 12)) story.append(Paragraph("Startup Idea Analysis Report", title_style)) story.append(Spacer(1, 12)) current_date = datetime.now().strftime("%B %d, %Y %H:%M") user_line = f"Prepared for: {user_name}" if user_name else "Prepared for: Anonymous User" story.append(Paragraph(user_line, styles['Normal'])) story.append(Paragraph(f"Date: {current_date}", styles['Normal'])) story.append(Spacer(1, 12)) # Removed Brand Names Section # Loop through other sections for category, content in data.items(): if category == "swot_scores": continue story.append(Paragraph(f"{category.upper()}", styles['Heading2'])) story.append(Spacer(1, 0.1 * inch)) if isinstance(content, list): for item in content: if isinstance(item, dict): text = f"• {item['name']}: {item['description']} ({item['link']})" else: text = f"• {item}" story.append(Paragraph(text, styles['Normal'])) elif isinstance(content, dict): for subcategory, bullets in content.items(): story.append(Paragraph(f"{subcategory.capitalize()}", styles['Heading3'])) for bullet in bullets: story.append(Paragraph(f"• {bullet}", styles['Normal'])) story.append(Spacer(1, 0.2 * inch)) doc.build(story) buffer.seek(0) return buffer.getvalue()