Update app.py
Browse files
app.py
CHANGED
|
@@ -1,189 +1,371 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# 🧳 Importing the whole circus—get ready for a wild ride!
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import time
|
| 6 |
import pandas as pd
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
-
from
|
|
|
|
| 13 |
import fitz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# 📜 Logging setup—because even AIs need a diary!
|
| 16 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
-
log_records = []
|
| 19 |
|
| 20 |
-
# 🤓 LogCaptureHandler class—catching logs like a pro fisherman!
|
| 21 |
class LogCaptureHandler(logging.Handler):
|
| 22 |
-
# 🎣 Hooking those logs right outta the stream!
|
| 23 |
def emit(self, record):
|
| 24 |
log_records.append(record)
|
| 25 |
|
| 26 |
-
logger.addHandler(LogCaptureHandler())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
def
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return f"{sequence}_{timestamp}.{ext}"
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
for
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
history.append(f"Uploaded Image: {output_path}") # 📜 Logging the fame!
|
| 50 |
-
selected_files[output_path] = False # ✅ Unchecked for now!
|
| 51 |
-
return f"Uploaded {len(uploaded)} images", history, selected_files
|
| 52 |
-
|
| 53 |
-
# 📜 Scribble some docs—PDFs and more, oh what a bore!
|
| 54 |
-
def upload_documents(files, history, selected_files):
|
| 55 |
-
if not files:
|
| 56 |
-
return "No files uploaded", history, selected_files # 📝 No docs, no drama!
|
| 57 |
-
uploaded = []
|
| 58 |
-
for file in files:
|
| 59 |
-
ext = file.name.split('.')[-1].lower() # 🕵️ Peeking at the paper type!
|
| 60 |
-
if ext in ["pdf"]: # Limiting to PDF for demo simplicity
|
| 61 |
-
output_path = f"doc_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Stamping the scroll!
|
| 62 |
with open(output_path, "wb") as f:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
uploaded = []
|
| 74 |
-
for
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
output_path = f"data_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Labeling the stats!
|
| 78 |
with open(output_path, "wb") as f:
|
| 79 |
-
f.write(
|
| 80 |
uploaded.append(output_path)
|
| 81 |
-
history.append(f"Uploaded
|
| 82 |
-
|
| 83 |
-
return f"Uploaded {len(uploaded)}
|
| 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 |
-
return
|
| 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 |
with gr.Row():
|
| 148 |
-
gr.
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
with gr.
|
| 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 |
demo.launch()
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import glob
|
| 4 |
+
import base64
|
| 5 |
import time
|
| 6 |
import pandas as pd
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel
|
| 11 |
+
from diffusers import StableDiffusionPipeline
|
| 12 |
+
from torch.utils.data import Dataset, DataLoader
|
| 13 |
+
import csv
|
| 14 |
import fitz
|
| 15 |
+
import requests
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import numpy as np
|
| 18 |
+
import logging
|
| 19 |
+
import asyncio
|
| 20 |
+
import aiofiles
|
| 21 |
+
from io import BytesIO
|
| 22 |
+
from dataclasses import dataclass
|
| 23 |
+
from typing import Optional, Tuple
|
| 24 |
+
import zipfile
|
| 25 |
+
import math
|
| 26 |
+
import random
|
| 27 |
+
import re
|
| 28 |
+
import gradio as gr
|
| 29 |
|
|
|
|
| 30 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 31 |
logger = logging.getLogger(__name__)
|
| 32 |
+
log_records = []
|
| 33 |
|
|
|
|
| 34 |
class LogCaptureHandler(logging.Handler):
|
|
|
|
| 35 |
def emit(self, record):
|
| 36 |
log_records.append(record)
|
| 37 |
|
| 38 |
+
logger.addHandler(LogCaptureHandler())
|
| 39 |
+
|
| 40 |
+
@dataclass
|
| 41 |
+
class ModelConfig:
|
| 42 |
+
name: str
|
| 43 |
+
base_model: str
|
| 44 |
+
size: str
|
| 45 |
+
domain: Optional[str] = None
|
| 46 |
+
model_type: str = "causal_lm"
|
| 47 |
+
@property
|
| 48 |
+
def model_path(self):
|
| 49 |
+
return f"models/{self.name}"
|
| 50 |
+
|
| 51 |
+
@dataclass
|
| 52 |
+
class DiffusionConfig:
|
| 53 |
+
name: str
|
| 54 |
+
base_model: str
|
| 55 |
+
size: str
|
| 56 |
+
domain: Optional[str] = None
|
| 57 |
+
@property
|
| 58 |
+
def model_path(self):
|
| 59 |
+
return f"diffusion_models/{self.name}"
|
| 60 |
|
| 61 |
+
class ModelBuilder:
|
| 62 |
+
def __init__(self):
|
| 63 |
+
self.config = None
|
| 64 |
+
self.model = None
|
| 65 |
+
self.tokenizer = None
|
| 66 |
+
self.jokes = ["Why did the AI go to therapy? Too many layers to unpack! 😂", "Training complete! Time for a binary coffee break. ☕"]
|
| 67 |
+
def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
|
| 68 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 69 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 70 |
+
if self.tokenizer.pad_token is None:
|
| 71 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 72 |
+
if config:
|
| 73 |
+
self.config = config
|
| 74 |
+
self.model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 75 |
+
return self
|
| 76 |
+
def save_model(self, path: str):
|
| 77 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 78 |
+
self.model.save_pretrained(path)
|
| 79 |
+
self.tokenizer.save_pretrained(path)
|
| 80 |
+
|
| 81 |
+
class DiffusionBuilder:
|
| 82 |
+
def __init__(self):
|
| 83 |
+
self.config = None
|
| 84 |
+
self.pipeline = None
|
| 85 |
+
def load_model(self, model_path: str, config: Optional[DiffusionConfig] = None):
|
| 86 |
+
self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu")
|
| 87 |
+
if config:
|
| 88 |
+
self.config = config
|
| 89 |
+
return self
|
| 90 |
+
def save_model(self, path: str):
|
| 91 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 92 |
+
self.pipeline.save_pretrained(path)
|
| 93 |
+
def generate(self, prompt: str):
|
| 94 |
+
return self.pipeline(prompt, num_inference_steps=20).images[0]
|
| 95 |
+
|
| 96 |
+
def generate_filename(sequence, ext="png"):
|
| 97 |
+
timestamp = time.strftime("%d%m%Y%H%M%S")
|
| 98 |
return f"{sequence}_{timestamp}.{ext}"
|
| 99 |
|
| 100 |
+
def pdf_url_to_filename(url):
|
| 101 |
+
safe_name = re.sub(r'[<>:"/\\|?*]', '_', url)
|
| 102 |
+
return f"{safe_name}.pdf"
|
| 103 |
|
| 104 |
+
def get_gallery_files(file_types=["png", "pdf"]):
|
| 105 |
+
return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")]))) # Deduplicate files
|
| 106 |
+
|
| 107 |
+
def get_model_files(model_type="causal_lm"):
|
| 108 |
+
path = "models/*" if model_type == "causal_lm" else "diffusion_models/*"
|
| 109 |
+
dirs = [d for d in glob.glob(path) if os.path.isdir(d)]
|
| 110 |
+
return dirs if dirs else ["None"]
|
| 111 |
+
|
| 112 |
+
def download_pdf(url, output_path):
|
| 113 |
+
try:
|
| 114 |
+
response = requests.get(url, stream=True, timeout=10)
|
| 115 |
+
if response.status_code == 200:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
with open(output_path, "wb") as f:
|
| 117 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 118 |
+
f.write(chunk)
|
| 119 |
+
return True
|
| 120 |
+
except requests.RequestException as e:
|
| 121 |
+
logger.error(f"Failed to download {url}: {e}")
|
| 122 |
+
return False
|
| 123 |
+
|
| 124 |
+
async def process_pdf_snapshot(pdf_path, mode="single"):
|
| 125 |
+
doc = fitz.open(pdf_path)
|
| 126 |
+
output_files = []
|
| 127 |
+
if mode == "single":
|
| 128 |
+
page = doc[0]
|
| 129 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 130 |
+
output_file = generate_filename("single", "png")
|
| 131 |
+
pix.save(output_file)
|
| 132 |
+
output_files.append(output_file)
|
| 133 |
+
elif mode == "twopage":
|
| 134 |
+
for i in range(min(2, len(doc))):
|
| 135 |
+
page = doc[i]
|
| 136 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 137 |
+
output_file = generate_filename(f"twopage_{i}", "png")
|
| 138 |
+
pix.save(output_file)
|
| 139 |
+
output_files.append(output_file)
|
| 140 |
+
elif mode == "allpages":
|
| 141 |
+
for i in range(len(doc)):
|
| 142 |
+
page = doc[i]
|
| 143 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 144 |
+
output_file = generate_filename(f"page_{i}", "png")
|
| 145 |
+
pix.save(output_file)
|
| 146 |
+
output_files.append(output_file)
|
| 147 |
+
doc.close()
|
| 148 |
+
return output_files
|
| 149 |
+
|
| 150 |
+
async def process_ocr(image, output_file):
|
| 151 |
+
tokenizer = AutoTokenizer.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True)
|
| 152 |
+
model = AutoModel.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True, torch_dtype=torch.float32).to("cpu").eval()
|
| 153 |
+
temp_file = f"temp_{int(time.time())}.png"
|
| 154 |
+
image.save(temp_file)
|
| 155 |
+
result = model.chat(tokenizer, temp_file, ocr_type='ocr')
|
| 156 |
+
os.remove(temp_file)
|
| 157 |
+
async with aiofiles.open(output_file, "w") as f:
|
| 158 |
+
await f.write(result)
|
| 159 |
+
return result
|
| 160 |
+
|
| 161 |
+
async def process_image_gen(prompt, output_file, builder):
|
| 162 |
+
if builder and isinstance(builder, DiffusionBuilder) and builder.pipeline:
|
| 163 |
+
pipeline = builder.pipeline
|
| 164 |
+
else:
|
| 165 |
+
pipeline = StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu")
|
| 166 |
+
gen_image = pipeline(prompt, num_inference_steps=20).images[0]
|
| 167 |
+
gen_image.save(output_file)
|
| 168 |
+
return gen_image
|
| 169 |
+
|
| 170 |
+
# Gradio Interface Functions
|
| 171 |
+
def update_gallery(history, asset_checkboxes):
|
| 172 |
+
all_files = get_gallery_files()
|
| 173 |
+
gallery_images = []
|
| 174 |
+
for file in all_files[:5]: # Limit to 5 for display
|
| 175 |
+
if file.endswith('.png'):
|
| 176 |
+
gallery_images.append(Image.open(file))
|
| 177 |
+
else:
|
| 178 |
+
doc = fitz.open(file)
|
| 179 |
+
pix = doc[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5))
|
| 180 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 181 |
+
gallery_images.append(img)
|
| 182 |
+
doc.close()
|
| 183 |
+
history.append(f"Gallery updated: {len(all_files)} files")
|
| 184 |
+
return gallery_images, history, asset_checkboxes
|
| 185 |
+
|
| 186 |
+
def camera_snap(image, cam_id, history, asset_checkboxes, cam_files):
|
| 187 |
+
if image is not None:
|
| 188 |
+
filename = generate_filename(f"cam{cam_id}")
|
| 189 |
+
image.save(filename)
|
| 190 |
+
history.append(f"Snapshot from Cam {cam_id}: {filename}")
|
| 191 |
+
asset_checkboxes[filename] = True
|
| 192 |
+
cam_files[cam_id] = filename
|
| 193 |
+
return f"Image saved as {filename}", Image.open(filename), history, asset_checkboxes, cam_files
|
| 194 |
+
elif cam_files.get(cam_id) and os.path.exists(cam_files[cam_id]):
|
| 195 |
+
return f"Showing previous capture: {cam_files[cam_id]}", Image.open(cam_files[cam_id]), history, asset_checkboxes, cam_files
|
| 196 |
+
return "No image captured", None, history, asset_checkboxes, cam_files
|
| 197 |
+
|
| 198 |
+
def download_pdfs(urls, history, asset_checkboxes):
|
| 199 |
+
urls = urls.strip().split("\n")
|
| 200 |
+
downloaded = []
|
| 201 |
+
for url in urls:
|
| 202 |
+
if url:
|
| 203 |
+
output_path = pdf_url_to_filename(url)
|
| 204 |
+
if download_pdf(url, output_path):
|
| 205 |
+
downloaded.append(output_path)
|
| 206 |
+
history.append(f"Downloaded PDF: {output_path}")
|
| 207 |
+
asset_checkboxes[output_path] = True
|
| 208 |
+
return f"Downloaded {len(downloaded)} PDFs", history, asset_checkboxes
|
| 209 |
+
|
| 210 |
+
def upload_pdfs(pdf_files, history, asset_checkboxes):
|
| 211 |
uploaded = []
|
| 212 |
+
for pdf_file in pdf_files:
|
| 213 |
+
if pdf_file:
|
| 214 |
+
output_path = f"uploaded_{int(time.time())}_{pdf_file.name}"
|
|
|
|
| 215 |
with open(output_path, "wb") as f:
|
| 216 |
+
f.write(pdf_file.read())
|
| 217 |
uploaded.append(output_path)
|
| 218 |
+
history.append(f"Uploaded PDF: {output_path}")
|
| 219 |
+
asset_checkboxes[output_path] = True
|
| 220 |
+
return f"Uploaded {len(uploaded)} PDFs", history, asset_checkboxes
|
| 221 |
+
|
| 222 |
+
def snapshot_pdfs(mode, history, asset_checkboxes):
|
| 223 |
+
selected_pdfs = [path for path in get_gallery_files() if path.endswith('.pdf') and asset_checkboxes.get(path, False)]
|
| 224 |
+
if not selected_pdfs:
|
| 225 |
+
return "No PDFs selected", [], history, asset_checkboxes
|
| 226 |
+
snapshots = []
|
| 227 |
+
mode_key = {"Single Page (High-Res)": "single", "Two Pages (High-Res)": "twopage", "All Pages (High-Res)": "allpages"}[mode]
|
| 228 |
+
for pdf_path in selected_pdfs:
|
| 229 |
+
snap_files = asyncio.run(process_pdf_snapshot(pdf_path, mode_key))
|
| 230 |
+
for snap in snap_files:
|
| 231 |
+
snapshots.append(Image.open(snap))
|
| 232 |
+
asset_checkboxes[snap] = True
|
| 233 |
+
history.append(f"Snapshot {mode_key}: {snap}")
|
| 234 |
+
return f"Generated {len(snapshots)} snapshots", snapshots, history, asset_checkboxes
|
| 235 |
+
|
| 236 |
+
def process_ocr_all(history, asset_checkboxes):
|
| 237 |
+
all_files = get_gallery_files()
|
| 238 |
+
if not all_files:
|
| 239 |
+
return "No assets to OCR", history, asset_checkboxes
|
| 240 |
+
full_text = "# OCR Results\n\n"
|
| 241 |
+
for file in all_files:
|
| 242 |
+
if file.endswith('.png'):
|
| 243 |
+
image = Image.open(file)
|
| 244 |
+
else:
|
| 245 |
+
doc = fitz.open(file)
|
| 246 |
+
pix = doc[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 247 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 248 |
+
doc.close()
|
| 249 |
+
output_file = generate_filename(f"ocr_{os.path.basename(file)}", "txt")
|
| 250 |
+
result = asyncio.run(process_ocr(image, output_file))
|
| 251 |
+
full_text += f"## {os.path.basename(file)}\n\n{result}\n\n"
|
| 252 |
+
history.append(f"OCR Test: {file} -> {output_file}")
|
| 253 |
+
md_output_file = f"full_ocr_{int(time.time())}.md"
|
| 254 |
+
with open(md_output_file, "w") as f:
|
| 255 |
+
f.write(full_text)
|
| 256 |
+
return f"Full OCR saved to {md_output_file}", history, asset_checkboxes
|
| 257 |
+
|
| 258 |
+
def process_ocr_single(file_path, history, asset_checkboxes):
|
| 259 |
+
if not file_path:
|
| 260 |
+
return "No file selected", None, "", history, asset_checkboxes
|
| 261 |
+
if file_path.endswith('.png'):
|
| 262 |
+
image = Image.open(file_path)
|
| 263 |
+
else:
|
| 264 |
+
doc = fitz.open(file_path)
|
| 265 |
+
pix = doc[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 266 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 267 |
+
doc.close()
|
| 268 |
+
output_file = generate_filename("ocr_output", "txt")
|
| 269 |
+
result = asyncio.run(process_ocr(image, output_file))
|
| 270 |
+
history.append(f"OCR Test: {file_path} -> {output_file}")
|
| 271 |
+
return f"OCR output saved to {output_file}", image, result, history, asset_checkboxes
|
| 272 |
+
|
| 273 |
+
def build_model(model_type, base_model, model_name, domain, history):
|
| 274 |
+
config = (ModelConfig if model_type == "Causal LM" else DiffusionConfig)(name=model_name, base_model=base_model, size="small", domain=domain)
|
| 275 |
+
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 276 |
+
builder.load_model(base_model, config)
|
| 277 |
+
builder.save_model(config.model_path)
|
| 278 |
+
history.append(f"Built {model_type} model: {model_name}")
|
| 279 |
+
return builder, f"Model saved to {config.model_path}", history
|
| 280 |
+
|
| 281 |
+
def image_gen(prompt, file_path, builder, history, asset_checkboxes):
|
| 282 |
+
if not file_path:
|
| 283 |
+
return "No file selected", None, history, asset_checkboxes
|
| 284 |
+
if file_path.endswith('.png'):
|
| 285 |
+
image = Image.open(file_path)
|
| 286 |
+
else:
|
| 287 |
+
doc = fitz.open(file_path)
|
| 288 |
+
pix = doc[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 289 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 290 |
+
doc.close()
|
| 291 |
+
output_file = generate_filename("gen_output", "png")
|
| 292 |
+
gen_image = asyncio.run(process_image_gen(prompt, output_file, builder))
|
| 293 |
+
history.append(f"Image Gen Test: {prompt} -> {output_file}")
|
| 294 |
+
asset_checkboxes[output_file] = True
|
| 295 |
+
return f"Image saved to {output_file}", gen_image, history, asset_checkboxes
|
| 296 |
+
|
| 297 |
+
# Gradio UI
|
| 298 |
+
with gr.Blocks(title="AI Vision & SFT Titans 🚀") as demo:
|
| 299 |
+
gr.Markdown("# AI Vision & SFT Titans 🚀")
|
| 300 |
+
history = gr.State(value=[])
|
| 301 |
+
builder = gr.State(value=None)
|
| 302 |
+
asset_checkboxes = gr.State(value={})
|
| 303 |
+
cam_files = gr.State(value={})
|
| 304 |
+
|
| 305 |
with gr.Row():
|
| 306 |
+
with gr.Column(scale=1):
|
| 307 |
+
gr.Markdown("## Captured Files 📜")
|
| 308 |
+
gallery_output = gr.Gallery(label="Asset Gallery", columns=2, height="auto")
|
| 309 |
+
gr.Button("Update Gallery").click(update_gallery, inputs=[history, asset_checkboxes], outputs=[gallery_output, history, asset_checkboxes])
|
| 310 |
+
gr.Markdown("## History 📜")
|
| 311 |
+
history_output = gr.Textbox(label="History", lines=5, interactive=False)
|
| 312 |
+
gr.Markdown("## Action Logs 📜")
|
| 313 |
+
log_output = gr.Textbox(label="Logs", value="\n".join([f"{r.asctime} - {r.levelname} - {r.message}" for r in log_records]), lines=5, interactive=False)
|
| 314 |
+
|
| 315 |
+
with gr.Column(scale=3):
|
| 316 |
+
with gr.Tabs():
|
| 317 |
+
with gr.TabItem("Camera Snap 📷"):
|
| 318 |
+
with gr.Row():
|
| 319 |
+
cam0_input = gr.Image(type="pil", label="Camera 0")
|
| 320 |
+
cam1_input = gr.Image(type="pil", label="Camera 1")
|
| 321 |
+
with gr.Row():
|
| 322 |
+
cam0_output = gr.Textbox(label="Cam 0 Status")
|
| 323 |
+
cam1_output = gr.Textbox(label="Cam 1 Status")
|
| 324 |
+
with gr.Row():
|
| 325 |
+
cam0_image = gr.Image(label="Cam 0 Preview")
|
| 326 |
+
cam1_image = gr.Image(label="Cam 1 Preview")
|
| 327 |
+
gr.Button("Capture Cam 0").click(camera_snap, inputs=[cam0_input, gr.State(value=0), history, asset_checkboxes, cam_files], outputs=[cam0_output, cam0_image, history, asset_checkboxes, cam_files])
|
| 328 |
+
gr.Button("Capture Cam 1").click(camera_snap, inputs=[cam1_input, gr.State(value=1), history, asset_checkboxes, cam_files], outputs=[cam1_output, cam1_image, history, asset_checkboxes, cam_files])
|
| 329 |
+
|
| 330 |
+
with gr.TabItem("Download PDFs 📥"):
|
| 331 |
+
url_input = gr.Textbox(label="Enter PDF URLs (one per line)", lines=5)
|
| 332 |
+
pdf_upload = gr.File(label="Upload PDFs", file_count="multiple", type="binary")
|
| 333 |
+
pdf_output = gr.Textbox(label="Status")
|
| 334 |
+
snapshot_mode = gr.Dropdown(["Single Page (High-Res)", "Two Pages (High-Res)", "All Pages (High-Res)"], label="Snapshot Mode")
|
| 335 |
+
snapshot_output = gr.Textbox(label="Snapshot Status")
|
| 336 |
+
snapshot_images = gr.Gallery(label="Snapshots", columns=2, height="auto")
|
| 337 |
+
gr.Button("Download URLs").click(download_pdfs, inputs=[url_input, history, asset_checkboxes], outputs=[pdf_output, history, asset_checkboxes])
|
| 338 |
+
gr.Button("Upload PDFs").click(upload_pdfs, inputs=[pdf_upload, history, asset_checkboxes], outputs=[pdf_output, history, asset_checkboxes])
|
| 339 |
+
gr.Button("Snapshot Selected").click(snapshot_pdfs, inputs=[snapshot_mode, history, asset_checkboxes], outputs=[snapshot_output, snapshot_images, history, asset_checkboxes])
|
| 340 |
+
|
| 341 |
+
with gr.TabItem("Test OCR 🔍"):
|
| 342 |
+
all_files = gr.Dropdown(choices=get_gallery_files(), label="Select File")
|
| 343 |
+
ocr_output = gr.Textbox(label="Status")
|
| 344 |
+
ocr_image = gr.Image(label="Input Image")
|
| 345 |
+
ocr_result = gr.Textbox(label="OCR Result", lines=5)
|
| 346 |
+
gr.Button("OCR All Assets").click(process_ocr_all, inputs=[history, asset_checkboxes], outputs=[ocr_output, history, asset_checkboxes])
|
| 347 |
+
gr.Button("OCR Selected").click(process_ocr_single, inputs=[all_files, history, asset_checkboxes], outputs=[ocr_output, ocr_image, ocr_result, history, asset_checkboxes])
|
| 348 |
+
|
| 349 |
+
with gr.TabItem("Build Titan 🌱"):
|
| 350 |
+
model_type = gr.Dropdown(["Causal LM", "Diffusion"], label="Model Type")
|
| 351 |
+
base_model = gr.Dropdown(
|
| 352 |
+
choices=["HuggingFaceTB/SmolLM-135M", "Qwen/Qwen1.5-0.5B-Chat"],
|
| 353 |
+
label="Base Model",
|
| 354 |
+
value="HuggingFaceTB/SmolLM-135M"
|
| 355 |
+
)
|
| 356 |
+
model_name = gr.Textbox(label="Model Name", value=f"tiny-titan-{int(time.time())}")
|
| 357 |
+
domain = gr.Textbox(label="Target Domain", value="general")
|
| 358 |
+
build_output = gr.Textbox(label="Status")
|
| 359 |
+
gr.Button("Build").click(build_model, inputs=[model_type, base_model, model_name, domain, history], outputs=[builder, build_output, history])
|
| 360 |
+
|
| 361 |
+
with gr.TabItem("Test Image Gen 🎨"):
|
| 362 |
+
gen_file = gr.Dropdown(choices=get_gallery_files(), label="Select Reference File")
|
| 363 |
+
gen_prompt = gr.Textbox(label="Prompt", value="Generate a neon superhero version of this image")
|
| 364 |
+
gen_output = gr.Textbox(label="Status")
|
| 365 |
+
gen_image = gr.Image(label="Generated Image")
|
| 366 |
+
gr.Button("Generate").click(image_gen, inputs=[gen_prompt, gen_file, builder, history, asset_checkboxes], outputs=[gen_output, gen_image, history, asset_checkboxes])
|
| 367 |
+
|
| 368 |
+
# Update history output on every interaction
|
| 369 |
+
demo.load(lambda h: "\n".join(h[-5:]), inputs=[history], outputs=[history_output])
|
| 370 |
+
|
| 371 |
demo.launch()
|