Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import csv
|
| 7 |
+
|
| 8 |
+
def pdf_to_csv(pdf_file):
|
| 9 |
+
# Open the uploaded PDF file (pdf_file is a TemporaryFile)
|
| 10 |
+
# pdf_reader = PyPDF2.PdfReader(pdf_file.name)
|
| 11 |
+
text_lines = []
|
| 12 |
+
|
| 13 |
+
file_name = os.path.basename(pdf_file.name)
|
| 14 |
+
|
| 15 |
+
text_lines.append(f"File Name: {file_name}")
|
| 16 |
+
csv_filename = "extracted_text.csv"
|
| 17 |
+
# Write each line into the CSV file (each line in its own row)
|
| 18 |
+
with open(csv_filename, "w", newline="", encoding="utf-8") as csvfile:
|
| 19 |
+
writer = csv.writer(csvfile)
|
| 20 |
+
for line in text_lines:
|
| 21 |
+
writer.writerow([line])
|
| 22 |
+
|
| 23 |
+
# Return the CSV file path so Gradio can offer it as a download
|
| 24 |
+
return csv_filename
|
| 25 |
+
|
| 26 |
+
# Create a simple single-page Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=pdf_to_csv,
|
| 29 |
+
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|
| 30 |
+
outputs=gr.File(label="Download CSV"),
|
| 31 |
+
title="PDF to CSV Converter",
|
| 32 |
+
description="Upload a PDF file, extract its text line-by-line, and download a CSV."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
demo.launch()
|
| 36 |
+
|