Spaces:
Runtime error
Runtime error
feat: new flow and new Unstructured receipt parser
Browse files
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 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
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 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
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)
|
|
|
|