Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,10 @@
|
|
| 1 |
from PIL import Image
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
# Load Hugging Face models
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
text_generation_model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 10 |
-
text_generation_tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 11 |
-
|
| 12 |
-
# Function to extract text from image
|
| 13 |
-
def extract_text_from_image(image):
|
| 14 |
-
inputs = image_to_text_tokenizer(image, return_tensors="pt")
|
| 15 |
-
with torch.no_grad():
|
| 16 |
-
outputs = image_to_text_model(**inputs)
|
| 17 |
-
return image_to_text_tokenizer.batch_decode(outputs.logits, skip_special_tokens=True)[0]
|
| 18 |
-
|
| 19 |
-
# Function to generate response
|
| 20 |
-
def generate_response(prompt, text):
|
| 21 |
-
inputs = text_generation_tokenizer(prompt + text, return_tensors="pt")
|
| 22 |
-
with torch.no_grad():
|
| 23 |
-
outputs = text_generation_model.generate(**inputs)
|
| 24 |
-
return text_generation_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 25 |
|
| 26 |
# Streamlit App
|
| 27 |
st.set_page_config(page_title="Invoice Extractor")
|
|
@@ -42,3 +25,19 @@ if uploaded_file is not None:
|
|
| 42 |
st.write(response)
|
| 43 |
else:
|
| 44 |
st.warning("Please upload an image.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import LayoutLMForTokenClassification, LayoutLMTokenizer
|
|
|
|
| 4 |
|
| 5 |
# Load Hugging Face models
|
| 6 |
+
layoutlm_model = LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased")
|
| 7 |
+
layoutlm_tokenizer = LayoutLMTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Streamlit App
|
| 10 |
st.set_page_config(page_title="Invoice Extractor")
|
|
|
|
| 25 |
st.write(response)
|
| 26 |
else:
|
| 27 |
st.warning("Please upload an image.")
|
| 28 |
+
|
| 29 |
+
# Function to extract text from image
|
| 30 |
+
def extract_text_from_image(image):
|
| 31 |
+
inputs = layoutlm_tokenizer(image, return_tensors="pt")
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = layoutlm_model(**inputs)
|
| 34 |
+
# Assume we want to extract only the text from the first token
|
| 35 |
+
tokens = layoutlm_tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 36 |
+
text = " ".join([token for token in tokens if token != "[PAD]"])
|
| 37 |
+
return text
|
| 38 |
+
|
| 39 |
+
# Function to generate response
|
| 40 |
+
def generate_response(prompt, text):
|
| 41 |
+
# You can use your existing code here for response generation
|
| 42 |
+
# For this example, let's simply return the extracted text with the prompt
|
| 43 |
+
return f"{prompt} {text}"
|