Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -135,6 +135,20 @@ Return ONLY pure JSON. No commentary.
|
|
| 135 |
"""
|
| 136 |
return apply_MAANGO_BIG15_framework(base_prompt)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def create_question_paper(code, name, partA, partB, partC, output_path):
|
| 139 |
doc = Document()
|
| 140 |
doc.add_heading("SNS College of Technology", level=1)
|
|
@@ -177,34 +191,28 @@ def create_answer_key(code, name, answers, output_path):
|
|
| 177 |
|
| 178 |
doc.save(output_path)
|
| 179 |
|
| 180 |
-
def generate_exam(exam_mode, subject, code, units, numA, numB, numC, syllabus_file
|
| 181 |
try:
|
| 182 |
-
progress(0, desc="Initializing...")
|
| 183 |
-
|
| 184 |
# Initialize models
|
| 185 |
model_q, model_a, model_t, serp = init_models()
|
| 186 |
if not model_q:
|
| 187 |
return None, "❌ API Keys not configured. Please set GROQ_API_KEY and SERPAPI_API_KEY in Hugging Face Spaces secrets."
|
| 188 |
|
| 189 |
# Extract syllabus
|
| 190 |
-
progress(0.2, desc="Extracting syllabus...")
|
| 191 |
syllabus_text = extract_text(syllabus_file.name)
|
| 192 |
selected_syllabus = extract_units(syllabus_text, units)
|
| 193 |
|
| 194 |
# Generate questions
|
| 195 |
-
progress(0.4, desc="Generating questions...")
|
| 196 |
q_prompt = build_question_prompt(subject, selected_syllabus, numA, numB, numC, exam_mode)
|
| 197 |
q_raw = model_q.invoke([HumanMessage(content=q_prompt)]).content
|
| 198 |
q_json = sanitize_json(q_raw)
|
| 199 |
|
| 200 |
# Generate answers
|
| 201 |
-
|
| 202 |
-
a_prompt = f"Generate answers for: {json.dumps(q_json)}"
|
| 203 |
a_raw = model_a.invoke([HumanMessage(content=a_prompt)]).content
|
| 204 |
a_json = sanitize_json(a_raw)
|
| 205 |
|
| 206 |
# Create files
|
| 207 |
-
progress(0.9, desc="Creating documents...")
|
| 208 |
qp_file = f"{code}_QuestionPaper.docx"
|
| 209 |
ak_file = f"{code}_AnswerKey.docx"
|
| 210 |
|
|
@@ -217,18 +225,13 @@ def generate_exam(exam_mode, subject, code, units, numA, numB, numC, syllabus_fi
|
|
| 217 |
zipf.write(qp_file)
|
| 218 |
zipf.write(ak_file)
|
| 219 |
|
| 220 |
-
progress(1.0, desc="Complete!")
|
| 221 |
return zip_file, f"✅ Successfully generated exam package for {subject}!"
|
| 222 |
|
| 223 |
except Exception as e:
|
| 224 |
return None, f"❌ Error: {str(e)}"
|
| 225 |
|
| 226 |
-
#
|
| 227 |
-
|
| 228 |
-
# Create Gradio interface
|
| 229 |
-
demo = gr.Blocks(theme=gr.themes.Soft())
|
| 230 |
-
|
| 231 |
-
with demo:
|
| 232 |
gr.HTML("""
|
| 233 |
<style>
|
| 234 |
.gradio-container {
|
|
@@ -250,10 +253,8 @@ with demo:
|
|
| 250 |
border-left: 4px solid #667eea;
|
| 251 |
}
|
| 252 |
</style>
|
| 253 |
-
""")
|
| 254 |
-
gr.HTML("""
|
| 255 |
<div class="header-text">
|
| 256 |
-
<h1
|
| 257 |
<p>AI-Powered Question Paper & Answer Key Generator</p>
|
| 258 |
<p style="font-size: 0.9rem; opacity: 0.9;">Powered by Advanced LLM Technology | Industry-Standard Framework</p>
|
| 259 |
</div>
|
|
@@ -281,7 +282,7 @@ with demo:
|
|
| 281 |
gr.Markdown("### 📁 Syllabus Upload")
|
| 282 |
syllabus_file = gr.File(label="Upload Syllabus", file_types=[".pdf", ".docx", ".txt"])
|
| 283 |
|
| 284 |
-
gr.
|
| 285 |
<div class="feature-box">
|
| 286 |
<h4>✨ Key Features</h4>
|
| 287 |
<ul>
|
|
@@ -309,7 +310,7 @@ with demo:
|
|
| 309 |
gr.Markdown("""
|
| 310 |
---
|
| 311 |
<center>
|
| 312 |
-
<p style="color: #666;">Developed with ❤️
|
| 313 |
</center>
|
| 314 |
""")
|
| 315 |
|
|
|
|
| 135 |
"""
|
| 136 |
return apply_MAANGO_BIG15_framework(base_prompt)
|
| 137 |
|
| 138 |
+
def build_answer_prompt(questions_json):
|
| 139 |
+
return f"""
|
| 140 |
+
Generate answers in VALID JSON format for these questions:
|
| 141 |
+
|
| 142 |
+
{{
|
| 143 |
+
"partA": [{{"question_text": "string", "answer": "detailed answer", "marks": 2}}],
|
| 144 |
+
"partB": [{{"question_text": "string", "answer": "model answer", "marks": 10}}],
|
| 145 |
+
"partC": [{{"question_text": "string", "answer": "comprehensive answer", "marks": 15}}]
|
| 146 |
+
}}
|
| 147 |
+
|
| 148 |
+
Questions: {json.dumps(questions_json)}
|
| 149 |
+
Return ONLY pure JSON.
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
def create_question_paper(code, name, partA, partB, partC, output_path):
|
| 153 |
doc = Document()
|
| 154 |
doc.add_heading("SNS College of Technology", level=1)
|
|
|
|
| 191 |
|
| 192 |
doc.save(output_path)
|
| 193 |
|
| 194 |
+
def generate_exam(exam_mode, subject, code, units, numA, numB, numC, syllabus_file):
|
| 195 |
try:
|
|
|
|
|
|
|
| 196 |
# Initialize models
|
| 197 |
model_q, model_a, model_t, serp = init_models()
|
| 198 |
if not model_q:
|
| 199 |
return None, "❌ API Keys not configured. Please set GROQ_API_KEY and SERPAPI_API_KEY in Hugging Face Spaces secrets."
|
| 200 |
|
| 201 |
# Extract syllabus
|
|
|
|
| 202 |
syllabus_text = extract_text(syllabus_file.name)
|
| 203 |
selected_syllabus = extract_units(syllabus_text, units)
|
| 204 |
|
| 205 |
# Generate questions
|
|
|
|
| 206 |
q_prompt = build_question_prompt(subject, selected_syllabus, numA, numB, numC, exam_mode)
|
| 207 |
q_raw = model_q.invoke([HumanMessage(content=q_prompt)]).content
|
| 208 |
q_json = sanitize_json(q_raw)
|
| 209 |
|
| 210 |
# Generate answers
|
| 211 |
+
a_prompt = build_answer_prompt(q_json)
|
|
|
|
| 212 |
a_raw = model_a.invoke([HumanMessage(content=a_prompt)]).content
|
| 213 |
a_json = sanitize_json(a_raw)
|
| 214 |
|
| 215 |
# Create files
|
|
|
|
| 216 |
qp_file = f"{code}_QuestionPaper.docx"
|
| 217 |
ak_file = f"{code}_AnswerKey.docx"
|
| 218 |
|
|
|
|
| 225 |
zipf.write(qp_file)
|
| 226 |
zipf.write(ak_file)
|
| 227 |
|
|
|
|
| 228 |
return zip_file, f"✅ Successfully generated exam package for {subject}!"
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
return None, f"❌ Error: {str(e)}"
|
| 232 |
|
| 233 |
+
# Create Gradio interface
|
| 234 |
+
with gr.Blocks(title="MAANGO BIG15 Exam Generator") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
gr.HTML("""
|
| 236 |
<style>
|
| 237 |
.gradio-container {
|
|
|
|
| 253 |
border-left: 4px solid #667eea;
|
| 254 |
}
|
| 255 |
</style>
|
|
|
|
|
|
|
| 256 |
<div class="header-text">
|
| 257 |
+
<h1>🎓 MAANGO BIG15 Exam Generator</h1>
|
| 258 |
<p>AI-Powered Question Paper & Answer Key Generator</p>
|
| 259 |
<p style="font-size: 0.9rem; opacity: 0.9;">Powered by Advanced LLM Technology | Industry-Standard Framework</p>
|
| 260 |
</div>
|
|
|
|
| 282 |
gr.Markdown("### 📁 Syllabus Upload")
|
| 283 |
syllabus_file = gr.File(label="Upload Syllabus", file_types=[".pdf", ".docx", ".txt"])
|
| 284 |
|
| 285 |
+
gr.HTML("""
|
| 286 |
<div class="feature-box">
|
| 287 |
<h4>✨ Key Features</h4>
|
| 288 |
<ul>
|
|
|
|
| 310 |
gr.Markdown("""
|
| 311 |
---
|
| 312 |
<center>
|
| 313 |
+
<p style="color: #666;">Developed with ❤️ using MAANGO BIG15 Framework | © 2024</p>
|
| 314 |
</center>
|
| 315 |
""")
|
| 316 |
|