codeboosterstech commited on
Commit
9908b22
·
verified ·
1 Parent(s): 28c10e5

Create multi_agent.py

Browse files
Files changed (1) hide show
  1. multi_agent.py +145 -0
multi_agent.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Enhanced multi-agent system with dynamic marks configuration
3
+ """
4
+
5
+ import json
6
+ import time
7
+ from typing import Dict, Any, Tuple
8
+ from agents import GeneratorAgent, VerifierAgent, FormatterAgent, SearchAgent
9
+ from prompts import get_generator_prompt, VERIFIER_PROMPT, FORMATTER_PROMPT
10
+
11
+ class MultiAgentSystem:
12
+ """Enhanced orchestrator with dynamic marks support"""
13
+
14
+ def __init__(self):
15
+ self.generator = GeneratorAgent()
16
+ self.verifier = VerifierAgent()
17
+ self.formatter = FormatterAgent()
18
+ self.searcher = SearchAgent()
19
+
20
+ self.progress = {
21
+ "stage": "ready",
22
+ "message": "System ready",
23
+ "percentage": 0
24
+ }
25
+
26
+ def update_progress(self, stage: str, message: str, percentage: int):
27
+ """Update generation progress"""
28
+ self.progress = {
29
+ "stage": stage,
30
+ "message": message,
31
+ "percentage": percentage
32
+ }
33
+ print(f"Progress: {stage} - {message} ({percentage}%)")
34
+
35
+ def extract_syllabus_from_pdf(self, pdf_file) -> str:
36
+ """Extract text from uploaded syllabus PDF"""
37
+ try:
38
+ import PyPDF2
39
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
40
+ text = ""
41
+ for page in pdf_reader.pages:
42
+ text += page.extract_text()
43
+ return text[:3000]
44
+ except Exception as e:
45
+ print(f"PDF extraction error: {e}")
46
+ return "Syllabus text extraction failed. Using default template."
47
+
48
+ def extract_reference_from_pdf(self, pdf_file) -> str:
49
+ """Extract text from reference question paper PDF"""
50
+ if pdf_file is None:
51
+ return "No reference question paper provided."
52
+
53
+ try:
54
+ import PyPDF2
55
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
56
+ text = ""
57
+ for page in pdf_reader.pages[:3]:
58
+ text += page.extract_text()
59
+ return text[:2000]
60
+ except Exception as e:
61
+ print(f"Reference PDF extraction error: {e}")
62
+ return "Reference paper extraction failed."
63
+
64
+ def generate_exam_package(self,
65
+ subject: str,
66
+ stream: str,
67
+ part_a_count: int,
68
+ part_b_count: int,
69
+ part_c_count: int,
70
+ part_a_marks: int = 2,
71
+ part_b_marks: int = 13,
72
+ part_c_marks: int = 14,
73
+ syllabus_file = None,
74
+ reference_file = None) -> Tuple[Dict[str, Any], str]:
75
+ """Enhanced main method with dynamic marks"""
76
+
77
+ try:
78
+ # Stage 1: Data Preparation
79
+ self.update_progress("preparation", "Extracting syllabus and reference data", 10)
80
+ syllabus_text = self.extract_syllabus_from_pdf(syllabus_file)
81
+ reference_text = self.extract_reference_from_pdf(reference_file)
82
+
83
+ # Stage 2: Realtime Search
84
+ self.update_progress("search", "Fetching recent developments", 20)
85
+ realtime_updates = self.searcher.get_realtime_updates(subject)
86
+
87
+ # Stage 3: Generation with dynamic marks
88
+ self.update_progress("generation", "Generating question paper structure", 40)
89
+ generator_prompt = get_generator_prompt(
90
+ subject=subject,
91
+ stream=stream,
92
+ syllabus_text=syllabus_text,
93
+ reference_text=reference_text,
94
+ realtime_updates=realtime_updates,
95
+ part_a_count=part_a_count,
96
+ part_b_count=part_b_count,
97
+ part_c_count=part_c_count,
98
+ part_a_marks=part_a_marks,
99
+ part_b_marks=part_b_marks,
100
+ part_c_marks=part_c_marks
101
+ )
102
+
103
+ generated_content = self.generator.generate_question_paper(generator_prompt)
104
+
105
+ if "error" in generated_content:
106
+ return {}, "Generation failed: " + generated_content["error"]
107
+
108
+ # Stage 4: Verification with dynamic marks
109
+ self.update_progress("verification", "Verifying quality and standards", 60)
110
+ verifier_prompt = VERIFIER_PROMPT.format(
111
+ generated_content=json.dumps(generated_content, indent=2),
112
+ bloom_mix="60% R/U, 40% A/An/Ev" if stream == "CSE" else "50% R/U, 50% A/An/Ev",
113
+ part_a_count=part_a_count,
114
+ part_b_count=part_b_count,
115
+ part_c_count=part_c_count,
116
+ part_a_marks=part_a_marks,
117
+ part_b_marks=part_b_marks,
118
+ part_c_marks=part_c_marks,
119
+ tag_requirements="Company tags" if stream == "CSE" else "GATE tags"
120
+ )
121
+
122
+ verification_result = self.verifier.verify_content(verifier_prompt)
123
+
124
+ # Stage 5: Formatting
125
+ self.update_progress("formatting", "Creating final outputs and answers", 80)
126
+ formatter_prompt = FORMATTER_PROMPT.format(
127
+ original_content=json.dumps(generated_content, indent=2),
128
+ corrections=json.dumps(verification_result, indent=2)
129
+ )
130
+
131
+ final_output = self.formatter.format_final_output(formatter_prompt)
132
+
133
+ # Stage 6: Completion
134
+ self.update_progress("completion", "Package generation complete", 100)
135
+
136
+ return final_output, "Success"
137
+
138
+ except Exception as e:
139
+ error_msg = f"System error: {str(e)}"
140
+ print(error_msg)
141
+ return {}, error_msg
142
+
143
+ def get_progress(self) -> Dict[str, Any]:
144
+ """Get current progress"""
145
+ return self.progress