Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -49,12 +49,13 @@ def convert_to_images(file_path):
|
|
| 49 |
# -----------------------------------------------------------------------------
|
| 50 |
def scan_slides(client, image_paths):
|
| 51 |
inventory = []
|
|
|
|
| 52 |
|
| 53 |
-
# Process
|
| 54 |
-
# Gemini 2.0 is fast enough.
|
| 55 |
-
|
| 56 |
for i, img_path in enumerate(image_paths):
|
| 57 |
slide_num = i + 1
|
|
|
|
|
|
|
| 58 |
print(f"Scanning Slide {slide_num}...")
|
| 59 |
|
| 60 |
with open(img_path, "rb") as f:
|
|
@@ -88,14 +89,18 @@ def scan_slides(client, image_paths):
|
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error scanning slide {slide_num}: {e}")
|
| 90 |
|
| 91 |
-
|
| 92 |
|
| 93 |
# -----------------------------------------------------------------------------
|
| 94 |
# LOGIC: PASS 2 (COACH CRITIQUE)
|
| 95 |
# -----------------------------------------------------------------------------
|
| 96 |
def generate_critique(client, inventory):
|
| 97 |
# Filter Appendix
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# Script
|
| 101 |
script = []
|
|
@@ -171,29 +176,44 @@ def generate_critique(client, inventory):
|
|
| 171 |
# -----------------------------------------------------------------------------
|
| 172 |
def process_presentation(file_obj, password):
|
| 173 |
print("--- NEW JOB STARTED ---")
|
|
|
|
|
|
|
|
|
|
| 174 |
if password != ACCESS_PASSWORD:
|
| 175 |
-
return "β Incorrect Password", "", ""
|
| 176 |
|
| 177 |
if not API_KEY:
|
| 178 |
-
return "β Server Error: API Key missing", "", ""
|
| 179 |
|
| 180 |
client = genai.Client(api_key=API_KEY)
|
| 181 |
|
| 182 |
try:
|
| 183 |
# 1. Convert
|
| 184 |
print("Step 1: Converting PDF...")
|
| 185 |
-
yield "Converting PDF...", "", "", None, None
|
| 186 |
images = convert_to_images(file_obj.name)
|
| 187 |
print(f" Converted {len(images)} slides.")
|
| 188 |
|
| 189 |
# 2. Scan
|
| 190 |
-
yield f"Scanning {len(images)} slides... (This takes ~30s)", "", "", None, None
|
| 191 |
print("Step 2: Scanning Slides (Pass 1)...")
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
print(" Scan Complete.")
|
| 194 |
|
| 195 |
# 3. Coach
|
| 196 |
-
yield "Dr. Jones is thinking (Pass 2)...", "", "", None, None
|
| 197 |
print("Step 3: Calling Gemini Coach (Pass 2)...")
|
| 198 |
critique = generate_critique(client, inventory)
|
| 199 |
print(" Critique Received.")
|
|
@@ -220,12 +240,12 @@ def process_presentation(file_obj, password):
|
|
| 220 |
f.write("## Story Roadmap\n")
|
| 221 |
f.write(table_md)
|
| 222 |
|
| 223 |
-
# Return: Status, Summary, Table, Preview Image, Report File
|
| 224 |
-
yield "β
Done!", summary, table_md, images[0], report_filename
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
print(f"CRITICAL ERROR: {e}")
|
| 228 |
-
yield f"β Error: {str(e)}", "", "", None, None
|
| 229 |
|
| 230 |
with gr.Blocks(title="Dr. Jones AI Coach") as demo:
|
| 231 |
gr.Markdown("# π Dr. Jones: AI Presentation Coach")
|
|
@@ -245,7 +265,10 @@ with gr.Blocks(title="Dr. Jones AI Coach") as demo:
|
|
| 245 |
with gr.Row():
|
| 246 |
with gr.Column(scale=1):
|
| 247 |
preview_img = gr.Image(label="Presentation Title Slide", interactive=False)
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
with gr.Column(scale=2):
|
| 251 |
gr.Markdown("### π¨βπ« Coach Summary")
|
|
@@ -258,7 +281,7 @@ with gr.Blocks(title="Dr. Jones AI Coach") as demo:
|
|
| 258 |
btn.click(
|
| 259 |
fn=process_presentation,
|
| 260 |
inputs=[file_input, pass_input],
|
| 261 |
-
outputs=[status, summary_display, roadmap_display, preview_img, download_btn]
|
| 262 |
)
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|
|
|
|
| 49 |
# -----------------------------------------------------------------------------
|
| 50 |
def scan_slides(client, image_paths):
|
| 51 |
inventory = []
|
| 52 |
+
total = len(image_paths)
|
| 53 |
|
| 54 |
+
# Process 1-by-1 to preserve context and allow progress updates
|
|
|
|
|
|
|
| 55 |
for i, img_path in enumerate(image_paths):
|
| 56 |
slide_num = i + 1
|
| 57 |
+
yield f"Reading Slide {slide_num}/{total}...", None
|
| 58 |
+
|
| 59 |
print(f"Scanning Slide {slide_num}...")
|
| 60 |
|
| 61 |
with open(img_path, "rb") as f:
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
print(f"Error scanning slide {slide_num}: {e}")
|
| 91 |
|
| 92 |
+
yield "Scan Complete", inventory
|
| 93 |
|
| 94 |
# -----------------------------------------------------------------------------
|
| 95 |
# LOGIC: PASS 2 (COACH CRITIQUE)
|
| 96 |
# -----------------------------------------------------------------------------
|
| 97 |
def generate_critique(client, inventory):
|
| 98 |
# Filter Appendix
|
| 99 |
+
def get_title(slide):
|
| 100 |
+
t = slide.get("title")
|
| 101 |
+
return t if t else ""
|
| 102 |
+
|
| 103 |
+
active = [s for s in inventory if "appendix" not in get_title(s).lower()]
|
| 104 |
|
| 105 |
# Script
|
| 106 |
script = []
|
|
|
|
| 176 |
# -----------------------------------------------------------------------------
|
| 177 |
def process_presentation(file_obj, password):
|
| 178 |
print("--- NEW JOB STARTED ---")
|
| 179 |
+
if file_obj is None:
|
| 180 |
+
return "β Error: No file uploaded", "", "", None, None, ""
|
| 181 |
+
|
| 182 |
if password != ACCESS_PASSWORD:
|
| 183 |
+
return "β Incorrect Password", "", "", None, None, ""
|
| 184 |
|
| 185 |
if not API_KEY:
|
| 186 |
+
return "β Server Error: API Key missing", "", "", None, None, ""
|
| 187 |
|
| 188 |
client = genai.Client(api_key=API_KEY)
|
| 189 |
|
| 190 |
try:
|
| 191 |
# 1. Convert
|
| 192 |
print("Step 1: Converting PDF...")
|
| 193 |
+
yield "Converting PDF...", "", "", None, None, "Converting PDF..."
|
| 194 |
images = convert_to_images(file_obj.name)
|
| 195 |
print(f" Converted {len(images)} slides.")
|
| 196 |
|
| 197 |
# 2. Scan
|
| 198 |
+
yield f"Scanning {len(images)} slides... (This takes ~30s)", "", "", None, None, "Starting Scan..."
|
| 199 |
print("Step 2: Scanning Slides (Pass 1)...")
|
| 200 |
+
|
| 201 |
+
# Iterate through the generator from scan_slides
|
| 202 |
+
scanner = scan_slides(client, images)
|
| 203 |
+
inventory = []
|
| 204 |
+
|
| 205 |
+
for msg, result in scanner:
|
| 206 |
+
if result is None:
|
| 207 |
+
# Still scanning, update status
|
| 208 |
+
yield msg, "", "", None, None, msg
|
| 209 |
+
else:
|
| 210 |
+
# Scan complete, result is the inventory
|
| 211 |
+
inventory = result
|
| 212 |
+
|
| 213 |
print(" Scan Complete.")
|
| 214 |
|
| 215 |
# 3. Coach
|
| 216 |
+
yield "Dr. Jones is thinking (Pass 2)...", "", "", None, None, "Analyzing Story Arc..."
|
| 217 |
print("Step 3: Calling Gemini Coach (Pass 2)...")
|
| 218 |
critique = generate_critique(client, inventory)
|
| 219 |
print(" Critique Received.")
|
|
|
|
| 240 |
f.write("## Story Roadmap\n")
|
| 241 |
f.write(table_md)
|
| 242 |
|
| 243 |
+
# Return: Status, Summary, Table, Preview Image, Report File, Small Status
|
| 244 |
+
yield "β
Done!", summary, table_md, images[0], report_filename, ""
|
| 245 |
|
| 246 |
except Exception as e:
|
| 247 |
print(f"CRITICAL ERROR: {e}")
|
| 248 |
+
yield f"β Error: {str(e)}", "", "", None, None, "Error"
|
| 249 |
|
| 250 |
with gr.Blocks(title="Dr. Jones AI Coach") as demo:
|
| 251 |
gr.Markdown("# π Dr. Jones: AI Presentation Coach")
|
|
|
|
| 265 |
with gr.Row():
|
| 266 |
with gr.Column(scale=1):
|
| 267 |
preview_img = gr.Image(label="Presentation Title Slide", interactive=False)
|
| 268 |
+
with gr.Row():
|
| 269 |
+
download_btn = gr.File(label="Download Full Report", scale=2)
|
| 270 |
+
with gr.Column(scale=1):
|
| 271 |
+
progress_status = gr.Markdown(value="") # Temporary status next to download
|
| 272 |
|
| 273 |
with gr.Column(scale=2):
|
| 274 |
gr.Markdown("### π¨βπ« Coach Summary")
|
|
|
|
| 281 |
btn.click(
|
| 282 |
fn=process_presentation,
|
| 283 |
inputs=[file_input, pass_input],
|
| 284 |
+
outputs=[status, summary_display, roadmap_display, preview_img, download_btn, progress_status]
|
| 285 |
)
|
| 286 |
|
| 287 |
if __name__ == "__main__":
|