Spaces:
Runtime error
Runtime error
Gradio app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from pytesseract import pytesseract
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
nlp = pipeline(
|
| 10 |
+
"document-question-answering",
|
| 11 |
+
model="impira/layoutlm-document-qa",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Function to perform the question answering
|
| 16 |
+
def perform_question_answering(image, question):
|
| 17 |
+
answer = nlp(image, question)
|
| 18 |
+
|
| 19 |
+
# Format the answer as JSON
|
| 20 |
+
answer_json = json.dumps(answer, indent=4)
|
| 21 |
+
|
| 22 |
+
return answer_json
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Create the Gradio interface
|
| 26 |
+
inputs = [
|
| 27 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
| 28 |
+
gr.inputs.Textbox(label="Question")
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
outputs = gr.outputs.Textbox(label="Answer")
|
| 32 |
+
|
| 33 |
+
iface = gr.Interface(fn=perform_question_answering, inputs=inputs, outputs="text")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Launch the Gradio interface
|
| 37 |
+
iface.launch()
|