Spaces:
Running
Running
File size: 7,323 Bytes
fa9a583 cd5e862 fa9a583 4e1f4a3 fa9a583 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# PDF_ingestion_tab.py
# Gradio UI for ingesting PDFs into the database
import os
import shutil
import tempfile
# Imports
#
# External Imports
import gradio as gr
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import load_preset_prompts
from App_Function_Libraries.Gradio_UI.Chat_ui import update_user_prompt
from App_Function_Libraries.PDF.PDF_Ingestion_Lib import extract_metadata_from_pdf, extract_text_and_format_from_pdf, \
process_and_cleanup_pdf
#
#
########################################################################################################################
#
# Functions:
def create_pdf_ingestion_tab():
with gr.TabItem("PDF Ingestion"):
# TODO - Add functionality to extract metadata from pdf as part of conversion process in marker
gr.Markdown("# Ingest PDF Files and Extract Metadata")
with gr.Row():
with gr.Column():
pdf_file_input = gr.File(label="Uploaded PDF File", file_types=[".pdf"], visible=False)
pdf_upload_button = gr.UploadButton("Click to Upload PDF", file_types=[".pdf"])
pdf_title_input = gr.Textbox(label="Title (Optional)")
pdf_author_input = gr.Textbox(label="Author (Optional)")
pdf_keywords_input = gr.Textbox(label="Keywords (Optional, comma-separated)")
with gr.Row():
custom_prompt_checkbox = gr.Checkbox(label="Use a Custom Prompt",
value=False,
visible=True)
preset_prompt_checkbox = gr.Checkbox(label="Use a pre-set Prompt",
value=False,
visible=True)
with gr.Row():
preset_prompt = gr.Dropdown(label="Select Preset Prompt",
choices=load_preset_prompts(),
visible=False)
with gr.Row():
custom_prompt_input = gr.Textbox(label="Custom Prompt",
placeholder="Enter custom prompt here",
lines=3,
visible=False)
with gr.Row():
system_prompt_input = gr.Textbox(label="System Prompt",
value="""
<s>You are a bulleted notes specialist.
[INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]
**Bulleted Note Creation Guidelines**
**Headings**:
- Based on referenced topics, not categories like quotes or terms
- Surrounded by **bold** formatting
- Not listed as bullet points
- No space between headings and list items underneath
**Emphasis**:
- **Important terms** set in bold font
- **Text ending in a colon**: also bolded
**Review**:
- Ensure adherence to specified format
- Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]""",
lines=3,
visible=False)
custom_prompt_checkbox.change(
fn=lambda x: (gr.update(visible=x), gr.update(visible=x)),
inputs=[custom_prompt_checkbox],
outputs=[custom_prompt_input, system_prompt_input]
)
preset_prompt_checkbox.change(
fn=lambda x: gr.update(visible=x),
inputs=[preset_prompt_checkbox],
outputs=[preset_prompt]
)
def update_prompts(preset_name):
prompts = update_user_prompt(preset_name)
return (
gr.update(value=prompts["user_prompt"], visible=True),
gr.update(value=prompts["system_prompt"], visible=True)
)
preset_prompt.change(
update_prompts,
inputs=preset_prompt,
outputs=[custom_prompt_input, system_prompt_input]
)
pdf_ingest_button = gr.Button("Ingest PDF")
pdf_upload_button.upload(fn=lambda file: file, inputs=pdf_upload_button, outputs=pdf_file_input)
with gr.Column():
pdf_result_output = gr.Textbox(label="Result")
pdf_ingest_button.click(
fn=process_and_cleanup_pdf,
inputs=[pdf_file_input, pdf_title_input, pdf_author_input, pdf_keywords_input],
outputs=pdf_result_output
)
def test_pdf_ingestion(pdf_file):
if pdf_file is None:
return "No file uploaded", ""
try:
# Create a temporary directory
with tempfile.TemporaryDirectory() as temp_dir:
# Create a path for the temporary PDF file
temp_path = os.path.join(temp_dir, "temp.pdf")
# Copy the contents of the uploaded file to the temporary file
shutil.copy(pdf_file.name, temp_path)
# Extract text and convert to Markdown
markdown_text = extract_text_and_format_from_pdf(temp_path)
# Extract metadata from PDF
metadata = extract_metadata_from_pdf(temp_path)
# Use metadata for title and author if not provided
title = metadata.get('title', os.path.splitext(os.path.basename(pdf_file.name))[0])
author = metadata.get('author', 'Unknown')
result = f"PDF '{title}' by {author} processed successfully."
return result, markdown_text
except Exception as e:
return f"Error ingesting PDF: {str(e)}", ""
def create_pdf_ingestion_test_tab():
with gr.TabItem("Test PDF Ingestion"):
with gr.Row():
with gr.Column():
pdf_file_input = gr.File(label="Upload PDF for testing")
test_button = gr.Button("Test PDF Ingestion")
with gr.Column():
test_output = gr.Textbox(label="Test Result")
pdf_content_output = gr.Textbox(label="PDF Content", lines=200)
test_button.click(
fn=test_pdf_ingestion,
inputs=[pdf_file_input],
outputs=[test_output, pdf_content_output]
)
|