Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131 unmodified line15161718192039 unmodified lines606162636465666768697071727374757677787980818283848586878889909192935 unmodified lines991001011021031041051061071081091102 unmodified lines1131141151161171181191201211221232 unmodified lines1261271281291301311321331341351 unmodified line13713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519624 unmodified lines22122222322422522622722822987 unmodified lines3173183193203213223233246 unmodified lines33133233333433533633720 unmodified lines35835936036136236336436536636736836937037143 unmodified lines41541641741841942042122 unmodified lines4444454464474484494504514524534544554564574584594604614624634644654664674684694704712 unmodified lines4744754764774784794804814824834844854864874884894904914924934944954964977 unmodified lines505506507508509import pickleimport faissfrom flask import Flask, request, jsonify, render_template_stringfrom sentence_transformers import SentenceTransformerfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom huggingface_hub import hf_hub_downloadimport torchimport osfrom functools import lru_cacheimport hashlibapp = Flask(__name__)1 unmodified lineprint("Loading models and data...")print("=" * 50)# ------------------------------# Load embedding model (CPU)# ------------------------------39 unmodified linesprint(f"β Chemistry: {len(SUBJECTS['chemistry']['chunks'])} chunks loaded")print(f"β Physics: {len(SUBJECTS['physics']['chunks'])} chunks loaded")# ------------------------------# Load LLM model (CPU) with optimizations# ------------------------------model_name = "Qwen/Qwen2.5-3B-Instruct"print(f"Loading LLM: {model_name}")tokenizer = AutoTokenizer.from_pretrained(model_name)device = "cpu"# OPTIMIZATION: Load model with better dtype for CPUmodel = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float32,low_cpu_mem_usage=True # Optimization: Better memory management).to(device)# OPTIMIZATION: Set model to eval mode and optimize for inferencemodel.eval()if hasattr(torch, 'set_num_threads'):torch.set_num_threads(4) # Optimization: Use multiple CPU threadsprint(f"β LLM loaded on {device}")print("=" * 50)print("All models loaded successfully!")print("=" * 50)# ------------------------------# OPTIMIZATION: Add caching for MCQ generation# ------------------------------MCQ_CACHE = {}MAX_CACHE_SIZE = 1005 unmodified linesdef cache_mcq(key, mcqs):"""Cache generated MCQs with size limit"""if len(MCQ_CACHE) >= MAX_CACHE_SIZE:# Remove oldest entryMCQ_CACHE.pop(next(iter(MCQ_CACHE)))MCQ_CACHE[key] = mcqs# ------------------------------# RAG Search in specific subject (optimized)# ------------------------------def rag_search(query, subject, k=5):if subject not in SUBJECTS:2 unmodified lineschunks = SUBJECTS[subject]["chunks"]index = SUBJECTS[subject]["index"]# OPTIMIZATION: Encode query (already fast with sentence-transformers)q_emb = embed_model.encode([query], show_progress_bar=False).astype("float32")D, I = index.search(q_emb, k)# Get the actual chunksresults = []for idx in I[0]:if idx < len(chunks):2 unmodified linesreturn "\n\n".join(results)# ------------------------------# OPTIMIZED MCQ Generation with reduced tokens# ------------------------------def generate_mcqs(context, topic, subject):# OPTIMIZATION: Check cache firstcontext_hash = hashlib.md5(context.encode()).hexdigest()[:8]cache_key = get_cache_key(topic, subject, context_hash)1 unmodified lineprint("β Using cached MCQs")return MCQ_CACHE[cache_key]# OPTIMIZATION: Shortened prompt for faster generationprompt = f"""You are a Class-12 {subject.title()} teacher creating MCQs.Topic: "{topic}"Context:{context}Generate exactly 5 MCQs in this format:Q1. [Question]A) [Option]B) [Option]C) [Option]D) [Option]Correct Answer: [Letter] - [Reason]Rules: Make correct answer from context, realistic distractors.Generate 5 MCQs:"""# OPTIMIZATION: Reduced max_length for faster tokenizationinputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1536).to(device)# OPTIMIZATION: Use torch.no_grad() for inference (saves memory)with torch.no_grad():# OPTIMIZATION: Reduced max_new_tokens from 900 to 600 (sufficient for 5 MCQs)# OPTIMIZATION: Reduced temperature from 0.15 to 0.1 (faster, more deterministic)# OPTIMIZATION: Added num_beams=1 (greedy decoding, faster than sampling)outputs = model.generate(**inputs,max_new_tokens=600, # Reduced from 900temperature=0.1, # Reduced from 0.15top_p=0.85, # Slightly adjusteddo_sample=True,repetition_penalty=1.15,pad_token_id=tokenizer.eos_token_id # Optimization: Explicit pad token)result = tokenizer.decode(outputs[0], skip_special_tokens=True)# Extract only the generated MCQsif "Generate 5 MCQs:" in result:result = result.split("Generate 5 MCQs:")[-1].strip()# OPTIMIZATION: Cache the resultcache_mcq(cache_key, result)return resultdef verify_and_correct_answers(mcqs_text, context):"""This function is kept for future enhancements"""return mcqs_text# ------------------------------# HTML UI (with improved loading message)# ------------------------------HTML_TEMPLATE = """<!DOCTYPE html>24 unmodified lines}.header h1 { font-size: 2.5em; margin-bottom: 10px; }.content { padding: 40px; }.form-group {margin-bottom: 25px;}label {display: block;font-weight: 600;87 unmodified lines.bio { background: #d4edda; color: #155724; }.chem { background: #d1ecf1; color: #0c5460; }.phy { background: #f8d7da; color: #721c24; }.optimization-badge {background: #28a745;color: white;padding: 5px 12px;border-radius: 15px;6 unmodified lines<div class="container"><div class="header"><h1>π Class 12 PCB MCQ Generator</h1><p style="font-size: 1.1em; margin-bottom: 15px;">Generate practice MCQs from your textbooks <span class="optimization-badge">β‘ Optimized</span></p><div><span class="subject-tag bio">Biology</span><span class="subject-tag chem">Chemistry</span>20 unmodified lines<div class="loading" id="loading"><div class="spinner"></div><p style="color: #666; font-size: 16px;">Generating MCQs... This may take 20-40 seconds</p><p style="color: #999; font-size: 13px; margin-top: 10px;">β‘ Optimized for faster generation</p></div><div class="result" id="result"><h3>π Generated MCQs:</h3><div style="background: #fff3cd; padding: 12px; border-radius: 6px; margin-bottom: 15px; color: #856404; font-size: 14px;">β οΈ <strong>Note:</strong> AI-generated answers may occasionally be incorrect. Please verify answers using your textbook.</div><div class="mcq-content" id="mcqContent"></div></div>43 unmodified lines}}// Allow Enter key to submitdocument.getElementById('topic').addEventListener('keypress', function(e) {if (e.key === 'Enter') {generateMCQs();22 unmodified linesreturn jsonify({"error": "Topic is required"}), 400if subject not in SUBJECTS:return jsonify({"error": "Invalid subject. Choose biology, chemistry, or physics."}), 400print(f"\nπ Searching {subject} for topic: {topic}")# Retrieve context from RAGcontext = rag_search(topic, subject, k=5)if not context or len(context.strip()) < 50:return jsonify({"error": f"No relevant content found in {subject} for topic: {topic}"}), 404print(f"β Found context ({len(context)} chars)")# Generate MCQs (now with caching)print("π€ Generating MCQs...")mcqs = generate_mcqs(context, topic, subject)print("β MCQs generated successfully")return jsonify({"mcqs": mcqs, "subject": subject})except Exception as e:print(f"β Error in /generate: {e}")import tracebacktraceback.print_exc()return jsonify({"error": str(e)}), 5002 unmodified linesdef health():return jsonify({"status": "healthy","subjects": {"biology": len(SUBJECTS["biology"]["chunks"]),"chemistry": len(SUBJECTS["chemistry"]["chunks"]),"physics": len(SUBJECTS["physics"]["chunks"])},"cache_size": len(MCQ_CACHE)})# OPTIMIZATION: Add cache stats endpoint@app.route("/cache/stats")def cache_stats():return jsonify({"cached_topics": len(MCQ_CACHE),"max_cache_size": MAX_CACHE_SIZE,"cache_keys": list(MCQ_CACHE.keys())})# OPTIMIZATION: Add cache clear endpoint (optional)@app.route("/cache/clear", methods=["POST"])def clear_cache():MCQ_CACHE.clear()7 unmodified linesprint(f"\nπ Starting Flask on 0.0.0.0:{port}\n")app.run(host="0.0.0.0", port=port, debug=False)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185191 unmodified line52152252352452552652752852953053153253353453553653753839 unmodified lines5785795805815825835845855865875885895 unmodified lines5955965975985996006016026036046052 unmodified lines6086096106116126136146156162 unmodified lines6196206216226236246256266276281 unmodified line63063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072124 unmodified lines74674774874975075175287 unmodified lines8408418428438448458468476 unmodified lines85485585685785885986086186286320 unmodified lines88488588688788888989089189289389489589689743 unmodified lines94194294394494594622 unmodified lines9699709719729739749759769779789799809819829839849859869879889899909912 unmodified lines99499599699799899910001001100210037 unmodified lines101110121013# import pickle# import faiss# from flask import Flask, request, jsonify, render_template_string# from sentence_transformers import SentenceTransformer# from transformers import AutoTokenizer, AutoModelForCausalLM# from huggingface_hub import hf_hub_download# import torch# import os# from functools import lru_cache# import hashlib# app = Flask(__name__)# print("=" * 50)# print("Loading models and data...")# print("=" * 50)# # ------------------------------# # Load embedding model (CPU)# # ------------------------------# embed_model = SentenceTransformer("all-MiniLM-L6-v2")# print("β Embedding model loaded")# # ------------------------------# # Download files from Hugging Face# # ------------------------------# REPO_ID = "Redfire-1234/pcb_tutor"# print("Downloading subject files from Hugging Face...")# # Download Biology files# bio_chunks_path = hf_hub_download(repo_id=REPO_ID, filename="bio_chunks.pkl", repo_type="model")# faiss_bio_path = hf_hub_download(repo_id=REPO_ID, filename="faiss_bio.bin", repo_type="model")# # Download Chemistry files# chem_chunks_path = hf_hub_download(repo_id=REPO_ID, filename="chem_chunks.pkl", repo_type="model")# faiss_chem_path = hf_hub_download(repo_id=REPO_ID, filename="faiss_chem.bin", repo_type="model")# # Download Physics files# phy_chunks_path = hf_hub_download(repo_id=REPO_ID, filename="phy_chunks.pkl", repo_type="model")# faiss_phy_path = hf_hub_download(repo_id=REPO_ID, filename="faiss_phy.bin", repo_type="model")# # Load all subjects into memory# SUBJECTS = {# "biology": {# "chunks": pickle.load(open(bio_chunks_path, "rb")),# "index": faiss.read_index(faiss_bio_path)# },# "chemistry": {# "chunks": pickle.load(open(chem_chunks_path, "rb")),# "index": faiss.read_index(faiss_chem_path)# },# "physics": {# "chunks": pickle.load(open(phy_chunks_path, "rb")),# "index": faiss.read_index(faiss_phy_path)# }# }# print(f"β Biology: {len(SUBJECTS['biology']['chunks'])} chunks loaded")# print(f"β Chemistry: {len(SUBJECTS['chemistry']['chunks'])} chunks loaded")# print(f"β Physics: {len(SUBJECTS['physics']['chunks'])} chunks loaded")# # ------------------------------# # Load LLM model (CPU) with optimizations# # ------------------------------# model_name = "Qwen/Qwen2.5-3B-Instruct"# print(f"Loading LLM: {model_name}")# tokenizer = AutoTokenizer.from_pretrained(model_name)# device = "cpu"# # OPTIMIZATION: Load model with better dtype for CPU# model = AutoModelForCausalLM.from_pretrained(# model_name,# torch_dtype=torch.float32,# low_cpu_mem_usage=True # Optimization: Better memory management# ).to(device)# # OPTIMIZATION: Set model to eval mode and optimize for inference# model.eval()# if hasattr(torch, 'set_num_threads'):# torch.set_num_threads(4) # Optimization: Use multiple CPU threads# print(f"β LLM loaded on {device}")# print("=" * 50)# print("All models loaded successfully!")# print("=" * 50)# # ------------------------------# # OPTIMIZATION: Add caching for MCQ generation# # ------------------------------# MCQ_CACHE = {}# MAX_CACHE_SIZE = 100# def get_cache_key(topic, subject, context_hash):# """Generate a unique cache key"""# return f"{subject}:{topic}:{context_hash}"# def cache_mcq(key, mcqs):# """Cache generated MCQs with size limit"""# if len(MCQ_CACHE) >= MAX_CACHE_SIZE:# # Remove oldest entry# MCQ_CACHE.pop(next(iter(MCQ_CACHE)))# MCQ_CACHE[key] = mcqs# # ------------------------------# # RAG Search in specific subject (optimized)# # ------------------------------# def rag_search(query, subject, k=5):# if subject not in SUBJECTS:# return None# chunks = SUBJECTS[subject]["chunks"]# index = SUBJECTS[subject]["index"]# # OPTIMIZATION: Encode query (already fast with sentence-transformers)# q_emb = embed_model.encode([query], show_progress_bar=False).astype("float32")# D, I = index.search(q_emb, k)# # Get the actual chunks# results = []# for idx in I[0]:# if idx < len(chunks):# results.append(chunks[idx])# return "\n\n".join(results)# # ------------------------------# # OPTIMIZED MCQ Generation with reduced tokens# # ------------------------------# def generate_mcqs(context, topic, subject):# # OPTIMIZATION: Check cache first# context_hash = hashlib.md5(context.encode()).hexdigest()[:8]# cache_key = get_cache_key(topic, subject, context_hash)# if cache_key in MCQ_CACHE:# print("β Using cached MCQs")# return MCQ_CACHE[cache_key]# # OPTIMIZATION: Shortened prompt for faster generation# prompt = f"""You are a Class-12 {subject.title()} teacher creating MCQs.# Topic: "{topic}"# Context:# {context}# Generate exactly 5 MCQs in this format:# Q1. [Question]# A) [Option]# B) [Option]# C) [Option]# D) [Option]# Correct Answer: [Letter] - [Reason]# Rules: Make correct answer from context, realistic distractors.# Generate 5 MCQs:"""# # OPTIMIZATION: Reduced max_length for faster tokenization# inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1536).to(device)# # OPTIMIZATION: Use torch.no_grad() for inference (saves memory)# with torch.no_grad():# # OPTIMIZATION: Reduced max_new_tokens from 900 to 600 (sufficient for 5 MCQs)# # OPTIMIZATION: Reduced temperature from 0.15 to 0.1 (faster, more deterministic)# # OPTIMIZATION: Added num_beams=1 (greedy decoding, faster than sampling)# outputs = model.generate(# **inputs,# max_new_tokens=600, # Reduced from 900# temperature=0.1, # Reduced from 0.15# top_p=0.85, # Slightly adjusted# do_sample=True,# repetition_penalty=1.15,# pad_token_id=tokenizer.eos_token_id # Optimization: Explicit pad token# )# result = tokenizer.decode(outputs[0], skip_special_tokens=True)# # Extract only the generated MCQs# if "Generate 5 MCQs:" in result:# result = result.split("Generate 5 MCQs:")[-1].strip()# # OPTIMIZATION: Cache the result# cache_mcq(cache_key, result)# return result# def verify_and_correct_answers(mcqs_text, context):# """# This function is kept for future enhancements# """# return mcqs_text# # ------------------------------# # HTML UI (with improved loading message)# # ------------------------------# HTML_TEMPLATE = """# <!DOCTYPE html># <html># <head># <title>Class 12 PCB MCQ Generator</title># <style># * { margin: 0; padding: 0; box-sizing: border-box; }# body {# font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;# background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);# min-height: 100vh;# padding: 20px;# }# .container {# max-width: 900px;# margin: 0 auto;# background: white;# border-radius: 20px;# box-shadow: 0 20px 60px rgba(0,0,0,0.3);# overflow: hidden;# }# .header {# background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);# color: white;# padding: 30px;# text-align: center;# }# .header h1 { font-size: 2.5em; margin-bottom: 10px; }# .content { padding: 40px; }# .form-group {# margin-bottom: 25px;# }# label {# display: block;# font-weight: 600;# margin-bottom: 10px;# color: #333;# font-size: 16px;# }# select, input {# width: 100%;# padding: 15px;# border: 2px solid #e0e0e0;# border-radius: 10px;# font-size: 16px;# font-family: inherit;# transition: border-color 0.3s;# }# select:focus, input:focus {# outline: none;# border-color: #667eea;# }# button {# width: 100%;# padding: 18px;# background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);# color: white;# border: none;# border-radius: 10px;# font-size: 18px;# font-weight: 600;# cursor: pointer;# transition: all 0.3s;# }# button:hover {# transform: translateY(-2px);# box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);# }# button:disabled {# background: #ccc;# cursor: not-allowed;# transform: none;# }# .result {# margin-top: 30px;# padding: 25px;# background: #f8f9fa;# border-radius: 10px;# border-left: 4px solid #667eea;# display: none;# }# .result.show { display: block; }# .result h3 {# color: #667eea;# margin-bottom: 20px;# font-size: 1.4em;# }# .mcq-content {# background: white;# padding: 25px;# border-radius: 8px;# white-space: pre-wrap;# line-height: 1.9;# font-size: 15px;# }# .loading {# text-align: center;# padding: 30px;# display: none;# }# .loading.show { display: block; }# .spinner {# border: 4px solid #f3f3f3;# border-top: 4px solid #667eea;# border-radius: 50%;# width: 50px;# height: 50px;# animation: spin 1s linear infinite;# margin: 0 auto 15px;# }# @keyframes spin {# 0% { transform: rotate(0deg); }# 100% { transform: rotate(360deg); }# }# .subject-tag {# display: inline-block;# padding: 5px 15px;# border-radius: 20px;# font-size: 13px;# font-weight: 600;# margin-right: 10px;# }# .bio { background: #d4edda; color: #155724; }# .chem { background: #d1ecf1; color: #0c5460; }# .phy { background: #f8d7da; color: #721c24; }# .optimization-badge {# background: #28a745;# color: white;# padding: 5px 12px;# border-radius: 15px;# font-size: 12px;# margin-left: 10px;# }# </style># </head># <body># <div class="container"># <div class="header"># <h1>π Class 12 PCB MCQ Generator</h1># <p style="font-size: 1.1em; margin-bottom: 15px;">Generate practice MCQs from your textbooks <span class="optimization-badge">β‘ Optimized</span></p># <div># <span class="subject-tag bio">Biology</span># <span class="subject-tag chem">Chemistry</span># <span class="subject-tag phy">Physics</span># </div># </div># <div class="content"># <div class="form-group"># <label for="subject">π Select Subject</label># <select id="subject"># <option value="biology">Biology</option># <option value="chemistry">Chemistry</option># <option value="physics">Physics</option># </select># </div># <div class="form-group"># <label for="topic">βοΈ Enter Topic</label># <input type="text" id="topic" placeholder="e.g., Mitochondria, Chemical Bonding, Newton's Laws"># </div># <button onclick="generateMCQs()">π Generate 5 MCQs</button># <div class="loading" id="loading"># <div class="spinner"></div># <p style="color: #666; font-size: 16px;">Generating MCQs... This may take 20-40 seconds</p># <p style="color: #999; font-size: 13px; margin-top: 10px;">β‘ Optimized for faster generation</p># </div># <div class="result" id="result"># <h3>π Generated MCQs:</h3># <div style="background: #fff3cd; padding: 12px; border-radius: 6px; margin-bottom: 15px; color: #856404; font-size: 14px;"># β οΈ <strong>Note:</strong> AI-generated answers may occasionally be incorrect. Please verify answers using your textbook.# </div># <div class="mcq-content" id="mcqContent"></div># </div># </div># </div># <script># async function generateMCQs() {# const subject = document.getElementById('subject').value;# const topic = document.getElementById('topic').value.trim();# if (!topic) {# alert('β οΈ Please enter a topic!');# return;# }# const loading = document.getElementById('loading');# const result = document.getElementById('result');# const btn = document.querySelector('button');# loading.classList.add('show');# result.classList.remove('show');# btn.disabled = true;# btn.textContent = 'β³ Generating...';# try {# const response = await fetch('/generate', {# method: 'POST',# headers: {'Content-Type': 'application/json'},# body: JSON.stringify({subject, topic})# });# const data = await response.json();# if (data.error) {# alert('β Error: ' + data.error);# return;# }# document.getElementById('mcqContent').textContent = data.mcqs;# result.classList.add('show');# } catch (error) {# alert('β Error: ' + error.message);# } finally {# loading.classList.remove('show');# btn.disabled = false;# btn.textContent = 'π Generate 5 MCQs';# }# }# // Allow Enter key to submit# document.getElementById('topic').addEventListener('keypress', function(e) {# if (e.key === 'Enter') {# generateMCQs();# }# });# </script># </body># </html># """# # ------------------------------# # Routes# # ------------------------------# @app.route("/")# def home():# return render_template_string(HTML_TEMPLATE)# @app.route("/generate", methods=["POST"])# def generate():# try:# data = request.json# subject = data.get("subject", "").lower()# topic = data.get("topic", "")# if not topic:# return jsonify({"error": "Topic is required"}), 400# if subject not in SUBJECTS:# return jsonify({"error": "Invalid subject. Choose biology, chemistry, or physics."}), 400# print(f"\nπ Searching {subject} for topic: {topic}")# # Retrieve context from RAG# context = rag_search(topic, subject, k=5)# if not context or len(context.strip()) < 50:# return jsonify({"error": f"No relevant content found in {subject} for topic: {topic}"}), 404# print(f"β Found context ({len(context)} chars)")# # Generate MCQs (now with caching)# print("π€ Generating MCQs...")# mcqs = generate_mcqs(context, topic, subject)# print("β MCQs generated successfully")# return jsonify({"mcqs": mcqs, "subject": subject})# except Exception as e:# print(f"β Error in /generate: {e}")# import traceback# traceback.print_exc()# return jsonify({"error": str(e)}), 500# @app.route("/health")# def health():# return jsonify({# "status": "healthy",# "subjects": {# "biology": len(SUBJECTS["biology"]["chunks"]),# "chemistry": len(SUBJECTS["chemistry"]["chunks"]),# "physics": len(SUBJECTS["physics"]["chunks"])# },# "cache_size": len(MCQ_CACHE)# })# # OPTIMIZATION: Add cache stats endpoint# @app.route("/cache/stats")# def cache_stats():# return jsonify({# "cached_topics": len(MCQ_CACHE),# "max_cache_size": MAX_CACHE_SIZE,# "cache_keys": list(MCQ_CACHE.keys())# })# # OPTIMIZATION: Add cache clear endpoint (optional)# @app.route("/cache/clear", methods=["POST"])# def clear_cache():# MCQ_CACHE.clear()# return jsonify({"status": "Cache cleared"})# # ------------------------------# # Run the App# # ------------------------------# if __name__ == "__main__":# port = int(os.environ.get("PORT", 7860))# print(f"\nπ Starting Flask on 0.0.0.0:{port}\n")# app.run(host="0.0.0.0", port=port, debug=False)import pickleimport faissfrom flask import Flask, request, jsonify, render_template_stringfrom sentence_transformers import SentenceTransformerfrom huggingface_hub import hf_hub_downloadimport hashlibimport reimport osfrom groq import Groqapp = Flask(__name__)1 unmodified lineprint("Loading models and data...")print("=" * 50)# ------------------------------# Initialize Groq API Client# ------------------------------# Get your free API key from: https://console.groq.com/keysGROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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") # Set this in your environmentif not GROQ_API_KEY:print("β οΈ WARNING: GROQ_API_KEY not set. Get one from https://console.groq.com/keys")print("Set it with: export GROQ_API_KEY='your-key-here'")groq_client = Groq(api_key=GROQ_API_KEY)print("β Groq API client initialized")# ------------------------------# Load embedding model (CPU)# ------------------------------39 unmodified linesprint(f"β Chemistry: {len(SUBJECTS['chemistry']['chunks'])} chunks loaded")print(f"β Physics: {len(SUBJECTS['physics']['chunks'])} chunks loaded")print("=" * 50)print("All models loaded successfully!")print("=" * 50)# ------------------------------# Caching for MCQ generation# ------------------------------MCQ_CACHE = {}MAX_CACHE_SIZE = 1005 unmodified linesdef cache_mcq(key, mcqs):"""Cache generated MCQs with size limit"""if len(MCQ_CACHE) >= MAX_CACHE_SIZE:MCQ_CACHE.pop(next(iter(MCQ_CACHE)))MCQ_CACHE[key] = mcqs# ------------------------------# RAG Search in specific subject# ------------------------------def rag_search(query, subject, k=5):if subject not in SUBJECTS:2 unmodified lineschunks = SUBJECTS[subject]["chunks"]index = SUBJECTS[subject]["index"]q_emb = embed_model.encode([query], show_progress_bar=False).astype("float32")D, I = index.search(q_emb, k)results = []for idx in I[0]:if idx < len(chunks):2 unmodified linesreturn "\n\n".join(results)# ------------------------------# MCQ Generation using Groq API# ------------------------------def generate_mcqs(context, topic, subject):# Check cache firstcontext_hash = hashlib.md5(context.encode()).hexdigest()[:8]cache_key = get_cache_key(topic, subject, context_hash)1 unmodified lineprint("β Using cached MCQs")return MCQ_CACHE[cache_key]print("π€ Generating MCQs using Groq API...")# Prompt for MCQ generationprompt = f"""You are a Class-12 {subject.title()} teacher creating MCQs for students.Topic: "{topic}"Reference material from textbook:{context[:1500]}TASK: Generate exactly 5 multiple-choice questions based on the reference material above.FORMAT (follow this EXACTLY):Q1. [Clear question based on the material]A) [First option]B) [Second option]C) [Third option]D) [Fourth option]Answer: [A/B/C/D] - [Brief explanation why this is correct based on the material]REQUIREMENTS:- Questions must be answerable from the reference material- All 4 options should be plausible- The correct answer must be clearly supported by the material- Keep explanations brief (1-2 sentences)- Generate all 5 questions in the format aboveGenerate 5 MCQs now:"""try:# Call Groq APIchat_completion = groq_client.chat.completions.create(messages=[{"role": "system","content": "You are an expert Class-12 teacher who creates high-quality multiple-choice questions from textbook content. You always follow the exact format specified."},{"role": "user","content": prompt}],model="llama-3.3-70b-versatile", # Fast and accuratetemperature=0.3,max_tokens=1500,top_p=0.9)result = chat_completion.choices[0].message.content.strip()# Clean the outputresult = clean_mcq_output(result)# Cache the resultcache_mcq(cache_key, result)print("β MCQs generated successfully")return resultexcept Exception as e:print(f"β Groq API Error: {e}")return f"Error generating MCQs: {str(e)}\n\nPlease make sure GROQ_API_KEY is set correctly."def clean_mcq_output(text):"""Clean and format the MCQ output"""lines = text.split('\n')cleaned_lines = []for line in lines:line = line.strip()# Keep question lines, options, and answersif (re.match(r'^Q\d+\.', line) orline.startswith(('A)', 'B)', 'C)', 'D)', 'Answer:', 'Correct Answer:')) ornot line):# Normalize answer formatif line.startswith('Correct Answer:'):line = line.replace('Correct Answer:', 'Answer:')cleaned_lines.append(line)return '\n'.join(cleaned_lines)# ------------------------------# HTML UI# ------------------------------HTML_TEMPLATE = """<!DOCTYPE html>24 unmodified lines}.header h1 { font-size: 2.5em; margin-bottom: 10px; }.content { padding: 40px; }.form-group { margin-bottom: 25px; }label {display: block;font-weight: 600;87 unmodified lines.bio { background: #d4edda; color: #155724; }.chem { background: #d1ecf1; color: #0c5460; }.phy { background: #f8d7da; color: #721c24; }.api-badge {background: #17a2b8;color: white;padding: 5px 12px;border-radius: 15px;6 unmodified lines<div class="container"><div class="header"><h1>π Class 12 PCB MCQ Generator</h1><p style="font-size: 1.1em; margin-bottom: 15px;">Generate practice MCQs from your textbooks<span class="api-badge">β‘ Powered by Llama 3.3 70B</span></p><div><span class="subject-tag bio">Biology</span><span class="subject-tag chem">Chemistry</span>20 unmodified lines<div class="loading" id="loading"><div class="spinner"></div><p style="color: #666; font-size: 16px;">Generating high-quality MCQs...</p><p style="color: #999; font-size: 13px; margin-top: 10px;">β‘ Using Llama 3.3 70B via Groq API (5-10 seconds)</p></div><div class="result" id="result"><h3>π Generated MCQs:</h3><div style="background: #d4edda; padding: 12px; border-radius: 6px; margin-bottom: 15px; color: #155724; font-size: 14px;">β <strong>High Accuracy:</strong> Generated by Llama 3.3 70B - One of the most capable models available!</div><div class="mcq-content" id="mcqContent"></div></div>43 unmodified lines}}document.getElementById('topic').addEventListener('keypress', function(e) {if (e.key === 'Enter') {generateMCQs();22 unmodified linesreturn jsonify({"error": "Topic is required"}), 400if subject not in SUBJECTS:return jsonify({"error": "Invalid subject"}), 400print(f"\nπ Searching {subject} for topic: {topic}")context = rag_search(topic, subject, k=5)if not context or len(context.strip()) < 50:return jsonify({"error": f"No relevant content found for topic: {topic}"}), 404print(f"β Found context ({len(context)} chars)")mcqs = generate_mcqs(context, topic, subject)return jsonify({"mcqs": mcqs, "subject": subject})except Exception as e:print(f"β Error: {e}")import tracebacktraceback.print_exc()return jsonify({"error": str(e)}), 5002 unmodified linesdef health():return jsonify({"status": "healthy","model": "llama-3.3-70b-versatile (Groq API)","cache_size": len(MCQ_CACHE)})@app.route("/cache/clear", methods=["POST"])def clear_cache():MCQ_CACHE.clear()7 unmodified linesprint(f"\nπ Starting Flask on 0.0.0.0:{port}\n")app.run(host="0.0.0.0", port=port, debug=False)
app.py+620-116