Update app.py
Browse files152 unmodified lines1531541551561561571581591601601611621631641641651661671671681691701711701721731741751741761771781795 unmodified lines1851861871861881891901911921931921941951961971951981992002019 unmodified lines211212213211214215216217214218219220221222223117 unmodified lines341342343338339340341342343344344345346347348349350351352348349350351152 unmodified linesdef process_pdf(pdf_file, questions_per_chunk: int = 2, max_chunks: int = 20): """Main processing function.""" if pdf_file is None: return "Please upload a PDF file.", None, None return "Please upload a PDF file.", "", "", "Your flashcards will appear here..." try: # Extract text yield "π Extracting text from PDF...", None, None yield "π Extracting text from PDF...", "", "", "Processing..." raw_text = extract_text_from_pdf(pdf_file) if raw_text.startswith("Error"): return raw_text, None, None yield raw_text, "", "", "Error occurred" return if len(raw_text.strip()) < 100: return "PDF appears to be empty or contains no extractable text.", None, None yield "PDF appears to be empty or contains no extractable text.", "", "", "Error occurred" return # Clean text yield "π§Ή Cleaning text...", None, None yield "π§Ή Cleaning text...", "", "", "Processing..." cleaned_text = clean_text(raw_text) # Chunk text yield "βοΈ Chunking text into sections...", None, None yield "βοΈ Chunking text into sections...", "", "", "Processing..." chunks = chunk_text(cleaned_text) # Limit chunks for CPU performance5 unmodified lines for i, chunk in enumerate(chunks): progress = f"π΄ Generating flashcards... ({i+1}/{total_chunks} chunks processed)" yield progress, None, None yield progress, "", "", "Processing..." cards = generate_qa_pairs(chunk, questions_per_chunk) all_flashcards.extend(cards) if not all_flashcards: return "Could not generate flashcards from this PDF. Try a PDF with more textual content.", None, None yield "Could not generate flashcards from this PDF. Try a PDF with more textual content.", "", "", "No flashcards generated" return # Format output yield "β
Finalizing...", None, None yield "β
Finalizing...", "", "", "Almost done..." # Create formatted display display_text = format_flashcards_display(all_flashcards)9 unmodified lines csv_lines.append(f'"{q}","{a}"') csv_output = "\n".join(csv_lines) return display_text, csv_output, json_output # FINAL OUTPUT - this updates all components yield "β
Done! Generated {} flashcards".format(len(all_flashcards)), csv_output, json_output, display_text except Exception as e: return f"Error processing PDF: {str(e)}", None, None error_msg = f"Error processing PDF: {str(e)}" print(error_msg) yield error_msg, "", "", error_msg
def format_flashcards_display(flashcards: List[Dict]) -> str: """Format flashcards for nice display."""117 unmodified lines ) gr.Markdown("*Raw JSON data for custom applications*") # Event handlers def update_display(status): """Update display when processing is done.""" if status and not status.startswith(("π", "π§Ή", "βοΈ", "π΄", "β
")): return status return gr.update() # FIXED: Direct binding without the broken .then() chain process_btn.click( fn=process_pdf, inputs=[pdf_input, questions_per_chunk, max_chunks], outputs=[status_text, csv_output, json_output] ).then( fn=update_display, inputs=status_text, outputs=output_display outputs=[status_text, csv_output, json_output, output_display] ) # Example sectionapp.py+19-23