Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
|
| 4 |
+
# Function to extract text from a PDF
|
| 5 |
+
def pdf_to_text(pdf_file):
|
| 6 |
+
try:
|
| 7 |
+
reader = PdfReader(pdf_file.name)
|
| 8 |
+
text = ""
|
| 9 |
+
for page in reader.pages:
|
| 10 |
+
text += page.extract_text()
|
| 11 |
+
return text if text.strip() else "No text found in the PDF."
|
| 12 |
+
except Exception as e:
|
| 13 |
+
return f"Error: {e}"
|
| 14 |
+
|
| 15 |
+
# Gradio Interface
|
| 16 |
+
def main():
|
| 17 |
+
with gr.Blocks() as app:
|
| 18 |
+
gr.Markdown("## PDF to Text Converter")
|
| 19 |
+
with gr.Row():
|
| 20 |
+
pdf_input = gr.File(label="Upload PDF", type="filepath")
|
| 21 |
+
output_text = gr.Textbox(label="Extracted Text", lines=20, interactive=False)
|
| 22 |
+
convert_button = gr.Button("Convert")
|
| 23 |
+
convert_button.click(pdf_to_text, inputs=pdf_input, outputs=output_text)
|
| 24 |
+
|
| 25 |
+
app.launch()
|
| 26 |
+
|
| 27 |
+
# Run the Gradio app
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
main()
|