Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567895 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261276 unmodified lines1341351361371381391401411421431441451461471481491501511526 unmodified lines1591601611621631641651 unmodified lineimport gradio as grimport google.generativeai as genaifrom markdown_pdf import MarkdownPdf, Sectionimport pikepdf# ---------- PROMPTS ----------PROMPTS = {95 unmodified linespdf.save(filename)return filename# ---------- HELPER: PDF Compression ----------def compress_pdf(input_path, output_path):try:pdf = pikepdf.open(input_path)pdf.save(output_path, optimize_version=True)pdf.close()return output_pathexcept Exception as e:print(f"❌ Compression failed for {input_path}: {e}")return input_pathdef check_and_compress(path):if os.path.getsize(path) > 20 * 1024 * 1024: # 20 MBcompressed_path = os.path.splitext(path)[0] + "_compressed.pdf"print(f"⚡ Compressing {os.path.basename(path)} (>20MB)...")return compress_pdf(path, compressed_path)return path# ---------- HELPER: Create Model with Fallback ----------def create_model():try:6 unmodified lines# ---------- PIPELINE: ALIGN + GRADE ----------def align_and_grade(qp_file, ms_file, ans_file):try:# Ensure files are <20MBqp_file = check_and_compress(qp_file)ms_file = check_and_compress(ms_file)ans_file = check_and_compress(ans_file)# Uploadsqp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")model = create_model()# Step 1: Alignmentresp = model.generate_content([PROMPTS["ALIGNMENT_PROMPT"]["content"],qp_uploaded,6 unmodified linesaligned_pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")# Step 2: Grading (automatic)response = model.generate_content([PROMPTS["GRADING_PROMPT"]["content"],aligned_text1 unmodified line234567895 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381396 unmodified lines1461471481491501511521531541551561571581591601611621631646 unmodified lines1711721731741751761771 unmodified lineimport gradio as grimport google.generativeai as genaifrom markdown_pdf import MarkdownPdf, Sectionimport subprocess# ---------- PROMPTS ----------PROMPTS = {95 unmodified linespdf.save(filename)return filename# ---------- HELPER: Compress PDF ----------def compress_pdf(input_path, output_path=None, max_size=20*1024*1024):"""Compress PDF using Ghostscript if larger than max_size (default 20MB)."""if output_path is None:base, ext = os.path.splitext(input_path)output_path = f"{base}_compressed{ext}"if os.path.getsize(input_path) <= max_size:return input_path # No compression neededtry:gs_cmd = ["gs", "-sDEVICE=pdfwrite","-dCompatibilityLevel=1.4","-dPDFSETTINGS=/ebook", # options: /screen, /ebook, /printer"-dNOPAUSE", "-dQUIET", "-dBATCH",f"-sOutputFile={output_path}", input_path]subprocess.run(gs_cmd, check=True)if os.path.getsize(output_path) <= max_size:print(f"✅ Compressed {input_path} → {output_path}")return output_pathelse:print(f"⚠️ Compression failed to reduce below {max_size/1024/1024} MB")return input_pathexcept Exception as e:print(f"⚠️ Compression error: {e}")return input_path# ---------- HELPER: Create Model with Fallback ----------def create_model():try:6 unmodified lines# ---------- PIPELINE: ALIGN + GRADE ----------def align_and_grade(qp_file, ms_file, ans_file):try:# Step 0: Compress if neededqp_file = compress_pdf(qp_file, "qp_compressed.pdf")ms_file = compress_pdf(ms_file, "ms_compressed.pdf")ans_file = compress_pdf(ans_file, "ans_compressed.pdf")# Step 1: Uploadsqp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")model = create_model()# Step 2: Alignmentresp = model.generate_content([PROMPTS["ALIGNMENT_PROMPT"]["content"],qp_uploaded,6 unmodified linesaligned_pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")# Step 3: Gradingresponse = model.generate_content([PROMPTS["GRADING_PROMPT"]["content"],aligned_text
app.py+34-22