Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,43 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
"""
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
| 7 |
import tempfile
|
|
|
|
| 8 |
from multi_agent import MultiAgentSystem
|
| 9 |
from docx_builder import generate_all_documents
|
| 10 |
|
| 11 |
# Initialize the multi-agent system
|
| 12 |
agent_system = MultiAgentSystem()
|
| 13 |
|
| 14 |
-
def generate_exam_package(subject, stream, part_a_count, part_b_count, part_c_count,
|
| 15 |
-
|
| 16 |
-
"""Enhanced main function with dynamic marks configuration"""
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
# Convert counts
|
| 20 |
-
part_a_count = int(part_a_count)
|
| 21 |
-
part_b_count = int(part_b_count)
|
| 22 |
-
part_c_count = int(part_c_count)
|
| 23 |
-
part_a_marks = int(part_a_marks)
|
| 24 |
-
part_b_marks = int(part_b_marks)
|
| 25 |
-
part_c_marks = int(part_c_marks)
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
final_output, status = agent_system.generate_exam_package(
|
| 29 |
subject=subject,
|
| 30 |
stream=stream,
|
| 31 |
part_a_count=part_a_count,
|
| 32 |
part_b_count=part_b_count,
|
| 33 |
part_c_count=part_c_count,
|
| 34 |
-
part_a_marks=part_a_marks,
|
| 35 |
-
part_b_marks=part_b_marks,
|
| 36 |
-
part_c_marks=part_c_marks,
|
| 37 |
syllabus_file=syllabus_file,
|
| 38 |
reference_file=reference_file
|
| 39 |
)
|
| 40 |
|
| 41 |
if status != "Success":
|
| 42 |
-
return None, None, None, status
|
| 43 |
|
| 44 |
# Create temporary directory for outputs
|
| 45 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
@@ -47,173 +45,182 @@ def generate_exam_package(subject, stream, part_a_count, part_b_count, part_c_co
|
|
| 47 |
files = generate_all_documents(final_output, subject, stream, temp_dir)
|
| 48 |
|
| 49 |
# Return file paths
|
| 50 |
-
return files["question_paper"], files["answer_key"], files["obe_summary"], "Generation completed successfully!"
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
-
error_msg = f"Error: {str(e)}"
|
| 54 |
print(error_msg)
|
| 55 |
return None, None, None, error_msg
|
| 56 |
|
| 57 |
def update_progress_ui():
|
| 58 |
"""Update progress in UI"""
|
| 59 |
progress = agent_system.get_progress()
|
| 60 |
-
return f"{progress['stage'].upper()}: {progress['message']} ({progress['percentage']}%)"
|
| 61 |
|
| 62 |
-
def
|
| 63 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except:
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
#
|
| 71 |
with gr.Blocks(title="AI Exam Generator", theme=gr.themes.Soft()) as demo:
|
| 72 |
-
gr.Markdown("
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
-
with gr.Column():
|
| 77 |
# Input section
|
|
|
|
|
|
|
| 78 |
subject_name = gr.Textbox(
|
| 79 |
label="Subject Name",
|
| 80 |
-
placeholder="e.g., Data Structures,
|
| 81 |
value="Data Structures"
|
| 82 |
)
|
| 83 |
|
| 84 |
stream = gr.Dropdown(
|
| 85 |
choices=["CSE", "Non-CSE"],
|
| 86 |
-
label="Stream",
|
| 87 |
-
value="CSE"
|
|
|
|
| 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 |
-
value=5,
|
| 115 |
-
minimum=1,
|
| 116 |
-
maximum=20,
|
| 117 |
-
step=1
|
| 118 |
-
)
|
| 119 |
-
part_b_marks = gr.Number(
|
| 120 |
-
label="Marks per Question",
|
| 121 |
-
value=13,
|
| 122 |
-
minimum=5,
|
| 123 |
-
maximum=20,
|
| 124 |
-
step=1
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
with gr.Group():
|
| 128 |
-
gr.Markdown("### Part C Configuration")
|
| 129 |
-
with gr.Row():
|
| 130 |
-
part_c_count = gr.Number(
|
| 131 |
-
label="Number of Questions",
|
| 132 |
-
value=1,
|
| 133 |
-
minimum=1,
|
| 134 |
-
maximum=5,
|
| 135 |
-
step=1
|
| 136 |
-
)
|
| 137 |
-
part_c_marks = gr.Number(
|
| 138 |
-
label="Marks per Question",
|
| 139 |
-
value=14,
|
| 140 |
-
minimum=10,
|
| 141 |
-
maximum=30,
|
| 142 |
-
step=1
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
# Total marks display
|
| 146 |
-
total_marks_display = gr.Textbox(
|
| 147 |
-
label="Marks Summary",
|
| 148 |
-
value="π Total Marks: 0",
|
| 149 |
-
interactive=False
|
| 150 |
-
)
|
| 151 |
|
| 152 |
syllabus_pdf = gr.File(
|
| 153 |
-
label="Syllabus PDF",
|
| 154 |
file_types=[".pdf"],
|
| 155 |
-
type="filepath"
|
|
|
|
| 156 |
)
|
| 157 |
|
| 158 |
reference_qp = gr.File(
|
| 159 |
-
label="Reference Question Paper
|
| 160 |
file_types=[".pdf"],
|
| 161 |
-
type="filepath"
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
-
generate_btn = gr.Button("Generate Exam Package", variant="primary", size="lg")
|
| 165 |
|
| 166 |
-
with gr.Column():
|
| 167 |
# Output section
|
|
|
|
|
|
|
| 168 |
status_display = gr.Textbox(
|
| 169 |
-
label="
|
| 170 |
-
value="Ready to generate",
|
| 171 |
-
interactive=False
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
-
with gr.
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
# Progress section
|
|
|
|
| 194 |
progress_text = gr.Textbox(
|
| 195 |
label="Real-time Progress",
|
| 196 |
-
value="System ready",
|
| 197 |
interactive=False,
|
| 198 |
-
every=
|
| 199 |
)
|
| 200 |
|
| 201 |
-
#
|
| 202 |
-
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
#
|
| 212 |
generate_btn.click(
|
| 213 |
fn=generate_exam_package,
|
| 214 |
-
inputs=[subject_name, stream, part_a_count, part_b_count, part_c_count,
|
| 215 |
-
part_a_marks, part_b_marks, part_c_marks, syllabus_pdf, reference_qp],
|
| 216 |
outputs=[qp_download, answers_download, obe_download, status_display]
|
|
|
|
|
|
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
# Progress updates
|
|
@@ -223,6 +230,13 @@ with gr.Blocks(title="AI Exam Generator", theme=gr.themes.Soft()) as demo:
|
|
| 223 |
outputs=[progress_text]
|
| 224 |
)
|
| 225 |
|
|
|
|
| 226 |
if __name__ == "__main__":
|
| 227 |
-
|
| 228 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
Main Gradio application optimized for Hugging Face Spaces
|
| 3 |
"""
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
+
import json
|
| 9 |
from multi_agent import MultiAgentSystem
|
| 10 |
from docx_builder import generate_all_documents
|
| 11 |
|
| 12 |
# Initialize the multi-agent system
|
| 13 |
agent_system = MultiAgentSystem()
|
| 14 |
|
| 15 |
+
def generate_exam_package(subject, stream, part_a_count, part_b_count, part_c_count, syllabus_file, reference_file):
|
| 16 |
+
"""Main function called by Gradio interface"""
|
|
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
# Convert counts to integers
|
| 20 |
+
part_a_count = int(part_a_count) if part_a_count else 5
|
| 21 |
+
part_b_count = int(part_b_count) if part_b_count else 5
|
| 22 |
+
part_c_count = int(part_c_count) if part_c_count else 1
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Validate inputs
|
| 25 |
+
if not subject or not syllabus_file:
|
| 26 |
+
return None, None, None, "β Please provide subject name and syllabus PDF"
|
| 27 |
+
|
| 28 |
+
# Generate exam package
|
| 29 |
final_output, status = agent_system.generate_exam_package(
|
| 30 |
subject=subject,
|
| 31 |
stream=stream,
|
| 32 |
part_a_count=part_a_count,
|
| 33 |
part_b_count=part_b_count,
|
| 34 |
part_c_count=part_c_count,
|
|
|
|
|
|
|
|
|
|
| 35 |
syllabus_file=syllabus_file,
|
| 36 |
reference_file=reference_file
|
| 37 |
)
|
| 38 |
|
| 39 |
if status != "Success":
|
| 40 |
+
return None, None, None, f"β {status}"
|
| 41 |
|
| 42 |
# Create temporary directory for outputs
|
| 43 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 45 |
files = generate_all_documents(final_output, subject, stream, temp_dir)
|
| 46 |
|
| 47 |
# Return file paths
|
| 48 |
+
return files["question_paper"], files["answer_key"], files["obe_summary"], "β
Generation completed successfully!"
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
+
error_msg = f"β Error: {str(e)}"
|
| 52 |
print(error_msg)
|
| 53 |
return None, None, None, error_msg
|
| 54 |
|
| 55 |
def update_progress_ui():
|
| 56 |
"""Update progress in UI"""
|
| 57 |
progress = agent_system.get_progress()
|
| 58 |
+
return f"π {progress['stage'].upper()}: {progress['message']} ({progress['percentage']}%)"
|
| 59 |
|
| 60 |
+
def validate_inputs(subject, part_a_count, part_b_count, part_c_count):
|
| 61 |
+
"""Validate user inputs"""
|
| 62 |
+
errors = []
|
| 63 |
+
if not subject:
|
| 64 |
+
errors.append("Subject name is required")
|
| 65 |
+
|
| 66 |
try:
|
| 67 |
+
if part_a_count and int(part_a_count) <= 0:
|
| 68 |
+
errors.append("Part A count must be positive")
|
| 69 |
+
if part_b_count and int(part_b_count) <= 0:
|
| 70 |
+
errors.append("Part B count must be positive")
|
| 71 |
+
if part_c_count and int(part_c_count) <= 0:
|
| 72 |
+
errors.append("Part C count must be positive")
|
| 73 |
except:
|
| 74 |
+
errors.append("Question counts must be numbers")
|
| 75 |
+
|
| 76 |
+
return errors
|
| 77 |
|
| 78 |
+
# Gradio interface
|
| 79 |
with gr.Blocks(title="AI Exam Generator", theme=gr.themes.Soft()) as demo:
|
| 80 |
+
gr.Markdown("""
|
| 81 |
+
# π AI-Powered Question Paper Generator
|
| 82 |
+
*Generate complete exam packages with OBE compliance using multi-agent AI system*
|
| 83 |
+
""")
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
+
with gr.Column(scale=1):
|
| 87 |
# Input section
|
| 88 |
+
gr.Markdown("### π Exam Configuration")
|
| 89 |
+
|
| 90 |
subject_name = gr.Textbox(
|
| 91 |
label="Subject Name",
|
| 92 |
+
placeholder="e.g., Data Structures, Algorithms, Database Systems",
|
| 93 |
value="Data Structures"
|
| 94 |
)
|
| 95 |
|
| 96 |
stream = gr.Dropdown(
|
| 97 |
choices=["CSE", "Non-CSE"],
|
| 98 |
+
label="Engineering Stream",
|
| 99 |
+
value="CSE",
|
| 100 |
+
info="CSE: Company tags | Non-CSE: GATE patterns"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
with gr.Row():
|
| 104 |
+
part_a_count = gr.Number(
|
| 105 |
+
label="Part A Questions",
|
| 106 |
+
value=5,
|
| 107 |
+
minimum=1,
|
| 108 |
+
maximum=20,
|
| 109 |
+
info="2 marks each"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
part_b_count = gr.Number(
|
| 113 |
+
label="Part B Questions",
|
| 114 |
+
value=5,
|
| 115 |
+
minimum=1,
|
| 116 |
+
maximum=10,
|
| 117 |
+
info="13 marks each"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
part_c_count = gr.Number(
|
| 121 |
+
label="Part C Questions",
|
| 122 |
+
value=1,
|
| 123 |
+
minimum=1,
|
| 124 |
+
maximum=3,
|
| 125 |
+
info="14 marks each"
|
| 126 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
syllabus_pdf = gr.File(
|
| 129 |
+
label="Upload Syllabus PDF",
|
| 130 |
file_types=[".pdf"],
|
| 131 |
+
type="filepath",
|
| 132 |
+
file_count="single"
|
| 133 |
)
|
| 134 |
|
| 135 |
reference_qp = gr.File(
|
| 136 |
+
label="Reference Question Paper (Optional)",
|
| 137 |
file_types=[".pdf"],
|
| 138 |
+
type="filepath",
|
| 139 |
+
file_count="single"
|
| 140 |
)
|
| 141 |
|
| 142 |
+
generate_btn = gr.Button("π― Generate Exam Package", variant="primary", size="lg")
|
| 143 |
|
| 144 |
+
with gr.Column(scale=1):
|
| 145 |
# Output section
|
| 146 |
+
gr.Markdown("### π€ Generated Outputs")
|
| 147 |
+
|
| 148 |
status_display = gr.Textbox(
|
| 149 |
+
label="Status",
|
| 150 |
+
value="π’ Ready to generate exam package",
|
| 151 |
+
interactive=False,
|
| 152 |
+
lines=2
|
| 153 |
)
|
| 154 |
|
| 155 |
+
with gr.Group():
|
| 156 |
+
gr.Markdown("#### π Download Files")
|
| 157 |
+
with gr.Row():
|
| 158 |
+
qp_download = gr.File(
|
| 159 |
+
label="Question Paper",
|
| 160 |
+
file_types=[".docx"],
|
| 161 |
+
interactive=False,
|
| 162 |
+
visible=False
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
answers_download = gr.File(
|
| 166 |
+
label="Answer Key",
|
| 167 |
+
file_types=[".docx"],
|
| 168 |
+
interactive=False,
|
| 169 |
+
visible=False
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
obe_download = gr.File(
|
| 173 |
+
label="OBE Summary",
|
| 174 |
+
file_types=[".docx"],
|
| 175 |
+
interactive=False,
|
| 176 |
+
visible=False
|
| 177 |
+
)
|
| 178 |
|
| 179 |
# Progress section
|
| 180 |
+
gr.Markdown("### π Generation Progress")
|
| 181 |
progress_text = gr.Textbox(
|
| 182 |
label="Real-time Progress",
|
| 183 |
+
value="π’ System initialized and ready",
|
| 184 |
interactive=False,
|
| 185 |
+
every=1
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# Instructions
|
| 189 |
+
gr.Markdown("""
|
| 190 |
+
## π How to Use This System
|
| 191 |
|
| 192 |
+
1. **Enter Subject Details**: Provide the subject name and select engineering stream
|
| 193 |
+
2. **Configure Questions**: Set the number of questions for each part (A, B, C)
|
| 194 |
+
3. **Upload Syllabus**: PDF file containing course syllabus and learning outcomes
|
| 195 |
+
4. **Optional Reference**: Upload previous question papers for style reference
|
| 196 |
+
5. **Generate**: Click the button to create your complete exam package
|
| 197 |
+
6. **Download**: Get all three DOCX files (Question Paper, Answer Key, OBE Summary)
|
| 198 |
+
|
| 199 |
+
## π― Features
|
| 200 |
+
|
| 201 |
+
- **π€ Multi-Agent AI**: Three specialized agents for generation, verification, and formatting
|
| 202 |
+
- **π OBE Compliant**: Automatic Outcome-Based Education reporting
|
| 203 |
+
- **π Real-time Updates**: Incorporates recent developments in the field
|
| 204 |
+
- **π Stream-specific**: CSE (company tags) vs Non-CSE (GATE patterns)
|
| 205 |
+
- **β
Quality Verified**: Automated validation of standards and requirements
|
| 206 |
+
- **π Professional Output**: Ready-to-use DOCX files with proper formatting
|
| 207 |
+
|
| 208 |
+
## β οΈ Important Notes
|
| 209 |
+
|
| 210 |
+
- Ensure your syllabus PDF is readable and contains clear learning outcomes
|
| 211 |
+
- For best results, provide a descriptive subject name
|
| 212 |
+
- The system works without API keys but with enhanced capabilities when configured
|
| 213 |
+
- Generation may take 1-2 minutes depending on complexity
|
| 214 |
+
""")
|
| 215 |
|
| 216 |
+
# Event handlers
|
| 217 |
generate_btn.click(
|
| 218 |
fn=generate_exam_package,
|
| 219 |
+
inputs=[subject_name, stream, part_a_count, part_b_count, part_c_count, syllabus_pdf, reference_qp],
|
|
|
|
| 220 |
outputs=[qp_download, answers_download, obe_download, status_display]
|
| 221 |
+
).then(
|
| 222 |
+
fn=lambda: (gr.File(visible=True), gr.File(visible=True), gr.File(visible=True)),
|
| 223 |
+
outputs=[qp_download, answers_download, obe_download]
|
| 224 |
)
|
| 225 |
|
| 226 |
# Progress updates
|
|
|
|
| 230 |
outputs=[progress_text]
|
| 231 |
)
|
| 232 |
|
| 233 |
+
# Deployment configuration
|
| 234 |
if __name__ == "__main__":
|
| 235 |
+
# For Hugging Face Spaces
|
| 236 |
+
demo.queue(concurrency_count=1, max_size=10)
|
| 237 |
+
demo.launch(
|
| 238 |
+
server_name="0.0.0.0",
|
| 239 |
+
server_port=7860,
|
| 240 |
+
share=False,
|
| 241 |
+
show_error=True
|
| 242 |
+
)
|