atz21 commited on
Commit
0b422e6
·
verified ·
1 Parent(s): a11a229

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from markdown_pdf import MarkdownPdf, Section
5
- import pikepdf
6
 
7
  # ---------- PROMPTS ----------
8
  PROMPTS = {
@@ -104,24 +104,36 @@ def save_as_pdf(text, filename="output.pdf"):
104
  pdf.save(filename)
105
  return filename
106
 
107
- # ---------- HELPER: PDF Compression ----------
108
- def compress_pdf(input_path, output_path):
 
 
 
 
 
 
 
 
109
  try:
110
- pdf = pikepdf.open(input_path)
111
- pdf.save(output_path, optimize_version=True)
112
- pdf.close()
113
- return output_path
 
 
 
 
 
 
 
 
 
 
 
114
  except Exception as e:
115
- print(f" Compression failed for {input_path}: {e}")
116
  return input_path
117
 
118
- def check_and_compress(path):
119
- if os.path.getsize(path) > 20 * 1024 * 1024: # 20 MB
120
- compressed_path = os.path.splitext(path)[0] + "_compressed.pdf"
121
- print(f"⚡ Compressing {os.path.basename(path)} (>20MB)...")
122
- return compress_pdf(path, compressed_path)
123
- return path
124
-
125
  # ---------- HELPER: Create Model with Fallback ----------
126
  def create_model():
127
  try:
@@ -134,19 +146,19 @@ def create_model():
134
  # ---------- PIPELINE: ALIGN + GRADE ----------
135
  def align_and_grade(qp_file, ms_file, ans_file):
136
  try:
137
- # Ensure files are <20MB
138
- qp_file = check_and_compress(qp_file)
139
- ms_file = check_and_compress(ms_file)
140
- ans_file = check_and_compress(ans_file)
141
 
142
- # Uploads
143
  qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
144
  ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")
145
  ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
146
 
147
  model = create_model()
148
 
149
- # Step 1: Alignment
150
  resp = model.generate_content([
151
  PROMPTS["ALIGNMENT_PROMPT"]["content"],
152
  qp_uploaded,
@@ -159,7 +171,7 @@ def align_and_grade(qp_file, ms_file, ans_file):
159
 
160
  aligned_pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")
161
 
162
- # Step 2: Grading (automatic)
163
  response = model.generate_content([
164
  PROMPTS["GRADING_PROMPT"]["content"],
165
  aligned_text
 
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from markdown_pdf import MarkdownPdf, Section
5
+ import subprocess
6
 
7
  # ---------- PROMPTS ----------
8
  PROMPTS = {
 
104
  pdf.save(filename)
105
  return filename
106
 
107
+ # ---------- HELPER: Compress PDF ----------
108
+ def compress_pdf(input_path, output_path=None, max_size=20*1024*1024):
109
+ """Compress PDF using Ghostscript if larger than max_size (default 20MB)."""
110
+ if output_path is None:
111
+ base, ext = os.path.splitext(input_path)
112
+ output_path = f"{base}_compressed{ext}"
113
+
114
+ if os.path.getsize(input_path) <= max_size:
115
+ return input_path # No compression needed
116
+
117
  try:
118
+ gs_cmd = [
119
+ "gs", "-sDEVICE=pdfwrite",
120
+ "-dCompatibilityLevel=1.4",
121
+ "-dPDFSETTINGS=/ebook", # options: /screen, /ebook, /printer
122
+ "-dNOPAUSE", "-dQUIET", "-dBATCH",
123
+ f"-sOutputFile={output_path}", input_path
124
+ ]
125
+ subprocess.run(gs_cmd, check=True)
126
+
127
+ if os.path.getsize(output_path) <= max_size:
128
+ print(f"✅ Compressed {input_path} → {output_path}")
129
+ return output_path
130
+ else:
131
+ print(f"⚠️ Compression failed to reduce below {max_size/1024/1024} MB")
132
+ return input_path
133
  except Exception as e:
134
+ print(f"⚠️ Compression error: {e}")
135
  return input_path
136
 
 
 
 
 
 
 
 
137
  # ---------- HELPER: Create Model with Fallback ----------
138
  def create_model():
139
  try:
 
146
  # ---------- PIPELINE: ALIGN + GRADE ----------
147
  def align_and_grade(qp_file, ms_file, ans_file):
148
  try:
149
+ # Step 0: Compress if needed
150
+ qp_file = compress_pdf(qp_file, "qp_compressed.pdf")
151
+ ms_file = compress_pdf(ms_file, "ms_compressed.pdf")
152
+ ans_file = compress_pdf(ans_file, "ans_compressed.pdf")
153
 
154
+ # Step 1: Uploads
155
  qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
156
  ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")
157
  ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
158
 
159
  model = create_model()
160
 
161
+ # Step 2: Alignment
162
  resp = model.generate_content([
163
  PROMPTS["ALIGNMENT_PROMPT"]["content"],
164
  qp_uploaded,
 
171
 
172
  aligned_pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")
173
 
174
+ # Step 3: Grading
175
  response = model.generate_content([
176
  PROMPTS["GRADING_PROMPT"]["content"],
177
  aligned_text