sotirios-slv commited on
Commit
9ebbd2b
1 Parent(s): 6cf24d5

Fleshed out download file option

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -1,10 +1,12 @@
1
  from PIL import Image
2
  import pytesseract
3
  import gradio as gr
 
4
  import os
5
  from flair.data import Sentence
6
  from flair.models import SequenceTagger
7
  from segtok.segmenter import split_single
 
8
 
9
  tagger = SequenceTagger.load("ner-ontonotes")
10
 
@@ -39,13 +41,21 @@ def run(image, lang="eng"):
39
  return result, ner
40
 
41
 
42
- def download_output(ocr_text: str, named_entities: str):
43
- print("Download output!")
 
 
 
 
 
 
 
 
44
 
45
- print("OCR text: ", len(ocr_text))
46
- print("Named Entities: ", len(named_entities))
47
 
48
- return True
 
49
 
50
 
51
  with gr.Blocks() as demo:
@@ -55,6 +65,8 @@ with gr.Blocks() as demo:
55
  image_in = gr.Image(type="pil")
56
  lang = gr.Dropdown(choices, value="eng")
57
  btn = gr.Button("Run")
 
 
58
  with gr.Column():
59
  ocr_text = gr.TextArea(label="OCR output")
60
  with gr.Column():
@@ -65,6 +77,10 @@ with gr.Blocks() as demo:
65
  download_btn = gr.Button("Download output")
66
 
67
  btn.click(fn=run, inputs=[image_in, lang], outputs=[ocr_text, ner])
68
- download_btn.click(fn=download_output, inputs=[ocr_text, ner], outputs=[])
 
 
 
 
69
 
70
  demo.launch()
 
1
  from PIL import Image
2
  import pytesseract
3
  import gradio as gr
4
+ from datetime import datetime
5
  import os
6
  from flair.data import Sentence
7
  from flair.models import SequenceTagger
8
  from segtok.segmenter import split_single
9
+ import pandas as pd
10
 
11
  tagger = SequenceTagger.load("ner-ontonotes")
12
 
 
41
  return result, ner
42
 
43
 
44
+ def download_output(image_name: str, ocr_text: str, named_entities: str):
45
+ try:
46
+ columns = ["OCR text", "Named entities"]
47
+ named_entities_list = named_entities.split("\n")
48
+ data = {ocr_text: named_entities_list}
49
+ now = datetime.now()
50
+ datetime_now = now.strftime("%Y%m%d_%H%M%S")
51
+ output_file = f"analysed_{image_name}_{datetime_now}.csv"
52
+ output_df = pd.DataFrame(data=data, columns=columns)
53
+ output_df.to_csv(output_file, index=False)
54
 
55
+ return output_file
 
56
 
57
+ except Exception as e:
58
+ raise gr.Error(f"Something went wrong: here's the error: {e}")
59
 
60
 
61
  with gr.Blocks() as demo:
 
65
  image_in = gr.Image(type="pil")
66
  lang = gr.Dropdown(choices, value="eng")
67
  btn = gr.Button("Run")
68
+ image_name = "Test"
69
+ print("image_in", image_in)
70
  with gr.Column():
71
  ocr_text = gr.TextArea(label="OCR output")
72
  with gr.Column():
 
77
  download_btn = gr.Button("Download output")
78
 
79
  btn.click(fn=run, inputs=[image_in, lang], outputs=[ocr_text, ner])
80
+ download_btn.click(
81
+ fn=download_output,
82
+ inputs=[image_name, ocr_text, ner],
83
+ outputs=[gr.components.File()],
84
+ )
85
 
86
  demo.launch()