Spaces:
Sleeping
Sleeping
Commit ·
9ebbd2b
1
Parent(s): 6cf24d5
Fleshed out download file option
Browse files12345678910111228 unmodified lines41424342434445464748495051525354454655564857585960613 unmodified lines65666768697071724 unmodified lines7778796880818283848586from PIL import Imageimport pytesseractimport gradio as grfrom datetime import datetimeimport osfrom flair.data import Sentencefrom flair.models import SequenceTaggerfrom segtok.segmenter import split_singleimport pandas as pd
tagger = SequenceTagger.load("ner-ontonotes")
28 unmodified lines return result, ner
def download_output(ocr_text: str, named_entities: str): print("Download output!")def download_output(image_name: str, ocr_text: str, named_entities: str): try: columns = ["OCR text", "Named entities"] named_entities_list = named_entities.split("\n") data = {ocr_text: named_entities_list} now = datetime.now() datetime_now = now.strftime("%Y%m%d_%H%M%S") output_file = f"analysed_{image_name}_{datetime_now}.csv" output_df = pd.DataFrame(data=data, columns=columns) output_df.to_csv(output_file, index=False)
print("OCR text: ", len(ocr_text)) print("Named Entities: ", len(named_entities)) return output_file
return True except Exception as e: raise gr.Error(f"Something went wrong: here's the error: {e}")
with gr.Blocks() as demo:3 unmodified lines image_in = gr.Image(type="pil") lang = gr.Dropdown(choices, value="eng") btn = gr.Button("Run") image_name = "Test" print("image_in", image_in) with gr.Column(): ocr_text = gr.TextArea(label="OCR output") with gr.Column():4 unmodified lines download_btn = gr.Button("Download output")
btn.click(fn=run, inputs=[image_in, lang], outputs=[ocr_text, ner]) download_btn.click(fn=download_output, inputs=[ocr_text, ner], outputs=[]) download_btn.click( fn=download_output, inputs=[image_name, ocr_text, ner], outputs=[gr.components.File()], )
demo.launch()app.py+22-6