-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (104 loc) · 4.04 KB
/
main.py
File metadata and controls
137 lines (104 loc) · 4.04 KB
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
import os
from utils import task, ai, cover, md_to_html, html_to_doc, doc_to_pdf
from colorama import init, Fore, Style
from docx2pdf import convert as docx_to_pdf
# Initialize colorama for colored logs
init(autoreset=True)
def read_tasks(file_path):
"""Read and parse the tasks from the specified JSON file."""
print(f"{Fore.CYAN}Reading {file_path}...")
data = task.parse(file_path)
print(f"{Fore.GREEN}Tasks found: {len(data)}")
return data
def generate_report(t, topic, gap, cache_dir):
"""Generate or fetch the report for the given task."""
filepath = os.path.join(cache_dir, f"{t['Subject Code']}.md")
if os.path.exists(filepath):
print(
f"{Fore.YELLOW}Reading cached markdown content for {t['Subject']}...")
with open(filepath, 'r', encoding='utf-8') as file:
return file.read()
else:
print(
f"{Fore.BLUE}Generating markdown content from AI for {topic} in {t['Subject']}...")
res = ai.prompt(f"""
You are an academic report writer.
Write a detailed, well-structured, university-level report on:
Topic: "{topic}"
Subject: "{t["Subject"]}"
Requirements:
1. Use formal academic tone.
2. Structure the report using proper Markdown headings:
- # Title
- ## Abstract
- ## Introduction
- ## Background / Theory
- ## Core Concepts
- ## Methodology / Working Mechanism (if applicable)
- ## Applications
- ## Advantages and Limitations
- ## Challenges
- ## Future Scope
- ## Conclusion
- ## References
3. The report must:
- Be detailed and analytical (not generic).
- Explain concepts clearly with technical depth.
- Include examples where appropriate.
- Avoid conversational tone.
- Avoid repetition and filler text.
- Avoid bullet overload (prefer paragraphs unless listing is required).
- Be at least 2000+ words.
4. In References section:
- Include realistic academic-style references.
- Do not fabricate exact URLs.
- Use standard citation style (APA-like format).
5. Ensure logical flow between sections.
6. Do not mention AI or that this was generated.
Return only Markdown content.
""")
with open(filepath, 'w', encoding='utf-8') as file:
file.write(res)
return res
def generate_cover_doc(topic, t, gap):
"""Generate the cover page for the report."""
print(
f"{Fore.MAGENTA}Generating cover for labels:\n{', '.join([f'{key}: {value}' for key, value in t.items()])}")
doc = cover.generate_doc_with_intro(topic, t, gap)
doc.add_page_break()
return doc
def convert_to_doc(res, doc):
"""Convert AI-provided markdown content to docx format."""
print(f"{Fore.CYAN}Converting AI-provided data to docx format...")
html = md_to_html.convert(res)
html_to_doc.convert_and_add(html, doc)
def save_doc(doc, doc_dir, t):
"""Save the generated docx to the specified directory."""
output_file = os.path.join(doc_dir, f"{t['Subject']}_{t['Name']}.docx")
doc.save(output_file)
print(f"{Fore.GREEN}Doc File generated successfully for {t['Name']}\n\n")
def process_task(t, cache_dir, doc_dir):
"""Process a single task to generate the report."""
topic = t['topic']
gap = t.get('gap', 5)
# Clean up unnecessary fields
del t['topic']
t.pop('gap', None)
print(f"{Fore.BLUE}{Style.BRIGHT}Generating report for topic: {topic}...")
# Generate or fetch report content
res = generate_report(t, topic, gap, cache_dir)
# Generate the cover page
doc = generate_cover_doc(topic, t, gap)
# Convert markdown content to docx format
convert_to_doc(res, doc)
# Save the generated doc
save_doc(doc, doc_dir, t)
def generate_pdfs(task_file='task.json', cache_dir='./cache', doc_dir='./docs'):
"""Main function to generate PDFs for all tasks."""
tasks = read_tasks(task_file)
for i, t in enumerate(tasks):
print(f"{Fore.YELLOW}Processing task {i+1}/{len(tasks)}...")
process_task(t, cache_dir, doc_dir)
if __name__ == '__main__':
generate_pdfs()
doc_to_pdf.convert_docs_to_pdfs()