Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,8 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
| 8 |
# Function to list uploaded PDFs
|
| 9 |
def list_pdfs():
|
| 10 |
files = os.listdir(UPLOAD_FOLDER)
|
| 11 |
-
pdf_list = [f'<a href="
|
| 12 |
-
return '
|
| 13 |
|
| 14 |
# Function to handle PDF upload
|
| 15 |
def upload_pdf(pdf_file):
|
|
@@ -26,8 +26,20 @@ with gr.Blocks() as demo:
|
|
| 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()
|
|
|
|
| 8 |
# Function to list uploaded PDFs
|
| 9 |
def list_pdfs():
|
| 10 |
files = os.listdir(UPLOAD_FOLDER)
|
| 11 |
+
pdf_list = [f'<a href="javascript:view_pdf(\'{file}\')">{file}</a>' for file in files]
|
| 12 |
+
return '<br>'.join(pdf_list)
|
| 13 |
|
| 14 |
# Function to handle PDF upload
|
| 15 |
def upload_pdf(pdf_file):
|
|
|
|
| 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 |
+
pdf_viewer = gr.HTML("<iframe id='pdf_viewer' style='width:100%; height:500px;'></iframe>")
|
| 30 |
|
| 31 |
upload_btn.click(upload_pdf, inputs=pdf_upload, outputs=pdf_list)
|
| 32 |
|
| 33 |
+
# Custom JS to view PDF in iframe
|
| 34 |
+
js = """
|
| 35 |
+
<script>
|
| 36 |
+
function view_pdf(file) {
|
| 37 |
+
const viewer = document.getElementById('pdf_viewer');
|
| 38 |
+
viewer.src = `/file=uploads/${file}`;
|
| 39 |
+
}
|
| 40 |
+
</script>
|
| 41 |
+
"""
|
| 42 |
+
pdf_list.update(js)
|
| 43 |
+
|
| 44 |
# Run the app
|
| 45 |
demo.launch()
|