File size: 13,277 Bytes
93d81ef
 
 
 
 
 
4a4962a
93d81ef
 
 
4a4962a
93d81ef
 
 
7522b53
93d81ef
52274e6
 
 
 
93d81ef
 
 
 
7522b53
67955c5
 
93d81ef
 
 
 
67955c5
 
93d81ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7522b53
93d81ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7522b53
 
93d81ef
7522b53
93d81ef
 
 
 
 
 
7522b53
93d81ef
 
 
 
 
 
 
 
7522b53
 
93d81ef
 
 
 
 
 
 
7522b53
 
93d81ef
 
7522b53
93d81ef
 
 
 
 
 
 
7522b53
 
 
 
 
 
 
 
 
 
 
93d81ef
7522b53
93d81ef
 
7522b53
93d81ef
 
 
 
 
7522b53
93d81ef
7522b53
93d81ef
 
 
585e282
 
93d81ef
 
7522b53
93d81ef
 
 
 
 
 
 
 
 
7522b53
93d81ef
 
 
7522b53
93d81ef
 
 
 
7522b53
93d81ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7522b53
93d81ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7522b53
93d81ef
 
 
 
 
7522b53
93d81ef
 
 
 
 
7522b53
93d81ef
 
 
 
7522b53
93d81ef
7522b53
93d81ef
 
7522b53
93d81ef
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import gradio as gr
import requests
import json
from base64 import b64encode
import fitz
import os
import pickle
import pytesseract
import numpy as np
from langchain import OpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.prompts import PromptTemplate
from langchain.chains.summarize import load_summarize_chain
from langchain.callbacks import get_openai_callback
import logging

logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S')
chkey = os.environ["API_TOKEN"]
token = b64encode(f"{chkey}".encode('utf-8')).decode("ascii")

