laverdes commited on
Commit
2294783
1 Parent(s): af15a96

feat: new flow and new Unstructured receipt parser

Browse files
Files changed (1) hide show
  1. app.py +47 -40
app.py CHANGED
@@ -68,7 +68,7 @@ photo = None
68
  with st.sidebar:
69
  information = st.radio(
70
  "What information inside the 🧾s are you interested in extracting?",
71
- ('Receipt Summary', 'Receipt Menu Details', 'Extract all'))
72
  receipt = st.selectbox('Pick one 🧾', ['1', '2', '3', '4', '5', '6'], index=1)
73
 
74
  # file upload
@@ -103,44 +103,51 @@ else:
103
  with col1:
104
  st.image(image, caption='Your target receipt')
105
 
106
- with st.spinner(f'baking the 🍩s...'):
107
- if information == 'Receipt Summary':
108
- processor = DonutProcessor.from_pretrained("unstructuredio/donut-base-sroie")
109
- pretrained_model = VisionEncoderDecoderModel.from_pretrained("unstructuredio/donut-base-sroie")
110
- task_prompt = f"<s>"
111
- device = "cuda" if torch.cuda.is_available() else "cpu"
112
- pretrained_model.to(device)
113
-
114
- elif information == 'Receipt Menu Details':
115
- processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
116
- pretrained_model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
117
- task_prompt = f"<s_cord-v2>"
118
- device = "cuda" if torch.cuda.is_available() else "cpu"
119
- pretrained_model.to(device)
120
 
121
- else:
122
- processor_a = DonutProcessor.from_pretrained("unstructuredio/donut-base-sroie")
123
- processor_b = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
124
- pretrained_model_a = VisionEncoderDecoderModel.from_pretrained("unstructuredio/donut-base-sroie")
125
- pretrained_model_b = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- device = "cuda" if torch.cuda.is_available() else "cpu"
128
-
129
- with col2:
130
- if information == 'Extract all':
131
- st.info(f'parsing 🧾 (extracting all)...')
132
- pretrained_model, processor, task_prompt = pretrained_model_a, processor_a, f"<s>"
133
- pretrained_model.to(device)
134
- parsed_receipt_info_a, _ = run_prediction(image)
135
- pretrained_model, processor, task_prompt = pretrained_model_b, processor_b, f"<s_cord-v2>"
136
- pretrained_model.to(device)
137
- parsed_receipt_info_b, _ = run_prediction(image)
138
- st.text(f'\nReceipt Summary:')
139
- st.json(parsed_receipt_info_a)
140
- st.text(f'\nReceipt Menu Details:')
141
- st.json(parsed_receipt_info_b)
142
- else:
143
- st.info(f'parsing 🧾...')
144
- parsed_receipt_info, _ = run_prediction(image)
145
- st.text(f'\n{information}')
146
- st.json(parsed_receipt_info)
 
68
  with st.sidebar:
69
  information = st.radio(
70
  "What information inside the 🧾s are you interested in extracting?",
71
+ ('Receipt Summary', 'Receipt Menu Details', 'Extract all', 'Unstructured.io Parser'))
72
  receipt = st.selectbox('Pick one 🧾', ['1', '2', '3', '4', '5', '6'], index=1)
73
 
74
  # file upload
 
103
  with col1:
104
  st.image(image, caption='Your target receipt')
105
 
106
+ if st.button('Parse receipt! 🐍'):
107
+ with st.spinner(f'baking the 🍩s...'):
108
+ if information == 'Receipt Summary':
109
+ processor = DonutProcessor.from_pretrained("unstructuredio/donut-base-sroie")
110
+ pretrained_model = VisionEncoderDecoderModel.from_pretrained("unstructuredio/donut-base-sroie")
111
+ task_prompt = f"<s>"
112
+ device = "cuda" if torch.cuda.is_available() else "cpu"
113
+ pretrained_model.to(device)
 
 
 
 
 
 
114
 
115
+ elif information == 'Receipt Menu Details':
116
+ processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
117
+ pretrained_model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
118
+ task_prompt = f"<s_cord-v2>"
119
+ device = "cuda" if torch.cuda.is_available() else "cpu"
120
+ pretrained_model.to(device)
121
+
122
+ elif information == 'Unstructured.io Parser':
123
+ processor = DonutProcessor.from_pretrained("unstructuredio/donut-base-labelstudio-A1.0")
124
+ pretrained_model = VisionEncoderDecoderModel.from_pretrained("unstructuredio/donut-base-labelstudio-A1.0")
125
+ task_prompt = f"<s>"
126
+ device = "cuda" if torch.cuda.is_available() else "cpu"
127
+ pretrained_model.to(device)
128
+
129
+ else: # Extract all
130
+ processor_a = DonutProcessor.from_pretrained("unstructuredio/donut-base-sroie")
131
+ processor_b = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
132
+ pretrained_model_a = VisionEncoderDecoderModel.from_pretrained("unstructuredio/donut-base-sroie")
133
+ pretrained_model_b = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
134
+ device = "cuda" if torch.cuda.is_available() else "cpu"
135
 
136
+ with col2:
137
+ if information == 'Extract all':
138
+ st.info(f'parsing 🧾 (extracting all)...')
139
+ pretrained_model, processor, task_prompt = pretrained_model_a, processor_a, f"<s>"
140
+ pretrained_model.to(device)
141
+ parsed_receipt_info_a, _ = run_prediction(image)
142
+ pretrained_model, processor, task_prompt = pretrained_model_b, processor_b, f"<s_cord-v2>"
143
+ pretrained_model.to(device)
144
+ parsed_receipt_info_b, _ = run_prediction(image)
145
+ st.text(f'\nReceipt Summary:')
146
+ st.json(parsed_receipt_info_a)
147
+ st.text(f'\nReceipt Menu Details:')
148
+ st.json(parsed_receipt_info_b)
149
+ else:
150
+ st.info(f'parsing 🧾...')
151
+ parsed_receipt_info, _ = run_prediction(image)
152
+ st.text(f'\n{information}')
153
+ st.json(parsed_receipt_info)