pratikshahp commited on
Commit
efdae19
·
verified ·
1 Parent(s): 474d16e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
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
- inputs = layoutlm_tokenizer(image, return_tensors="pt")
 
 
 
 
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("Gemini Application")
24
 
25
- prompt = st.text_input("Input Prompt: ", key="input")
26
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
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("Tell me about the invoice")
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)