Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,24 @@ def update_status(state):
|
|
| 28 |
state = 'processing' #current state becomes
|
| 29 |
return (gr.update(value="snowangel.gif",visible=True),gr.update(value="snowangel.gif",visible=True))
|
| 30 |
|
| 31 |
-
def process_document(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# prepare encoder inputs
|
| 34 |
pixel_values = processor(image, return_tensors="pt").pixel_values
|
|
@@ -65,7 +82,7 @@ paragraph2 = '<p><strong>Training</strong>:<br />The model was trained with a fe
|
|
| 65 |
#demo = gr.Interface(fn=process_document,inputs=gr_image,outputs="json",title="Demo: Donut 🍩 for invoice header retrieval", description=description,
|
| 66 |
# article=article,enable_queue=True, examples=[["example.jpg"], ["example_2.jpg"], ["example_3.jpg"]], cache_examples=False)
|
| 67 |
paragraph3 = '<p><strong>Try it out:</strong><br />To use it, simply upload your image and click 'submit', or click one of the examples to load them.<br /><em>(because this is running on the free cpu tier, it will take about 40 secs before you see a result. On a GPU it takes less than 2 seconds)</em></p><p> </p><p>Have fun 😎</p><p>Toon Beerten</p>'
|
| 68 |
-
|
| 69 |
css = "#inp {height: auto !important; width: 100% !important;}"
|
| 70 |
# css = "@media screen and (max-width: 600px) { .output_image, .input_image {height:20rem !important; width: 100% !important;} }"
|
| 71 |
# css = ".output_image, .input_image {height: 600px !important}"
|
|
@@ -89,10 +106,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 89 |
with gr.Row().style(equal_height=True,height=200,rounded=False):
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
img2 = gr.Image("drinking.gif",label=' ',visible=False).style(rounded=True)
|
| 92 |
-
with gr.Column(scale=
|
| 93 |
-
btn = gr.Button("↓ Extract ↓")
|
| 94 |
-
with gr.Column(scale=
|
| 95 |
-
img3 = gr.Image("snowangel.gif",label=' ',visible=False).style(rounded=True)
|
|
|
|
| 96 |
with gr.Row().style():
|
| 97 |
with gr.Column(scale=2):
|
| 98 |
imgout = gr.Image(label='Uploaded document:',elem_id="inp")
|
|
@@ -100,7 +118,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 100 |
jsonout = gr.JSON(label='Extracted information:')
|
| 101 |
#imgout.clear(fn=update_status,inputs=state,outputs=[img2,img3])
|
| 102 |
#imgout.change(fn=update_status,inputs=state,outputs=[img2,img3])
|
| 103 |
-
btn.click(fn=process_document, inputs=inp, outputs=[jsonout,imgout])
|
|
|
|
|
|
|
| 104 |
|
| 105 |
demo.launch()
|
| 106 |
|
|
|
|
| 28 |
state = 'processing' #current state becomes
|
| 29 |
return (gr.update(value="snowangel.gif",visible=True),gr.update(value="snowangel.gif",visible=True))
|
| 30 |
|
| 31 |
+
def process_document(image,sendimg):
|
| 32 |
+
|
| 33 |
+
if sendimg == True:
|
| 34 |
+
im1 = Image.fromarray(image)
|
| 35 |
+
elif sendimg == False:
|
| 36 |
+
im1 = Image.open('./no_image.jpg')
|
| 37 |
+
|
| 38 |
+
#send notification through telegram
|
| 39 |
+
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 40 |
+
CHAT_ID = os.getenv('TELEGRAM_CHANNEL_ID')
|
| 41 |
+
url = f'https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={CHAT_ID}'
|
| 42 |
+
bio = BytesIO()
|
| 43 |
+
bio.name = 'image.jpeg'
|
| 44 |
+
im1.save(bio, 'JPEG')
|
| 45 |
+
bio.seek(0)
|
| 46 |
+
media = {"type": "photo", "media": "attach://photo", "caption": "New doc is being tried out:"}
|
| 47 |
+
data = {"media": json.dumps(media)}
|
| 48 |
+
response = requests.post(url, files={'photo': bio}, data=data)
|
| 49 |
|
| 50 |
# prepare encoder inputs
|
| 51 |
pixel_values = processor(image, return_tensors="pt").pixel_values
|
|
|
|
| 82 |
#demo = gr.Interface(fn=process_document,inputs=gr_image,outputs="json",title="Demo: Donut 🍩 for invoice header retrieval", description=description,
|
| 83 |
# article=article,enable_queue=True, examples=[["example.jpg"], ["example_2.jpg"], ["example_3.jpg"]], cache_examples=False)
|
| 84 |
paragraph3 = '<p><strong>Try it out:</strong><br />To use it, simply upload your image and click 'submit', or click one of the examples to load them.<br /><em>(because this is running on the free cpu tier, it will take about 40 secs before you see a result. On a GPU it takes less than 2 seconds)</em></p><p> </p><p>Have fun 😎</p><p>Toon Beerten</p>'
|
| 85 |
+
smallprint = '<p>✤ <span style="font-size:10px">To get an idea of the usage, you can opt to get me personally notified via Telegram with the image uploaded. All data collected will be automatically deleted after 48 hours</span></p>'
|
| 86 |
css = "#inp {height: auto !important; width: 100% !important;}"
|
| 87 |
# css = "@media screen and (max-width: 600px) { .output_image, .input_image {height:20rem !important; width: 100% !important;} }"
|
| 88 |
# css = ".output_image, .input_image {height: 600px !important}"
|
|
|
|
| 106 |
with gr.Row().style(equal_height=True,height=200,rounded=False):
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
img2 = gr.Image("drinking.gif",label=' ',visible=False).style(rounded=True)
|
| 109 |
+
with gr.Column(scale=2):
|
| 110 |
+
btn = gr.Button(" ↓ Extract ↓ ")
|
| 111 |
+
with gr.Column(scale=2):
|
| 112 |
+
#img3 = gr.Image("snowangel.gif",label=' ',visible=False).style(rounded=True)
|
| 113 |
+
sendimg = gr.Checkbox(value=True, label="Allow data collection for analytical purpose ✤"),
|
| 114 |
with gr.Row().style():
|
| 115 |
with gr.Column(scale=2):
|
| 116 |
imgout = gr.Image(label='Uploaded document:',elem_id="inp")
|
|
|
|
| 118 |
jsonout = gr.JSON(label='Extracted information:')
|
| 119 |
#imgout.clear(fn=update_status,inputs=state,outputs=[img2,img3])
|
| 120 |
#imgout.change(fn=update_status,inputs=state,outputs=[img2,img3])
|
| 121 |
+
btn.click(fn=process_document, inputs=[inp,sendimg], outputs=[jsonout,imgout])
|
| 122 |
+
|
| 123 |
+
gr.HTML(smallprint)
|
| 124 |
|
| 125 |
demo.launch()
|
| 126 |
|