deepsh2207 commited on
Commit
6dd82f5
·
1 Parent(s): 03bcc29

Updated UDOP code

Browse files
Files changed (4) hide show
  1. .gitignore +2 -1
  2. app.py +14 -18
  3. packages.txt +1 -0
  4. 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 ViltProcessor, ViltForQuestionAnswering
3
  import torch
4
 
5
  torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
6
 
7
- processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
8
- model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
 
9
 
10
- def answer_question(image, text):
11
- encoding = processor(image, text, return_tensors="pt")
12
-
13
- # forward pass
14
- with torch.no_grad():
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 predicted_answer
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: ViLT"
29
- description = "Gradio Demo for ViLT (Vision and Language Transformer), fine-tuned on VQAv2, a model that can answer questions from images. To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
30
- article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2102.03334' target='_blank'>ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision</a> | <a href='https://github.com/dandelin/ViLT' target='_blank'>Github Repo</a></p>"
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=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