eagle0504 commited on
Commit
b7a3caf
1 Parent(s): 6a12942

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -32
app.py CHANGED
@@ -5,6 +5,8 @@ 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
 
9
  # Load OpenAI API Key from environment variable
10
  OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
@@ -21,37 +23,57 @@ with st.sidebar:
21
  st.title("🖼️ Upload and Display Images")
22
 
23
  # Display a placeholder for uploaded image
24
- st.warning("Please upload an image file!")
25
- uploaded_image = st.file_uploader("Upload an Image", type=['TXT', 'PDF'], label_visibility="collapsed")
26
 
27
- if uploaded_image:
28
- pil_image = Image.open(uploaded_image)
29
- resized_image = resize_image(pil_image)
30
-
31
- with st.expander("Original Image", expanded=False):
32
- st.image(pil_image, caption="Uploaded Image", use_column_width=True)
33
-
34
- # Convert image to base64 and send to Textract API
35
- image_base64 = convert_image_to_base64(resized_image)
36
- payload = {"image": image_base64}
37
- result_dict = post_request_and_parse_response(TEXTRACT_API_URL, payload)
38
-
39
- # Draw bounding boxes
40
- image_with_boxes = draw_bounding_boxes_for_textract(resized_image.copy(), result_dict)
41
-
42
- with st.expander("Image with Bounding Boxes", expanded=True):
43
- st.image(image_with_boxes, caption="Image with Bounding Boxes", use_column_width=True)
44
-
45
- # Extract text from Textract
46
- cleaned_up_body = extract_text_from_textract_blocks(result_dict['body'])
47
-
48
- # Display JSON body in the sidebar inside an expander (default not expanded)
49
- with st.expander("View JSON Body", expanded=False):
50
- st.json(result_dict)
51
-
52
- # Display cleaned-up body (text extracted from JSON) in the sidebar inside an expander (default not expanded)
53
- with st.expander("View Cleaned-up Text", expanded=False):
54
- st.text(cleaned_up_body)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # Add some space at the bottom of the sidebar before the "Clear Session" button
57
  st.sidebar.markdown("<br><br><br><br>", unsafe_allow_html=True)
@@ -69,7 +91,7 @@ for message in st.session_state.messages:
69
  st.markdown(message["content"])
70
 
71
  # Initialize ChatGPTClient with session state history
72
- if uploaded_image:
73
  history_copy = st.session_state.messages.copy()
74
 
75
  if cleaned_up_body:
@@ -89,7 +111,7 @@ if prompt := st.chat_input("Ask me about the image"):
89
  st.session_state.messages.append({"role": "user", "content": prompt})
90
 
91
  # Generate a response using ChatGPTClient
92
- if uploaded_image:
93
  response = bot.generate_response(prompt)
94
  else:
95
  response = "Please upload an image before asking questions."
 
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
 
11
  # Load OpenAI API Key from environment variable
12
  OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
 
23
  st.title("🖼️ Upload and Display Images")
24
 
25
  # Display a placeholder for uploaded image
26
+ st.warning("Please upload an image or a single-page PDF file!")
27
+ uploaded_file = st.file_uploader("Upload an Image or PDF", type=['TXT', 'PDF'], label_visibility="collapsed")
28
 
29
+ pil_image = None
30
+ if uploaded_file:
31
+ # Handle PDF file
32
+ if uploaded_file.type == "application/pdf":
33
+ with tempfile.NamedTemporaryFile(delete=False) as temp_pdf:
34
+ temp_pdf.write(uploaded_file.read())
35
+ temp_pdf_path = temp_pdf.name
36
+
37
+ # Convert PDF to image
38
+ try:
39
+ pages = convert_from_path(temp_pdf_path, dpi=200)
40
+ if len(pages) != 1:
41
+ st.warning("Please upload a PDF with only one page!")
42
+ else:
43
+ pil_image = pages[0]
44
+ except Exception as e:
45
+ st.error(f"Failed to convert PDF to image: {e}")
46
+ else:
47
+ # Handle image file
48
+ pil_image = Image.open(uploaded_file)
49
+
50
+ if pil_image:
51
+ resized_image = resize_image(pil_image)
52
+
53
+ with st.expander("Original Image", expanded=False):
54
+ st.image(pil_image, caption="Uploaded Image", use_column_width=True)
55
+
56
+ # Convert image to base64 and send to Textract API
57
+ image_base64 = convert_image_to_base64(resized_image)
58
+ payload = {"image": image_base64}
59
+ result_dict = post_request_and_parse_response(TEXTRACT_API_URL, payload)
60
+
61
+ # Draw bounding boxes
62
+ image_with_boxes = draw_bounding_boxes_for_textract(resized_image.copy(), result_dict)
63
+
64
+ with st.expander("Image with Bounding Boxes", expanded=True):
65
+ st.image(image_with_boxes, caption="Image with Bounding Boxes", use_column_width=True)
66
+
67
+ # Extract text from Textract
68
+ cleaned_up_body = extract_text_from_textract_blocks(result_dict['body'])
69
+
70
+ # Display JSON body in the sidebar inside an expander (default not expanded)
71
+ with st.expander("View JSON Body", expanded=False):
72
+ st.json(result_dict)
73
+
74
+ # Display cleaned-up body (text extracted from JSON) in the sidebar inside an expander (default not expanded)
75
+ with st.expander("View Cleaned-up Text", expanded=False):
76
+ st.text(cleaned_up_body)
77
 
78
  # Add some space at the bottom of the sidebar before the "Clear Session" button
79
  st.sidebar.markdown("<br><br><br><br>", unsafe_allow_html=True)
 
91
  st.markdown(message["content"])
92
 
93
  # Initialize ChatGPTClient with session state history
94
+ if uploaded_file and pil_image:
95
  history_copy = st.session_state.messages.copy()
96
 
97
  if cleaned_up_body:
 
111
  st.session_state.messages.append({"role": "user", "content": prompt})
112
 
113
  # Generate a response using ChatGPTClient
114
+ if uploaded_file and pil_image:
115
  response = bot.generate_response(prompt)
116
  else:
117
  response = "Please upload an image before asking questions."