Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ from langchain_core.messages import HumanMessage
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
| 16 |
-
# Initialize models (will use environment variables)
|
| 17 |
def init_models():
|
| 18 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 19 |
serp_key = os.getenv("SERPAPI_API_KEY")
|
|
@@ -23,12 +22,11 @@ def init_models():
|
|
| 23 |
|
| 24 |
model_question_gen = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_key)
|
| 25 |
model_answer_gen = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_key)
|
| 26 |
-
model_trend_analyzer = ChatGroq(model="
|
| 27 |
serp = SerpAPIWrapper(serpapi_api_key=serp_key)
|
| 28 |
|
| 29 |
return model_question_gen, model_answer_gen, model_trend_analyzer, serp
|
| 30 |
|
| 31 |
-
# Utility functions
|
| 32 |
def extract_docx(path):
|
| 33 |
d = docx.Document(path)
|
| 34 |
return "\n".join(p.text for p in d.paragraphs if p.text.strip())
|
|
@@ -193,33 +191,27 @@ def create_answer_key(code, name, answers, 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 |
|
| 219 |
create_question_paper(code, subject, q_json["partA"], q_json["partB"], q_json["partC"], qp_file)
|
| 220 |
create_answer_key(code, subject, a_json, ak_file)
|
| 221 |
|
| 222 |
-
# Create zip
|
| 223 |
zip_file = f"{code}_ExamPackage.zip"
|
| 224 |
with zipfile.ZipFile(zip_file, 'w') as zipf:
|
| 225 |
zipf.write(qp_file)
|
|
@@ -230,39 +222,110 @@ def generate_exam(exam_mode, subject, code, units, numA, numB, numC, syllabus_fi
|
|
| 230 |
except Exception as e:
|
| 231 |
return None, f"β Error: {str(e)}"
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
gr.HTML("""
|
| 236 |
-
<
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
}
|
| 241 |
-
.header-text {
|
| 242 |
-
text-align: center;
|
| 243 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 244 |
-
color: white;
|
| 245 |
-
padding: 2rem;
|
| 246 |
-
border-radius: 10px;
|
| 247 |
-
margin-bottom: 2rem;
|
| 248 |
-
}
|
| 249 |
-
.feature-box {
|
| 250 |
-
background: #f8f9fa;
|
| 251 |
-
padding: 1rem;
|
| 252 |
-
border-radius: 8px;
|
| 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>
|
| 261 |
""")
|
| 262 |
|
| 263 |
with gr.Row():
|
| 264 |
with gr.Column(scale=1):
|
| 265 |
-
gr.
|
| 266 |
exam_mode = gr.Dropdown(
|
| 267 |
choices=["Continuous Assessment (CA)", "End Semester Exam (ESE)", "GATE Style Internal Exam"],
|
| 268 |
label="Exam Mode",
|
|
@@ -272,26 +335,26 @@ with gr.Blocks(title="MAANGO BIG15 Exam Generator") as demo:
|
|
| 272 |
code = gr.Textbox(label="Subject Code", placeholder="e.g., CS301")
|
| 273 |
units = gr.Textbox(label="Units Range", value="1-5", placeholder="e.g., 1-3 or 1,3,5")
|
| 274 |
|
| 275 |
-
gr.
|
| 276 |
with gr.Row():
|
| 277 |
numA = gr.Number(label="Part A (Short)", value=10, precision=0)
|
| 278 |
numB = gr.Number(label="Part B (Descriptive)", value=5, precision=0)
|
| 279 |
numC = gr.Number(label="Part C (Case Study)", value=1, precision=0)
|
| 280 |
|
| 281 |
with gr.Column(scale=1):
|
| 282 |
-
gr.
|
| 283 |
syllabus_file = gr.File(label="Upload Syllabus", file_types=[".pdf", ".docx", ".txt"])
|
| 284 |
|
| 285 |
gr.HTML("""
|
| 286 |
-
<div class="feature-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
</div>
|
| 296 |
""")
|
| 297 |
|
|
@@ -307,11 +370,10 @@ with gr.Blocks(title="MAANGO BIG15 Exam Generator") as demo:
|
|
| 307 |
outputs=[output_file, status_msg]
|
| 308 |
)
|
| 309 |
|
| 310 |
-
gr.
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
</center>
|
| 315 |
""")
|
| 316 |
|
| 317 |
if __name__ == "__main__":
|
|
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
|
|
|
| 16 |
def init_models():
|
| 17 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 18 |
serp_key = os.getenv("SERPAPI_API_KEY")
|
|
|
|
| 22 |
|
| 23 |
model_question_gen = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_key)
|
| 24 |
model_answer_gen = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_key)
|
| 25 |
+
model_trend_analyzer = ChatGroq(model="groq/compound", api_key=groq_key)
|
| 26 |
serp = SerpAPIWrapper(serpapi_api_key=serp_key)
|
| 27 |
|
| 28 |
return model_question_gen, model_answer_gen, model_trend_analyzer, serp
|
| 29 |
|
|
|
|
| 30 |
def extract_docx(path):
|
| 31 |
d = docx.Document(path)
|
| 32 |
return "\n".join(p.text for p in d.paragraphs if p.text.strip())
|
|
|
|
| 191 |
|
| 192 |
def generate_exam(exam_mode, subject, code, units, numA, numB, numC, syllabus_file):
|
| 193 |
try:
|
|
|
|
| 194 |
model_q, model_a, model_t, serp = init_models()
|
| 195 |
if not model_q:
|
| 196 |
return None, "β API Keys not configured. Please set GROQ_API_KEY and SERPAPI_API_KEY in Hugging Face Spaces secrets."
|
| 197 |
|
|
|
|
| 198 |
syllabus_text = extract_text(syllabus_file.name)
|
| 199 |
selected_syllabus = extract_units(syllabus_text, units)
|
| 200 |
|
|
|
|
| 201 |
q_prompt = build_question_prompt(subject, selected_syllabus, numA, numB, numC, exam_mode)
|
| 202 |
q_raw = model_q.invoke([HumanMessage(content=q_prompt)]).content
|
| 203 |
q_json = sanitize_json(q_raw)
|
| 204 |
|
|
|
|
| 205 |
a_prompt = build_answer_prompt(q_json)
|
| 206 |
a_raw = model_a.invoke([HumanMessage(content=a_prompt)]).content
|
| 207 |
a_json = sanitize_json(a_raw)
|
| 208 |
|
|
|
|
| 209 |
qp_file = f"{code}_QuestionPaper.docx"
|
| 210 |
ak_file = f"{code}_AnswerKey.docx"
|
| 211 |
|
| 212 |
create_question_paper(code, subject, q_json["partA"], q_json["partB"], q_json["partC"], qp_file)
|
| 213 |
create_answer_key(code, subject, a_json, ak_file)
|
| 214 |
|
|
|
|
| 215 |
zip_file = f"{code}_ExamPackage.zip"
|
| 216 |
with zipfile.ZipFile(zip_file, 'w') as zipf:
|
| 217 |
zipf.write(qp_file)
|
|
|
|
| 222 |
except Exception as e:
|
| 223 |
return None, f"β Error: {str(e)}"
|
| 224 |
|
| 225 |
+
css = """
|
| 226 |
+
* {
|
| 227 |
+
box-sizing: border-box;
|
| 228 |
+
}
|
| 229 |
+
body, .gradio-container {
|
| 230 |
+
margin: 0 !important;
|
| 231 |
+
padding: 0 !important;
|
| 232 |
+
width: 100% !important;
|
| 233 |
+
max-width: 100% !important;
|
| 234 |
+
}
|
| 235 |
+
.contain {
|
| 236 |
+
max-width: 100% !important;
|
| 237 |
+
width: 100% !important;
|
| 238 |
+
padding: 0 !important;
|
| 239 |
+
margin: 0 !important;
|
| 240 |
+
}
|
| 241 |
+
.main {
|
| 242 |
+
width: 100% !important;
|
| 243 |
+
max-width: 100% !important;
|
| 244 |
+
padding: 20px !important;
|
| 245 |
+
}
|
| 246 |
+
#component-0 {
|
| 247 |
+
max-width: 100% !important;
|
| 248 |
+
width: 100% !important;
|
| 249 |
+
}
|
| 250 |
+
.header-gradient {
|
| 251 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 252 |
+
color: white;
|
| 253 |
+
padding: 40px 20px;
|
| 254 |
+
text-align: center;
|
| 255 |
+
border-radius: 15px;
|
| 256 |
+
margin-bottom: 30px;
|
| 257 |
+
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
|
| 258 |
+
}
|
| 259 |
+
.header-gradient h1 {
|
| 260 |
+
font-size: 2.5rem;
|
| 261 |
+
margin: 0 0 10px 0;
|
| 262 |
+
font-weight: 700;
|
| 263 |
+
}
|
| 264 |
+
.header-gradient p {
|
| 265 |
+
margin: 5px 0;
|
| 266 |
+
font-size: 1.1rem;
|
| 267 |
+
opacity: 0.95;
|
| 268 |
+
}
|
| 269 |
+
.feature-card {
|
| 270 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 271 |
+
padding: 25px;
|
| 272 |
+
border-radius: 12px;
|
| 273 |
+
border-left: 5px solid #667eea;
|
| 274 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 275 |
+
height: 100%;
|
| 276 |
+
}
|
| 277 |
+
.feature-card h4 {
|
| 278 |
+
color: #667eea;
|
| 279 |
+
margin-top: 0;
|
| 280 |
+
font-size: 1.3rem;
|
| 281 |
+
}
|
| 282 |
+
.feature-card ul {
|
| 283 |
+
list-style: none;
|
| 284 |
+
padding: 0;
|
| 285 |
+
margin: 15px 0 0 0;
|
| 286 |
+
}
|
| 287 |
+
.feature-card li {
|
| 288 |
+
padding: 8px 0;
|
| 289 |
+
font-size: 1rem;
|
| 290 |
+
color: #2d3748;
|
| 291 |
+
}
|
| 292 |
+
.section-title {
|
| 293 |
+
color: #667eea;
|
| 294 |
+
font-size: 1.4rem;
|
| 295 |
+
font-weight: 600;
|
| 296 |
+
margin: 20px 0 15px 0;
|
| 297 |
+
padding-bottom: 10px;
|
| 298 |
+
border-bottom: 2px solid #667eea;
|
| 299 |
+
}
|
| 300 |
+
footer {
|
| 301 |
+
text-align: center;
|
| 302 |
+
padding: 20px;
|
| 303 |
+
color: #718096;
|
| 304 |
+
margin-top: 40px;
|
| 305 |
+
border-top: 1px solid #e2e8f0;
|
| 306 |
+
}
|
| 307 |
+
@media (max-width: 768px) {
|
| 308 |
+
.header-gradient h1 {
|
| 309 |
+
font-size: 1.8rem;
|
| 310 |
+
}
|
| 311 |
+
.header-gradient p {
|
| 312 |
+
font-size: 0.95rem;
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
"""
|
| 316 |
+
|
| 317 |
+
with gr.Blocks(css=css) as demo:
|
| 318 |
gr.HTML("""
|
| 319 |
+
<div class="header-gradient">
|
| 320 |
+
<h1>SNS Tech - Q&A Agent x Codeboosters Tech</h1>
|
| 321 |
+
<p><strong>AI-Powered Question Paper & Answer Key Generator</strong></p>
|
| 322 |
+
<p>Powered by Advanced LLM Technology | Industry-Standard Framework | Developed by Codeboosters Tech Team</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
</div>
|
| 324 |
""")
|
| 325 |
|
| 326 |
with gr.Row():
|
| 327 |
with gr.Column(scale=1):
|
| 328 |
+
gr.HTML('<h3 class="section-title">π Exam Configuration</h3>')
|
| 329 |
exam_mode = gr.Dropdown(
|
| 330 |
choices=["Continuous Assessment (CA)", "End Semester Exam (ESE)", "GATE Style Internal Exam"],
|
| 331 |
label="Exam Mode",
|
|
|
|
| 335 |
code = gr.Textbox(label="Subject Code", placeholder="e.g., CS301")
|
| 336 |
units = gr.Textbox(label="Units Range", value="1-5", placeholder="e.g., 1-3 or 1,3,5")
|
| 337 |
|
| 338 |
+
gr.HTML('<h3 class="section-title">π Question Distribution</h3>')
|
| 339 |
with gr.Row():
|
| 340 |
numA = gr.Number(label="Part A (Short)", value=10, precision=0)
|
| 341 |
numB = gr.Number(label="Part B (Descriptive)", value=5, precision=0)
|
| 342 |
numC = gr.Number(label="Part C (Case Study)", value=1, precision=0)
|
| 343 |
|
| 344 |
with gr.Column(scale=1):
|
| 345 |
+
gr.HTML('<h3 class="section-title">π Syllabus Upload</h3>')
|
| 346 |
syllabus_file = gr.File(label="Upload Syllabus", file_types=[".pdf", ".docx", ".txt"])
|
| 347 |
|
| 348 |
gr.HTML("""
|
| 349 |
+
<div class="feature-card">
|
| 350 |
+
<h4>β¨ Key Features</h4>
|
| 351 |
+
<ul>
|
| 352 |
+
<li>π― Bloom's Taxonomy Alignment</li>
|
| 353 |
+
<li>π’ Industry Tag Integration (TCS/Infosys/Wipro)</li>
|
| 354 |
+
<li>π Balanced Unit Coverage</li>
|
| 355 |
+
<li>π GATE-Style Question Design</li>
|
| 356 |
+
<li>π Automatic Answer Key Generation</li>
|
| 357 |
+
</ul>
|
| 358 |
</div>
|
| 359 |
""")
|
| 360 |
|
|
|
|
| 370 |
outputs=[output_file, status_msg]
|
| 371 |
)
|
| 372 |
|
| 373 |
+
gr.HTML("""
|
| 374 |
+
<footer>
|
| 375 |
+
<p>Developed with β€οΈ by Veerakumar C B | Β© 2024</p>
|
| 376 |
+
</footer>
|
|
|
|
| 377 |
""")
|
| 378 |
|
| 379 |
if __name__ == "__main__":
|