Spaces:
Runtime error
Runtime error
Commit
·
6dd82f5
1
Parent(s):
03bcc29
Updated UDOP code
Browse files- .gitignore +2 -1
- app.py +14 -18
- packages.txt +1 -0
- requirements.txt +3 -1
.gitignore
CHANGED
@@ -2,4 +2,5 @@ venv
|
|
2 |
venv/
|
3 |
*.ipynb
|
4 |
flagged
|
5 |
-
*.jpg
|
|
|
|
2 |
venv/
|
3 |
*.ipynb
|
4 |
flagged
|
5 |
+
*.jpg
|
6 |
+
VilT
|
app.py
CHANGED
@@ -1,33 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
outputs = model(**encoding)
|
16 |
-
|
17 |
-
logits = outputs.logits
|
18 |
-
idx = logits.argmax(-1).item()
|
19 |
-
predicted_answer = model.config.id2label[idx]
|
20 |
|
21 |
-
return
|
22 |
|
23 |
image = gr.Image(type="pil")
|
24 |
question = gr.Textbox(label="Question")
|
25 |
answer = gr.Textbox(label="Predicted answer")
|
26 |
examples = [["cats.jpg", "How many cats are there?"]]
|
27 |
|
28 |
-
title = "Interactive demo:
|
29 |
-
description = "Gradio Demo for
|
30 |
-
|
31 |
|
32 |
interface = gr.Interface(fn=answer_question,
|
33 |
inputs=[image, question],
|
@@ -35,5 +31,5 @@ interface = gr.Interface(fn=answer_question,
|
|
35 |
examples=examples,
|
36 |
title=title,
|
37 |
description=description,
|
38 |
-
article=
|
39 |
interface.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import UdopProcessor, UdopForConditionalGeneration
|
3 |
import torch
|
4 |
|
5 |
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
6 |
|
7 |
+
repo_id = "microsoft/udop-large"
|
8 |
+
processor = UdopProcessor.from_pretrained(repo_id)
|
9 |
+
model = UdopForConditionalGeneration.from_pretrained(repo_id)
|
10 |
|
11 |
+
|
12 |
+
def answer_question(img, user_query):
|
13 |
+
encoding = processor(images=img, text=user_query, return_tensors="pt")
|
14 |
+
outputs = model.generate(**encoding, max_new_tokens=20)
|
15 |
+
generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
return generated_text
|
18 |
|
19 |
image = gr.Image(type="pil")
|
20 |
question = gr.Textbox(label="Question")
|
21 |
answer = gr.Textbox(label="Predicted answer")
|
22 |
examples = [["cats.jpg", "How many cats are there?"]]
|
23 |
|
24 |
+
title = "Interactive demo: UDOP"
|
25 |
+
description = "Gradio Demo for UDOP, a model that can answer questions from images/pdfs. To use it, simply upload your image or pdf and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
|
26 |
+
tochange_article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2212.02623' target='_blank'>Unifying Vision, Text, and Layout for Universal Document Processing</a> | <a href='https://github.com/microsoft/UDOP' target='_blank'>Github Repo</a></p>"
|
27 |
|
28 |
interface = gr.Interface(fn=answer_question,
|
29 |
inputs=[image, question],
|
|
|
31 |
examples=examples,
|
32 |
title=title,
|
33 |
description=description,
|
34 |
+
article=tochange_article)
|
35 |
interface.launch(debug=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
tesseract-ocr
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
gradio
|
2 |
torch
|
3 |
-
git+https://github.com/huggingface/transformers.git
|
|
|
|
|
|
1 |
gradio
|
2 |
torch
|
3 |
+
git+https://github.com/huggingface/transformers.git
|
4 |
+
sentencepiece
|
5 |
+
pytesseract
|