Commit ·
5bca57b
1
Parent(s): 5c6a451
requirements
Browse files
app.py
CHANGED
|
@@ -1,18 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
model = AutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-invoices")
|
| 7 |
|
| 8 |
def answer_question(image, question):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
outputs = model(**inputs)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
answer = tokenizer.decode(inputs
|
| 16 |
return answer
|
| 17 |
|
| 18 |
iface = gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForDocumentQuestionAnswering
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# Load processor and model
|
| 6 |
+
processor = AutoProcessor.from_pretrained("impira/layoutlm-invoices")
|
| 7 |
model = AutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-invoices")
|
| 8 |
|
| 9 |
def answer_question(image, question):
|
| 10 |
+
# Ensure RGB mode
|
| 11 |
+
if image.mode != "RGB":
|
| 12 |
+
image = image.convert("RGB")
|
| 13 |
+
|
| 14 |
+
inputs = processor(image, question, return_tensors="pt")
|
| 15 |
outputs = model(**inputs)
|
| 16 |
+
|
| 17 |
+
start = outputs.start_logits.argmax(-1).item()
|
| 18 |
+
end = outputs.end_logits.argmax(-1).item() + 1
|
| 19 |
+
|
| 20 |
+
answer = processor.tokenizer.decode(inputs["input_ids"][0][start:end])
|
| 21 |
return answer
|
| 22 |
|
| 23 |
iface = gr.Interface(
|