Mohamed-Riyaz commited on
Commit
fd4d5da
Β·
verified Β·
1 Parent(s): 2676856

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +19 -0
  2. app.py +74 -0
  3. requirements.txt +4 -0
  4. templates/index.html +259 -0
  5. templates/result.html +244 -0
  6. uploads/file.txt +0 -0
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y \
6
+ libgl1-mesa-glx \
7
+ libglib2.0-0 \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ COPY requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ RUN mkdir -p uploads
16
+
17
+ EXPOSE 7860
18
+
19
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, flash, redirect, url_for
2
+ import os
3
+ import easyocr
4
+ import logging
5
+ from werkzeug.utils import secure_filename
6
+ import time
7
+
8
+ app = Flask(__name__)
9
+ app.secret_key = 'your-secret-key-here' # Change this!
10
+
11
+ UPLOAD_FOLDER = "uploads"
12
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'bmp'}
13
+ MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
14
+
15
+ app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
16
+ app.config["MAX_CONTENT_LENGTH"] = MAX_FILE_SIZE
17
+
18
+ # Setup logging
19
+ logging.basicConfig(level=logging.INFO)
20
+
21
+ # Initialize OCR reader once (not per request)
22
+ reader = easyocr.Reader(['en'])
23
+
24
+ def allowed_file(filename):
25
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
26
+
27
+ @app.route('/')
28
+ def home():
29
+ return render_template("index.html")
30
+
31
+ @app.route('/upload', methods=['POST'])
32
+ def upload():
33
+ start_time = time.time()
34
+
35
+ if 'image' not in request.files:
36
+ flash('No file uploaded')
37
+ return redirect(url_for('home'))
38
+
39
+ file = request.files['image']
40
+
41
+ if file.filename == '':
42
+ flash('No file selected')
43
+ return redirect(url_for('home'))
44
+
45
+ if not allowed_file(file.filename):
46
+ flash('File type not allowed. Please upload an image.')
47
+ return redirect(url_for('home'))
48
+
49
+ try:
50
+ filename = secure_filename(file.filename)
51
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
52
+ file.save(filepath)
53
+
54
+ # Process image
55
+ result = reader.readtext(filepath, detail=0, paragraph=True)
56
+
57
+ # Clean up
58
+ os.remove(filepath)
59
+
60
+ text = "\n".join(result) if result else "No text detected"
61
+
62
+ processing_time = round(time.time() - start_time, 2)
63
+ logging.info(f"Processed {filename} in {processing_time}s")
64
+
65
+ return render_template("result.html", text=text, processing_time=processing_time)
66
+
67
+ except Exception as e:
68
+ logging.error(f"Error processing image: {str(e)}")
69
+ flash('Error processing image. Please try again.')
70
+ return redirect(url_for('home'))
71
+
72
+ if __name__ == "__main__":
73
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
74
+ app.run(host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ flask
2
+ easyocr
3
+ gunicorn
4
+ werkzeug
templates/index.html ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>OCR Text Extractor</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ min-height: 100vh;
18
+ display: flex;
19
+ justify-content: center;
20
+ align-items: center;
21
+ padding: 20px;
22
+ }
23
+
24
+ .container {
25
+ background: white;
26
+ border-radius: 20px;
27
+ padding: 50px;
28
+ max-width: 600px;
29
+ width: 100%;
30
+ box-shadow: 0 20px 60px rgba(0,0,0,0.3);
31
+ text-align: center;
32
+ }
33
+
34
+ h1 {
35
+ color: #333;
36
+ margin-bottom: 10px;
37
+ font-size: 2.5em;
38
+ }
39
+
40
+ .subtitle {
41
+ color: #666;
42
+ margin-bottom: 30px;
43
+ font-size: 1em;
44
+ }
45
+
46
+ .upload-area {
47
+ border: 3px dashed #ddd;
48
+ border-radius: 15px;
49
+ padding: 40px 20px;
50
+ transition: all 0.3s ease;
51
+ cursor: pointer;
52
+ background: #fafafa;
53
+ }
54
+
55
+ .upload-area:hover {
56
+ border-color: #667eea;
57
+ background: #f0f2ff;
58
+ }
59
+
60
+ .upload-area.dragover {
61
+ border-color: #667eea;
62
+ background: #e8ebff;
63
+ transform: scale(1.02);
64
+ }
65
+
66
+ .upload-icon {
67
+ font-size: 3em;
68
+ margin-bottom: 15px;
69
+ }
70
+
71
+ .upload-text {
72
+ color: #666;
73
+ font-size: 1.1em;
74
+ }
75
+
76
+ .upload-text strong {
77
+ color: #667eea;
78
+ }
79
+
80
+ #fileInput {
81
+ display: none;
82
+ }
83
+
84
+ #previewContainer {
85
+ margin-top: 20px;
86
+ display: none;
87
+ }
88
+
89
+ #imagePreview {
90
+ max-width: 100%;
91
+ max-height: 300px;
92
+ border-radius: 10px;
93
+ box-shadow: 0 4px 10px rgba(0,0,0,0.1);
94
+ }
95
+
96
+ .file-info {
97
+ margin-top: 10px;
98
+ color: #666;
99
+ font-size: 0.9em;
100
+ }
101
+
102
+ .upload-btn {
103
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
104
+ color: white;
105
+ border: none;
106
+ padding: 15px 50px;
107
+ font-size: 1.1em;
108
+ border-radius: 50px;
109
+ cursor: pointer;
110
+ transition: all 0.3s ease;
111
+ margin-top: 20px;
112
+ font-weight: 600;
113
+ letter-spacing: 0.5px;
114
+ }
115
+
116
+ .upload-btn:hover {
117
+ transform: translateY(-2px);
118
+ box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
119
+ }
120
+
121
+ .upload-btn:disabled {
122
+ opacity: 0.6;
123
+ cursor: not-allowed;
124
+ transform: none;
125
+ }
126
+
127
+ .loading {
128
+ display: none;
129
+ margin-top: 20px;
130
+ padding: 15px;
131
+ background: #f0f2ff;
132
+ border-radius: 10px;
133
+ color: #667eea;
134
+ }
135
+
136
+ .loading.active {
137
+ display: block;
138
+ }
139
+
140
+ .spinner {
141
+ display: inline-block;
142
+ width: 20px;
143
+ height: 20px;
144
+ border: 3px solid #ddd;
145
+ border-radius: 50%;
146
+ border-top-color: #667eea;
147
+ animation: spin 1s ease-in-out infinite;
148
+ margin-right: 10px;
149
+ vertical-align: middle;
150
+ }
151
+
152
+ @keyframes spin {
153
+ to { transform: rotate(360deg); }
154
+ }
155
+
156
+ @media (max-width: 600px) {
157
+ .container {
158
+ padding: 30px 20px;
159
+ }
160
+ h1 {
161
+ font-size: 2em;
162
+ }
163
+ }
164
+ </style>
165
+ </head>
166
+ <body>
167
+ <div class="container">
168
+ <h1>πŸ“ OCR Text Extractor</h1>
169
+ <p class="subtitle">Upload an image and extract text from it instantly</p>
170
+
171
+ <form action="/upload" method="POST" enctype="multipart/form-data" id="uploadForm">
172
+ <div class="upload-area" id="dropZone">
173
+ <div class="upload-icon">πŸ–ΌοΈ</div>
174
+ <div class="upload-text">
175
+ <strong>Click to browse</strong> or drag & drop your image here
176
+ </div>
177
+ <div class="file-info">Supports: JPG, PNG, GIF, BMP</div>
178
+ <input type="file" name="image" id="fileInput" accept="image/*">
179
+ </div>
180
+
181
+ <div id="previewContainer">
182
+ <img id="imagePreview" src="#" alt="Preview">
183
+ <div class="file-info" id="fileName"></div>
184
+ </div>
185
+
186
+ <button type="submit" class="upload-btn" id="submitBtn">Extract Text ✨</button>
187
+ </form>
188
+
189
+ <div class="loading" id="loadingIndicator">
190
+ <span class="spinner"></span>
191
+ Processing your image...
192
+ </div>
193
+ </div>
194
+
195
+ <script>
196
+ const dropZone = document.getElementById('dropZone');
197
+ const fileInput = document.getElementById('fileInput');
198
+ const previewContainer = document.getElementById('previewContainer');
199
+ const imagePreview = document.getElementById('imagePreview');
200
+ const fileName = document.getElementById('fileName');
201
+ const submitBtn = document.getElementById('submitBtn');
202
+ const loadingIndicator = document.getElementById('loadingIndicator');
203
+ const form = document.getElementById('uploadForm');
204
+
205
+ // Click to upload
206
+ dropZone.addEventListener('click', () => {
207
+ fileInput.click();
208
+ });
209
+
210
+ // File selected
211
+ fileInput.addEventListener('change', (e) => {
212
+ handleFile(e.target.files[0]);
213
+ });
214
+
215
+ // Drag and drop
216
+ dropZone.addEventListener('dragover', (e) => {
217
+ e.preventDefault();
218
+ dropZone.classList.add('dragover');
219
+ });
220
+
221
+ dropZone.addEventListener('dragleave', () => {
222
+ dropZone.classList.remove('dragover');
223
+ });
224
+
225
+ dropZone.addEventListener('drop', (e) => {
226
+ e.preventDefault();
227
+ dropZone.classList.remove('dragover');
228
+ const file = e.dataTransfer.files[0];
229
+ handleFile(file);
230
+ fileInput.files = e.dataTransfer.files;
231
+ });
232
+
233
+ function handleFile(file) {
234
+ if (file && file.type.startsWith('image/')) {
235
+ const reader = new FileReader();
236
+ reader.onload = (e) => {
237
+ imagePreview.src = e.target.result;
238
+ previewContainer.style.display = 'block';
239
+ fileName.textContent = `πŸ“Ž ${file.name} (${(file.size / 1024 / 1024).toFixed(2)} MB)`;
240
+ dropZone.style.display = 'none';
241
+ };
242
+ reader.readAsDataURL(file);
243
+ } else {
244
+ alert('Please upload a valid image file.');
245
+ fileInput.value = '';
246
+ }
247
+ }
248
+
249
+ // Show loading on submit
250
+ form.addEventListener('submit', () => {
251
+ if (fileInput.files.length > 0) {
252
+ submitBtn.disabled = true;
253
+ loadingIndicator.classList.add('active');
254
+ submitBtn.textContent = 'Processing...';
255
+ }
256
+ });
257
+ </script>
258
+ </body>
259
+ </html>
templates/result.html ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>OCR Result</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ min-height: 100vh;
18
+ display: flex;
19
+ justify-content: center;
20
+ align-items: center;
21
+ padding: 20px;
22
+ }
23
+
24
+ .container {
25
+ background: white;
26
+ border-radius: 20px;
27
+ padding: 40px;
28
+ max-width: 800px;
29
+ width: 100%;
30
+ box-shadow: 0 20px 60px rgba(0,0,0,0.3);
31
+ }
32
+
33
+ h1 {
34
+ color: #333;
35
+ margin-bottom: 10px;
36
+ font-size: 2em;
37
+ display: flex;
38
+ align-items: center;
39
+ gap: 10px;
40
+ }
41
+
42
+ .stats {
43
+ color: #666;
44
+ margin-bottom: 25px;
45
+ font-size: 0.95em;
46
+ display: flex;
47
+ gap: 20px;
48
+ flex-wrap: wrap;
49
+ }
50
+
51
+ .stats span {
52
+ background: #f0f2ff;
53
+ padding: 5px 15px;
54
+ border-radius: 20px;
55
+ }
56
+
57
+ .result-box {
58
+ background: #f8f9fa;
59
+ border-radius: 12px;
60
+ padding: 25px;
61
+ border: 2px solid #e9ecef;
62
+ min-height: 200px;
63
+ position: relative;
64
+ }
65
+
66
+ pre {
67
+ white-space: pre-wrap;
68
+ word-wrap: break-word;
69
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
70
+ font-size: 1em;
71
+ line-height: 1.8;
72
+ color: #333;
73
+ margin: 0;
74
+ }
75
+
76
+ .empty-result {
77
+ color: #999;
78
+ font-style: italic;
79
+ }
80
+
81
+ .actions {
82
+ margin-top: 25px;
83
+ display: flex;
84
+ gap: 15px;
85
+ flex-wrap: wrap;
86
+ }
87
+
88
+ .btn {
89
+ padding: 12px 30px;
90
+ border: none;
91
+ border-radius: 50px;
92
+ font-size: 1em;
93
+ cursor: pointer;
94
+ transition: all 0.3s ease;
95
+ text-decoration: none;
96
+ display: inline-block;
97
+ font-weight: 600;
98
+ }
99
+
100
+ .btn-primary {
101
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
102
+ color: white;
103
+ }
104
+
105
+ .btn-primary:hover {
106
+ transform: translateY(-2px);
107
+ box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
108
+ }
109
+
110
+ .btn-secondary {
111
+ background: #e9ecef;
112
+ color: #333;
113
+ }
114
+
115
+ .btn-secondary:hover {
116
+ background: #dee2e6;
117
+ transform: translateY(-2px);
118
+ }
119
+
120
+ .btn-success {
121
+ background: #28a745;
122
+ color: white;
123
+ }
124
+
125
+ .btn-success:hover {
126
+ background: #218838;
127
+ transform: translateY(-2px);
128
+ }
129
+
130
+ .toast {
131
+ position: fixed;
132
+ bottom: 20px;
133
+ right: 20px;
134
+ background: #28a745;
135
+ color: white;
136
+ padding: 15px 25px;
137
+ border-radius: 10px;
138
+ box-shadow: 0 4px 15px rgba(0,0,0,0.2);
139
+ transform: translateY(100px);
140
+ opacity: 0;
141
+ transition: all 0.3s ease;
142
+ }
143
+
144
+ .toast.show {
145
+ transform: translateY(0);
146
+ opacity: 1;
147
+ }
148
+
149
+ @media (max-width: 600px) {
150
+ .container {
151
+ padding: 20px;
152
+ }
153
+ .actions {
154
+ flex-direction: column;
155
+ }
156
+ .btn {
157
+ text-align: center;
158
+ }
159
+ }
160
+ </style>
161
+ </head>
162
+ <body>
163
+ <div class="container">
164
+ <h1>πŸ“„ Extracted Text</h1>
165
+ <div class="stats">
166
+ <span id="charCount">Characters: 0</span>
167
+ <span id="wordCount">Words: 0</span>
168
+ </div>
169
+
170
+ <div class="result-box">
171
+ <pre id="textDisplay">{% if text %}{{ text }}{% else %}<span class="empty-result">No text detected in the image. Please try another image.</span>{% endif %}</pre>
172
+ </div>
173
+
174
+ <div class="actions">
175
+ <a href="/" class="btn btn-primary">πŸ“€ Upload Another</a>
176
+ <button class="btn btn-success" onclick="copyText()">πŸ“‹ Copy to Clipboard</button>
177
+ <button class="btn btn-secondary" onclick="downloadText()">πŸ’Ύ Download as TXT</button>
178
+ </div>
179
+ </div>
180
+
181
+ <div class="toast" id="toast">βœ… Copied to clipboard!</div>
182
+
183
+ <script>
184
+ const textElement = document.getElementById('textDisplay');
185
+ const text = textElement.textContent.trim();
186
+
187
+ // Update character and word count
188
+ if (text && text !== 'No text detected in the image. Please try another image.') {
189
+ document.getElementById('charCount').textContent = `Characters: ${text.length}`;
190
+ document.getElementById('wordCount').textContent = `Words: ${text.split(/\s+/).filter(w => w).length}`;
191
+ }
192
+
193
+ // Copy to clipboard
194
+ function copyText() {
195
+ const textToCopy = textElement.textContent;
196
+ if (textToCopy && textToCopy !== 'No text detected in the image. Please try another image.') {
197
+ navigator.clipboard.writeText(textToCopy).then(() => {
198
+ showToast();
199
+ }).catch(() => {
200
+ // Fallback for older browsers
201
+ const textarea = document.createElement('textarea');
202
+ textarea.value = textToCopy;
203
+ document.body.appendChild(textarea);
204
+ textarea.select();
205
+ document.execCommand('copy');
206
+ document.body.removeChild(textarea);
207
+ showToast();
208
+ });
209
+ }
210
+ }
211
+
212
+ function showToast() {
213
+ const toast = document.getElementById('toast');
214
+ toast.classList.add('show');
215
+ setTimeout(() => {
216
+ toast.classList.remove('show');
217
+ }, 2000);
218
+ }
219
+
220
+ // Download as .txt file
221
+ function downloadText() {
222
+ const textToDownload = textElement.textContent;
223
+ if (textToDownload && textToDownload !== 'No text detected in the image. Please try another image.') {
224
+ const blob = new Blob([textToDownload], { type: 'text/plain' });
225
+ const url = URL.createObjectURL(blob);
226
+ const a = document.createElement('a');
227
+ a.href = url;
228
+ a.download = `extracted_text_${Date.now()}.txt`;
229
+ document.body.appendChild(a);
230
+ a.click();
231
+ document.body.removeChild(a);
232
+ URL.revokeObjectURL(url);
233
+ }
234
+ }
235
+
236
+ // Keyboard shortcuts
237
+ document.addEventListener('keydown', (e) => {
238
+ if ((e.ctrlKey || e.metaKey) && e.key === 'c') {
239
+ // Let default copy work
240
+ }
241
+ });
242
+ </script>
243
+ </body>
244
+ </html>
uploads/file.txt ADDED
File without changes