Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,11 @@ layoutlm_tokenizer = LayoutLMTokenizer.from_pretrained(model_name)
|
|
| 10 |
|
| 11 |
# Function to extract text from image using LayoutLM
|
| 12 |
def extract_text_from_image(image):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with torch.no_grad():
|
| 15 |
outputs = layoutlm_model(**inputs)
|
| 16 |
predicted_tokens = layoutlm_tokenizer.convert_ids_to_tokens(outputs.logits.argmax(2).squeeze().tolist())
|
|
@@ -18,31 +22,31 @@ def extract_text_from_image(image):
|
|
| 18 |
extracted_text = " ".join([token for token in predicted_tokens if token not in ['[CLS]', '[SEP]', '[PAD]']])
|
| 19 |
return extracted_text
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Streamlit App
|
| 22 |
st.set_page_config(page_title="Invoice Extractor")
|
| 23 |
-
st.header("
|
| 24 |
|
| 25 |
-
prompt = st.text_input("Input Prompt: ",
|
| 26 |
-
uploaded_file = st.file_uploader("
|
| 27 |
-
image =
|
| 28 |
|
| 29 |
if uploaded_file is not None:
|
| 30 |
image = Image.open(uploaded_file)
|
| 31 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 32 |
|
| 33 |
-
submit = st.button("
|
| 34 |
-
|
| 35 |
-
input_prompt = """
|
| 36 |
-
You are an expert in understanding invoices. You will receive input images as invoices and
|
| 37 |
-
you will have to answer questions based on the input image.
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
if submit:
|
| 41 |
-
if image:
|
|
|
|
|
|
|
| 42 |
extracted_text = extract_text_from_image(image)
|
| 43 |
# Implement your response generation logic here based on the extracted text and prompt
|
| 44 |
response = "Response generated based on the extracted text and prompt."
|
| 45 |
st.subheader("Generated Response:")
|
| 46 |
st.write(response)
|
| 47 |
-
else:
|
| 48 |
-
st.warning("Please upload an image.")
|
|
|
|
| 10 |
|
| 11 |
# Function to extract text from image using LayoutLM
|
| 12 |
def extract_text_from_image(image):
|
| 13 |
+
# Convert image to text using OCR (Optical Character Recognition) before tokenizing
|
| 14 |
+
# For simplicity, we'll assume the image is converted to text directly without OCR
|
| 15 |
+
# You may need to use OCR libraries like pytesseract for real-world scenarios
|
| 16 |
+
text = image_to_text(image)
|
| 17 |
+
inputs = layoutlm_tokenizer(text, return_tensors="pt")
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = layoutlm_model(**inputs)
|
| 20 |
predicted_tokens = layoutlm_tokenizer.convert_ids_to_tokens(outputs.logits.argmax(2).squeeze().tolist())
|
|
|
|
| 22 |
extracted_text = " ".join([token for token in predicted_tokens if token not in ['[CLS]', '[SEP]', '[PAD]']])
|
| 23 |
return extracted_text
|
| 24 |
|
| 25 |
+
# Function to convert image to text (replace with OCR library if needed)
|
| 26 |
+
def image_to_text(image):
|
| 27 |
+
# For simplicity, return a placeholder text
|
| 28 |
+
return "Invoice text extracted from the image."
|
| 29 |
+
|
| 30 |
# Streamlit App
|
| 31 |
st.set_page_config(page_title="Invoice Extractor")
|
| 32 |
+
st.header("Invoice Extractor")
|
| 33 |
|
| 34 |
+
prompt = st.text_input("Input Prompt: ", "Please provide details about this invoice.")
|
| 35 |
+
uploaded_file = st.file_uploader("Upload an invoice image...", type=["jpg", "jpeg", "png"])
|
| 36 |
+
image = None
|
| 37 |
|
| 38 |
if uploaded_file is not None:
|
| 39 |
image = Image.open(uploaded_file)
|
| 40 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 41 |
|
| 42 |
+
submit = st.button("Extract and Generate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if submit:
|
| 45 |
+
if image is None:
|
| 46 |
+
st.warning("Please upload an image.")
|
| 47 |
+
else:
|
| 48 |
extracted_text = extract_text_from_image(image)
|
| 49 |
# Implement your response generation logic here based on the extracted text and prompt
|
| 50 |
response = "Response generated based on the extracted text and prompt."
|
| 51 |
st.subheader("Generated Response:")
|
| 52 |
st.write(response)
|
|
|
|
|
|