no-more-plag-v2 / app.py
ayush-thakur02's picture
Update app.py
1d499ae verified
import gradio as gr
import PyPDF2
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import random
import string
import io
def generate_random_gibberish(length=1000, line_length=100):
"""Generate random gibberish consisting of alphanumeric characters and symbols, split into lines of 100 chars."""
chars = string.ascii_letters + string.digits + string.punctuation
gibberish_chars = []
i = 0
while i < length:
# Generate word length between 3-5 characters
word_length = random.randint(3, 5)
# Generate a "word" of random characters
word = ''.join(random.choice(chars) for _ in range(word_length))
gibberish_chars.append(word)
# Add space after the word (except for the last word)
if i + word_length < length:
gibberish_chars.append(' ')
i += word_length + 1 # +1 for the space
# Join all characters and words
gibberish = ''.join(gibberish_chars)
# Split the gibberish into lines of specified length
return [gibberish[i:i+line_length] for i in range(0, len(gibberish), line_length)]
def add_invisible_text_to_pdf(input_pdf_bytes, num_pages_to_modify=5):
import tempfile
import os
# Create a file-like object from the uploaded PDF bytes
input_pdf_path = io.BytesIO(input_pdf_bytes)
reader = PyPDF2.PdfReader(input_pdf_path)
writer = PyPDF2.PdfWriter()
# Loop through the pages of the existing PDF
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
# If this page is in the set of pages we want to modify
if page_num < num_pages_to_modify:
packet = io.BytesIO()
# Create a new PDF to overlay on the existing one
c = canvas.Canvas(packet, pagesize=letter)
# Set invisible text color (white)
c.setFillColorRGB(1, 1, 1, 0) # White color (invisible on white background)
c.setFont("Helvetica", 6) # Use a smaller font for better coverage
width, height = letter # Get the page dimensions
# Generate enough gibberish to cover the entire page
gibberish_lines = generate_random_gibberish(length=10000, line_length=100)
y_position = height - 10 # Start near the top of the page
# Loop through the text and write it line by line
for line in gibberish_lines:
if y_position < 0: # If we go past the bottom of the page, stop
break
c.drawString(10, y_position, line) # Draw the text starting from left to right
y_position -= 8 # Move down for the next line
c.save()
# Move the "packet" to the start of the file and merge it with the original PDF
packet.seek(0)
overlay_pdf = PyPDF2.PdfReader(packet)
overlay_page = overlay_pdf.pages[0]
# Merge the overlay page with the original page
page.merge_page(overlay_page)
# Add the (modified or original) page to the writer
writer.add_page(page)
# Save the new PDF to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
writer.write(tmp_file)
tmp_file_path = tmp_file.name
return tmp_file_path
# Define Gradio interface
def gradio_app(input_pdf, num_pages_to_modify):
output_pdf = add_invisible_text_to_pdf(input_pdf, num_pages_to_modify)
return output_pdf
# Create the Gradio interface
iface = gr.Interface(
fn=gradio_app,
inputs=[
gr.File(label="Upload PDF", type="binary"),
gr.Slider(minimum=1, maximum=20, value=5, label="Number of Pages to Modify")
],
outputs=gr.File(label="Output PDF"),
title="PDF Gibberish Inserter",
# description="Upload a PDF and this app will add invisible gibberish text to the first few pages. You can choose how many pages to modify."
)
# Launch the app
iface.launch()