ajimeno commited on
Commit
ca53d7c
1 Parent(s): 7a740e0

First commit

Browse files
Files changed (5) hide show
  1. README.md +7 -6
  2. app.py +92 -0
  3. document.png +0 -0
  4. requirements.txt +7 -0
  5. rsz_unstructured_logo.png +0 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
- title: Unstructured Chipper App
3
- emoji: 📉
4
  colorFrom: green
5
- colorTo: purple
6
  sdk: streamlit
7
- sdk_version: 1.26.0
8
  app_file: app.py
9
  pinned: false
10
- license: cc-by-nc-4.0
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Ved Fine Tuned
3
+ emoji: 🦀
4
  colorFrom: green
5
+ colorTo: blue
6
  sdk: streamlit
7
+ sdk_version: 1.19.0
8
  app_file: app.py
9
  pinned: false
10
+ license: other
11
+ duplicated_from: unstructuredio/ved-pre-trained
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from torch import nn
4
+ import streamlit as st
5
+ import os
6
+
7
+ from PIL import Image
8
+ from io import BytesIO
9
+ from transformers import VisionEncoderDecoderModel, VisionEncoderDecoderConfig, DonutProcessor, DonutImageProcessor, AutoTokenizer
10
+
11
+ def run_prediction(sample, model, processor):
12
+
13
+ pixel_values = processor(np.array(
14
+ sample,
15
+ np.float32,
16
+ ), return_tensors="pt").pixel_values
17
+
18
+ with torch.no_grad():
19
+ outputs = model.generate(
20
+ pixel_values.to(device),
21
+ decoder_input_ids=processor.tokenizer("<s><s_plain>", add_special_tokens=False, return_tensors="pt").input_ids.to(device),
22
+ do_sample=True,
23
+ top_p=0.92,
24
+ top_k=5,
25
+ no_repeat_ngram_size=10,
26
+ num_beams=3
27
+ )
28
+
29
+ # process output
30
+ prediction = processor.batch_decode(outputs)[0]
31
+ print(prediction)
32
+
33
+ return prediction
34
+
35
+
36
+ logo = Image.open("./rsz_unstructured_logo.png")
37
+ st.image(logo)
38
+
39
+ st.markdown('''
40
+ ### Chipper
41
+ Chipper is an OCR-free Document Understanding Transformer. It was pre-trained with over 1M documents from public sources and fine-tuned on a large range of documents.
42
+
43
+ At [Unstructured.io](https://github.com/Unstructured-IO/unstructured) we are on a mission to build custom preprocessing pipelines for labeling, training, or production ML-ready pipelines.
44
+ Come and join us in our public repos and contribute! Each of your contributions and feedback holds great value and is very significant to the community.
45
+ ''')
46
+
47
+ image_upload = None
48
+ photo = None
49
+ with st.sidebar:
50
+ # file upload
51
+ uploaded_file = st.file_uploader("Upload a document")
52
+ if uploaded_file is not None:
53
+ # To read file as bytes:
54
+ image_bytes_data = uploaded_file.getvalue()
55
+ image_upload = Image.open(BytesIO(image_bytes_data))
56
+
57
+ if image_upload:
58
+ image = image_upload
59
+ else:
60
+ image = Image.open(f"./document.png")
61
+
62
+ st.image(image, caption='Your target document')
63
+
64
+ with st.spinner(f'Processing the document ...'):
65
+ pre_trained_model = "unstructuredio/chipper-fast-fine-tuning"
66
+ processor = DonutProcessor.from_pretrained(pre_trained_model)
67
+ model = VisionEncoderDecoderModel.from_pretrained(pre_trained_model)
68
+
69
+ from huggingface_hub import hf_hub_download
70
+
71
+ lm_head_file = hf_hub_download(
72
+ repo_id=pre_trained_model, filename="lm_head.pth"
73
+ )
74
+
75
+ rank = 128
76
+ model.decoder.lm_head = nn.Sequential(
77
+ nn.Linear(model.decoder.lm_head.weight.shape[1], rank, bias=False),
78
+ nn.Linear(rank, rank, bias=False),
79
+ nn.Linear(rank, model.decoder.lm_head.weight.shape[0], bias=True),
80
+ )
81
+
82
+ model.decoder.lm_head.load_state_dict(torch.load(lm_head_file))
83
+
84
+ device = "cuda" if torch.cuda.is_available() else "cpu"
85
+
86
+ model.eval()
87
+ model.to(device)
88
+
89
+ st.info(f'Parsing document')
90
+ parsed_info = run_prediction(image.convert("RGB"), model, processor)
91
+ st.text(f'\nDocument:')
92
+ st.text_area('Output text', value=parsed_info, height=800)
document.png ADDED
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ altair<5
2
+ huggingface_hub
3
+ numpy
4
+ opencv-python
5
+ streamlit
6
+ torch==1.13.1
7
+ transformers
rsz_unstructured_logo.png ADDED