Spaces:
Sleeping
Sleeping
amarsaikhan commited on
Commit ·
adc5ef7
1
Parent(s): bb76dd5
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
checkpoint = "openai/clip-vit-large-patch14"
|
| 6 |
+
classifier = pipeline(model=checkpoint, task="zero-shot-image-classification")
|
| 7 |
+
def get_best_label(predictions):
|
| 8 |
+
max_score = 0
|
| 9 |
+
label = ""
|
| 10 |
+
for p in predictions:
|
| 11 |
+
if p['score'] > max_score:
|
| 12 |
+
max_score = p['score']
|
| 13 |
+
label = p['label']
|
| 14 |
+
return label, max_score
|
| 15 |
+
|
| 16 |
+
st.markdown('<h1 style="color:black;">Document Classifier</h1>', unsafe_allow_html=True)
|
| 17 |
+
st.markdown('<h2 style="color:gray;">This model can classify input image to the following categories:</h2>', unsafe_allow_html=True)
|
| 18 |
+
st.markdown('<h3 style="color:gray;"> <ul> <li>Invoice</li> <li>Bank statement</li> <li>Credit bureau</li> </ul> </h3>', unsafe_allow_html=True)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
upload= st.file_uploader('Insert image for classification', type=['png','jpg'])
|
| 23 |
+
c1, c2= st.columns(2)
|
| 24 |
+
if upload is not None:
|
| 25 |
+
image = Image.open(upload)
|
| 26 |
+
c1.header('Input Image')
|
| 27 |
+
c1.image(image)
|
| 28 |
+
print("c1", c1)
|
| 29 |
+
print("c2", c2)
|
| 30 |
+
c2.header('Output')
|
| 31 |
+
c2.subheader('Predicted class :')
|
| 32 |
+
predictions = classifier(image, candidate_labels=["invoice, receipt", "bank statement, financial statement", "credit report"])
|
| 33 |
+
c2.subheader('Predicted class :' + str(get_best_label(predictions)))
|
| 34 |
+
c2.write(str(predictions))
|