VsinghiAssociates commited on
Commit
d48b2ff
·
verified ·
1 Parent(s): 1da1572

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile.hf +43 -0
  2. README.md +121 -11
  3. app.py +91 -0
  4. main.py +342 -0
  5. requirements.txt +6 -0
Dockerfile.hf ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Install system dependencies required for ocrmypdf and Python packages
4
+ RUN apt-get update && apt-get install -y --no-install-recommends \
5
+ ocrmypdf \
6
+ tesseract-ocr \
7
+ poppler-utils \
8
+ libsnappy-dev \
9
+ zlib1g-dev \
10
+ libbz2-dev \
11
+ libreadline-dev \
12
+ libsqlite3-dev \
13
+ wget \
14
+ curl \
15
+ llvm \
16
+ libncurses5-dev \
17
+ libncursesw5-dev \
18
+ xz-utils \
19
+ tk-dev \
20
+ libssl-dev \
21
+ libffi-dev \
22
+ liblzma-dev \
23
+ python3-dev \
24
+ libopenjp2-7-dev \
25
+ libharfbuzz-dev \
26
+ libfribidi-dev \
27
+ libxcb-composite0-dev \
28
+ libatlas-base-dev \
29
+ git \
30
+ gcc \
31
+ && rm -rf /var/lib/apt/lists/*
32
+
33
+ WORKDIR /app
34
+
35
+ COPY requirements.txt .
36
+
37
+ RUN pip install --no-cache-dir -r requirements.txt
38
+
39
+ COPY . .
40
+
41
+ EXPOSE 7860
42
+
43
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,11 +1,121 @@
1
- ---
2
- title: Ocr New
3
- emoji: 🌖
4
- colorFrom: pink
5
- colorTo: green
6
- sdk: docker
7
- pinned: false
8
- license: apache-2.0
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Document Converter Pro
2
+
3
+ A powerful document conversion tool that combines PDF OCR conversion and Excel to CSV conversion in a single interface.
4
+
5
+ ## Features
6
+
7
+ - **PDF OCR Conversion**: Convert scanned PDFs to searchable, text-based PDFs using ocrmypdf
8
+ - **Excel to CSV Conversion**: Convert Excel files to CSV format quickly and efficiently
9
+ - **Web Interface**: Built with Gradio for an intuitive user experience
10
+ - **Optimized Processing**: Fast conversion with optimized parameters
11
+
12
+ ## Deployment to Hugging Face Spaces
13
+
14
+ This application is ready to be deployed on Hugging Face Spaces as a free, public application.
15
+
16
+ ### Steps to Deploy:
17
+
18
+ 1. **Sign up on Hugging Face**
19
+ - Go to [huggingface.co](https://huggingface.co) and create an account
20
+
21
+ 2. **Create a New Space**
22
+ - Click on your profile icon → "New Space"
23
+ - Fill in the details:
24
+ - Name: Choose a unique name for your application
25
+ - License: Select appropriate license
26
+ - SDK: Select "Docker" (required for ocrmypdf)
27
+ - Visibility: Public or Private
28
+
29
+ 3. **Configure the Space**
30
+ - Use the files provided in this repository:
31
+ - `app.py` - Main application file
32
+ - `requirements.txt` - Python dependencies
33
+ - `Dockerfile` - Container configuration (see below)
34
+
35
+ 4. **Create a Dockerfile** (needed for ocrmypdf dependencies)
36
+ ```Dockerfile
37
+ FROM huggingface/transformers-all-deepspeed:latest
38
+
39
+ RUN apt-get update && apt-get install -y \
40
+ ocrmypdf \
41
+ tesseract-ocr \
42
+ poppler-utils \
43
+ && rm -rf /var/lib/apt/lists/*
44
+
45
+ COPY requirements.txt .
46
+ RUN pip install -r requirements.txt
47
+
48
+ COPY . .
49
+
50
+ CMD ["python", "app.py"]
51
+ ```
52
+
53
+ 5. **Alternative Dockerfile** (lighter image)
54
+ ```Dockerfile
55
+ FROM python:3.10-slim
56
+
57
+ # Install system dependencies
58
+ RUN apt-get update && apt-get install -y \
59
+ ocrmypdf \
60
+ tesseract-ocr \
61
+ poppler-utils \
62
+ gcc \
63
+ && rm -rf /var/lib/apt/lists/*
64
+
65
+ WORKDIR /app
66
+
67
+ COPY requirements.txt .
68
+ RUN pip install -r requirements.txt
69
+
70
+ COPY . .
71
+
72
+ EXPOSE 7860
73
+
74
+ CMD ["python", "app.py"]
75
+ ```
76
+
77
+ 6. **Upload Files**
78
+ - Upload all files to your Space repository
79
+ - Or connect your GitHub repository for automatic synchronization
80
+
81
+ 7. **Wait for Build**
82
+ - Hugging Face will automatically build and deploy your application
83
+ - This may take several minutes due to the size of OCR dependencies
84
+
85
+ ## How to Use
86
+
87
+ ### PDF OCR Conversion:
88
+ 1. Navigate to the "PDF OCR Converter" tab
89
+ 2. Upload your PDF file
90
+ 3. Click "Process PDF with OCR"
91
+ 4. Wait for processing to complete
92
+ 5. Download the OCRed PDF when finished
93
+
94
+ ### Excel to CSV Conversion:
95
+ 1. Navigate to the "Excel to CSV Converter" tab
96
+ 2. Upload your Excel file (.xlsx or .xls)
97
+ 3. Click "Convert to CSV"
98
+ 4. Download the converted CSV file when finished
99
+
100
+ ## Technical Details
101
+
102
+ The application uses:
103
+ - **ocrmypdf**: For PDF OCR processing with parameters: `--force-ocr --deskew --rotate-pages --oversample 300`
104
+ - **pandas**: For fast Excel to CSV conversion with optimized settings
105
+ - **Gradio**: For the user interface
106
+ - **openpyxl**: For Excel file processing
107
+
108
+ ## Requirements
109
+
110
+ - Python 3.8+
111
+ - System dependencies: ocrmypdf, tesseract-ocr, poppler-utils
112
+
113
+ ## Local Development
114
+
115
+ To run locally:
116
+ ```bash
117
+ pip install -r requirements.txt
118
+ python app.py
119
+ ```
120
+
121
+ Then visit `http://localhost:7860` in your browser.
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import ocrmypdf
3
+ import pandas as pd
4
+ import openpyxl
5
+ import tempfile
6
+ import os
7
+ import uuid
8
+ from werkzeug.utils import secure_filename
9
+
10
+ def ocr_pdf(input_pdf):
11
+ """Process PDF with OCR using ocrmypdf"""
12
+ if input_pdf is None:
13
+ return None, "Please upload a PDF file"
14
+
15
+ with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_output:
16
+ try:
17
+ # Process with ocrmypdf using the parameters you specified
18
+ ocrmypdf.ocr(
19
+ input_pdf.name,
20
+ temp_output.name,
21
+ force_ocr=True,
22
+ deskew=True,
23
+ rotate_pages=True,
24
+ oversample=300
25
+ )
26
+ return temp_output.name, "PDF processed successfully!"
27
+ except Exception as e:
28
+ return None, f"Error processing PDF: {str(e)}"
29
+
30
+ def convert_excel_to_csv(input_excel):
31
+ """Convert Excel file to CSV"""
32
+ if input_excel is None:
33
+ return None, "Please upload an Excel file"
34
+
35
+ try:
36
+ # Read Excel file with optimizations for speed
37
+ df = pd.read_excel(
38
+ input_excel.name,
39
+ engine="openpyxl",
40
+ dtype=str, # Big speed boost
41
+ parse_dates=False, # No date parsing
42
+ keep_default_na=False
43
+ )
44
+
45
+ # Create temporary CSV file
46
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False, newline='', encoding='utf-8') as temp_csv:
47
+ df.to_csv(temp_csv.name, index=False, encoding="utf-8")
48
+ return temp_csv.name, "Excel converted to CSV successfully!"
49
+ except Exception as e:
50
+ return None, f"Error converting Excel: {str(e)}"
51
+
52
+ # Create Gradio interface with tabs
53
+ with gr.Blocks(title="Document Converter Pro", theme=gr.themes.Soft()) as demo:
54
+ gr.Markdown("# 📄 Document Converter Pro")
55
+ gr.Markdown("Convert your documents with ease using our powerful online tools")
56
+
57
+ with gr.Tabs():
58
+ with gr.TabItem("PDF OCR Converter"):
59
+ gr.Markdown("### Convert scanned PDFs to searchable, text-based PDFs")
60
+ with gr.Row():
61
+ with gr.Column():
62
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
63
+ pdf_btn = gr.Button("Process PDF with OCR")
64
+ with gr.Column():
65
+ pdf_output = gr.File(label="Download OCRed PDF")
66
+ pdf_status = gr.Textbox(label="Status", interactive=False)
67
+
68
+ pdf_btn.click(
69
+ fn=ocr_pdf,
70
+ inputs=pdf_input,
71
+ outputs=[pdf_output, pdf_status]
72
+ )
73
+
74
+ with gr.TabItem("Excel to CSV Converter"):
75
+ gr.Markdown("### Convert Excel files to CSV format quickly and efficiently")
76
+ with gr.Row():
77
+ with gr.Column():
78
+ excel_input = gr.File(label="Upload Excel", file_types=[".xlsx", ".xls"])
79
+ excel_btn = gr.Button("Convert to CSV")
80
+ with gr.Column():
81
+ excel_output = gr.File(label="Download CSV")
82
+ excel_status = gr.Textbox(label="Status", interactive=False)
83
+
84
+ excel_btn.click(
85
+ fn=convert_excel_to_csv,
86
+ inputs=excel_input,
87
+ outputs=[excel_output, excel_status]
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch(server_port=int(os.getenv("PORT", 7860)))
main.py ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, send_file, redirect, url_for, flash, jsonify
2
+ import os
3
+ import subprocess
4
+ import tempfile
5
+ import uuid
6
+ from werkzeug.utils import secure_filename
7
+ import threading
8
+ import time
9
+
10
+ app = Flask(__name__)
11
+ app.secret_key = 'your-secret-key-here'
12
+
13
+ # Import for Excel processing
14
+ import pandas as pd
15
+ import openpyxl
16
+
17
+ # Configuration
18
+ UPLOAD_FOLDER = 'uploads'
19
+ PROCESSED_FOLDER = 'processed'
20
+ ALLOWED_EXTENSIONS = {'pdf'}
21
+
22
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
23
+ app.config['PROCESSED_FOLDER'] = PROCESSED_FOLDER
24
+
25
+ # Create necessary directories
26
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
27
+ os.makedirs(PROCESSED_FOLDER, exist_ok=True)
28
+
29
+ # Global dictionary to store processing status
30
+ processing_status = {}
31
+
32
+ def allowed_file(filename):
33
+ """Check if the uploaded file has an allowed extension"""
34
+ return '.' in filename and \
35
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
36
+
37
+
38
+ def validate_pdf_file(file_path):
39
+ """Validate that the uploaded file is actually a PDF"""
40
+ try:
41
+ # Check if file exists and has PDF header
42
+ if not os.path.exists(file_path):
43
+ return False, "File does not exist"
44
+
45
+ # Read first few bytes to check PDF header
46
+ with open(file_path, 'rb') as f:
47
+ header = f.read(4)
48
+ if header != b'%PDF':
49
+ return False, "File is not a valid PDF"
50
+
51
+ # Check file size (max 50MB)
52
+ file_size = os.path.getsize(file_path)
53
+ max_size = 50 * 1024 * 1024 # 50MB
54
+ if file_size > max_size:
55
+ return False, f"File size too large. Max size is 50MB, got {file_size / (1024*1024):.1f}MB"
56
+
57
+ return True, "Valid PDF"
58
+ except Exception as e:
59
+ return False, f"Error validating PDF: {str(e)}"
60
+
61
+ def process_pdf_with_ocr(input_path, output_path, job_id=None):
62
+ """Process PDF with OCR using ocrmypdf"""
63
+ try:
64
+ # Using the ocrmypdf command similar to what was provided
65
+ cmd = [
66
+ 'ocrmypdf',
67
+ '--force-ocr',
68
+ '--deskew',
69
+ '--rotate-pages',
70
+ '--oversample', '300',
71
+ input_path,
72
+ output_path
73
+ ]
74
+
75
+ import logging
76
+ logging.basicConfig(level=logging.INFO)
77
+ logger = logging.getLogger(__name__)
78
+
79
+ logger.info(f"Executing OCR command: {' '.join(cmd)}")
80
+
81
+ # Run the command and capture output in real-time
82
+ result = subprocess.run(
83
+ cmd,
84
+ capture_output=True,
85
+ text=True,
86
+ timeout=600 # 10 minute timeout
87
+ )
88
+
89
+ logger.info(f"OCR process completed with return code: {result.returncode}")
90
+ if result.stdout:
91
+ logger.info(f"OCR stdout: {result.stdout}")
92
+ if result.stderr:
93
+ logger.info(f"OCR stderr: {result.stderr}")
94
+
95
+ if result.returncode != 0:
96
+ raise Exception(f"OCR processing failed with return code {result.returncode}: {result.stderr}")
97
+
98
+ return True
99
+ except subprocess.TimeoutExpired:
100
+ raise Exception("Processing timed out after 10 minutes")
101
+ except Exception as e:
102
+ import logging
103
+ logging.basicConfig(level=logging.INFO)
104
+ logger = logging.getLogger(__name__)
105
+ logger.error(f"OCR processing error: {str(e)}")
106
+ raise e
107
+
108
+ # Main routes
109
+ @app.route('/')
110
+ def index():
111
+ """Main page route"""
112
+ return render_template('index.html')
113
+
114
+ @app.route('/pdf-ocr')
115
+ def pdf_ocr():
116
+ """PDF OCR converter page route"""
117
+ return render_template('pdf_ocr.html')
118
+
119
+ @app.route('/excel-to-csv')
120
+ def excel_to_csv():
121
+ """Excel to CSV converter page route"""
122
+ return render_template('excel_to_csv.html')
123
+
124
+ # Routes for PDF OCR functionality
125
+ @app.route('/upload', methods=['POST'])
126
+ def upload_file():
127
+ """Handle PDF file upload"""
128
+ if 'pdf_file' not in request.files:
129
+ return jsonify({'success': False, 'message': 'No file selected'})
130
+
131
+ file = request.files['pdf_file']
132
+
133
+ if file.filename == '':
134
+ return jsonify({'success': False, 'message': 'No file selected'})
135
+
136
+ if file and allowed_file(file.filename):
137
+ filename = secure_filename(file.filename)
138
+ unique_filename = f"{uuid.uuid4()}_{filename}"
139
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
140
+ file.save(filepath)
141
+
142
+ # Validate the PDF file
143
+ is_valid, validation_msg = validate_pdf_file(filepath)
144
+ if not is_valid:
145
+ # Clean up invalid file
146
+ try:
147
+ os.remove(filepath)
148
+ except:
149
+ pass # Ignore cleanup errors
150
+ return jsonify({'success': False, 'message': f'Invalid PDF file: {validation_msg}'})
151
+
152
+ # Generate unique output filename
153
+ output_filename = f"ocr_{unique_filename}"
154
+ output_path = os.path.join(app.config['PROCESSED_FOLDER'], output_filename)
155
+
156
+ # Store processing status
157
+ job_id = str(uuid.uuid4())
158
+ processing_status[job_id] = {
159
+ 'status': 'processing',
160
+ 'input_file': filepath,
161
+ 'output_file': output_path,
162
+ 'progress': 0
163
+ }
164
+
165
+ # Start processing in a separate thread
166
+ thread = threading.Thread(target=process_file_thread, args=(job_id,))
167
+ thread.start()
168
+
169
+ return jsonify({
170
+ 'success': True,
171
+ 'job_id': job_id,
172
+ 'message': 'File uploaded successfully. Processing started...'
173
+ })
174
+ else:
175
+ return jsonify({'success': False, 'message': 'Invalid file type. Please upload a PDF.'})
176
+
177
+ def process_file_thread(job_id):
178
+ """Process file in a separate thread"""
179
+ status = processing_status.get(job_id)
180
+ if not status:
181
+ return
182
+
183
+ try:
184
+ # Update progress
185
+ processing_status[job_id]['progress'] = 5
186
+ time.sleep(0.5) # Brief pause
187
+
188
+ # Estimate progress based on file size
189
+ input_size = os.path.getsize(status['input_file'])
190
+ estimated_pages = max(1, input_size // (100 * 1024)) # Rough estimate: 1 page per 100KB
191
+
192
+ # Update progress incrementally
193
+ base_progress = 5
194
+ progress_increment = (90 - base_progress) / estimated_pages # 90% total for processing
195
+
196
+ for page in range(estimated_pages):
197
+ # In a real implementation, we would track actual OCR progress
198
+ # Here we simulate progress based on estimated pages
199
+ simulated_progress = int(base_progress + (page * progress_increment))
200
+ processing_status[job_id]['progress'] = min(simulated_progress, 95)
201
+ time.sleep(0.1) # Small delay to simulate processing time per page
202
+
203
+ # Perform OCR processing
204
+ process_pdf_with_ocr(status['input_file'], status['output_file'], job_id)
205
+
206
+ # Update status when complete
207
+ processing_status[job_id]['status'] = 'completed'
208
+ processing_status[job_id]['progress'] = 100
209
+ except Exception as e:
210
+ processing_status[job_id]['status'] = 'failed'
211
+ processing_status[job_id]['error'] = str(e)
212
+ processing_status[job_id]['progress'] = 0
213
+
214
+ # Clean up failed output file if it exists
215
+ try:
216
+ if os.path.exists(status['output_file']):
217
+ os.remove(status['output_file'])
218
+ except:
219
+ pass # Ignore cleanup errors
220
+
221
+ @app.route('/status/<job_id>')
222
+ def get_status(job_id):
223
+ """Get processing status for a job"""
224
+ if job_id in processing_status:
225
+ return jsonify(processing_status[job_id])
226
+ else:
227
+ return jsonify({'status': 'not_found'})
228
+
229
+ @app.route('/download/<job_id>')
230
+ def download_file(job_id):
231
+ """Download processed file"""
232
+ if job_id in processing_status:
233
+ status = processing_status[job_id]
234
+ if status['status'] == 'completed':
235
+ return send_file(status['output_file'], as_attachment=True,
236
+ download_name=f"ocr_processed_{os.path.basename(status['output_file'])}")
237
+ else:
238
+ flash('File is not ready for download')
239
+ return redirect(url_for('index'))
240
+ else:
241
+ flash('Invalid job ID')
242
+ return redirect(url_for('index'))
243
+
244
+ @app.route('/cleanup', methods=['POST'])
245
+ def cleanup():
246
+ """Clean up temporary files"""
247
+ # Clean up uploaded files older than 1 hour
248
+ current_time = time.time()
249
+ for filename in os.listdir(app.config['UPLOAD_FOLDER']):
250
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
251
+ if os.path.isfile(file_path):
252
+ if current_time - os.path.getmtime(file_path) > 3600: # 1 hour
253
+ os.remove(file_path)
254
+
255
+ # Clean up processed files older than 1 hour
256
+ for filename in os.listdir(app.config['PROCESSED_FOLDER']):
257
+ file_path = os.path.join(app.config['PROCESSED_FOLDER'], filename)
258
+ if os.path.isfile(file_path):
259
+ if current_time - os.path.getmtime(file_path) > 3600: # 1 hour
260
+ os.remove(file_path)
261
+
262
+ # Clean up old processing statuses
263
+ expired_jobs = []
264
+ for job_id, status in processing_status.items():
265
+ if status['status'] == 'completed' and current_time - os.path.getmtime(status['output_file']) > 3600:
266
+ expired_jobs.append(job_id)
267
+
268
+ for job_id in expired_jobs:
269
+ del processing_status[job_id]
270
+
271
+ return jsonify({'success': True, 'message': 'Cleanup completed'})
272
+
273
+
274
+ # Routes for Excel to CSV functionality
275
+ @app.route('/convert-excel', methods=['POST'])
276
+ def convert_excel_to_csv():
277
+ """Convert Excel files to CSV"""
278
+ try:
279
+ if 'excel_file' not in request.files:
280
+ return jsonify({'success': False, 'message': 'No file selected'})
281
+
282
+ file = request.files['excel_file']
283
+
284
+ if file.filename == '':
285
+ return jsonify({'success': False, 'message': 'No file selected'})
286
+
287
+ if file and file.filename.lower().endswith(('.xlsx', '.xls')):
288
+ filename = secure_filename(file.filename)
289
+ unique_filename = f"{uuid.uuid4()}_{filename}"
290
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
291
+ file.save(filepath)
292
+
293
+ # Convert Excel to CSV
294
+ output_filename = f"{os.path.splitext(unique_filename)[0]}.csv"
295
+ output_path = os.path.join(app.config['PROCESSED_FOLDER'], output_filename)
296
+
297
+ # Process Excel file to CSV
298
+ df = pd.read_excel(
299
+ filepath,
300
+ engine="openpyxl",
301
+ dtype=str,
302
+ parse_dates=False,
303
+ keep_default_na=False
304
+ )
305
+
306
+ df.to_csv(output_path, index=False, encoding="utf-8")
307
+
308
+ # Clean up uploaded file
309
+ try:
310
+ os.remove(filepath)
311
+ except:
312
+ pass # Ignore cleanup errors
313
+
314
+ return jsonify({
315
+ 'success': True,
316
+ 'output_filename': output_filename,
317
+ 'message': 'File converted successfully!'
318
+ })
319
+ else:
320
+ return jsonify({'success': False, 'message': 'Invalid file type. Please upload an Excel file (.xlsx or .xls)'})
321
+
322
+ except Exception as e:
323
+ return jsonify({'success': False, 'message': f'Conversion failed: {str(e)}'})
324
+
325
+ @app.route('/download-csv/<filename>')
326
+ def download_csv(filename):
327
+ """Download converted CSV file"""
328
+ try:
329
+ file_path = os.path.join(app.config['PROCESSED_FOLDER'], filename)
330
+ if os.path.exists(file_path):
331
+ return send_file(file_path, as_attachment=True,
332
+ download_name=f"converted_{filename}")
333
+ else:
334
+ flash('File not found')
335
+ return redirect(url_for('excel_to_csv'))
336
+ except Exception as e:
337
+ flash(f'Download failed: {str(e)}')
338
+ return redirect(url_for('excel_to_csv'))
339
+
340
+
341
+ if __name__ == '__main__':
342
+ app.run(debug=True, host='0.0.0.0', port=5000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Flask==2.3.3
2
+ Werkzeug==2.3.7
3
+ pandas==2.2.2
4
+ openpyxl==3.1.2
5
+ gradio==4.28.3
6
+ ocrmypdf==16.11.1