-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
848 lines (739 loc) · 34.9 KB
/
Copy pathapp.py
File metadata and controls
848 lines (739 loc) · 34.9 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
import os
import re
import uuid
import json
import time
import io
import shutil
import subprocess
import tempfile
import threading
import csv as csv_mod
from pathlib import Path
from flask import Flask, request, jsonify, send_file, render_template
from gtts import gTTS
from gtts.lang import tts_langs
import markdown
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
# ─── Optional deps ────────────────────────────────────────────────────────────
try:
import pyttsx3
OFFLINE_TTS_AVAILABLE = True
except ImportError:
OFFLINE_TTS_AVAILABLE = False
try:
import fitz # PyMuPDF
PDF_AVAILABLE = True
except ImportError:
PDF_AVAILABLE = False
try:
import openpyxl
XLSX_AVAILABLE = True
except ImportError:
XLSX_AVAILABLE = False
try:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib import colors
REPORTLAB_AVAILABLE = True
except ImportError:
REPORTLAB_AVAILABLE = False
LO_BIN = shutil.which('libreoffice') or shutil.which('soffice')
# ─── App setup ────────────────────────────────────────────────────────────────
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024
UPLOAD_FOLDER = Path('uploads')
OUTPUT_FOLDER = Path('output')
UPLOAD_FOLDER.mkdir(exist_ok=True)
OUTPUT_FOLDER.mkdir(exist_ok=True)
jobs = {}
pdf_sessions = {}
# ══════════════════════════════════════════════════════════════════════════════
# HELPERS
# ══════════════════════════════════════════════════════════════════════════════
def clean_text(text: str) -> str:
text = re.sub(r'https?://\S+', '', text)
text = re.sub(r'[`*#_~>|]+', ' ', text)
text = re.sub(r'\n{3,}', '\n\n', text)
text = re.sub(r'[ \t]{2,}', ' ', text)
text = re.sub(r'[\u200b\u200c\u200d\ufeff]', '', text)
return text.strip()
def libreoffice_to_pdf(src: Path, out_dir: Path) -> Path:
"""Convert src to PDF using LibreOffice with an isolated per-call profile
so concurrent conversions never conflict."""
if not LO_BIN:
raise RuntimeError('LibreOffice is not installed on this server.')
with tempfile.TemporaryDirectory() as ptmp:
profile = Path(ptmp) / 'profile'
profile.mkdir()
result = subprocess.run(
[LO_BIN, '--headless', '--norestore',
f'-env:UserInstallation=file://{profile}',
'--convert-to', 'pdf',
'--outdir', str(out_dir),
str(src)],
capture_output=True, text=True, timeout=120,
)
pdf_out = out_dir / (src.stem + '.pdf')
if result.returncode != 0 or not pdf_out.exists():
detail = (result.stderr or result.stdout or 'no output').strip()[:400]
raise RuntimeError(f'LibreOffice failed (rc={result.returncode}): {detail}')
return pdf_out
def _cleanup_old_pdf_sessions():
now = time.time()
expired = [k for k, v in list(pdf_sessions.items()) if now - v['created_at'] > 7200]
for k in expired:
try:
pdf_sessions[k]['path'].unlink(missing_ok=True)
except Exception:
pass
pdf_sessions.pop(k, None)
# ══════════════════════════════════════════════════════════════════════════════
# VOICE EXTRACTORS
# ══════════════════════════════════════════════════════════════════════════════
def extract_md(fp):
raw = fp.read_text(encoding='utf-8')
html = markdown.markdown(raw, extensions=['tables', 'fenced_code'])
soup = BeautifulSoup(html, 'html.parser')
for tag in soup.find_all(['code', 'pre']):
tag.decompose()
return clean_text(soup.get_text(separator=' '))
def extract_docx(fp):
doc = Document(fp)
return clean_text('\n'.join(p.text for p in doc.paragraphs if p.text.strip()))
def extract_txt(fp):
try: return clean_text(fp.read_text(encoding='utf-8'))
except: return clean_text(fp.read_text(encoding='latin-1'))
VOICE_EXTRACTORS = {
'.md': extract_md, '.markdown': extract_md,
'.docx': extract_docx, '.doc': extract_docx,
'.txt': extract_txt,
}
# ══════════════════════════════════════════════════════════════════════════════
# READER PARSERS
# ══════════════════════════════════════════════════════════════════════════════
def parse_txt(fp):
try: text = fp.read_text(encoding='utf-8')
except: text = fp.read_text(encoding='latin-1')
return {'type': 'text', 'lines': len(text.splitlines()), 'chars': len(text), 'content': text}
def parse_md(fp):
raw = fp.read_text(encoding='utf-8')
html = markdown.markdown(raw, extensions=['tables', 'fenced_code', 'nl2br'])
return {'type': 'markdown', 'chars': len(raw), 'html': html, 'raw': raw}
def parse_pdf_file(fp: Path) -> dict:
"""Register a PDF as a render session. Pages are served on-demand as PNG images."""
if not PDF_AVAILABLE:
return {'type': 'pdf', 'error': 'PyMuPDF not installed. Run: pip install pymupdf'}
doc = fitz.open(str(fp))
total = len(doc)
text_pages = [doc[i].get_text('text') for i in range(min(total, 200))]
doc.close()
token = str(uuid.uuid4())
dest = UPLOAD_FOLDER / f'pdf_{token}.pdf'
shutil.copy2(str(fp), str(dest))
pdf_sessions[token] = {'path': dest, 'total': total, 'created_at': time.time()}
_cleanup_old_pdf_sessions()
return {'type': 'pdf', 'token': token, 'total_pages': total, 'text_pages': text_pages}
def _docx_to_rich_html(fp: Path) -> dict:
"""
Extract rich content from a .docx preserving:
- Heading levels (h1–h4) with Google-Docs-style colours
- Bold / italic / underline inline runs
- Paragraph alignment
- Bullet and numbered lists
- Tables with borders
- Inline images (embedded as base64 data URIs so no extra route needed)
"""
import base64
from docx.oxml.ns import qn
doc = Document(fp)
html = []
raw_text = []
def run_html(run):
text = (run.text
.replace('&', '&')
.replace('<', '<')
.replace('>', '>'))
if not text:
return ''
if run.bold: text = f'<strong>{text}</strong>'
if run.italic: text = f'<em>{text}</em>'
if run.underline: text = f'<u>{text}</u>'
return text
def para_images(para):
"""Return list of base64 data-URI strings for every inline image in the paragraph."""
imgs = []
for blip in para._element.findall('.//' + qn('a:blip')):
rId = blip.get(qn('r:embed'))
if not rId:
continue
try:
part = doc.part.related_parts[rId]
ct = part.content_type # image/png, image/jpeg, etc.
# Skip WMF/EMF vector formats browsers can't render
if ct in ('image/x-wmf', 'image/x-emf', 'image/emf', 'image/wmf'):
continue
b64 = base64.b64encode(part.blob).decode()
imgs.append(f'data:{ct};base64,{b64}')
except Exception:
pass
return imgs
for para in doc.paragraphs:
# ── Images first (paragraph may contain ONLY a drawing) ──────────────
images = para_images(para)
for src in images:
html.append(f'<p class="doc-p doc-img-wrap"><img class="doc-img" src="{src}" alt=""></p>')
text = para.text.strip()
if not text and not images:
html.append('<div class="doc-spacer"></div>')
continue
if not text: # image-only paragraph — already handled
continue
raw_text.append(para.text)
style = para.style.name.lower() if para.style else ''
inline = ''.join(run_html(r) for r in para.runs) or para.text.replace('&','&').replace('<','<').replace('>','>')
# Alignment
align = ''
try:
if para.alignment:
from docx.enum.text import WD_ALIGN_PARAGRAPH as WDA
if para.alignment == WDA.CENTER: align = ' style="text-align:center"'
elif para.alignment == WDA.RIGHT: align = ' style="text-align:right"'
elif para.alignment == WDA.JUSTIFY: align = ' style="text-align:justify"'
except Exception:
pass
if 'title' in style: html.append(f'<h1 class="doc-title"{align}>{inline}</h1>')
elif 'heading 1' in style: html.append(f'<h1 class="doc-h1"{align}>{inline}</h1>')
elif 'heading 2' in style: html.append(f'<h2 class="doc-h2"{align}>{inline}</h2>')
elif 'heading 3' in style: html.append(f'<h3 class="doc-h3"{align}>{inline}</h3>')
elif 'heading 4' in style or 'heading 5' in style:
html.append(f'<h4 class="doc-h4"{align}>{inline}</h4>')
elif 'list bullet' in style or 'list paragraph' in style:
html.append(f'<li class="doc-li"{align}>{inline}</li>')
elif 'list number' in style:
html.append(f'<li class="doc-li doc-li-num"{align}>{inline}</li>')
else:
html.append(f'<p class="doc-p"{align}>{inline}</p>')
# ── Tables ────────────────────────────────────────────────────────────────
for tbl in doc.tables:
html.append('<table class="doc-table"><tbody>')
for ri, row in enumerate(tbl.rows):
html.append('<tr>')
for cell in row.cells:
tag = 'th' if ri == 0 else 'td'
cell_txt = (cell.text
.replace('&', '&')
.replace('<', '<')
.replace('>', '>'))
html.append(f'<{tag} class="doc-td">{cell_txt}</{tag}>')
html.append('</tr>')
html.append('</tbody></table>')
return {
'type': 'docx',
'html': '\n'.join(html),
'raw_text': '\n'.join(raw_text),
'chars': sum(len(p.text) for p in doc.paragraphs),
'paragraph_count': len([p for p in doc.paragraphs if p.text.strip()]),
'table_count': len(doc.tables),
}
def parse_docx_file(fp: Path) -> dict:
"""
Primary: convert docx → PDF via LibreOffice, render pages as PNG images
(pixel-perfect, preserves all formatting and images).
Fallback: extract rich styled HTML from python-docx so it still looks
like a word processor view even without LibreOffice.
"""
if LO_BIN:
try:
with tempfile.TemporaryDirectory() as tmpdir:
pdf_path = libreoffice_to_pdf(fp, Path(tmpdir))
data = parse_pdf_file(pdf_path) # copies PDF out before tmpdir closes
data['original_type'] = 'docx'
doc = Document(fp)
data['docx_text'] = '\n'.join(p.text for p in doc.paragraphs if p.text.strip())
return data
except Exception as exc:
app.logger.warning(f'docx render via LibreOffice failed: {exc} — using rich HTML fallback')
# Rich HTML fallback — styled headings, tables, bold/italic
return _docx_to_rich_html(fp)
def parse_csv(fp):
try: text = fp.read_text(encoding='utf-8')
except: text = fp.read_text(encoding='latin-1')
rows = list(csv_mod.reader(io.StringIO(text)))
if not rows:
return {'type': 'csv', 'headers': [], 'rows': [], 'total_rows': 0, 'col_count': 0}
headers, data_rows = rows[0], rows[1:]
return {
'type': 'csv', 'headers': headers, 'rows': data_rows[:500],
'total_rows': len(data_rows), 'truncated': len(data_rows) > 500,
'col_count': len(headers),
}
def parse_json(fp):
try: text = fp.read_text(encoding='utf-8')
except: text = fp.read_text(encoding='latin-1')
try:
data = json.loads(text)
pretty = json.dumps(data, indent=2, ensure_ascii=False)
trunc = len(pretty) > 100_000
if trunc:
pretty = pretty[:100_000] + '\n… (truncated)'
kind = 'array' if isinstance(data, list) else 'object' if isinstance(data, dict) else 'value'
return {'type': 'json', 'kind': kind,
'length': len(data) if isinstance(data, (list, dict)) else None,
'pretty': pretty, 'truncated': trunc, 'raw_size': len(text)}
except json.JSONDecodeError as e:
return {'type': 'json', 'error': str(e), 'raw': text[:5000]}
def parse_xlsx(fp):
if not XLSX_AVAILABLE:
return {'type': 'xlsx', 'error': 'openpyxl not installed. Run: pip install openpyxl'}
wb = openpyxl.load_workbook(fp, read_only=True, data_only=True)
sheets = []
for name in wb.sheetnames:
ws = wb[name]
rows = [
['' if v is None else str(v) for v in row]
for i, row in enumerate(ws.iter_rows(values_only=True)) if i < 502
]
headers = rows[0] if rows else []
total = max(0, (ws.max_row or 0) - 1)
sheets.append({
'name': name, 'headers': headers,
'rows': rows[1:] if len(rows) > 1 else [],
'total_rows': total, 'truncated': total > 501,
'col_count': len(headers),
})
wb.close()
return {'type': 'xlsx', 'sheets': sheets, 'sheet_count': len(sheets)}
READER_PARSERS = {
'.txt': parse_txt, '.md': parse_md, '.markdown': parse_md,
'.docx': parse_docx_file, '.doc': parse_docx_file,
'.csv': parse_csv, '.json': parse_json,
'.xlsx': parse_xlsx, '.xls': parse_xlsx,
'.pdf': parse_pdf_file,
}
READER_ICONS = {
'.txt': '📄', '.md': '📝', '.markdown': '📝',
'.docx': '📘', '.doc': '📘',
'.csv': '📊', '.xlsx': '📊', '.xls': '📊',
'.json': '🔧', '.pdf': '📕',
}
# ══════════════════════════════════════════════════════════════════════════════
# TTS ENGINE
# ══════════════════════════════════════════════════════════════════════════════
def _tts_worker(text, lang, slow, job_id, engine):
try:
jobs[job_id]['status'] = 'processing'
jobs[job_id]['progress'] = 10
out = OUTPUT_FOLDER / f'{job_id}.mp3'
if engine == 'offline' and OFFLINE_TTS_AVAILABLE:
_tts_offline(text, out, slow, job_id)
else:
_tts_gtts(text, lang, slow, job_id, out)
jobs[job_id].update({'status': 'done', 'progress': 100,
'filename': f'{job_id}.mp3', 'file_size': out.stat().st_size})
except Exception as e:
jobs[job_id].update({'status': 'error', 'error': str(e)})
def _tts_gtts(text, lang, slow, job_id, out):
chunks = _split_chunks(text)
files = []
for i, chunk in enumerate(chunks):
cp = OUTPUT_FOLDER / f'{job_id}_c{i}.mp3'
gTTS(text=chunk, lang=lang, slow=slow).save(str(cp))
files.append(cp)
jobs[job_id]['progress'] = 10 + int(((i + 1) / len(chunks)) * 80)
if len(files) == 1:
files[0].rename(out)
else:
with open(out, 'wb') as fh:
for f in files:
fh.write(f.read_bytes())
f.unlink()
def _tts_offline(text, out, slow, job_id):
engine = pyttsx3.init()
engine.setProperty('rate', int(engine.getProperty('rate') * (0.7 if slow else 1.0)))
wav = out.with_suffix('.wav')
engine.save_to_file(text, str(wav))
engine.runAndWait()
jobs[job_id]['progress'] = 80
if wav.exists():
try:
subprocess.run(['ffmpeg', '-y', '-i', str(wav),
'-codec:a', 'libmp3lame', '-qscale:a', '2', str(out)],
capture_output=True, timeout=60)
wav.unlink(missing_ok=True)
except Exception:
wav.rename(out)
def _split_chunks(text, max_len=4000):
sentences = re.split(r'(?<=[.!?])\s+', text)
chunks, cur = [], ''
for s in sentences:
if len(cur) + len(s) + 1 <= max_len:
cur = (cur + ' ' + s).strip()
else:
if cur:
chunks.append(cur)
if len(s) > max_len:
words, buf = s.split(), ''
for w in words:
if len(buf) + len(w) + 1 <= max_len:
buf = (buf + ' ' + w).strip()
else:
if buf:
chunks.append(buf)
buf = w
if buf:
chunks.append(buf)
cur = ''
else:
cur = s
if cur:
chunks.append(cur)
return chunks or [text[:max_len]]
# ══════════════════════════════════════════════════════════════════════════════
# PAGE ROUTES
# ══════════════════════════════════════════════════════════════════════════════
@app.route('/')
def index():
return render_template('index.html')
@app.route('/reader')
def reader_page():
return render_template('reader.html')
@app.route('/editor')
def editor_page():
return render_template('editor.html')
# ══════════════════════════════════════════════════════════════════════════════
# VOICE API
# ══════════════════════════════════════════════════════════════════════════════
@app.route('/api/capabilities')
def capabilities():
return jsonify({
'offline_tts': OFFLINE_TTS_AVAILABLE,
'pdf_reader': PDF_AVAILABLE,
'xlsx_reader': XLSX_AVAILABLE,
'pdf_export': REPORTLAB_AVAILABLE,
'libreoffice': LO_BIN is not None,
})
@app.route('/api/languages')
def get_languages():
return jsonify(tts_langs())
@app.route('/api/convert/text', methods=['POST'])
def convert_text():
data = request.get_json()
text = data.get('text', '').strip()
if not text:
return jsonify({'error': 'No text provided'}), 400
if len(text) > 50_000:
return jsonify({'error': 'Text too long (max 50,000 chars)'}), 400
job_id = str(uuid.uuid4())
jobs[job_id] = {'status': 'queued', 'progress': 0, 'filename': None, 'error': None,
'original_name': data.get('filename', 'voice_note'),
'created_at': time.time(), 'char_count': len(text)}
threading.Thread(
target=_tts_worker,
args=(text, data.get('lang', 'en'), data.get('slow', False),
job_id, data.get('engine', 'gtts')),
daemon=True,
).start()
return jsonify({'job_id': job_id})
@app.route('/api/convert/file', methods=['POST'])
def convert_file():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
ext = Path(file.filename).suffix.lower()
if ext not in VOICE_EXTRACTORS:
return jsonify({'error': f'Unsupported type: {ext}'}), 400
p = UPLOAD_FOLDER / f'{uuid.uuid4()}{ext}'
try:
file.save(p)
text = VOICE_EXTRACTORS[ext](p)
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
p.unlink(missing_ok=True)
if not text.strip():
return jsonify({'error': 'No readable text found'}), 400
job_id = str(uuid.uuid4())
jobs[job_id] = {'status': 'queued', 'progress': 0, 'filename': None, 'error': None,
'original_name': Path(file.filename).stem,
'created_at': time.time(), 'char_count': len(text)}
threading.Thread(
target=_tts_worker,
args=(text, request.form.get('lang', 'en'),
request.form.get('slow', 'false').lower() == 'true',
job_id, request.form.get('engine', 'gtts')),
daemon=True,
).start()
return jsonify({'job_id': job_id, 'char_count': len(text), 'preview': text[:300]})
@app.route('/api/job/<job_id>')
def job_status(job_id):
j = jobs.get(job_id)
return jsonify(j) if j else (jsonify({'error': 'Not found'}), 404)
@app.route('/api/stream/<job_id>')
def stream_audio(job_id):
j = jobs.get(job_id)
if not j or j['status'] != 'done':
return jsonify({'error': 'Not ready'}), 404
return send_file(OUTPUT_FOLDER / j['filename'], mimetype='audio/mpeg')
@app.route('/api/download/<job_id>')
def download_audio(job_id):
j = jobs.get(job_id)
if not j or j['status'] != 'done':
return jsonify({'error': 'Not ready'}), 404
fp = OUTPUT_FOLDER / j['filename']
if not fp.exists():
return jsonify({'error': 'File missing'}), 404
return send_file(fp, as_attachment=True,
download_name=f"{j['original_name']}.mp3", mimetype='audio/mpeg')
@app.route('/api/history')
def get_history():
h = [{'job_id': k, 'original_name': v.get('original_name', 'note'),
'char_count': v.get('char_count', 0), 'file_size': v.get('file_size', 0),
'created_at': v.get('created_at', 0)}
for k, v in jobs.items() if v['status'] == 'done']
return jsonify(sorted(h, key=lambda x: x['created_at'], reverse=True))
@app.route('/api/delete/<job_id>', methods=['DELETE'])
def delete_job(job_id):
j = jobs.pop(job_id, None)
if not j:
return jsonify({'error': 'Not found'}), 404
(OUTPUT_FOLDER / f'{job_id}.mp3').unlink(missing_ok=True)
return jsonify({'ok': True})
# ══════════════════════════════════════════════════════════════════════════════
# READER API
# ══════════════════════════════════════════════════════════════════════════════
@app.route('/api/reader/open', methods=['POST'])
def reader_open():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
# Guard: filename can be None or empty if browser omits Content-Disposition filename
filename = file.filename or ''
if not filename:
return jsonify({'error': 'File has no name — try using the Open File button instead of drag-drop'}), 400
ext = Path(filename).suffix.lower()
if ext not in READER_PARSERS:
return jsonify({'error': f'Unsupported: {ext}. Supported: {", ".join(sorted(READER_PARSERS))}'}), 400
p = UPLOAD_FOLDER / f'{uuid.uuid4()}{ext}'
try:
file.save(p)
size = p.stat().st_size
result = READER_PARSERS[ext](p)
result.update({'filename': filename, 'icon': READER_ICONS.get(ext, '📄'),
'ext': ext, 'size': size})
return jsonify(result)
except Exception as e:
app.logger.exception('reader_open failed')
return jsonify({'error': str(e)}), 500
finally:
p.unlink(missing_ok=True)
@app.route('/api/reader/pdf-page/<token>/<int:page_num>')
def pdf_page_image(token, page_num):
session = pdf_sessions.get(token)
if not session:
return jsonify({'error': 'Session expired or not found'}), 404
if not (0 <= page_num < session['total']):
return jsonify({'error': 'Page out of range'}), 400
doc = fitz.open(str(session['path']))
pix = doc[page_num].get_pixmap(matrix=fitz.Matrix(2.0, 2.0), alpha=False)
data = pix.tobytes('png')
doc.close()
return send_file(io.BytesIO(data), mimetype='image/png', max_age=3600)
@app.route('/api/reader/pdf-close/<token>', methods=['DELETE'])
def pdf_close(token):
session = pdf_sessions.pop(token, None)
if session:
session['path'].unlink(missing_ok=True)
return jsonify({'ok': True})
@app.route('/api/reader/extract-text', methods=['POST'])
def reader_extract_text():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
ext = Path(file.filename).suffix.lower()
p = UPLOAD_FOLDER / f'{uuid.uuid4()}{ext}'
try:
file.save(p)
text = ''
if ext in VOICE_EXTRACTORS:
text = VOICE_EXTRACTORS[ext](p)
elif ext == '.csv':
d = parse_csv(p)
text = '\n'.join(['\t'.join(d['headers'])] + ['\t'.join(r) for r in d['rows']])
elif ext in ('.xlsx', '.xls'):
d = parse_xlsx(p)
parts = []
for s in d.get('sheets', []):
parts.append('Sheet: ' + s['name'] + '\n' +
'\n'.join(['\t'.join(s['headers'])] + ['\t'.join(r) for r in s['rows']]))
text = '\n\n'.join(parts)
elif ext == '.json':
text = parse_json(p).get('pretty', '')
elif ext == '.pdf':
if PDF_AVAILABLE:
doc = fitz.open(str(p))
pages = [doc[i].get_text('text') for i in range(len(doc))]
doc.close()
text = '\n\n'.join(t for t in pages if t.strip())
else:
text = 'PDF reading not available (install pymupdf).'
return jsonify({'text': clean_text(text), 'char_count': len(text)})
except Exception as e:
app.logger.exception('reader_extract_text failed')
return jsonify({'error': str(e)}), 500
finally:
p.unlink(missing_ok=True)
@app.route('/api/reader/docx-to-pdf', methods=['POST'])
def docx_to_pdf():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
ext = Path(file.filename).suffix.lower()
if ext not in ('.docx', '.doc'):
return jsonify({'error': 'Only .docx / .doc files are supported'}), 400
if not LO_BIN:
return jsonify({'error': 'LibreOffice is not installed on this server'}), 500
safe = re.sub(r'[^\w\s-]', '', Path(file.filename).stem).strip() or 'document'
p = UPLOAD_FOLDER / f'{uuid.uuid4()}{ext}'
try:
file.save(p)
with tempfile.TemporaryDirectory() as tmpdir:
pdf_src = libreoffice_to_pdf(p, Path(tmpdir))
out = OUTPUT_FOLDER / f'{uuid.uuid4()}_{safe}.pdf'
shutil.copy2(str(pdf_src), str(out))
return send_file(out, as_attachment=True,
download_name=f'{safe}.pdf', mimetype='application/pdf')
except RuntimeError as e:
return jsonify({'error': str(e)}), 500
except Exception as e:
app.logger.exception('docx_to_pdf failed')
return jsonify({'error': str(e)}), 500
finally:
p.unlink(missing_ok=True)
# ══════════════════════════════════════════════════════════════════════════════
# EDITOR API
# ══════════════════════════════════════════════════════════════════════════════
@app.route('/api/editor/open', methods=['POST'])
def editor_open():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
ext = Path(file.filename).suffix.lower()
if ext not in ('.docx', '.doc', '.txt', '.md', '.markdown'):
return jsonify({'error': 'Open .docx, .txt, or .md files for editing'}), 400
p = UPLOAD_FOLDER / f'{uuid.uuid4()}{ext}'
try:
file.save(p)
if ext in ('.docx', '.doc'):
doc = Document(p)
blocks = []
for para in doc.paragraphs:
if not para.text.strip():
continue
s = para.style.name.lower()
tag = ('h1' if ('heading 1' in s or 'title' in s) else
'h2' if 'heading 2' in s else
'h3' if 'heading 3' in s else 'p')
blocks.append({'tag': tag, 'text': para.text})
return jsonify({'blocks': blocks, 'filename': file.filename})
else:
raw = p.read_text(encoding='utf-8')
blocks = []
for line in raw.splitlines():
if line.startswith('### '): blocks.append({'tag': 'h3', 'text': line[4:]})
elif line.startswith('## '): blocks.append({'tag': 'h2', 'text': line[3:]})
elif line.startswith('# '): blocks.append({'tag': 'h1', 'text': line[2:]})
elif line.strip(): blocks.append({'tag': 'p', 'text': line})
return jsonify({'blocks': blocks, 'filename': file.filename})
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
p.unlink(missing_ok=True)
@app.route('/api/editor/save-docx', methods=['POST'])
def editor_save_docx():
data = request.get_json()
blocks = data.get('blocks', [])
title = data.get('title', 'document')
safe = re.sub(r'[^\w\s-]', '', title).strip() or 'document'
doc = Document()
for section in doc.sections:
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1.2)
section.right_margin = Inches(1.2)
for block in blocks:
tag = block.get('tag', 'p')
text = block.get('text', '').strip()
if not text: continue
if tag == 'h1': doc.add_heading(text, level=1)
elif tag == 'h2': doc.add_heading(text, level=2)
elif tag == 'h3': doc.add_heading(text, level=3)
else: doc.add_paragraph(text)
out = OUTPUT_FOLDER / f'{uuid.uuid4()}_{safe}.docx'
doc.save(out)
return send_file(out, as_attachment=True, download_name=f'{safe}.docx',
mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
@app.route('/api/editor/save-pdf', methods=['POST'])
def editor_save_pdf():
"""Save editor content as PDF. Uses LibreOffice if available (best quality),
otherwise falls back to reportlab."""
data = request.get_json()
blocks = data.get('blocks', [])
title = data.get('title', 'document')
safe = re.sub(r'[^\w\s-]', '', title).strip() or 'document'
if LO_BIN:
try:
doc = Document()
for section in doc.sections:
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1.2)
section.right_margin = Inches(1.2)
for block in blocks:
tag = block.get('tag', 'p')
text = block.get('text', '').strip()
if not text: continue
if tag == 'h1': doc.add_heading(text, level=1)
elif tag == 'h2': doc.add_heading(text, level=2)
elif tag == 'h3': doc.add_heading(text, level=3)
else: doc.add_paragraph(text)
with tempfile.TemporaryDirectory() as tmpdir:
tmp_docx = Path(tmpdir) / f'{safe}.docx'
doc.save(str(tmp_docx))
pdf_src = libreoffice_to_pdf(tmp_docx, Path(tmpdir))
out = OUTPUT_FOLDER / f'{uuid.uuid4()}_{safe}.pdf'
shutil.copy2(str(pdf_src), str(out))
return send_file(out, as_attachment=True,
download_name=f'{safe}.pdf', mimetype='application/pdf')
except Exception as e:
app.logger.warning(f'editor save-pdf LibreOffice failed: {e}')
if not REPORTLAB_AVAILABLE:
return jsonify({'error': 'Neither LibreOffice nor reportlab is available'}), 500
out = OUTPUT_FOLDER / f'{uuid.uuid4()}_{safe}.pdf'
styles = getSampleStyleSheet()
custom = {
'h1': ParagraphStyle('H1', parent=styles['Heading1'], fontSize=22,
spaceAfter=12, spaceBefore=18, textColor=colors.HexColor('#1a1a2e')),
'h2': ParagraphStyle('H2', parent=styles['Heading2'], fontSize=16,
spaceAfter=8, spaceBefore=14, textColor=colors.HexColor('#16213e')),
'h3': ParagraphStyle('H3', parent=styles['Heading3'], fontSize=13,
spaceAfter=6, spaceBefore=10, textColor=colors.HexColor('#0f3460')),
'p': ParagraphStyle('Body', parent=styles['Normal'], fontSize=11, leading=18, spaceAfter=8),
}
story = [Paragraph(b.get('text', '').strip(), custom.get(b.get('tag', 'p'), custom['p']))
for b in blocks if b.get('text', '').strip()]
SimpleDocTemplate(str(out), pagesize=letter,
leftMargin=1.2 * inch, rightMargin=1.2 * inch,
topMargin=inch, bottomMargin=inch).build(story)
return send_file(out, as_attachment=True,
download_name=f'{safe}.pdf', mimetype='application/pdf')
# ══════════════════════════════════════════════════════════════════════════════
if __name__ == '__main__':
print('\n🎙️ VoiceNotes → http://localhost:5050')
print(' Reader → http://localhost:5050/reader')
print(' Editor → http://localhost:5050/editor\n')
app.run(debug=False, host='0.0.0.0', port=5050)