with gr.Blocks(gr.themes.Soft()) as demo:
    logging.info("*** App Starting ***")
    # Introduction
    intro = gr.Textbox(label="Introduction", interactive=False, value="An application to search for any Limited company in the UK, check when the latest Accounts are filed and summarize the account filing using OpenAI.\nUses UK Companies House API to search and get the company information. And Langchain's Summarization chain to create a summary. (Needs an OpenAI API key)")
    # Create input Text search box
    input_box = gr.Textbox(label="Input search string for a UK Company Name")
    # Button to initiate the Company Search
    search_btn = gr.Button("Search")
    # State variable to store the Document ID
    doc_id = gr.State()
    # Column to display the Company search result - List of companies
    with gr.Column(visible=False) as output_col:
        company_list_box = gr.Radio(choices=["Test1","Test2"],label="Company search result")
        #company_list = gr.DataFrame(headers=['Company Number','Company Name','Date Created','Registered Address'], interactive=False)
    
    # Text box to display the latest filing information
    display_filing = gr.Textbox(label="",interactive=False, visible=False)

    # Button to get the latest filing document
    submit_btn = gr.Button("Get latest filing", visible=False)

    # Text box Display the Document information
    display_filing_doc_info = gr.Textbox(label="",interactive=False, visible=False)

    # OpenAPI Key Input box
    openapi_key_input = gr.Textbox(label="OpenAI API Key", type='password', interactive=True, visible=False)

    # Button to initiate the processing of the Document - OPENAI Call initiated here too
    process_filing_btn = gr.Button("Summarize the Account filing", visible=False)

    # Text box showing the progress of the processing and the message
    processed_info = gr.Textbox(label="",interactive=False, visible=False)

    # Final Text box to display the summary of the Annual Accounts
    summary_text = gr.Textbox(label="Summary using OPENAI",interactive=False, visible=False)

    # Clear all fields to start again
    clear = gr.Button("Clear")

    # Function that does the Company search based on a search string. Gets the top 10 results 
    def company_search(text):
        logging.info("*** New Search Starting ***")
        logging.info(f'Search term : {text}')
        url = "https://api.company-information.service.gov.uk/advanced-search/companies?company_name_includes=" + text + "&company_status=active&size=10"
        logging.info(f'Calling Companies House API Advanced search : {url}')
        auth = f'Basic {token}'
        payload={}
        headers = {
            'Authorization': auth
        }
        response = requests.request("GET", url, headers=headers, data=payload)
        logging.info(f'API Response Code : {response.status_code}')
        select_resp = []
        if response.status_code == 200:
            resp = json.loads(response.text)
            for comp in resp["items"]:
                addr = []
                for key, value in comp["registered_office_address"].items():
                    addr.append(value) 
                select_resp.append(comp["company_number"] + " : " + comp["company_name"] + " : " + ', '.join(addr))
            resp_joined = (','.join(select_resp))
            logging.info(f'Response list : {resp_joined}')
            return {output_col: gr.update(visible=True), company_list_box: gr.update(choices=select_resp,interactive=True)}
        else:
            select_resp.append("No matching companies found")
            return {output_col: gr.update(visible=True), company_list_box: gr.update(choices=select_resp,interactive=False)}            
    
    # Function to get the Filing information of a selected company
    def company_selected(selected_company, docid):
        logging.info("* Company selected. Getting Filing History *")
        logging.info(f'User Selection : {selected_company}')
        regid = selected_company.split(' : ')[0]
        filings_url = "https://api.company-information.service.gov.uk/company/" + regid + "/filing-history?category=accounts&items_per_page=1"
        logging.info(f'Calling Companies House API Filings Endpoint : {filings_url}')
        auth = f'Basic {token}'
        payload={}
        headers = {
            'Authorization': auth
        }
        response = requests.request("GET", filings_url, headers=headers, data=payload)
        resp = json.loads(response.text)
        logging.info(f'API Response Code : {response.status_code}')
        if response.status_code == 200:
            if len(resp["items"])>0:
                resp_value = f'Latest filing done on {resp["items"][0]["date"]}.'
                if "links" in resp["items"][0]:
                    if "document_metadata" in resp["items"][0]["links"]:
                        docid = resp["items"][0]["links"]["document_metadata"].rsplit('/',1)[-1]
                        return {display_filing: gr.Textbox.update(visible=True, value=resp_value), submit_btn: gr.update(visible=True), doc_id : docid}
                    else:
                        resp_value += "But Document Metadata is not available."
                        return {display_filing: gr.Textbox.update(visible=True, value=resp_value), submit_btn: gr.update(visible=False), doc_id : "None"}
                else:
                    resp_value += "But Links to the filing not available."
                    return {display_filing: gr.Textbox.update(visible=True, value=resp_value), submit_btn: gr.update(visible=False), doc_id : "None"}
            else:
                return {display_filing: gr.Textbox.update(visible=True, value="No record of accounts filed for the company"), submit_btn: gr.update(visible=False), doc_id : "None"}
        else:
            return {display_filing: gr.Textbox.update(visible=True, value="No record of accounts filed for the company"), submit_btn: gr.update(visible=False), doc_id : "None"}

    # Function to get the Filing document related to the latest Annual Account filing
    def get_filing(docid):
        logging.info("* Getting Filing Document for latest filing *")
        doc_url = "https://document-api.company-information.service.gov.uk/document/" + docid + "/content"
        logging.info(f'Calling Companies House Documents API : {doc_url}')
        auth = f'Basic {token}'
        payload={}
        headers = {
            'Authorization': auth,
            'Accept': 'application/pdf'
        }
        response = requests.request("GET", doc_url, headers=headers, data=payload)
        logging.info(f'API Response Code : {response.status_code}')
        content_type = response.headers['Content-Type']
        resp_value = f'Filing document is of type {content_type}. '
        if content_type == 'application/pdf':
            filename = f'doc_{docid}.pdf'
            filepath = './data/'+filename
            with open(filepath, 'wb') as f:
                f.write(response.content)
            pdf_document = fitz.open(filepath)
            resp_value += f'PDF saved as: {filename}. There are a total of {pdf_document.page_count} pages'
            logging.info(resp_value)
            return {display_filing_doc_info: gr.Textbox.update(visible=True, value=resp_value), process_filing_btn: gr.update(visible=True), openapi_key_input: gr.update(visible=True), processed_info: gr.update(visible=True), doc_id : docid}
        else:
            resp_value += 'Work in progress to process these type of filings'
            logging.info(resp_value)
            return {display_filing_doc_info: gr.Textbox.update(visible=True, value=resp_value), process_filing_btn: gr.update(visible=False), openapi_key_input: gr.update(visible=False), processed_info: gr.update(visible=False), doc_id : "None"}

    # Function to initial the Langchain chain with call to OPENAI to Summarize the Annual report
    def langchain_summarize(contents,openai_api_key):
        logging.info("* Calling Langchain / OPENAI to get the summary *")
        concatenated_content = '`n`n'.join(contents)
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=1200,chunk_overlap=20,length_function=len)
        docs = text_splitter.create_documents([concatenated_content])
        prompt_template = """You are a financial analyst, analyzing the Annual report submitted by the limited company at UK Companies House. Write a concise summary of the following report:


        {text}

        
        CONCISE SUMMARY:"""
        PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
        llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
        #docs = [Document(page_content=t) for t in contents]
        chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)
        with get_openai_callback() as cb:
            resp = chain.run(docs)
            tkn_text = f'*** Spent a total of {cb.total_tokens} tokens ***'
        return resp, tkn_text

    # Function to extract text from the document and call the LangChain processing function with the text array of pages
    def process_filing(docid, openai_api_key, progress=gr.Progress()):
        logging.info("* Processing the filing document *")
        progress(0,desc="Starting...")
        filepath = f'./data/doc_{docid}.pdf'
        pdf_document = fitz.open(filepath)
        text_path = f'./text/doc_{docid}.pkl'
        if os.path.exists(text_path):
            # retrieve list from file
            with open(text_path, 'rb') as f:
                contents = pickle.load(f)
        else:
            zoom_x = 2.0  # horizontal zoom
            zoom_y = 2.0  # vertical zoom
            mat = fitz.Matrix(zoom_x, zoom_y)  # zoom factor 2 in each dimension
            contents = []
            for page in progress.tqdm(pdf_document, desc="Processing pages from PDF..."):
                # Convert the page to a PNG image using PyMuPDF
                pix = page.get_pixmap(matrix=mat)
                img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height,pix.width, pix.n)
                texts = pytesseract.image_to_string(img)
                contents.append(texts)
            # save list to file
            with open(text_path, 'wb') as f:
                pickle.dump(contents, f)

        resp_value = f'Total of {pdf_document.page_count} pages processed. '
        summary_path = f'./summary/doc_{docid}.txt'
        if os.path.exists(summary_path):
            with open(summary_path, 'r') as f:
                summary = f.read()
            logging.info(resp_value)
            return {processed_info: gr.Textbox.update(visible=True, value=resp_value), summary_text: gr.Textbox.update(visible=True, value=summary)}
        else:
            try:
                summary, tkn_text = langchain_summarize(contents, openai_api_key)
                resp_value += tkn_text
                logging.info(resp_value)
                with open(summary_path, 'wb') as f:
                    f.write(summary.encode())
                return {processed_info: gr.Textbox.update(visible=True, value=resp_value), summary_text: gr.Textbox.update(visible=True, value=summary)}
            except Exception as e:
                logging.info(e)
                resp_value += 'LLM Call failed. Please check the OpenAI key again'
                logging.info(resp_value)
                return {processed_info: gr.Textbox.update(visible=True, value=resp_value), summary_text: gr.Textbox.update(visible=False)}
            finally:
                logging.info(resp_value)
    
    def clear_screen():
        return {output_col: gr.update(visible=False),display_filing: gr.Textbox.update(visible=False),submit_btn: gr.update(visible=False), display_filing_doc_info:gr.update(visible=False), process_filing_btn:gr.update(visible=False),openapi_key_input:gr.update(visible=False),processed_info:gr.update(visible=False),summary_text:gr.update(visible=False)}

    search_btn.click(company_search,input_box,[company_list_box,output_col])
    company_list_box.change(company_selected,[company_list_box, doc_id],[display_filing, submit_btn, doc_id])
    submit_btn.click(get_filing,doc_id,[display_filing_doc_info, process_filing_btn, openapi_key_input, processed_info, doc_id])
    process_filing_btn.click(process_filing,[doc_id,openapi_key_input],[processed_info,summary_text])
    clear.click(clear_screen, None, [output_col,display_filing,submit_btn,display_filing_doc_info,process_filing_btn,openapi_key_input,processed_info,summary_text])

demo.queue(concurrency_count=3)
demo.launch()