Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Directory where PDFs will be saved
|
| 5 |
+
UPLOAD_FOLDER = 'uploads'
|
| 6 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
# Function to list uploaded PDFs
|
| 9 |
+
def list_pdfs():
|
| 10 |
+
files = os.listdir(UPLOAD_FOLDER)
|
| 11 |
+
pdf_list = [f'<a href="/file={UPLOAD_FOLDER}/{file}" target="_blank">{file}</a>' for file in files]
|
| 12 |
+
return '\n'.join(pdf_list)
|
| 13 |
+
|
| 14 |
+
# Function to handle PDF upload
|
| 15 |
+
def upload_pdf(pdf_file):
|
| 16 |
+
if pdf_file:
|
| 17 |
+
file_name = pdf_file.name
|
| 18 |
+
pdf_file.save(os.path.join(UPLOAD_FOLDER, file_name))
|
| 19 |
+
return list_pdfs()
|
| 20 |
+
return "No file uploaded."
|
| 21 |
+
|
| 22 |
+
# Gradio Interface
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
with gr.Column():
|
| 25 |
+
gr.Markdown("# PDF Catalog")
|
| 26 |
+
pdf_list = gr.HTML(list_pdfs)
|
| 27 |
+
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 28 |
+
upload_btn = gr.Button("Upload")
|
| 29 |
+
|
| 30 |
+
upload_btn.click(upload_pdf, inputs=pdf_upload, outputs=pdf_list)
|
| 31 |
+
|
| 32 |
+
# Run the app
|
| 33 |
+
demo.launch()
|