File size: 2,789 Bytes
bbbfa2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a90682
0bd0f00
bbbfa2a
93c87da
 
0bd0f00
8a90682
2804140
c53f752
2804140
bbbfa2a
c53f752
2804140
8a90682
c53f752
bbbfa2a
93c87da
 
 
 
44154a0
 
c53f752
44154a0
bbbfa2a
8a90682
 
 
c53f752
2804140
 
c53f752
2804140
8a90682
c53f752
8a90682
c53f752
bbbfa2a
 
8a90682
bbbfa2a
2804140
44154a0
93c87da
bbbfa2a
8a90682
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
# from flask import Flask, render_template, request, send_from_directory
# from paddleocr import PaddleOCR
# import os

# app = Flask(__name__)

# # Upload folder
# UPLOAD_FOLDER = 'uploads'
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# if not os.path.exists(UPLOAD_FOLDER):
#     os.makedirs(UPLOAD_FOLDER)

# # Initialize OCR
# ocr = PaddleOCR(use_angle_cls=True, lang='en')

# @app.route('/', methods=['GET', 'POST'])
# def upload_file():
#     text = None
#     filename = None
#     if request.method == 'POST':
#         file = request.files.get('file')
#         if not file or file.filename == '':
#             return render_template('index.html', error="No file selected")

#         filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
#         file.save(filepath)

#         # Run OCR
#         result = ocr.ocr(filepath, cls=True)
#         extracted_text = ""
#         for line in result:
#             for word_info in line:
#                 extracted_text += word_info[1][0] + " "

#         text = extracted_text
#         filename = file.filename

#     return render_template('index.html', text=text, filename=filename)

# @app.route('/uploads/<filename>')
# def uploaded_file(filename):
#     return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

# if __name__ == '__main__':
#     port = int(os.environ.get('PORT', 5000))  # <-- IMPORTANT
#     app.run(host='0.0.0.0', port=port)

from flask import Flask, render_template, request
import os
import time

app = Flask(__name__)

# Lightweight OCR loader
def get_ocr():
    from paddleocr import PaddleOCR
    return PaddleOCR(
        lang='en',
        use_angle_cls=False,
        use_gpu=False,
        det_limit_side_len=480,
        thread_num=1
    )

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files.get('file')
        if not file or file.filename == '':
            return render_template('index.html', error="Please select a file")

        try:
            # Verify file size
            file.seek(0, os.SEEK_END)
            if file.tell() > 300000:
                return render_template('index.html', error="Max 300KB file size")
            file.seek(0)

            # Process with OCR
            ocr = get_ocr()
            result = ocr.ocr(file.stream, cls=False)
            text = ' '.join([word[1][0] for line in result[0] for word in line if len(word) >= 2])
            
            return render_template('index.html', text=text)

        except Exception as e:
            return render_template('index.html', error="Processing error")

    return render_template('index.html')

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)