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+ from datetime import datetime, timedelta
6
+ from typing import List, Dict, Any, Optional
7
+ import warnings
8
+ warnings.filterwarnings('ignore')
9
+
10
+ # Core imports
11
+ import gradio as gr
12
+ import chromadb
13
+ from sentence_transformers import SentenceTransformer
14
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
15
+ import joblib
16
+ from sklearn.feature_extraction.text import TfidfVectorizer
17
+ from sklearn.metrics.pairwise import cosine_similarity
18
+ import plotly.graph_objects as go
19
+ import plotly.express as px
20
+ from plotly.subplots import make_subplots
21
+ import re
22
+ import uuid
23
+ from dataclasses import dataclass
24
+ from pathlib import Path
25
+
26
+ # Set up paths for different deployment environments
27
+ CHROMA_DB_PATH = os.getenv("CHROMA_DB_PATH", "./chroma_db")
28
+ PERSIST_DIR = os.getenv("PERSIST_DIR", CHROMA_DB_PATH)
29
+ Path(PERSIST_DIR).mkdir(parents=True, exist_ok=True)
30
+
31
+ print(f"πŸ—„οΈ ChromaDB will be stored at: {PERSIST_DIR}")
32
+
33
+ # Data Models
34
+ @dataclass
35
+ class StartupIdea:
36
+ """Data model for startup ideas"""
37
+ name: str
38
+ description: str
39
+ industry: str
40
+ target_market: str
41
+ problem_statement: str
42
+ solution: str
43
+ unique_value_proposition: str
44
+
45
+ @dataclass
46
+ class BusinessPlan:
47
+ """Comprehensive business plan structure"""
48
+ executive_summary: str
49
+ company_description: str
50
+ market_analysis: str
51
+ organization_management: str
52
+ service_product_line: str
53
+ marketing_sales: str
54
+ funding_request: str
55
+ financial_projections: Dict[str, Any]
56
+
57
+ @dataclass
58
+ class Flashcard:
59
+ """Learning flashcard structure"""
60
+ id: str
61
+ front: str
62
+ back: str
63
+ category: str
64
+ difficulty: str
65
+ tags: List[str]
66
+
67
+ @dataclass
68
+ class RoadmapMilestone:
69
+ """Roadmap milestone structure"""
70
+ id: str
71
+ title: str
72
+ description: str
73
+ timeline: str
74
+ dependencies: List[str]
75
+ priority: str
76
+ category: str
77
+
78
+ # RAG System Implementation
79
+ class RAGSystem:
80
+ """Retrieval-Augmented Generation system using ChromaDB"""
81
+
82
+ def __init__(self, model_name: str = "all-MiniLM-L6-v2"):
83
+ print("🧠 Initializing RAG System...")
84
+ try:
85
+ self.embedding_model = SentenceTransformer(model_name)
86
+ self.chroma_client = chromadb.PersistentClient(path=PERSIST_DIR)
87
+ self.collection_name = "startup_knowledge"
88
+ self.collection = None
89
+ self.text_splitter = RecursiveCharacterTextSplitter(
90
+ chunk_size=1000,
91
+ chunk_overlap=200,
92
+ length_function=len
93
+ )
94
+ print("βœ… RAG System initialized successfully")
95
+ except Exception as e:
96
+ print(f"❌ Error initializing RAG System: {e}")
97
+ raise
98
+
99
+ def initialize_collection(self):
100
+ """Initialize or get existing collection"""
101
+ try:
102
+ collections = self.chroma_client.list_collections()
103
+ existing_collection = next((c for c in collections if c.name == self.collection_name), None)
104
+
105
+ if existing_collection:
106
+ self.collection = self.chroma_client.get_collection(self.collection_name)
107
+ print(f"πŸ“š Found existing collection with {self.collection.count()} documents")
108
+ else:
109
+ self.collection = self.chroma_client.create_collection(
110
+ name=self.collection_name,
111
+ metadata={"description": "Startup knowledge base"}
112
+ )
113
+ print("πŸ“š Created new collection")
114
+
115
+ return self.collection
116
+
117
+ except Exception as e:
118
+ print(f"❌ Error initializing collection: {e}")
119
+ try:
120
+ self.collection = self.chroma_client.create_collection(
121
+ name=f"{self.collection_name}_{uuid.uuid4().hex[:8]}",
122
+ metadata={"description": "Startup knowledge base"}
123
+ )
124
+ print("πŸ“š Created fallback collection")
125
+ return self.collection
126
+ except Exception as e2:
127
+ print(f"❌ Fatal error creating collection: {e2}")
128
+ raise
129
+
130
+ def add_documents(self, documents: List[str], metadatas: List[Dict] = None):
131
+ """Add documents to the knowledge base"""
132
+ if not self.collection:
133
+ self.initialize_collection()
134
+
135
+ try:
136
+ all_chunks = []
137
+ all_metadatas = []
138
+
139
+ for i, doc in enumerate(documents):
140
+ chunks = self.text_splitter.split_text(doc)
141
+ all_chunks.extend(chunks)
142
+
143
+ doc_metadata = metadatas[i] if metadatas and i < len(metadatas) else {}
144
+ for chunk in chunks:
145
+ all_metadatas.append({
146
+ **doc_metadata,
147
+ "chunk_id": str(uuid.uuid4()),
148
+ "timestamp": datetime.now().isoformat()
149
+ })
150
+
151
+ print(f"πŸ”„ Generating embeddings for {len(all_chunks)} chunks...")
152
+ embeddings = self.embedding_model.encode(all_chunks).tolist()
153
+
154
+ ids = [str(uuid.uuid4()) for _ in all_chunks]
155
+ self.collection.add(
156
+ documents=all_chunks,
157
+ embeddings=embeddings,
158
+ metadatas=all_metadatas,
159
+ ids=ids
160
+ )
161
+
162
+ print(f"βœ… Added {len(all_chunks)} chunks to knowledge base")
163
+ return len(all_chunks)
164
+
165
+ except Exception as e:
166
+ print(f"❌ Error adding documents: {e}")
167
+ return 0
168
+
169
+ def retrieve_context(self, query: str, n_results: int = 5) -> List[Dict]:
170
+ """Retrieve relevant context for a query"""
171
+ if not self.collection:
172
+ self.initialize_collection()
173
+
174
+ try:
175
+ query_embedding = self.embedding_model.encode([query]).tolist()[0]
176
+
177
+ results = self.collection.query(
178
+ query_embeddings=[query_embedding],
179
+ n_results=min(n_results, max(1, self.collection.count()))
180
+ )
181
+
182
+ contexts = []
183
+ if results['documents'][0]:
184
+ for i, doc in enumerate(results['documents'][0]):
185
+ contexts.append({
186
+ "content": doc,
187
+ "metadata": results['metadatas'][0][i] if results['metadatas'][0] else {},
188
+ "distance": results['distances'][0][i] if results['distances'] and results['distances'][0] else 0
189
+ })
190
+
191
+ return contexts
192
+
193
+ except Exception as e:
194
+ print(f"❌ Error retrieving context: {e}")
195
+ return []
196
+
197
+ def generate_response(self, query: str, context: List[Dict]) -> str:
198
+ """Generate response using retrieved context"""
199
+ try:
200
+ context_text = "\n".join([ctx["content"] for ctx in context])
201
+ return self._generate_structured_response(query, context_text)
202
+ except Exception as e:
203
+ print(f"❌ Error generating response: {e}")
204
+ return "I apologize, but I encountered an error generating a response. Please try again."
205
+
206
+ def _generate_structured_response(self, query: str, context: str) -> str:
207
+ """Generate structured response based on query type"""
208
+ query_lower = query.lower()
209
+
210
+ if "market research" in query_lower or "competition" in query_lower:
211
+ return f"""**Market Analysis Framework:**
212
+
213
+ Based on startup best practices:
214
+ {context[:500]}...
215
+
216
+ **Key Recommendations:**
217
+ - Conduct thorough competitive analysis using TAM/SAM/SOM framework
218
+ - Identify unique value propositions that differentiate your solution
219
+ - Analyze market size and growth potential with real data
220
+ """
221
+
222
+ elif "business plan" in query_lower:
223
+ return f"""**Business Plan Development:**
224
+
225
+ Essential components:
226
+ {context[:500]}...
227
+
228
+ **Core Sections:**
229
+ - Executive Summary
230
+ - Market Analysis
231
+ - Financial Projections
232
+ """
233
+
234
+ else:
235
+ return f"""**Strategic Guidance:**
236
+
237
+ {context[:400]}...
238
+
239
+ **Key Considerations:**
240
+ - Validate assumptions with market data
241
+ - Focus on solving genuine problems
242
+ - Build strong unit economics
243
+ """
244
+
245
+ # Startup Knowledge Base
246
+ STARTUP_KNOWLEDGE_BASE = [
247
+ """
248
+ IDEA VALIDATION AND MARKET RESEARCH
249
+
250
+ Before building any product, entrepreneurs must prove market demand exists. This involves:
251
+
252
+ 1. Problem Identification: Identify genuine problems that specific customer segments face daily.
253
+
254
+ 2. Market Analysis: Determine total addressable market (TAM) size. Use TAM/SAM/SOM framework:
255
+ - TAM (Total Addressable Market): Total market demand
256
+ - SAM (Serviceable Addressable Market): Portion targeted by your products
257
+ - SOM (Serviceable Obtainable Market): Portion you can realistically capture
258
+
259
+ 3. Competitive Research: Analyze competitors' offerings, pricing, strengths, weaknesses.
260
+
261
+ 4. Customer Validation: Conduct 50+ interviews to validate core assumptions.
262
+
263
+ Key metrics: CAC, LTV, market penetration rate, NPS.
264
+ """,
265
+
266
+ """
267
+ BUSINESS PLAN DEVELOPMENT
268
+
269
+ A comprehensive business plan serves as roadmap and investor communication tool:
270
+
271
+ 1. Executive Summary: Mission, product, market, competitive advantages, funding needs.
272
+
273
+ 2. Company Description: Business model, legal structure, value proposition.
274
+
275
+ 3. Market Analysis: Industry overview, target market, competitive landscape.
276
+
277
+ 4. Organization & Management: Team, advisors, hiring plans.
278
+
279
+ 5. Financial Projections: 3-5 year forecasts, cash flow, break-even analysis.
280
+
281
+ 6. Funding Requirements: Capital needs, use of funds, expected ROI.
282
+ """,
283
+
284
+ """
285
+ FUNDING AND FINANCIAL MANAGEMENT
286
+
287
+ Securing and managing finances strategically:
288
+
289
+ 1. Funding Sources:
290
+ - Bootstrapping: Personal savings
291
+ - Angels: $25K-$100K early stage
292
+ - VC: $1M+ for high-growth potential
293
+ - Crowdfunding: Product launches
294
+ - Grants: SBIR/STTR programs
295
+
296
+ 2. Key Metrics:
297
+ - Burn Rate: Monthly cash expenditure
298
+ - Runway: Months of cash remaining
299
+ - CAC: Total marketing cost Γ· new customers
300
+ - LTV: Average revenue Γ— margin Γ— lifespan
301
+ - LTV/CAC should be 3:1 or higher
302
+
303
+ 3. Best Practices:
304
+ - Separate business and personal finances
305
+ - Maintain 6-12 months cash reserves
306
+ - Track MRR, ARR, gross margins
307
+ """,
308
+
309
+ """
310
+ LEGAL STRUCTURE AND COMPLIANCE
311
+
312
+ Choosing the right legal foundation:
313
+
314
+ 1. Business Structures:
315
+ - Sole Proprietorship: Simple, personal liability
316
+ - LLC: Asset protection, flexible taxes
317
+ - C-Corp: Best for VC funding, stock issuance
318
+ - Delaware C-Corp advantages for startups
319
+
320
+ 2. Essential Documents:
321
+ - Articles of Incorporation
322
+ - Founders' Agreement with vesting
323
+ - Employee NDAs and IP assignments
324
+ - Terms of service, privacy policy
325
+
326
+ 3. IP Protection:
327
+ - Trademarks: Brand names, logos
328
+ - Patents: Unique inventions
329
+ - Copyrights: Creative works
330
+ - Trade Secrets: Proprietary processes
331
+
332
+ 4. Compliance:
333
+ - Federal EIN
334
+ - Business licenses
335
+ - Industry permits
336
+ - Data privacy (GDPR, CCPA)
337
+ """,
338
+
339
+ """
340
+ TEAM BUILDING AND OPERATIONS
341
+
342
+ Building the right team:
343
+
344
+ 1. Founding Team:
345
+ - Complementary skills: technical, business, domain
346
+ - Aligned vision and values
347
+ - Equity split reflects contribution
348
+
349
+ 2. Hiring Strategy:
350
+ - Hire for adaptability and culture fit
351
+ - Focus on critical roles first
352
+ - 4-year vesting with 1-year cliff
353
+ - 0.1%-10% equity based on role
354
+
355
+ 3. Key Metrics by Function:
356
+ - Product: DAU/MAU, retention, feature adoption
357
+ - Sales: Conversion rates, cycle length, ACV
358
+ - Marketing: CPL, MQLs, brand awareness
359
+ - Finance: Revenue growth, margins, working capital
360
+
361
+ 4. Operational Excellence:
362
+ - Build MVP with core features
363
+ - Define and track KPIs
364
+ - Create scalable processes
365
+ - Implement feedback loops
366
+ """,
367
+
368
+ """
369
+ BRAND DEVELOPMENT AND MARKETING
370
+
371
+ Strong branding drives growth:
372
+
373
+ 1. Brand Identity:
374
+ - Define values, personality, voice
375
+ - Create unique selling proposition
376
+ - Develop consistent visual identity
377
+ - Establish market positioning
378
+
379
+ 2. Digital Presence:
380
+ - Professional website with clear CTA
381
+ - SEO optimization
382
+ - Social media strategy
383
+ - Content marketing calendar
384
+
385
+ 3. Acquisition Channels:
386
+ - Content Marketing: Thought leadership
387
+ - Social Media: Platform-specific strategies
388
+ - SEM: Targeted Google Ads
389
+ - Email: Drip campaigns, newsletters
390
+ - Partnerships: Co-marketing, affiliates
391
+
392
+ 4. Key Metrics:
393
+ - Website traffic and conversion rates
394
+ - CAC by channel
395
+ - LTV by acquisition source
396
+ - NPS and customer satisfaction
397
+ - Brand awareness and consideration
398
+
399
+ 5. Customer Success:
400
+ - Feedback loops and surveys
401
+ - Customer success programs
402
+ - Community building
403
+ - Referral programs
404
+ """
405
+ ]
406
+
407
+ KNOWLEDGE_METADATA = [
408
+ {"category": "validation", "topic": "market_research", "importance": "high"},
409
+ {"category": "planning", "topic": "business_plan", "importance": "high"},
410
+ {"category": "finance", "topic": "funding", "importance": "high"},
411
+ {"category": "legal", "topic": "structure", "importance": "medium"},
412
+ {"category": "operations", "topic": "team_building", "importance": "high"},
413
+ {"category": "marketing", "topic": "branding", "importance": "medium"},
414
+ ]
415
+
416
+ # Business Plan Generator
417
+ class StartupBlueprintGenerator:
418
+ """Main class for generating comprehensive startup blueprints"""
419
+
420
+ def __init__(self, rag_system: RAGSystem):
421
+ self.rag = rag_system
422
+ self.templates = self._load_templates()
423
+ self.model_data = {}
424
+
425
+ def _load_templates(self) -> Dict[str, str]:
426
+ """Load template structures"""
427
+ return {
428
+ "executive_summary": """## Executive Summary
429
+
430
+ **Company:** {company_name}
431
+ **Industry:** {industry}
432
+ **Mission:** {mission}
433
+
434
+ ### Problem Statement
435
+ {problem_statement}
436
+
437
+ ### Solution
438
+ {solution}
439
+
440
+ ### Market Opportunity
441
+ {market_opportunity}
442
+
443
+ ### Financial Highlights
444
+ - Projected Revenue Year 3: {projected_revenue}
445
+ - Funding Requirement: {funding_needed}
446
+ - Break-even Timeline: {breakeven_timeline}
447
+ """,
448
+ }
449
+
450
+ def generate_business_plan(self, startup_idea: StartupIdea) -> BusinessPlan:
451
+ """Generate comprehensive business plan"""
452
+ try:
453
+ print(f"πŸ“Š Generating business plan for {startup_idea.name}...")
454
+
455
+ exec_context = self.rag.retrieve_context(
456
+ f"executive summary for {startup_idea.industry} startup",
457
+ n_results=3
458
+ )
459
+
460
+ executive_summary = self.templates["executive_summary"].format(
461
+ company_name=startup_idea.name,
462
+ industry=startup_idea.industry,
463
+ mission=f"To solve {startup_idea.problem_statement} for {startup_idea.target_market}",
464
+ problem_statement=startup_idea.problem_statement,
465
+ solution=startup_idea.solution,
466
+ market_opportunity=f"Significant opportunity in the {startup_idea.industry} market",
467
+ projected_revenue="$1M - $5M",
468
+ funding_needed="$250K - $1M",
469
+ breakeven_timeline="18-24 months"
470
+ )
471
+
472
+ business_plan = BusinessPlan(
473
+ executive_summary=executive_summary,
474
+ company_description=f"{startup_idea.name} - {startup_idea.description}",
475
+ market_analysis=f"The {startup_idea.industry} market presents significant opportunities.",
476
+ organization_management="Founding team with complementary skills.",
477
+ service_product_line=startup_idea.solution,
478
+ marketing_sales="Multi-channel customer acquisition strategy.",
479
+ funding_request="Seeking seed funding for product development and market expansion.",
480
+ financial_projections={
481
+ "revenue_forecast": {"year_1": 100000, "year_2": 500000, "year_3": 1500000},
482
+ "key_metrics": {"cac": 50, "ltv": 500}
483
+ }
484
+ )
485
+
486
+ print("βœ… Business plan generated")
487
+ return business_plan
488
+
489
+ except Exception as e:
490
+ print(f"❌ Error: {e}")
491
+ return self._generate_fallback_plan(startup_idea)
492
+
493
+ def _generate_fallback_plan(self, idea: StartupIdea) -> BusinessPlan:
494
+ """Fallback business plan"""
495
+ return BusinessPlan(
496
+ executive_summary=f"**{idea.name}** addresses {idea.problem_statement}.",
497
+ company_description=f"{idea.name} serves {idea.target_market}.",
498
+ market_analysis=f"The {idea.industry} market has growth opportunities.",
499
+ organization_management="Experienced founding team.",
500
+ service_product_line=idea.solution,
501
+ marketing_sales="Digital marketing focused strategy.",
502
+ funding_request="Seeking seed funding.",
503
+ financial_projections={"revenue_forecast": {"year_1": 100000}}
504
+ )
505
+
506
+ # Flashcards Generator
507
+ class FlashcardsGenerator:
508
+ """Generate learning flashcards"""
509
+
510
+ def __init__(self, rag_system: RAGSystem):
511
+ self.rag = rag_system
512
+ self.flashcards_db = []
513
+
514
+ def generate_flashcards(self, topic: str, count: int = 10) -> List[Flashcard]:
515
+ """Generate flashcards for topic"""
516
+ try:
517
+ print(f"πŸ“š Generating {count} flashcards for {topic}...")
518
+
519
+ qa_patterns = self._get_qa_patterns(topic)
520
+ flashcards = []
521
+
522
+ for pattern in qa_patterns[:count]:
523
+ flashcard = Flashcard(
524
+ id=str(uuid.uuid4()),
525
+ front=pattern['question'],
526
+ back=pattern['answer'],
527
+ category=topic,
528
+ difficulty=pattern['difficulty'],
529
+ tags=pattern['tags']
530
+ )
531
+ flashcards.append(flashcard)
532
+
533
+ self.flashcards_db.extend(flashcards)
534
+ print(f"βœ… Generated {len(flashcards)} flashcards")
535
+ return flashcards
536
+
537
+ except Exception as e:
538
+ print(f"❌ Error: {e}")
539
+ return []
540
+
541
+ def _get_qa_patterns(self, topic: str) -> List[Dict]:
542
+ """Get Q&A patterns"""
543
+ patterns = {
544
+ 'SaaS': [
545
+ {
546
+ 'question': 'What are key SaaS metrics?',
547
+ 'answer': 'MRR, ARR, CAC, LTV, Churn Rate, NPS',
548
+ 'difficulty': 'Medium',
549
+ 'tags': ['saas', 'metrics']
550
+ },
551
+ ],
552
+ 'Market Research': [
553
+ {
554
+ 'question': 'What is TAM/SAM/SOM?',
555
+ 'answer': 'Total Addressable Market, Serviceable Addressable Market, Serviceable Obtainable Market',
556
+ 'difficulty': 'Medium',
557
+ 'tags': ['market-sizing']
558
+ },
559
+ ]
560
+ }
561
+
562
+ default = [
563
+ {
564
+ 'question': 'What are startup funding stages?',
565
+ 'answer': 'Pre-seed, Seed, Series A, Series B, Series C+',
566
+ 'difficulty': 'Easy',
567
+ 'tags': ['funding']
568
+ }
569
+ ]
570
+
571
+ return patterns.get(topic, default) + default[:5]
572
+
573
+ # Roadmap Generator
574
+ class RoadmapGenerator:
575
+ """Generate startup roadmaps"""
576
+
577
+ def __init__(self, rag_system: RAGSystem):
578
+ self.rag = rag_system
579
+ self.roadmap_templates = self._load_templates()
580
+
581
+ def _load_templates(self) -> Dict[str, List[Dict]]:
582
+ """Load roadmap templates"""
583
+ return {
584
+ 'pre_launch': [
585
+ {
586
+ 'phase': 'Ideation & Validation',
587
+ 'duration': '1-2 months',
588
+ 'milestones': [
589
+ 'Problem identification',
590
+ 'Market research',
591
+ 'Customer interviews',
592
+ 'MVP wireframes'
593
+ ]
594
+ },
595
+ ],
596
+ 'launch': [
597
+ {
598
+ 'phase': 'Go-to-Market',
599
+ 'duration': '2-3 months',
600
+ 'milestones': [
601
+ 'Brand development',
602
+ 'Marketing strategy',
603
+ 'Sales process',
604
+ 'Launch campaign'
605
+ ]
606
+ },
607
+ ],
608
+ }
609
+
610
+ def generate_roadmap(self, startup_idea: StartupIdea, phases: List[str] = None) -> Dict[str, Any]:
611
+ """Generate roadmap"""
612
+ try:
613
+ print(f"πŸ—ΊοΈ Generating roadmap for {startup_idea.name}...")
614
+
615
+ if phases is None:
616
+ phases = ['pre_launch', 'launch']
617
+
618
+ roadmap = {
619
+ 'startup_name': startup_idea.name,
620
+ 'industry': startup_idea.industry,
621
+ 'phases': [],
622
+ 'total_timeline': '6-12 months',
623
+ 'key_metrics': ['CAC', 'LTV', 'MRR']
624
+ }
625
+
626
+ current_month = 0
627
+ for phase_name in phases:
628
+ if phase_name in self.roadmap_templates:
629
+ for phase in self.roadmap_templates[phase_name]:
630
+ phase['start_month'] = current_month
631
+ phase['end_month'] = current_month + 2
632
+ current_month = phase['end_month']
633
+ roadmap['phases'].append(phase)
634
+
635
+ print("βœ… Roadmap generated")
636
+ return roadmap
637
+
638
+ except Exception as e:
639
+ print(f"❌ Error: {e}")
640
+ return self._generate_fallback(startup_idea)
641
+
642
+ def _generate_fallback(self, idea: StartupIdea) -> Dict[str, Any]:
643
+ """Fallback roadmap"""
644
+ return {
645
+ 'startup_name': idea.name,
646
+ 'industry': idea.industry,
647
+ 'phases': [
648
+ {
649
+ 'phase': 'Planning',
650
+ 'duration': '1-2 months',
651
+ 'milestones': ['Research', 'Planning'],
652
+ 'start_month': 0,
653
+ 'end_month': 2
654
+ }
655
+ ],
656
+ 'total_timeline': '3-6 months',
657
+ 'key_metrics': ['Revenue', 'Customers']
658
+ }
659
+
660
+ def export_roadmap(self, roadmap_data: Dict[str, Any], format: str = 'markdown') -> str:
661
+ """Export roadmap"""
662
+ if format == 'markdown':
663
+ md = f"# πŸ—ΊοΈ {roadmap_data['startup_name']} Roadmap\n\n"
664
+ md += f"**Industry:** {roadmap_data['industry']}\n\n"
665
+
666
+ for phase in roadmap_data['phases']:
667
+ md += f"## {phase['phase']}\n"
668
+ md += f"**Duration:** {phase['duration']}\n\n"
669
+ md += "**Milestones:**\n"
670
+ for milestone in phase['milestones']:
671
+ md += f"- {milestone}\n"
672
+ md += "\n"
673
+
674
+ return md
675
+ return json.dumps(roadmap_data, indent=2)
676
+
677
+ # Initialize Global Systems
678
+ print("πŸš€ Starting AI Startup Roadmap Generator...")
679
+
680
+ try:
681
+ rag_system = RAGSystem()
682
+ rag_system.initialize_collection()
683
+
684
+ if rag_system.collection and rag_system.collection.count() == 0:
685
+ print("πŸ“š Populating knowledge base...")
686
+ chunks_added = rag_system.add_documents(STARTUP_KNOWLEDGE_BASE, KNOWLEDGE_METADATA)
687
+ print(f"βœ… Added {chunks_added} chunks")
688
+ else:
689
+ count = rag_system.collection.count() if rag_system.collection else 0
690
+ print(f"πŸ“š Using existing knowledge base with {count} documents")
691
+
692
+ except Exception as e:
693
+ print(f"❌ Error initializing RAG: {e}")
694
+ rag_system = None
695
+
696
+ try:
697
+ blueprint_generator = StartupBlueprintGenerator(rag_system) if rag_system else None
698
+ flashcards_gen = FlashcardsGenerator(rag_system) if rag_system else None
699
+ roadmap_gen = RoadmapGenerator(rag_system) if rag_system else None
700
+ print("βœ… Generators initialized")
701
+ except Exception as e:
702
+ print(f"❌ Error initializing generators: {e}")
703
+
704
+ # Gradio Interface Functions
705
+ def generate_startup_blueprint(name, industry, target_market, problem, solution, value_prop):
706
+ """Main blueprint generation function"""
707
+
708
+ if not all([name, industry, problem, solution]):
709
+ return (
710
+ "❌ **Error:** Please fill in all required fields.",
711
+ "", "", ""
712
+ )
713
+
714
+ try:
715
+ idea = StartupIdea(
716
+ name=name,
717
+ description=f"{name} - {solution}",
718
+ industry=industry,
719
+ target_market=target_market or f"{industry} customers",
720
+ problem_statement=problem,
721
+ solution=solution,
722
+ unique_value_proposition=value_prop or "Innovative solution"
723
+ )
724
+
725
+ print(f"πŸš€ Processing {name}")
726
+
727
+ if blueprint_generator:
728
+ business_plan = blueprint_generator.generate_business_plan(idea)
729
+ business_plan_text = business_plan.executive_summary
730
+ else:
731
+ business_plan_text = f"**{name}** addresses {problem} through {solution}."
732
+
733
+ if flashcards_gen:
734
+ flashcards = flashcards_gen.generate_flashcards(industry, 5)
735
+ flashcards_text = "\n\n".join([
736
+ f"**Q:** {fc.front}\n**A:** {fc.back}"
737
+ for fc in flashcards
738
+ ])
739
+ else:
740
+ flashcards_text = "**Sample Flashcard**\n\n**Q:** What is product-market fit?\n**A:** When product satisfies strong market demand."
741
+
742
+ if roadmap_gen:
743
+ roadmap = roadmap_gen.generate_roadmap(idea)
744
+ roadmap_text = roadmap_gen.export_roadmap(roadmap, 'markdown')
745
+ else:
746
+ roadmap_text = f"# Roadmap for {name}\n\n## Phase 1: Planning\n- Market research\n- Business plan"
747
+
748
+ summary = f"""# πŸš€ Blueprint Generated!
749
+
750
+ **Startup:** {name}
751
+ **Industry:** {industry}
752
+ **Target:** {target_market or f"{industry} customers"}
753
+
754
+ ## πŸ“Š Assessment: 85/100
755
+
756
+ ### βœ… Strengths:
757
+ - Clear problem identification
758
+ - Well-defined solution
759
+ - Strong market positioning
760
+
761
+ ### πŸ’‘ Next Steps:
762
+ 1. Validate with 50+ customer interviews
763
+ 2. Build MVP with core features
764
+ 3. Track CAC and LTV metrics
765
+ 4. Prepare for seed funding
766
+
767
+ *Generated: {datetime.now().strftime("%B %d, %Y")}*
768
+ """
769
+
770
+ print(f"βœ… Blueprint complete for {name}")
771
+ return summary, business_plan_text, flashcards_text, roadmap_text
772
+
773
+ except Exception as e:
774
+ error_msg = f"❌ **Error:** {str(e)}\n\nPlease try again."
775
+ print(f"❌ Error: {e}")
776
+ return error_msg, "", "", ""
777
+
778
+ def generate_flashcards_only(category):
779
+ """Generate flashcards only"""
780
+ try:
781
+ if flashcards_gen:
782
+ flashcards = flashcards_gen.generate_flashcards(category, 8)
783
+ return "\n\n".join([
784
+ f"**Q:** {fc.front}\n**A:** {fc.back}\n*Difficulty: {fc.difficulty}*"
785
+ for fc in flashcards
786
+ ])
787
+ else:
788
+ return f"**{category} Flashcards**\n\n**Q:** Sample question\n**A:** Sample answer"
789
+ except Exception as e:
790
+ return f"❌ Error: {str(e)}"
791
+
792
+ def generate_roadmap_only(startup_name, industry_type):
793
+ """Generate roadmap only"""
794
+ try:
795
+ idea = StartupIdea(
796
+ name=startup_name or "Sample Startup",
797
+ description="Sample",
798
+ industry=industry_type,
799
+ target_market="General",
800
+ problem_statement="Problem",
801
+ solution="Solution",
802
+ unique_value_proposition="Value"
803
+ )
804
+
805
+ if roadmap_gen:
806
+ roadmap = roadmap_gen.generate_roadmap(idea)
807
+ return roadmap_gen.export_roadmap(roadmap, 'markdown')
808
+ else:
809
+ return f"# {startup_name} Roadmap\n\n## Planning Phase\n- Research\n- Development"
810
+ except Exception as e:
811
+ return f"❌ Error: {str(e)}"
812
+
813
+ # Create Gradio Interface
814
+ print("🎨 Creating interface...")
815
+
816
+ custom_css = """
817
+ .gradio-container {
818
+ max-width: 1200px !important;
819
+ }
820
+ """
821
+
822
+ with gr.Blocks(title="πŸš€ AI Startup Roadmap Generator", theme=gr.themes.Soft(), css=custom_css) as demo:
823
+
824
+ gr.Markdown("""
825
+ # πŸš€ AI Startup Roadmap Generator
826
+
827
+ ### Transform Your Startup Ideas into Comprehensive Business Plans
828
+
829
+ **Powered by RAG Technology & ChromaDB**
830
+ """)
831
+
832
+ with gr.Tabs():
833
+ with gr.TabItem("🏒 Complete Blueprint"):
834
+ gr.Markdown("### πŸ“ Enter Startup Information")
835
+
836
+ with gr.Row():
837
+ with gr.Column():
838
+ startup_name = gr.Textbox(
839
+ label="Startup Name *",
840
+ placeholder="e.g., EcoTrack"
841
+ )
842
+ industry = gr.Dropdown(
843
+ choices=["SaaS", "E-commerce", "FinTech", "HealthTech", "EdTech", "Other"],
844
+ label="Industry *",
845
+ value="SaaS"
846
+ )
847
+ target_market = gr.Textbox(
848
+ label="Target Market",
849
+ placeholder="e.g., Small businesses",
850
+ lines=2
851
+ )
852
+
853
+ with gr.Column():
854
+ problem_statement = gr.Textbox(
855
+ label="Problem Statement *",
856
+ placeholder="What problem do you solve?",
857
+ lines=3
858
+ )
859
+ solution = gr.Textbox(
860
+ label="Solution *",
861
+ placeholder="How do you solve it?",
862
+ lines=3
863
+ )
864
+ value_proposition = gr.Textbox(
865
+ label="Unique Value Proposition",
866
+ placeholder="What makes you different?",
867
+ lines=2
868
+ )
869
+
870
+ generate_btn = gr.Button("πŸš€ Generate Blueprint", variant="primary", size="lg")
871
+
872
+ gr.Markdown("### πŸ“Š Generated Blueprint")
873
+
874
+ with gr.Row():
875
+ with gr.Column():
876
+ summary_output = gr.Markdown(label="Summary")
877
+ with gr.Column():
878
+ business_plan_output = gr.Markdown(label="Business Plan")
879
+
880
+ with gr.Row():
881
+ with gr.Column():
882
+ flashcards_output = gr.Markdown(label="Flashcards")
883
+ with gr.Column():
884
+ roadmap_output = gr.Markdown(label="Roadmap")
885
+
886
+ generate_btn.click(
887
+ fn=generate_startup_blueprint,
888
+ inputs=[startup_name, industry, target_market, problem_statement, solution, value_proposition],
889
+ outputs=[summary_output, business_plan_output, flashcards_output, roadmap_output]
890
+ )
891
+
892
+ with gr.TabItem("πŸ“š Flashcards"):
893
+ gr.Markdown("### πŸ“š Learning Flashcards")
894
+
895
+ flashcard_category = gr.Dropdown(
896
+ choices=['Market Research', 'Business Planning', 'Financial Management', 'SaaS'],
897
+ label="Category",
898
+ value="Market Research"
899
+ )
900
+ flashcard_btn = gr.Button("πŸ“š Generate", variant="primary")
901
+ flashcard_only_output = gr.Markdown()
902
+
903
+ flashcard_btn.click(
904
+ fn=generate_flashcards_only,
905
+ inputs=[flashcard_category],
906
+ outputs=[flashcard_only_output]
907
+ )
908
+
909
+ with gr.TabItem("πŸ—ΊοΈ Roadmap"):
910
+ gr.Markdown("### πŸ—ΊοΈ Startup Roadmap")
911
+
912
+ with gr.Row():
913
+ roadmap_startup_name = gr.Textbox(label="Startup Name", value="My Startup")
914
+ roadmap_industry = gr.Dropdown(
915
+ choices=["SaaS", "E-commerce", "FinTech"],
916
+ label="Industry",
917
+ value="SaaS"
918
+ )
919
+
920
+ roadmap_btn = gr.Button("πŸ—ΊοΈ Generate", variant="primary")
921
+ roadmap_only_output = gr.Markdown()
922
+
923
+ roadmap_btn.click(
924
+ fn=generate_roadmap_only,
925
+ inputs=[roadmap_startup_name, roadmap_industry],
926
+ outputs=[roadmap_only_output]
927
+ )
928
+
929
+ gr.Markdown("""
930
+ ---
931
+ **Disclaimer:** Educational guidance based on startup best practices. Consult professionals for specific advice.
932
+ """)
933
+
934
+ # Launch Application
935
+ if __name__ == "__main__":
936
+ port = int(os.getenv("PORT", 7860))
937
+
938
+ print(f"""
939
+ πŸŽ‰ AI Startup Roadmap Generator Ready!
940
+
941
+ 🌐 Port: {port}
942
+ πŸ“Š RAG: {"βœ… Active" if rag_system else "❌ Fallback"}
943
+
944
+ πŸš€ Launching...
945
+ """)
946
+
947
+ demo.launch(
948
+ server_name="0.0.0.0",
949
+ server_port=port,
950
+ share=False,
951
+ show_error=True
952
+ )