Files
AI-Prototype/launch_viewer.py

113 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""
P44 OCR Viewer launcher.
Starts a local HTTP server at port 5044 and opens the viewer in the default browser.
Run from any directory; the server root is always the project folder (this file's location).
If port 5044 is already in use (server already running), just opens the browser tab.
"""
import http.server
import webbrowser
import threading
import socket
import os
import sys
import json
PORT = 5044
ROOT = os.path.dirname(os.path.abspath(__file__))
class QuietHandler(http.server.SimpleHTTPRequestHandler):
"""Serve files from ROOT, suppress request logs, disable browser caching."""
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=ROOT, **kwargs)
def log_message(self, fmt, *args):
pass # silence per-request noise
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
def do_POST(self):
"""Handle POST /save-flags and POST /save-corrections."""
length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(length)
try:
data = json.loads(body)
except json.JSONDecodeError:
self.send_response(400); self.end_headers()
return
if self.path == '/save-flags':
dest = os.path.join(ROOT, 'flags.json')
with open(dest, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{"ok":true}')
elif self.path == '/save-corrections':
dest = os.path.join(ROOT, 'corrections.json')
# Load existing, update single page entry, write back
existing = {}
if os.path.exists(dest):
try:
with open(dest, 'r', encoding='utf-8') as f:
existing = json.load(f)
except Exception:
pass
page = str(data.get('page', ''))
text = data.get('text', '')
if text.strip():
existing[page] = text
else:
existing.pop(page, None)
with open(dest, 'w', encoding='utf-8') as f:
json.dump(existing, f, indent=2)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{"ok":true}')
else:
self.send_response(404); self.end_headers()
def port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
def start_server():
server = http.server.HTTPServer(('localhost', PORT), QuietHandler)
server.serve_forever()
if __name__ == '__main__':
url = 'http://localhost:{}/p44-ocr-viewer.html'.format(PORT)
if port_in_use(PORT):
print('\n P44 OCR Viewer (server already running)')
print(' URL: {}\n'.format(url))
webbrowser.open(url)
else:
t = threading.Thread(target=start_server, daemon=True)
t.start()
print('\n P44 OCR Viewer')
print(' Serving : {}'.format(ROOT))
print(' URL : {}\n'.format(url))
webbrowser.open(url)
print('Press Enter (or Ctrl-C) to stop the server.\n')
try:
input()
except KeyboardInterrupt:
pass
print('Server stopped.')
sys.exit(0)