Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ def render_pdf_page(page, max_resolution=1540, scale=2.77):
|
|
| 35 |
def process_pdf(pdf_path, num_pages=1):
|
| 36 |
pdf = pdfium.PdfDocument(pdf_path)
|
| 37 |
total_pages = len(pdf)
|
| 38 |
-
pages_to_process = min(num_pages, total_pages, 5)
|
| 39 |
images = []
|
| 40 |
|
| 41 |
for i in range(pages_to_process):
|
|
@@ -60,7 +60,7 @@ def process_input(file_input, temperature, num_pages):
|
|
| 60 |
|
| 61 |
if file_path.lower().endswith('.pdf'):
|
| 62 |
try:
|
| 63 |
-
images_to_process, total_pages = process_pdf(file_path, num_pages)
|
| 64 |
if len(images_to_process) == 0:
|
| 65 |
yield "Error: Could not extract pages from PDF.", "", "", None
|
| 66 |
return
|
|
@@ -82,7 +82,7 @@ def process_input(file_input, temperature, num_pages):
|
|
| 82 |
yield f"Error opening image: {str(e)}", "", "", None
|
| 83 |
return
|
| 84 |
|
| 85 |
-
content = [{"type": "text", "text": ""}]
|
| 86 |
|
| 87 |
for img in images_to_process:
|
| 88 |
try:
|
|
@@ -97,12 +97,7 @@ def process_input(file_input, temperature, num_pages):
|
|
| 97 |
|
| 98 |
payload = {
|
| 99 |
"model": MODEL,
|
| 100 |
-
"messages": [
|
| 101 |
-
{
|
| 102 |
-
"role": "user",
|
| 103 |
-
"content": content
|
| 104 |
-
}
|
| 105 |
-
],
|
| 106 |
"temperature": temperature,
|
| 107 |
"stream": True
|
| 108 |
}
|
|
@@ -149,17 +144,17 @@ def process_input(file_input, temperature, num_pages):
|
|
| 149 |
|
| 150 |
|
| 151 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
| 152 |
-
gr.Markdown(
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
with gr.Row():
|
| 165 |
with gr.Column(scale=1):
|
|
@@ -200,8 +195,7 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 200 |
with gr.Column(scale=2):
|
| 201 |
output_text = gr.Markdown(
|
| 202 |
label="π Extracted Text (Rendered)",
|
| 203 |
-
value="
|
| 204 |
-
height=600
|
| 205 |
)
|
| 206 |
|
| 207 |
with gr.Row():
|
|
@@ -221,7 +215,7 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 221 |
)
|
| 222 |
|
| 223 |
clear_btn.click(
|
| 224 |
-
fn=lambda: (None, "", "", "", None, 1),
|
| 225 |
outputs=[file_input, output_text, raw_output, page_info, rendered_image, num_pages]
|
| 226 |
)
|
| 227 |
|
|
|
|
| 35 |
def process_pdf(pdf_path, num_pages=1):
|
| 36 |
pdf = pdfium.PdfDocument(pdf_path)
|
| 37 |
total_pages = len(pdf)
|
| 38 |
+
pages_to_process = min(int(num_pages), total_pages, 5)
|
| 39 |
images = []
|
| 40 |
|
| 41 |
for i in range(pages_to_process):
|
|
|
|
| 60 |
|
| 61 |
if file_path.lower().endswith('.pdf'):
|
| 62 |
try:
|
| 63 |
+
images_to_process, total_pages = process_pdf(file_path, int(num_pages))
|
| 64 |
if len(images_to_process) == 0:
|
| 65 |
yield "Error: Could not extract pages from PDF.", "", "", None
|
| 66 |
return
|
|
|
|
| 82 |
yield f"Error opening image: {str(e)}", "", "", None
|
| 83 |
return
|
| 84 |
|
| 85 |
+
content = [{"type": "text", "text": "Extract the text from this image."}]
|
| 86 |
|
| 87 |
for img in images_to_process:
|
| 88 |
try:
|
|
|
|
| 97 |
|
| 98 |
payload = {
|
| 99 |
"model": MODEL,
|
| 100 |
+
"messages": [{"role": "user", "content": content}],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
"temperature": temperature,
|
| 102 |
"stream": True
|
| 103 |
}
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
| 147 |
+
gr.Markdown("""
|
| 148 |
+
# π Image/PDF to Text Extraction
|
| 149 |
+
|
| 150 |
+
**π‘ How to use:**
|
| 151 |
+
1. Upload an image or PDF
|
| 152 |
+
2. For PDFs: choose how many pages to process (1-5, default is 1)
|
| 153 |
+
3. Adjust temperature if needed
|
| 154 |
+
4. Click "Extract Text"
|
| 155 |
+
|
| 156 |
+
**Note:** The Markdown rendering for tables is not always correct, check the raw output for complex tables!
|
| 157 |
+
""")
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Column(scale=1):
|
|
|
|
| 195 |
with gr.Column(scale=2):
|
| 196 |
output_text = gr.Markdown(
|
| 197 |
label="π Extracted Text (Rendered)",
|
| 198 |
+
value="*Extracted text will appear here...*"
|
|
|
|
| 199 |
)
|
| 200 |
|
| 201 |
with gr.Row():
|
|
|
|
| 215 |
)
|
| 216 |
|
| 217 |
clear_btn.click(
|
| 218 |
+
fn=lambda: (None, "*Extracted text will appear here...*", "", "", None, 1),
|
| 219 |
outputs=[file_input, output_text, raw_output, page_info, rendered_image, num_pages]
|
| 220 |
)
|
| 221 |
|