Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Commit 
							
							·
						
						932e485
	
1
								Parent(s):
							
							88f28f0
								
new files
Browse files- app.py +56 -47
 - static/style.css +1 -0
 
    	
        app.py
    CHANGED
    
    | 
         @@ -1,52 +1,61 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 1 | 
         
             
            import os
         
     | 
| 2 | 
         
            -
            from flask import Flask, request, jsonify
         
     | 
| 3 | 
         
             
            from paddleocr import PaddleOCR
         
     | 
| 
         | 
|
| 4 | 
         | 
| 5 | 
         
             
            app = Flask(__name__)
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 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 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            # app.py
         
     | 
| 2 | 
         
            +
            from flask import Flask, render_template, request, redirect, flash, url_for
         
     | 
| 3 | 
         
             
            import os
         
     | 
| 
         | 
|
| 4 | 
         
             
            from paddleocr import PaddleOCR
         
     | 
| 5 | 
         
            +
            from werkzeug.utils import secure_filename
         
     | 
| 6 | 
         | 
| 7 | 
         
             
            app = Flask(__name__)
         
     | 
| 8 | 
         
            +
            app.secret_key = os.environ.get('SECRET_KEY', 'change-this')  # Replace in production
         
     | 
| 9 | 
         
            +
            UPLOAD_FOLDER = os.path.join('static', 'uploads')
         
     | 
| 10 | 
         
            +
            app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
         
     | 
| 11 | 
         | 
| 12 | 
         
            +
            # Ensure upload directory exists
         
     | 
| 13 | 
         
            +
            os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
         
     | 
| 14 | 
         
            +
             
     | 
| 15 | 
         
            +
            # Initialize PaddleOCR once (CPU mode, English; angle_cls=False speeds up simple text)
         
     | 
| 16 | 
         
            +
            import os
         
     | 
| 17 | 
         
            +
            from paddleocr import PaddleOCR
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
            # create /tmp/ocr_models directory if it doesn't exist
         
     | 
| 20 | 
         
            +
            os.makedirs('/tmp/ocr_models', exist_ok=True)
         
     | 
| 21 | 
         
            +
             
     | 
| 22 | 
         
            +
            ocr = PaddleOCR(
         
     | 
| 23 | 
         
            +
                use_angle_cls=False,
         
     | 
| 24 | 
         
            +
                use_gpu=False,
         
     | 
| 25 | 
         
            +
                lang='en',
         
     | 
| 26 | 
         
            +
                det_model_dir='/tmp/ocr_models/det',
         
     | 
| 27 | 
         
            +
                rec_model_dir='/tmp/ocr_models/rec',
         
     | 
| 28 | 
         
            +
                cls_model_dir='/tmp/ocr_models/cls'
         
     | 
| 29 | 
         
            +
            )
         
     | 
| 30 | 
         
            +
             
     | 
| 31 | 
         
            +
            @app.route('/', methods=['GET', 'POST'])
         
     | 
| 32 | 
         
            +
            def index():
         
     | 
| 33 | 
         
            +
                extracted_text = None
         
     | 
| 34 | 
         
            +
                image_file = None
         
     | 
| 35 | 
         
            +
             
     | 
| 36 | 
         
            +
                if request.method == 'POST':
         
     | 
| 37 | 
         
            +
                    # Check file in request
         
     | 
| 38 | 
         
            +
                    if 'image' not in request.files:
         
     | 
| 39 | 
         
            +
                        flash('No file part in the request.')
         
     | 
| 40 | 
         
            +
                        return redirect(request.url)
         
     | 
| 41 | 
         
            +
                    file = request.files['image']
         
     | 
| 42 | 
         
            +
                    if file.filename == '':
         
     | 
| 43 | 
         
            +
                        flash('No image selected.')
         
     | 
| 44 | 
         
            +
                        return redirect(request.url)
         
     | 
| 45 | 
         
            +
             
     | 
| 46 | 
         
            +
                    # Save uploaded file
         
     | 
| 47 | 
         
            +
                    filename = secure_filename(file.filename)
         
     | 
| 48 | 
         
            +
                    file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
         
     | 
| 49 | 
         
            +
                    file.save(file_path)
         
     | 
| 50 | 
         
            +
             
     | 
| 51 | 
         
            +
                    # Run PaddleOCR on the saved image (CPU mode)
         
     | 
| 52 | 
         
            +
                    result = ocr.ocr(file_path, cls=False)
         
     | 
| 53 | 
         
            +
                    # Collect recognized text lines
         
     | 
| 54 | 
         
            +
                    lines = []
         
     | 
| 55 | 
         
            +
                    for res_line in result:
         
     | 
| 56 | 
         
            +
                        for box, (txt, prob) in res_line:
         
     | 
| 57 | 
         
            +
                            lines.append(txt)
         
     | 
| 58 | 
         
            +
                    extracted_text = "\n".join(lines)
         
     | 
| 59 | 
         
            +
                    image_file = filename
         
     | 
| 60 | 
         
            +
             
     | 
| 61 | 
         
            +
                return render_template('index.html', extracted_text=extracted_text, image_file=image_file)
         
     | 
    	
        static/style.css
    CHANGED
    
    | 
         @@ -104,6 +104,7 @@ button:hover { 
     | 
|
| 104 | 
         
             
              z-index: 1000;
         
     | 
| 105 | 
         
             
            } */
         
     | 
| 106 | 
         
             
            /* static/style.css */
         
     | 
| 
         | 
|
| 107 | 
         
             
            body {
         
     | 
| 108 | 
         
             
                background: #f0f2f5;
         
     | 
| 109 | 
         
             
                font-family: 'Segoe UI', Tahoma, sans-serif;
         
     | 
| 
         | 
|
| 104 | 
         
             
              z-index: 1000;
         
     | 
| 105 | 
         
             
            } */
         
     | 
| 106 | 
         
             
            /* static/style.css */
         
     | 
| 107 | 
         
            +
            /* static/style.css */
         
     | 
| 108 | 
         
             
            body {
         
     | 
| 109 | 
         
             
                background: #f0f2f5;
         
     | 
| 110 | 
         
             
                font-family: 'Segoe UI', Tahoma, sans-serif;
         
     |