File size: 4,514 Bytes
1ee396e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

# Redirect cache to a writable path inside container
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"

import gradio as gr
from impresso_pipelines.ocrqa import OCRQAPipeline

pipeline = OCRQAPipeline()

# Example OCR text (German text with typical OCR errors)
EXAMPLE_TEXT = """Vieles Seltsame geschieht auf Erden :
Nichts Seltsameres sieht der Mond
Als das Glück, das im Knopfloch wohnt.
Zaubrisch faßt es den ernsten Mann.
Ohne nach Weib u. Kind zu fragen
Reitet er aus, nach dem Glück zu jagen,
Nur nacb ihm war stets sein Vegehr.
Aber neben ihm reitet der Dämon her
Des Ehrgeizes mit finsterer Tücke,
Und so jagt er zuletzt auf die Brücke,
Die über dem Abgrund, d:m nächtlich schwarzen
Jählings abbricht."""

def process_ocr_qa(text):
    try:
        result = pipeline(text)
        
        # Format the output for better readability
        if isinstance(result, dict):
            output_lines = []
            for key, value in result.items():
                if key == 'corrections':
                    output_lines.append(f"📝 **{key.replace('_', ' ').title()}:**")
                    if isinstance(value, list) and value:
                        for correction in value:
                            output_lines.append(f"  • {correction}")
                    elif isinstance(value, dict) and value:
                        for sub_key, sub_value in value.items():
                            output_lines.append(f"  • {sub_key}: {sub_value}")
                    else:
                        output_lines.append(f"  No corrections found")
                elif key == 'quality_score':
                    output_lines.append(f"⭐ **Quality Score:** {value}")
                elif key == 'processed_text':
                    output_lines.append(f"✨ **Processed Text:**\n{value}")
                else:
                    output_lines.append(f"🔍 **{key.replace('_', ' ').title()}:** {value}")
            
            return "\n\n".join(output_lines)
        else:
            return f"✨ **Processed Result:**\n{result}"
            
    except Exception as e:
        print("❌ Pipeline error:", e)
        return f"Error: {e}"

# Create the interface with logo and improved description
with gr.Blocks(title="OCR QA Demo") as demo:
    # Add logo at the top
    gr.Image("logo.jpeg", label=None, show_label=False, container=False, height=100)
    
    gr.Markdown(
        """
        # 🔍 OCR Quality Assessment Pipeline Demo
        
        **OCR Quality Assessment** demonstrates how text extracted from OCR (Optical Character Recognition) 
        is analyzed and improved in the **Impresso** project. This pipeline identifies OCR errors, 
        assesses text quality, and provides corrections for better text processing.
        
        Try the example below (German text with typical OCR errors) or enter your own OCR text to see how it gets processed!
        """
    )
    
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(
                label="Enter OCR Text", 
                value=EXAMPLE_TEXT,
                lines=8,
                placeholder="Enter your OCR text here..."
            )
            submit_btn = gr.Button("🔍 Analyze OCR Quality", variant="primary")
        
        with gr.Column():
            with gr.Row():
                output = gr.Textbox(
                    label="Analysis Results", 
                    lines=15,
                    placeholder="Results will appear here...",
                    scale=10
                )
                info_btn = gr.Button("Pipeline Info", size="sm", scale=1)
    
    # Info modal/accordion for pipeline details
    with gr.Accordion("📝 About the OCR QA Pipeline", open=False, visible=False) as info_accordion:
        gr.Markdown(
            """
            - **OCR Error Detection**: Identifies common OCR mistakes and artifacts
            - **Quality Assessment**: Evaluates the overall quality of OCR text
            - **Text Correction**: Suggests improvements for detected errors
            - **Language Processing**: Handles multilingual OCR text processing
            """
        )
    
    submit_btn.click(
        fn=process_ocr_qa,
        inputs=[text_input],
        outputs=output
    )
    
    # Toggle info visibility when info button is clicked
    info_btn.click(
        fn=lambda: gr.Accordion(visible=True, open=True),
        outputs=info_accordion
    )

demo.launch(server_name="0.0.0.0", server_port=7860)