File size: 5,404 Bytes
70085b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d858b9
70085b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json
from jiwer import cer, wer
import re

pdf_file_path = 'dummy.pdf'

with open("page_transcriptions.json", encoding="utf-8") as f:
    
    data = json.load(f)

def send_request(url):
    
    try:
        
        with open(pdf_file_path, 'rb') as pdf_file:
            
            files = {
                'file': (
                    pdf_file_path,
                    pdf_file,
                    'application/pdf'
                )
            }
        
            response = requests.post(url, files=files)
    
    except Exception as e:
        
        return {"Error message: "f"Error occurred while sending request. Error message: {e}"}
    
    try:
        
        response_json = response.json()
        
    except Exception as e:
        
        return {
            "Error message": e,
            "Response": response.content
            }
    
    if isinstance(response_json, list):
        
        for page in response_json:
            
            if isinstance(page, dict):
            
                if "page_number" not in page.keys() or "MD_text" not in page.keys():
                    
                    return {
                        "Error message": "Response is not in desired structure. Desired structure: [{'page_number': 1, 'MD_text': 'Extracted text'}]",
                        "Response": response_json
                    }
                    
                if isinstance(page["page_number"], int) and isinstance(page["MD_text"], str):
                    
                    continue
                
                else:
                    
                    return {
                        "Error message": "'page_number' should be integer and 'MD_text' should be string.",
                        "Response": response_json
                    }
            
            else:
                
                return {
                    "Error message": "List should include only dictionaries.",
                    "Response": response_json
                }
            
        if len(response_json) != len(data):
            
            return {
                "Error message": "The number of pages are not equal between transcription and ground truth.",
                "Response": response_json
            }
            
        final_metrics = []
        total_reference = ""
        total_hypothesis = ""
        
        for page in response_json:
            
            for transcription in data:
                
                if page["page_number"] == transcription["page_number"]:
                    
                    reference = transcription['MD_text'].strip()
                    hypothesis = page['MD_text'].strip()
                    
                    reference = reference.lower()
                    hypothesis = hypothesis.lower()
                    
                    reference = reference.replace("\n", " ")
                    hypothesis = hypothesis.replace("\n", " ")
                    
                    reference = re.sub(r'\s+', ' ', reference)
                    hypothesis = re.sub(r'\s+', ' ', hypothesis)
                    
                    total_reference += reference
                    total_reference += " "
                    total_hypothesis += hypothesis
                    total_hypothesis += " "
                    
                    cer_value = max(1 - cer(reference, hypothesis), 0)
                    wer_value = max(1 - wer(reference, hypothesis), 0)
                    
                    final_metrics.append({"page_number": page["page_number"], "Character Success Rate (CSR)": round(cer_value, 4), "Word Success Rate (WSR)": round(wer_value, 4), "MD_text_used_for_metrics": hypothesis, "Ground_Truth_used_for_metrics": reference})
            
        global_cer = max(1 - cer(total_reference.strip(), total_hypothesis.strip()), 0)
        global_wer = max(1 - wer(total_reference.strip(), total_hypothesis.strip()), 0)
        
        final_metrics.append({"Global CSR": global_cer, "Global WSR": global_wer, "MD_text_used_for_metrics": total_hypothesis.strip(), "Ground_Truth_used_for_metrics": total_reference.strip()})
        
        return final_metrics

    else:
        
        return {
            "Error message": "Response should be list of dictionaries.",
            "Response": response_json
        }

with gr.Blocks() as demo:
    
    gr.Markdown(
        """
        # OCR Endpoint Response Validator and Quality Checker
        
        Character Success Rate (CSR) and Word Success Rate (WSR) are metrics that will be provided for each page and total.
        They are calculated by simply subtracting CER and WER from 1 respectively.
        If CER or WER is > 1, CSR or WSR is considered as 0. 
        
        Enter your endpoint below and click **Send** to get the result.

        Format:
        ```http://<host>/<endpoint>```
        """
    )
    
    output = gr.JSON(
        label="Output"
    )

    input_box = gr.Textbox(
        label="Input",
        lines=1,
        placeholder="Type your text here..."
    )

    send_btn = gr.Button("Send")

    send_btn.click(
        fn=send_request,
        inputs=input_box,
        outputs=output
    )

    input_box.submit(
        fn=send_request,
        inputs=input_box,
        outputs=output
    )

demo.launch()