Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
|
|
|
| 4 |
from helper import (
|
| 5 |
custom_file_uploader, resize_image, convert_image_to_base64, post_request_and_parse_response,
|
| 6 |
draw_bounding_boxes_for_textract, extract_text_from_textract_blocks, ChatGPTClient
|
| 7 |
)
|
| 8 |
-
from pdf2image import convert_from_path
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
| 11 |
|
|
@@ -25,25 +25,30 @@ with st.sidebar:
|
|
| 25 |
|
| 26 |
# Display a placeholder for uploaded image
|
| 27 |
st.warning("Please upload an image or a single-page PDF file!")
|
| 28 |
-
uploaded_file = st.file_uploader("Upload an Image or PDF", type=['PDF'], label_visibility="collapsed")
|
| 29 |
|
| 30 |
pil_image = None
|
| 31 |
if uploaded_file:
|
| 32 |
# Handle PDF file
|
| 33 |
if uploaded_file.type == "application/pdf":
|
| 34 |
-
with tempfile.NamedTemporaryFile(delete=False) as temp_pdf:
|
| 35 |
-
temp_pdf.write(uploaded_file.read())
|
| 36 |
-
temp_pdf_path = temp_pdf.name
|
| 37 |
-
|
| 38 |
-
# Convert PDF to image
|
| 39 |
try:
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if len(pages) != 1:
|
| 42 |
st.warning("Please upload a PDF with only one page!")
|
| 43 |
else:
|
| 44 |
pil_image = pages[0]
|
| 45 |
except Exception as e:
|
| 46 |
-
st.error(f"Failed to convert PDF to image: {e}
|
| 47 |
finally:
|
| 48 |
# Clean up the temporary file
|
| 49 |
if os.path.exists(temp_pdf_path):
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
from helper import (
|
| 6 |
custom_file_uploader, resize_image, convert_image_to_base64, post_request_and_parse_response,
|
| 7 |
draw_bounding_boxes_for_textract, extract_text_from_textract_blocks, ChatGPTClient
|
| 8 |
)
|
|
|
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
| 11 |
|
|
|
|
| 25 |
|
| 26 |
# Display a placeholder for uploaded image
|
| 27 |
st.warning("Please upload an image or a single-page PDF file!")
|
| 28 |
+
uploaded_file = st.file_uploader("Upload an Image or PDF", type=['TXT', 'PDF'], label_visibility="collapsed")
|
| 29 |
|
| 30 |
pil_image = None
|
| 31 |
if uploaded_file:
|
| 32 |
# Handle PDF file
|
| 33 |
if uploaded_file.type == "application/pdf":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
# Read PDF as bytes and convert to base64
|
| 36 |
+
pdf_bytes = uploaded_file.read()
|
| 37 |
+
pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
|
| 38 |
+
|
| 39 |
+
# Convert base64 to an image object
|
| 40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 41 |
+
temp_pdf.write(base64.b64decode(pdf_base64))
|
| 42 |
+
temp_pdf_path = temp_pdf.name
|
| 43 |
+
|
| 44 |
+
# Convert PDF to image using pdf2image (assumed single page)
|
| 45 |
+
pages = convert_from_path(temp_pdf_path, dpi=200)
|
| 46 |
if len(pages) != 1:
|
| 47 |
st.warning("Please upload a PDF with only one page!")
|
| 48 |
else:
|
| 49 |
pil_image = pages[0]
|
| 50 |
except Exception as e:
|
| 51 |
+
st.error(f"Failed to convert PDF to image: {e}")
|
| 52 |
finally:
|
| 53 |
# Clean up the temporary file
|
| 54 |
if os.path.exists(temp_pdf_path):
|