Redfire-1234 commited on
Commit
4ecc464
·
verified ·
1 Parent(s): 998d25f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -976,6 +976,61 @@ def rag_search(query, subject, k=5):
976
 
977
  return "\n\n".join(results)
978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979
  # ------------------------------
980
  # Chapter Detection (Using Actual Chapter Names)
981
  # ------------------------------
@@ -1502,8 +1557,22 @@ def generate():
1502
  if subject not in SUBJECTS:
1503
  return jsonify({"error": "Invalid subject"}), 400
1504
 
1505
- print(f"\n🔍 Searching {subject} for: {topic}")
 
 
 
 
 
 
 
 
 
 
 
 
 
1506
 
 
1507
  context = rag_search(topic, subject, k=5)
1508
 
1509
  if not context or len(context.strip()) < 50:
@@ -1511,6 +1580,7 @@ def generate():
1511
 
1512
  print(f"✓ Context found ({len(context)} chars)")
1513
 
 
1514
  mcqs, chapter = generate_mcqs(context, topic, subject)
1515
 
1516
  # Check if there was a subject mismatch
 
976
 
977
  return "\n\n".join(results)
978
 
979
+ # ------------------------------
980
+ # Topic Validation (Check if topic belongs to subject)
981
+ # ------------------------------
982
+ def validate_topic_subject(topic, subject):
983
+ """
984
+ Validate if the topic belongs to the selected subject using LLM
985
+ Returns True if valid, False otherwise
986
+ """
987
+ if not groq_client:
988
+ return True # Skip validation if API not available
989
+
990
+ validation_prompt = f"""You are a Class 12 PCB subject expert. Determine if the following topic belongs to {subject.title()}.
991
+
992
+ Topic: "{topic}"
993
+ Subject: {subject.title()}
994
+
995
+ Class 12 {subject.title()} covers:
996
+ {"- Reproduction, Genetics, Evolution, Plant Physiology, Human Systems, Ecology, Biotechnology" if subject == "biology" else ""}
997
+ {"- Solid State, Solutions, Thermodynamics, Electrochemistry, Organic Chemistry, Coordination Compounds" if subject == "chemistry" else ""}
998
+ {"- Rotational Dynamics, Fluids, Thermodynamics, Waves, Optics, Electromagnetism, Modern Physics, Semiconductors" if subject == "physics" else ""}
999
+
1000
+ Answer ONLY with "YES" if the topic belongs to {subject.title()}, or "NO" if it belongs to a different subject.
1001
+
1002
+ Answer:"""
1003
+
1004
+ try:
1005
+ response = groq_client.chat.completions.create(
1006
+ messages=[
1007
+ {
1008
+ "role": "system",
1009
+ "content": f"You are an expert at identifying which subject a topic belongs to. Answer only YES or NO."
1010
+ },
1011
+ {
1012
+ "role": "user",
1013
+ "content": validation_prompt
1014
+ }
1015
+ ],
1016
+ model="llama-3.3-70b-versatile",
1017
+ temperature=0.1,
1018
+ max_tokens=10
1019
+ )
1020
+
1021
+ result = response.choices[0].message.content.strip().upper()
1022
+
1023
+ if "YES" in result:
1024
+ print(f"✓ Topic '{topic}' validated for {subject}")
1025
+ return True
1026
+ else:
1027
+ print(f"❌ Topic '{topic}' does NOT belong to {subject}")
1028
+ return False
1029
+
1030
+ except Exception as e:
1031
+ print(f"⚠️ Validation failed: {e}")
1032
+ return True # Allow on error to avoid blocking
1033
+
1034
  # ------------------------------
1035
  # Chapter Detection (Using Actual Chapter Names)
1036
  # ------------------------------
 
1557
  if subject not in SUBJECTS:
1558
  return jsonify({"error": "Invalid subject"}), 400
1559
 
1560
+ print(f"\n🔍 Validating topic for {subject}...")
1561
+
1562
+ # STEP 1: Validate if topic belongs to subject (BEFORE RAG search)
1563
+ if not validate_topic_subject(topic, subject):
1564
+ subject_names = {
1565
+ "biology": "Biology",
1566
+ "chemistry": "Chemistry",
1567
+ "physics": "Physics"
1568
+ }
1569
+ error_msg = f"The topic '{topic}' does not appear to be related to {subject_names[subject]}.\n\nPlease either:\n• Enter a {subject_names[subject]}-related topic, or\n• Select the correct subject for this topic"
1570
+ return jsonify({"error": error_msg}), 400
1571
+
1572
+ print(f"✓ Topic validated for {subject}")
1573
+ print(f"🔍 Searching {subject} for: {topic}")
1574
 
1575
+ # STEP 2: RAG search
1576
  context = rag_search(topic, subject, k=5)
1577
 
1578
  if not context or len(context.strip()) < 50:
 
1580
 
1581
  print(f"✓ Context found ({len(context)} chars)")
1582
 
1583
+ # STEP 3: Generate MCQs
1584
  mcqs, chapter = generate_mcqs(context, topic, subject)
1585
 
1586
  # Check if there was a subject mismatch