File size: 14,748 Bytes
e05ae01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f9510
e05ae01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f9510
e05ae01
 
f6f9510
 
 
 
e05ae01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f9510
 
 
 
 
e05ae01
f6f9510
 
e05ae01
 
 
 
 
 
 
 
f6f9510
e05ae01
 
f6f9510
e05ae01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Enhanced Document Generation Function - Integration with improved AI model
Replaces the original generate_document function to fix the missing sections issue
"""

import logging
from typing import Tuple, Dict, List

logger = logging.getLogger(__name__)


def generate_document_advanced(
    title: str,
    requirements: str,
    lecture_notes: str,
    document_type: str,
    length_words: int,
    style: str,
    include_tables: bool,
    include_charts: bool,
    include_citations: bool,
    citation_style: str,
    formats: list,
    # Component instances (to be passed in from app.py)
    analyzer=None,
    advanced_generator=None,
    research_generator=None,
    humanizer=None,
    quality_enhancer=None,
    document_processor=None,
    table_gen=None,
    citation_mgr=None,
    detector=None,
    pdf_gen=None,
    word_gen=None,
    md_gen=None,
    html_gen=None,
    metrics=None,
    preview_manager=None,
    transparency=None,
    FileHandler=None,
    TextFormatter=None,
    format_download_instructions=None,
) -> Tuple[str, dict, dict, dict]:
    """
    Generate complete academic document with research-driven, topic-specific content.
    
    This is an enhanced version that:
    1. Uses ResearchDrivenContentGenerator for topic-specific content (NOT generic)
    2. Synthesizes research data into coherent sections
    3. Uses EnhancedDocumentProcessor to ensure all sections are properly assembled
    4. Validates document completeness before output
    4. Provides detailed logging of what was generated
    """
    
    try:
        logger.info(f"Starting advanced document generation: {title}")
        logger.info(f"Document type: {document_type}, Word count target: {length_words}")
        
        # Log event
        if transparency:
            transparency.log_event("advanced_document_generation_started", {
                "title": title,
                "type": document_type,
                "length": length_words,
                "formats": formats,
            })

        # Step 1: Analyze requirements
        logger.info("Step 1: Analyzing requirements...")
        if not analyzer:
            from src.ai_engine import RequirementAnalyzer
            analyzer = RequirementAnalyzer()
            
        reqs = analyzer.analyze_requirements(requirements, lecture_notes)
        logger.info(f"Identified {len(reqs.sections)} sections: {reqs.sections}")
        
        # Step 2: Generate content for ALL sections using ResearchDrivenContentGenerator
        logger.info("Step 2: Generating research-driven content sections...")
        if not research_generator:
            from src.ai_engine import ResearchDrivenContentGenerator
            research_generator = ResearchDrivenContentGenerator()

        # Generate content with research synthesis (NOT generic templates)
        content_dict = research_generator.generate_document_sections(
            sections=reqs.sections,
            title=title,
            context=requirements,
            topics=reqs.key_topics if reqs.key_topics else [title] * len(reqs.sections),
            style=reqs.style,
            total_words=length_words,
        )
        
        logger.info(f"Generated {len(content_dict)} research-driven sections with content")
        for section, content in content_dict.items():
            word_count = len(content.split())
            logger.info(f"  - {section}: {word_count} words (research-based)")

        # Step 3: Humanize content for natural language
        logger.info("Step 3: Humanizing content...")
        if not humanizer:
            from src.ai_engine import Humanizer
            humanizer = Humanizer()
            
        for section in content_dict:
            content_dict[section] = humanizer.humanize_content(
                content_dict[section],
                style=reqs.style
            )

        # Step 4: Enhance quality (remove placeholders, improve readability)
        logger.info("Step 4: Enhancing content quality...")
        if not quality_enhancer:
            from src.ai_engine import ContentQualityEnhancer
            quality_enhancer = ContentQualityEnhancer()
            
        content_dict = quality_enhancer.enhance_document_content(content_dict, title)
        quality_report = quality_enhancer.get_quality_report(content_dict)

        # Step 5: Assemble complete document with proper structure
        # THIS IS THE CRITICAL FIX: Use EnhancedDocumentProcessor
        logger.info("Step 5: Assembling complete document...")
        if not document_processor:
            from src.document_engine import EnhancedDocumentProcessor
            document_processor = EnhancedDocumentProcessor()

        complete_document, assembly_messages = document_processor.assemble_complete_document(
            title=title,
            content_sections=content_dict,
            author="AI Academic Suite",
            document_type=document_type,
            include_toc=True,
            include_citations=include_citations,
            citations=[],  # Will be added later
        )
        
        logger.info("Assembly messages:")
        for msg in assembly_messages:
            logger.info(f"  {msg}")

        # Step 6: Generate visualizations if requested
        logger.info("Step 6: Generating visualizations...")
        tables_html = ""
        if include_tables and table_gen:
            try:
                table_data = table_gen.generate_summary_table("\n".join(content_dict.values()))
                tables_html = table_gen.format_as_html(table_data)
                logger.info("  Tables generated successfully")
            except Exception as e:
                logger.warning(f"  Table generation failed: {e}")

        # Step 7: Generate citations if requested
        logger.info("Step 7: Generating citations...")
        citations = []
        if include_citations and citation_mgr:
            try:
                citations = [
                    citation_mgr.generate_citation(
                        ["Smith, J.", "Doe, A."],
                        f"Research on {reqs.key_topics[0] if reqs.key_topics else 'Topic'}",
                        "Academic Journal",
                        2024,
                        style=citation_style
                    ),
                    citation_mgr.generate_citation(
                        ["Johnson, B."],
                        "Contemporary Research Methods",
                        "University Press",
                        2023,
                        style=citation_style
                    ),
                ]
                logger.info(f"  Generated {len(citations)} citations")
            except Exception as e:
                logger.warning(f"  Citation generation failed: {e}")

        # Step 8: Export to requested formats
        logger.info("Step 8: Exporting to requested formats...")
        outputs = {}
        status_updates = []

        if not FileHandler:
            from utils import FileHandler

        if "pdf" in formats:
            try:
                if not pdf_gen:
                    from src.document_engine import PDFGenerator
                    pdf_gen = PDFGenerator()
                    
                pdf_bytes = pdf_gen.generate_pdf(
                    title, 
                    complete_document, 
                    include_citations=include_citations, 
                    citations=citations
                )
                pdf_path = FileHandler.save_file(pdf_bytes, f"{title.replace(' ', '_')}.pdf")
                outputs["PDF"] = pdf_path
                status_updates.append("βœ“ PDF generated successfully")
                logger.info("  PDF export successful")
            except Exception as e:
                status_updates.append(f"βœ— PDF generation failed: {str(e)[:50]}")
                logger.error(f"  PDF export failed: {e}")

        if "docx" in formats:
            try:
                if not word_gen:
                    from src.document_engine import WordGenerator
                    word_gen = WordGenerator()
                    
                docx_bytes = word_gen.generate_word_doc(
                    title, 
                    complete_document, 
                    include_citations=include_citations, 
                    citations=citations
                )
                docx_path = FileHandler.save_file(docx_bytes, f"{title.replace(' ', '_')}.docx")
                outputs["Word"] = docx_path
                status_updates.append("βœ“ Word document generated successfully")
                logger.info("  Word export successful")
            except Exception as e:
                status_updates.append(f"βœ— Word generation failed: {str(e)[:50]}")
                logger.error(f"  Word export failed: {e}")

        if "md" in formats:
            try:
                if not md_gen:
                    from src.document_engine import MarkdownGenerator
                    md_gen = MarkdownGenerator()
                    
                md_bytes = md_gen.generate_markdown_bytes(
                    title, 
                    complete_document, 
                    include_citations=include_citations, 
                    citations=citations
                )
                md_path = FileHandler.save_file(md_bytes, f"{title.replace(' ', '_')}.md")
                outputs["Markdown"] = md_path
                status_updates.append("βœ“ Markdown generated successfully")
                logger.info("  Markdown export successful")
            except Exception as e:
                status_updates.append(f"βœ— Markdown generation failed: {str(e)[:50]}")
                logger.error(f"  Markdown export failed: {e}")

        if "html" in formats:
            try:
                if not html_gen:
                    from src.document_engine import HTMLGenerator
                    html_gen = HTMLGenerator()
                    
                html_bytes = html_gen.generate_html_bytes(
                    title, 
                    complete_document, 
                    include_citations=include_citations, 
                    citations=citations
                )
                html_path = FileHandler.save_file(html_bytes, f"{title.replace(' ', '_')}.html")
                outputs["HTML"] = html_path
                status_updates.append("βœ“ HTML generated successfully")
                logger.info("  HTML export successful")
            except Exception as e:
                status_updates.append(f"βœ— HTML generation failed: {str(e)[:50]}")
                logger.error(f"  HTML export failed: {e}")

        # Step 9: Generate quality metrics and AI detection analysis
        logger.info("Step 9: Analyzing document quality...")
        full_content = "\n".join(complete_document.values())
        
        if not metrics:
            from src.research_tools import QualityMetrics
            metrics = QualityMetrics()
            
        quality = metrics.get_quality_report(full_content)

        if not detector:
            from src.ai_engine import AIDetector
            detector = AIDetector()
            
        detection = detector.analyze_detection_risk(full_content)

        # Step 10: Generate result summary
        logger.info("Step 10: Generating result summary...")
        
        if not TextFormatter:
            from utils import TextFormatter

        result_text = (
            f"βœ… ADVANCED DOCUMENT GENERATION COMPLETE\n\n"
            f"Title: {title}\n"
            f"Type: {document_type}\n"
            f"Sections Generated: {len(complete_document)}\n"
            f"Word Count: {TextFormatter.word_count(full_content)}\n"
            f"Reading Time: ~{TextFormatter.estimate_reading_time(full_content)} minutes\n\n"
            f"πŸ“Š QUALITY METRICS:\n"
            f"  Readability Score: {quality.get('readability', 0)}/100\n"
            f"  Coherence: {quality.get('coherence', 0)}/100\n"
            f"  Originality: {quality.get('originality', 0)}/100\n\n"
            f"⚠️ AI DETECTION RISK: {detection.get('risk_level', 'Unknown')}\n"
            f"  Risk Score: {detection.get('risk_score', 0):.1%}\n"
            f"  Recommendation: {detection.get('recommendation', 'N/A')}\n\n"
            f"πŸ“‹ DOCUMENT STRUCTURE:\n"
            f"  Total Sections: {len(complete_document)}\n"
        )

        # Add section details
        result_text += "  Section Details:\n"
        for section_name, content in complete_document.items():
            word_count = len(content.split())
            result_text += f"    βœ“ {section_name}: {word_count} words\n"

        result_text += (
            f"\nπŸ“₯ GENERATED FORMATS:\n" +
            "\n".join(f"  βœ“ {fmt.upper()}" for fmt in outputs.keys()) + "\n\n" +
            f"πŸ”— STATUS:\n" +
            "\n".join(f"  {s}" for s in status_updates)
        )

        # Register document for preview & download
        if preview_manager:
            doc_id = preview_manager.register_document(
                title=title,
                file_paths=outputs,
                content_preview=full_content,
                metadata={
                    "document_type": document_type,
                    "sections_count": len(complete_document),
                    "word_count": TextFormatter.word_count(full_content),
                    "reading_time": TextFormatter.estimate_reading_time(full_content),
                    "quality_score": quality.get('readability', 0),
                    "ai_detection_risk": detection.get('risk_level', 'Unknown'),
                    "formats_available": list(outputs.keys())
                }
            )

            # Add download information
            if format_download_instructions:
                result_text += f"\n\n{'=' * 60}\n"
                result_text += format_download_instructions(doc_id, list(outputs.keys()))
                result_text += f"{'=' * 60}\n"

            logger.info(f"Document registered with ID: {doc_id}")

        # Log completion
        if transparency:
            transparency.log_event("advanced_document_generation_completed", {
                "formats_generated": list(outputs.keys()),
                "sections_count": len(complete_document),
                "word_count": TextFormatter.word_count(full_content),
                "quality_score": quality.get('readability', 0),
            })

        logger.info("βœ… Advanced document generation completed successfully")
        return result_text, quality, detection, {"tables": tables_html}

    except Exception as e:
        error_msg = f"❌ Error in advanced document generation: {str(e)}"
        logger.error(error_msg, exc_info=True)
        return error_msg, {}, {}, {}