eagle0504 commited on
Commit
bed1057
·
verified ·
1 Parent(s): 0451020

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import os
4
+ from helper import (
5
+ 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"]
11
+ TEXTRACT_API_URL = "https://2tsig211e0.execute-api.us-east-1.amazonaws.com/my_textract"
12
+
13
+ st.set_page_config(page_title="Chat with OCR", layout="wide")
14
+
15
+ # Initialize chat history if not in session state
16
+ if "messages" not in st.session_state:
17
+ st.session_state.messages = []
18
+
19
+ # Sidebar for image upload
20
+ with st.sidebar:
21
+ st.title("Upload and Display Images")
22
+ uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
23
+
24
+ if uploaded_image:
25
+ pil_image = Image.open(uploaded_image)
26
+ resized_image = resize_image(pil_image)
27
+
28
+ with st.expander("Original Image", expanded=False):
29
+ st.image(pil_image, caption="Uploaded Image", use_column_width=True)
30
+
31
+ # Convert image to base64 and send to Textract API
32
+ image_base64 = convert_image_to_base64(resized_image)
33
+ payload = {"image": image_base64}
34
+ result_dict = post_request_and_parse_response(TEXTRACT_API_URL, payload)
35
+
36
+ # Draw bounding boxes
37
+ image_with_boxes = draw_bounding_boxes_for_textract(resized_image.copy(), result_dict)
38
+
39
+ with st.expander("Image with Bounding Boxes", expanded=True):
40
+ st.image(image_with_boxes, caption="Image with Bounding Boxes", use_column_width=True)
41
+
42
+ # Extract text from Textract
43
+ cleaned_up_body = extract_text_from_textract_blocks(result_dict['body'])
44
+
45
+ # Main chat interface
46
+ st.title("Chat with OCR Output")
47
+
48
+ # Display previous messages from session state
49
+ for message in st.session_state.messages:
50
+ with st.chat_message(message["role"]):
51
+ st.markdown(message["content"])
52
+
53
+ # Initialize ChatGPTClient with session state history
54
+ if uploaded_image:
55
+ history_copy = st.session_state.messages.copy()
56
+
57
+ if cleaned_up_body:
58
+ history_copy.append({"role": "system", "content": cleaned_up_body})
59
+
60
+ bot = ChatGPTClient(
61
+ api_key=OPENAI_API_KEY,
62
+ protocol="You are fed with the text portion of json file that come out of OCR after scanning an image. User will ask you questions about this json file.",
63
+ body=cleaned_up_body
64
+ )
65
+ bot.history = history_copy # Set ChatGPT history to session state messages
66
+
67
+ # React to user input
68
+ if prompt := st.chat_input("Ask me about the image"):
69
+ # Display user message in chat container
70
+ st.chat_message("user").markdown(prompt)
71
+ st.session_state.messages.append({"role": "user", "content": prompt})
72
+
73
+ # Generate a response using ChatGPTClient
74
+ if uploaded_image:
75
+ response = bot.generate_response(prompt)
76
+ else:
77
+ response = "Please upload an image before asking questions."
78
+
79
+ # Display assistant message in chat container
80
+ st.chat_message("assistant").markdown(response)
81
+ st.session_state.messages.append({"role": "assistant", "content": response})