Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
-
|
| 2 |
-
import asyncio
|
| 3 |
-
import base64
|
| 4 |
-
import fitz
|
| 5 |
-
import glob
|
| 6 |
-
import logging
|
| 7 |
import os
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
import re
|
| 12 |
-
import requests
|
| 13 |
import shutil
|
| 14 |
import streamlit as st
|
| 15 |
-
import
|
| 16 |
import torch
|
| 17 |
-
import
|
| 18 |
-
|
| 19 |
-
from
|
| 20 |
-
from datetime import datetime
|
| 21 |
from diffusers import StableDiffusionPipeline
|
| 22 |
-
from
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
from PIL import Image
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 36 |
logger = logging.getLogger(__name__)
|
| 37 |
log_records = []
|
|
|
|
| 38 |
class LogCaptureHandler(logging.Handler):
|
| 39 |
def emit(self, record):
|
| 40 |
log_records.append(record)
|
|
|
|
| 41 |
logger.addHandler(LogCaptureHandler())
|
| 42 |
|
| 43 |
-
#
|
| 44 |
st.set_page_config(
|
| 45 |
page_title="AI Vision & SFT Titans 🚀",
|
| 46 |
page_icon="🤖",
|
|
@@ -53,24 +53,22 @@ st.set_page_config(
|
|
| 53 |
}
|
| 54 |
)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
st.session_state
|
| 59 |
-
|
| 60 |
-
st.session_state
|
| 61 |
-
|
| 62 |
-
st.session_state
|
| 63 |
-
|
| 64 |
-
st.session_state
|
| 65 |
-
st.session_state
|
| 66 |
-
st.session_state
|
| 67 |
-
st.session_state
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
@dataclass # ModelConfig: A blueprint for model configurations.
|
| 74 |
class ModelConfig:
|
| 75 |
name: str
|
| 76 |
base_model: str
|
|
@@ -78,31 +76,138 @@ class ModelConfig:
|
|
| 78 |
domain: Optional[str] = None
|
| 79 |
model_type: str = "causal_lm"
|
| 80 |
@property
|
| 81 |
-
def model_path(self):
|
| 82 |
return f"models/{self.name}"
|
| 83 |
|
| 84 |
-
@dataclass
|
| 85 |
class DiffusionConfig:
|
| 86 |
name: str
|
| 87 |
base_model: str
|
| 88 |
size: str
|
| 89 |
-
domain: Optional[str] = None
|
| 90 |
@property
|
| 91 |
def model_path(self):
|
| 92 |
return f"diffusion_models/{self.name}"
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
class ModelBuilder:
|
| 95 |
def __init__(self):
|
| 96 |
self.config = None
|
| 97 |
self.model = None
|
| 98 |
self.tokenizer = None
|
| 99 |
-
self.
|
| 100 |
-
|
| 101 |
-
"Training complete! Time for a binary coffee break. ☕",
|
| 102 |
-
"I told my neural network a joke; it couldn't stop dropping bits! 🤖",
|
| 103 |
-
"I asked the AI for a pun, and it said, 'I'm punning on parallel processing!' 😄",
|
| 104 |
-
"Debugging my code is like a stand-up routine—always a series of exceptions! 😆"
|
| 105 |
-
]
|
| 106 |
def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
|
| 107 |
with st.spinner(f"Loading {model_path}... ⏳"):
|
| 108 |
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
|
@@ -114,12 +219,53 @@ class ModelBuilder:
|
|
| 114 |
self.model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 115 |
st.success(f"Model loaded! 🎉 {random.choice(self.jokes)}")
|
| 116 |
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
def save_model(self, path: str):
|
| 118 |
with st.spinner("Saving model... 💾"):
|
| 119 |
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 120 |
self.model.save_pretrained(path)
|
| 121 |
self.tokenizer.save_pretrained(path)
|
| 122 |
st.success(f"Model saved at {path}! ✅")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
class DiffusionBuilder:
|
| 125 |
def __init__(self):
|
|
@@ -130,7 +276,32 @@ class DiffusionBuilder:
|
|
| 130 |
self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu")
|
| 131 |
if config:
|
| 132 |
self.config = config
|
| 133 |
-
st.success("Diffusion model loaded! 🎨")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
return self
|
| 135 |
def save_model(self, path: str):
|
| 136 |
with st.spinner("Saving diffusion model... 💾"):
|
|
@@ -140,25 +311,34 @@ class DiffusionBuilder:
|
|
| 140 |
def generate(self, prompt: str):
|
| 141 |
return self.pipeline(prompt, num_inference_steps=20).images[0]
|
| 142 |
|
|
|
|
| 143 |
def generate_filename(sequence, ext="png"):
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
def pdf_url_to_filename(url):
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
| 149 |
def get_download_link(file_path, mime_type="application/pdf", label="Download"):
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
def zip_directory(directory_path, zip_path):
|
| 153 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
def get_model_files(model_type="causal_lm"):
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
-
def get_gallery_files(file_types=["png"
|
| 161 |
-
return sorted(
|
| 162 |
|
| 163 |
def get_pdf_files():
|
| 164 |
return sorted(glob.glob("*.pdf"))
|
|
@@ -170,15 +350,12 @@ def download_pdf(url, output_path):
|
|
| 170 |
with open(output_path, "wb") as f:
|
| 171 |
for chunk in response.iter_content(chunk_size=8192):
|
| 172 |
f.write(chunk)
|
| 173 |
-
|
| 174 |
-
else:
|
| 175 |
-
ret = False
|
| 176 |
except requests.RequestException as e:
|
| 177 |
logger.error(f"Failed to download {url}: {e}")
|
| 178 |
-
|
| 179 |
-
return ret
|
| 180 |
|
| 181 |
-
# Async
|
| 182 |
async def process_pdf_snapshot(pdf_path, mode="single"):
|
| 183 |
start_time = time.time()
|
| 184 |
status = st.empty()
|
|
@@ -188,107 +365,206 @@ async def process_pdf_snapshot(pdf_path, mode="single"):
|
|
| 188 |
output_files = []
|
| 189 |
if mode == "single":
|
| 190 |
page = doc[0]
|
| 191 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 192 |
output_file = generate_filename("single", "png")
|
| 193 |
pix.save(output_file)
|
| 194 |
output_files.append(output_file)
|
| 195 |
elif mode == "twopage":
|
| 196 |
for i in range(min(2, len(doc))):
|
| 197 |
page = doc[i]
|
| 198 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 199 |
output_file = generate_filename(f"twopage_{i}", "png")
|
| 200 |
pix.save(output_file)
|
| 201 |
output_files.append(output_file)
|
| 202 |
-
elif mode == "
|
| 203 |
for i in range(len(doc)):
|
| 204 |
page = doc[i]
|
| 205 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(
|
| 206 |
-
output_file = generate_filename(f"
|
| 207 |
pix.save(output_file)
|
| 208 |
output_files.append(output_file)
|
| 209 |
doc.close()
|
| 210 |
elapsed = int(time.time() - start_time)
|
| 211 |
status.text(f"PDF Snapshot ({mode}) completed in {elapsed}s!")
|
|
|
|
| 212 |
return output_files
|
| 213 |
except Exception as e:
|
| 214 |
status.error(f"Failed to process PDF: {str(e)}")
|
| 215 |
return []
|
| 216 |
|
| 217 |
-
# Async OCR: Convert images to text.
|
| 218 |
async def process_ocr(image, output_file):
|
| 219 |
start_time = time.time()
|
| 220 |
status = st.empty()
|
| 221 |
status.text("Processing GOT-OCR2_0... (0s)")
|
| 222 |
tokenizer = AutoTokenizer.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True)
|
| 223 |
model = AutoModel.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True, torch_dtype=torch.float32).to("cpu").eval()
|
| 224 |
-
|
| 225 |
-
image.save(temp_file)
|
| 226 |
-
result = model.chat(tokenizer, temp_file, ocr_type='ocr')
|
| 227 |
-
os.remove(temp_file)
|
| 228 |
elapsed = int(time.time() - start_time)
|
| 229 |
status.text(f"GOT-OCR2_0 completed in {elapsed}s!")
|
| 230 |
async with aiofiles.open(output_file, "w") as f:
|
| 231 |
await f.write(result)
|
|
|
|
| 232 |
return result
|
| 233 |
|
| 234 |
-
# Async Image Gen: Your image genie.
|
| 235 |
async def process_image_gen(prompt, output_file):
|
| 236 |
start_time = time.time()
|
| 237 |
status = st.empty()
|
| 238 |
status.text("Processing Image Gen... (0s)")
|
| 239 |
-
pipeline = (
|
| 240 |
-
if st.session_state.get('builder') and isinstance(st.session_state['builder'], DiffusionBuilder)
|
| 241 |
-
and st.session_state['builder'].pipeline
|
| 242 |
-
else StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu"))
|
| 243 |
gen_image = pipeline(prompt, num_inference_steps=20).images[0]
|
| 244 |
elapsed = int(time.time() - start_time)
|
| 245 |
status.text(f"Image Gen completed in {elapsed}s!")
|
| 246 |
gen_image.save(output_file)
|
|
|
|
| 247 |
return gen_image
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
]
|
| 260 |
-
|
| 261 |
-
try:
|
| 262 |
-
response = client.chat.completions.create(model=model, messages=messages, max_tokens=300)
|
| 263 |
-
return response.choices[0].message.content
|
| 264 |
-
except Exception as e:
|
| 265 |
-
return f"Error processing image with GPT: {str(e)}"
|
| 266 |
|
| 267 |
-
|
| 268 |
-
def
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
|
| 279 |
-
st.sidebar.
|
| 280 |
-
|
| 281 |
-
|
|
|
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
])
|
| 288 |
-
(tab_camera, tab_download, tab_ocr, tab_build, tab_imggen, tab_pdf_process, tab_image_process, tab_md_gallery) = tabs
|
| 289 |
|
| 290 |
-
|
| 291 |
-
with tab_camera:
|
| 292 |
st.header("Camera Snap 📷")
|
| 293 |
st.subheader("Single Capture")
|
| 294 |
cols = st.columns(2)
|
|
@@ -296,48 +572,48 @@ with tab_camera:
|
|
| 296 |
cam0_img = st.camera_input("Take a picture - Cam 0", key="cam0")
|
| 297 |
if cam0_img:
|
| 298 |
filename = generate_filename("cam0")
|
| 299 |
-
if st.session_state['cam0_file'] and os.path.exists(st.session_state['cam0_file']):
|
| 300 |
-
os.remove(st.session_state['cam0_file'])
|
| 301 |
with open(filename, "wb") as f:
|
| 302 |
f.write(cam0_img.getvalue())
|
| 303 |
-
st.session_state['cam0_file'] = filename
|
| 304 |
entry = f"Snapshot from Cam 0: {filename}"
|
| 305 |
-
st.session_state['history']
|
|
|
|
| 306 |
st.image(Image.open(filename), caption="Camera 0", use_container_width=True)
|
| 307 |
logger.info(f"Saved snapshot from Camera 0: {filename}")
|
|
|
|
| 308 |
with cols[1]:
|
| 309 |
cam1_img = st.camera_input("Take a picture - Cam 1", key="cam1")
|
| 310 |
if cam1_img:
|
| 311 |
filename = generate_filename("cam1")
|
| 312 |
-
if st.session_state['cam1_file'] and os.path.exists(st.session_state['cam1_file']):
|
| 313 |
-
os.remove(st.session_state['cam1_file'])
|
| 314 |
with open(filename, "wb") as f:
|
| 315 |
f.write(cam1_img.getvalue())
|
| 316 |
-
st.session_state['cam1_file'] = filename
|
| 317 |
entry = f"Snapshot from Cam 1: {filename}"
|
| 318 |
-
st.session_state['history']
|
|
|
|
| 319 |
st.image(Image.open(filename), caption="Camera 1", use_container_width=True)
|
| 320 |
logger.info(f"Saved snapshot from Camera 1: {filename}")
|
|
|
|
| 321 |
|
| 322 |
-
|
| 323 |
-
with tab_download:
|
| 324 |
st.header("Download PDFs 📥")
|
|
|
|
| 325 |
if st.button("Examples 📚"):
|
| 326 |
example_urls = [
|
| 327 |
-
"https://arxiv.org/pdf/2308.03892",
|
| 328 |
-
"https://arxiv.org/pdf/1912.01703",
|
| 329 |
-
"https://arxiv.org/pdf/2408.11039",
|
| 330 |
-
"https://arxiv.org/pdf/2109.10282",
|
| 331 |
-
"https://arxiv.org/pdf/2112.10752",
|
| 332 |
-
"https://arxiv.org/pdf/2308.11236",
|
| 333 |
-
"https://arxiv.org/pdf/1706.03762",
|
| 334 |
-
"https://arxiv.org/pdf/2006.11239",
|
| 335 |
-
"https://arxiv.org/pdf/2305.11207",
|
| 336 |
-
"https://arxiv.org/pdf/2106.09685",
|
| 337 |
-
"https://arxiv.org/pdf/2005.11401",
|
| 338 |
-
"https://arxiv.org/pdf/2106.10504"
|
| 339 |
]
|
| 340 |
st.session_state['pdf_urls'] = "\n".join(example_urls)
|
|
|
|
|
|
|
| 341 |
url_input = st.text_area("Enter PDF URLs (one per line)", value=st.session_state.get('pdf_urls', ""), height=200)
|
| 342 |
if st.button("Robo-Download 🤖"):
|
| 343 |
urls = url_input.strip().split("\n")
|
|
@@ -354,8 +630,8 @@ with tab_download:
|
|
| 354 |
st.session_state['downloaded_pdfs'][url] = output_path
|
| 355 |
logger.info(f"Downloaded PDF from {url} to {output_path}")
|
| 356 |
entry = f"Downloaded PDF: {output_path}"
|
| 357 |
-
st.session_state['history']
|
| 358 |
-
|
| 359 |
else:
|
| 360 |
st.error(f"Failed to nab {url} 😿")
|
| 361 |
else:
|
|
@@ -363,50 +639,218 @@ with tab_download:
|
|
| 363 |
st.session_state['downloaded_pdfs'][url] = output_path
|
| 364 |
progress_bar.progress((idx + 1) / total_urls)
|
| 365 |
status_text.text("Robo-Download complete! 🚀")
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
if st.button("Snapshot Selected 📸"):
|
| 368 |
-
selected_pdfs = [path for path in
|
| 369 |
if selected_pdfs:
|
| 370 |
for pdf_path in selected_pdfs:
|
| 371 |
-
|
| 372 |
-
st.warning(f"File not found: {pdf_path}. Skipping.")
|
| 373 |
-
continue
|
| 374 |
-
mode_key = {"Single Page (High-Res)": "single",
|
| 375 |
-
"Two Pages (High-Res)": "twopage",
|
| 376 |
-
"All Pages (High-Res)": "allpages"}[mode]
|
| 377 |
snapshots = asyncio.run(process_pdf_snapshot(pdf_path, mode_key))
|
| 378 |
for snapshot in snapshots:
|
| 379 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
| 380 |
-
st.session_state['asset_checkboxes'][snapshot] = True
|
| 381 |
-
# No update_gallery() call here; will update once later.
|
| 382 |
else:
|
| 383 |
-
st.warning("No PDFs selected for snapshotting! Check some boxes
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
st.header("Test OCR 🔍")
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
if all_files:
|
| 390 |
-
if st.button("OCR All Assets 🚀"):
|
| 391 |
-
full_text = "# OCR Results\n\n"
|
| 392 |
-
for file in all_files:
|
| 393 |
-
if file.endswith('.png'):
|
| 394 |
-
image = Image.open(file)
|
| 395 |
-
else:
|
| 396 |
-
doc = fitz.open(file)
|
| 397 |
-
pix = doc[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 398 |
-
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 399 |
-
doc.close()
|
| 400 |
-
output_file = generate_filename(f"ocr_{os.path.basename(file)}", "txt")
|
| 401 |
-
result = asyncio.run(process_ocr(image, output_file))
|
| 402 |
-
full_text += f"## {os.path.basename(file)}\n\n{result}\n\n"
|
| 403 |
-
entry = f"OCR Test: {file} -> {output_file}"
|
| 404 |
-
st.session_state['history'].append(entry)
|
| 405 |
-
md_output_file = f"full_ocr_{int(time.time())}.md"
|
| 406 |
-
with open(md_output_file, "w") as f:
|
| 407 |
-
f.write(full_text)
|
| 408 |
-
st.success(f"Full OCR saved to {md_output_file}")
|
| 409 |
-
st.markdown(get_download_link(md_output_file, "text/markdown", "Download Full OCR Markdown"), unsafe_allow_html=True)
|
| 410 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="ocr_select")
|
| 411 |
if selected_file:
|
| 412 |
if selected_file.endswith('.png'):
|
|
@@ -422,60 +866,19 @@ with tab_ocr:
|
|
| 422 |
st.session_state['processing']['ocr'] = True
|
| 423 |
result = asyncio.run(process_ocr(image, output_file))
|
| 424 |
entry = f"OCR Test: {selected_file} -> {output_file}"
|
| 425 |
-
st.session_state['history']
|
|
|
|
| 426 |
st.text_area("OCR Result", result, height=200, key="ocr_result")
|
| 427 |
st.success(f"OCR output saved to {output_file}")
|
| 428 |
st.session_state['processing']['ocr'] = False
|
| 429 |
-
if selected_file.endswith('.pdf') and st.button("OCR All Pages 🚀", key="ocr_all_pages"):
|
| 430 |
-
doc = fitz.open(selected_file)
|
| 431 |
-
full_text = f"# OCR Results for {os.path.basename(selected_file)}\n\n"
|
| 432 |
-
for i in range(len(doc)):
|
| 433 |
-
pix = doc[i].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 434 |
-
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 435 |
-
output_file = generate_filename(f"ocr_page_{i}", "txt")
|
| 436 |
-
result = asyncio.run(process_ocr(image, output_file))
|
| 437 |
-
full_text += f"## Page {i + 1}\n\n{result}\n\n"
|
| 438 |
-
entry = f"OCR Test: {selected_file} Page {i + 1} -> {output_file}"
|
| 439 |
-
st.session_state['history'].append(entry)
|
| 440 |
-
md_output_file = f"full_ocr_{os.path.basename(selected_file)}_{int(time.time())}.md"
|
| 441 |
-
with open(md_output_file, "w") as f:
|
| 442 |
-
f.write(full_text)
|
| 443 |
-
st.success(f"Full OCR saved to {md_output_file}")
|
| 444 |
-
st.markdown(get_download_link(md_output_file, "text/markdown", "Download Full OCR Markdown"), unsafe_allow_html=True)
|
| 445 |
else:
|
| 446 |
-
st.warning("No
|
| 447 |
|
| 448 |
-
|
| 449 |
-
with tab_build:
|
| 450 |
-
st.header("Build Titan 🌱")
|
| 451 |
-
model_type = st.selectbox("Model Type", ["Causal LM", "Diffusion"], key="build_type")
|
| 452 |
-
base_model = st.selectbox(
|
| 453 |
-
"Select Tiny Model",
|
| 454 |
-
["HuggingFaceTB/SmolLM-135M", "Qwen/Qwen1.5-0.5B-Chat"] if model_type == "Causal LM"
|
| 455 |
-
else ["OFA-Sys/small-stable-diffusion-v0", "stabilityai/stable-diffusion-2-base"]
|
| 456 |
-
)
|
| 457 |
-
model_name = st.text_input("Model Name", f"tiny-titan-{int(time.time())}")
|
| 458 |
-
domain = st.text_input("Target Domain", "general")
|
| 459 |
-
if st.button("Download Model ⬇️"):
|
| 460 |
-
config = (ModelConfig if model_type == "Causal LM" else DiffusionConfig)(
|
| 461 |
-
name=model_name, base_model=base_model, size="small", domain=domain
|
| 462 |
-
)
|
| 463 |
-
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 464 |
-
builder.load_model(base_model, config)
|
| 465 |
-
builder.save_model(config.model_path)
|
| 466 |
-
st.session_state['builder'] = builder
|
| 467 |
-
st.session_state['model_loaded'] = True
|
| 468 |
-
st.session_state['selected_model_type'] = model_type
|
| 469 |
-
st.session_state['selected_model'] = config.model_path
|
| 470 |
-
entry = f"Built {model_type} model: {model_name}"
|
| 471 |
-
st.session_state['history'].append(entry)
|
| 472 |
-
st.success(f"Model downloaded and saved to {config.model_path}! 🎉")
|
| 473 |
-
st.experimental_rerun()
|
| 474 |
-
|
| 475 |
-
# ----------------- TAB: Test Image Gen -----------------
|
| 476 |
-
with tab_imggen:
|
| 477 |
st.header("Test Image Gen 🎨")
|
| 478 |
-
|
|
|
|
|
|
|
| 479 |
if all_files:
|
| 480 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="gen_select")
|
| 481 |
if selected_file:
|
|
@@ -487,196 +890,65 @@ with tab_imggen:
|
|
| 487 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 488 |
doc.close()
|
| 489 |
st.image(image, caption="Reference Image", use_container_width=True)
|
| 490 |
-
prompt = st.text_area("Prompt", "Generate a
|
| 491 |
if st.button("Run Image Gen 🚀", key="gen_run"):
|
| 492 |
output_file = generate_filename("gen_output", "png")
|
| 493 |
st.session_state['processing']['gen'] = True
|
| 494 |
result = asyncio.run(process_image_gen(prompt, output_file))
|
| 495 |
entry = f"Image Gen Test: {prompt} -> {output_file}"
|
| 496 |
-
st.session_state['history']
|
|
|
|
| 497 |
st.image(result, caption="Generated Image", use_container_width=True)
|
| 498 |
st.success(f"Image saved to {output_file}")
|
| 499 |
st.session_state['processing']['gen'] = False
|
| 500 |
else:
|
| 501 |
-
st.warning("No images or PDFs
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
st.
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
doc = fitz.open(temp_pdf_path)
|
| 521 |
-
st.write(f"Processing {pdf_file.name} with {len(doc)} pages")
|
| 522 |
-
if view_mode == "Single Page":
|
| 523 |
-
for i, page in enumerate(doc):
|
| 524 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 525 |
-
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 526 |
-
st.image(img, caption=f"{pdf_file.name} Page {i+1}")
|
| 527 |
-
gpt_text = process_image_with_prompt(img, "Extract the electronic text from image", model=selected_gpt_model, detail=detail_level)
|
| 528 |
-
combined_text += f"\n## {pdf_file.name} - Page {i+1}\n\n{gpt_text}\n"
|
| 529 |
-
else:
|
| 530 |
-
pages = list(doc)
|
| 531 |
-
for i in range(0, len(pages), 2):
|
| 532 |
-
if i+1 < len(pages):
|
| 533 |
-
pix1 = pages[i].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 534 |
-
img1 = Image.frombytes("RGB", [pix1.width, pix1.height], pix1.samples)
|
| 535 |
-
pix2 = pages[i+1].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 536 |
-
img2 = Image.frombytes("RGB", [pix2.width, pix2.height], pix2.samples)
|
| 537 |
-
total_width = img1.width + img2.width
|
| 538 |
-
max_height = max(img1.height, img2.height)
|
| 539 |
-
combined_img = Image.new("RGB", (total_width, max_height))
|
| 540 |
-
combined_img.paste(img1, (0, 0))
|
| 541 |
-
combined_img.paste(img2, (img1.width, 0))
|
| 542 |
-
st.image(combined_img, caption=f"{pdf_file.name} Pages {i+1}-{i+2}")
|
| 543 |
-
gpt_text = process_image_with_prompt(combined_img, "Extract the electronic text from image", model=selected_gpt_model, detail=detail_level)
|
| 544 |
-
combined_text += f"\n## {pdf_file.name} - Pages {i+1}-{i+2}\n\n{gpt_text}\n"
|
| 545 |
-
else:
|
| 546 |
-
pix = pages[i].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 547 |
-
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 548 |
-
st.image(img, caption=f"{pdf_file.name} Page {i+1}")
|
| 549 |
-
gpt_text = process_image_with_prompt(img, "Extract the electronic text from image", model=selected_gpt_model, detail=detail_level)
|
| 550 |
-
combined_text += f"\n## {pdf_file.name} - Page {i+1}\n\n{gpt_text}\n"
|
| 551 |
doc.close()
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
gpt_models = ["gpt-4o", "gpt-4o-mini"]
|
| 567 |
-
selected_gpt_model = st.selectbox("Select GPT Model", gpt_models, key="img_gpt_model")
|
| 568 |
-
detail_level = st.selectbox("Detail Level", ["auto", "low", "high"], key="img_detail_level")
|
| 569 |
-
prompt_img = st.text_input("Enter prompt for image processing", "Extract the electronic text from image", key="img_process_prompt")
|
| 570 |
-
uploaded_images = st.file_uploader("Upload image files", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="image_process_uploader")
|
| 571 |
-
if st.button("Process Uploaded Images", key="process_images"):
|
| 572 |
-
combined_text = ""
|
| 573 |
-
for img_file in uploaded_images:
|
| 574 |
-
try:
|
| 575 |
-
img = Image.open(img_file)
|
| 576 |
-
st.image(img, caption=img_file.name)
|
| 577 |
-
gpt_text = process_image_with_prompt(img, prompt_img, model=selected_gpt_model, detail=detail_level)
|
| 578 |
-
combined_text += f"\n## {img_file.name}\n\n{gpt_text}\n"
|
| 579 |
-
except Exception as e:
|
| 580 |
-
st.error(f"Error processing image {img_file.name}: {str(e)}")
|
| 581 |
-
output_filename = generate_filename("processed_image", "md")
|
| 582 |
-
with open(output_filename, "w", encoding="utf-8") as f:
|
| 583 |
-
f.write(combined_text)
|
| 584 |
-
st.success(f"Image processing complete. MD file saved as {output_filename}")
|
| 585 |
-
st.markdown(get_download_link(output_filename, "text/markdown", "Download Processed Image MD"), unsafe_allow_html=True)
|
| 586 |
-
|
| 587 |
-
# ----------------- TAB: MD Gallery -----------------
|
| 588 |
-
with tab_md_gallery:
|
| 589 |
-
st.header("MD Gallery and GPT Processing")
|
| 590 |
-
gpt_models = ["gpt-4o", "gpt-4o-mini"]
|
| 591 |
-
selected_gpt_model = st.selectbox("Select GPT Model", gpt_models, key="md_gpt_model")
|
| 592 |
-
md_files = sorted(glob.glob("*.md"))
|
| 593 |
-
if md_files:
|
| 594 |
-
st.subheader("Individual File Processing")
|
| 595 |
-
cols = st.columns(2)
|
| 596 |
-
for idx, md_file in enumerate(md_files):
|
| 597 |
-
with cols[idx % 2]:
|
| 598 |
-
st.write(md_file)
|
| 599 |
-
if st.button(f"Process {md_file}", key=f"process_md_{md_file}"):
|
| 600 |
-
try:
|
| 601 |
-
with open(md_file, "r", encoding="utf-8") as f:
|
| 602 |
-
content = f.read()
|
| 603 |
-
prompt_md = "Summarize this into markdown outline with emojis and number the topics 1..12"
|
| 604 |
-
result_text = process_text_with_prompt(content, prompt_md, model=selected_gpt_model)
|
| 605 |
-
st.markdown(result_text)
|
| 606 |
-
output_filename = generate_filename(f"processed_{os.path.splitext(md_file)[0]}", "md")
|
| 607 |
-
with open(output_filename, "w", encoding="utf-8") as f:
|
| 608 |
-
f.write(result_text)
|
| 609 |
-
st.markdown(get_download_link(output_filename, "text/markdown", f"Download {output_filename}"), unsafe_allow_html=True)
|
| 610 |
-
except Exception as e:
|
| 611 |
-
st.error(f"Error processing {md_file}: {str(e)}")
|
| 612 |
-
st.subheader("Batch Processing")
|
| 613 |
-
st.write("Select MD files to combine and process:")
|
| 614 |
-
selected_md = {}
|
| 615 |
-
for md_file in md_files:
|
| 616 |
-
selected_md[md_file] = st.checkbox(md_file, key=f"checkbox_md_{md_file}")
|
| 617 |
-
batch_prompt = st.text_input("Enter batch processing prompt", "Summarize this into markdown outline with emojis and number the topics 1..12", key="batch_prompt")
|
| 618 |
-
if st.button("Process Selected MD Files", key="process_batch_md"):
|
| 619 |
-
combined_content = ""
|
| 620 |
-
for md_file, selected in selected_md.items():
|
| 621 |
-
if selected:
|
| 622 |
-
try:
|
| 623 |
-
with open(md_file, "r", encoding="utf-8") as f:
|
| 624 |
-
combined_content += f"\n## {md_file}\n" + f.read() + "\n"
|
| 625 |
-
except Exception as e:
|
| 626 |
-
st.error(f"Error reading {md_file}: {str(e)}")
|
| 627 |
-
if combined_content:
|
| 628 |
-
result_text = process_text_with_prompt(combined_content, batch_prompt, model=selected_gpt_model)
|
| 629 |
-
st.markdown(result_text)
|
| 630 |
-
output_filename = generate_filename("batch_processed_md", "md")
|
| 631 |
-
with open(output_filename, "w", encoding="utf-8") as f:
|
| 632 |
-
f.write(result_text)
|
| 633 |
-
st.success(f"Batch processing complete. MD file saved as {output_filename}")
|
| 634 |
-
st.markdown(get_download_link(output_filename, "text/markdown", "Download Batch Processed MD"), unsafe_allow_html=True)
|
| 635 |
else:
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
else:
|
| 638 |
-
st.warning("No
|
| 639 |
|
| 640 |
-
#
|
| 641 |
-
|
| 642 |
-
def update_gallery():
|
| 643 |
-
container = st.session_state['asset_gallery_container']
|
| 644 |
-
container.empty() # Clear previous gallery content.
|
| 645 |
-
all_files = get_gallery_files()
|
| 646 |
-
if all_files:
|
| 647 |
-
container.markdown("### Asset Gallery 📸📖")
|
| 648 |
-
cols = container.columns(2)
|
| 649 |
-
for idx, file in enumerate(all_files[:st.session_state['gallery_size']]):
|
| 650 |
-
with cols[idx % 2]:
|
| 651 |
-
st.session_state['unique_counter'] += 1
|
| 652 |
-
unique_id = st.session_state['unique_counter']
|
| 653 |
-
if file.endswith('.png'):
|
| 654 |
-
st.image(Image.open(file), caption=os.path.basename(file), use_container_width=True)
|
| 655 |
-
else:
|
| 656 |
-
doc = fitz.open(file)
|
| 657 |
-
pix = doc[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5))
|
| 658 |
-
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 659 |
-
st.image(img, caption=os.path.basename(file), use_container_width=True)
|
| 660 |
-
doc.close()
|
| 661 |
-
checkbox_key = f"asset_{file}_{unique_id}"
|
| 662 |
-
st.session_state['asset_checkboxes'][file] = st.checkbox("Use for SFT/Input", value=st.session_state['asset_checkboxes'].get(file, False), key=checkbox_key)
|
| 663 |
-
mime_type = "image/png" if file.endswith('.png') else "application/pdf"
|
| 664 |
-
st.markdown(get_download_link(file, mime_type, "Snag It! 📥"), unsafe_allow_html=True)
|
| 665 |
-
if st.button("Zap It! 🗑️", key=f"delete_{file}_{unique_id}"):
|
| 666 |
-
os.remove(file)
|
| 667 |
-
st.session_state['asset_checkboxes'].pop(file, None)
|
| 668 |
-
st.success(f"Asset {os.path.basename(file)} vaporized! 💨")
|
| 669 |
-
st.experimental_rerun()
|
| 670 |
-
|
| 671 |
-
# Call the gallery update once after all tabs have been processed.
|
| 672 |
-
update_gallery()
|
| 673 |
-
|
| 674 |
-
# Finally, update the Action Logs and History in the sidebar.
|
| 675 |
-
st.sidebar.subheader("Action Logs 📜")
|
| 676 |
-
for record in log_records:
|
| 677 |
-
st.sidebar.write(f"{record.asctime} - {record.levelname} - {record.message}")
|
| 678 |
-
|
| 679 |
-
st.sidebar.subheader("History 📜")
|
| 680 |
-
for entry in st.session_state.get("history", []):
|
| 681 |
-
if entry is not None:
|
| 682 |
-
st.sidebar.write(entry)
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import glob
|
| 4 |
+
import base64
|
| 5 |
+
import time
|
|
|
|
|
|
|
| 6 |
import shutil
|
| 7 |
import streamlit as st
|
| 8 |
+
import pandas as pd
|
| 9 |
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel
|
|
|
|
| 13 |
from diffusers import StableDiffusionPipeline
|
| 14 |
+
from torch.utils.data import Dataset, DataLoader
|
| 15 |
+
import csv
|
| 16 |
+
import fitz # PyMuPDF
|
| 17 |
+
import requests
|
| 18 |
from PIL import Image
|
| 19 |
+
import cv2
|
| 20 |
+
import numpy as np
|
| 21 |
+
import logging
|
| 22 |
+
import asyncio
|
| 23 |
+
import aiofiles
|
| 24 |
+
from io import BytesIO
|
| 25 |
+
from dataclasses import dataclass
|
| 26 |
+
from typing import Optional, Tuple
|
| 27 |
+
import zipfile
|
| 28 |
+
import math
|
| 29 |
+
import random
|
| 30 |
+
import re
|
| 31 |
|
| 32 |
+
# Logging setup with custom buffer
|
| 33 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 34 |
logger = logging.getLogger(__name__)
|
| 35 |
log_records = []
|
| 36 |
+
|
| 37 |
class LogCaptureHandler(logging.Handler):
|
| 38 |
def emit(self, record):
|
| 39 |
log_records.append(record)
|
| 40 |
+
|
| 41 |
logger.addHandler(LogCaptureHandler())
|
| 42 |
|
| 43 |
+
# Page Configuration
|
| 44 |
st.set_page_config(
|
| 45 |
page_title="AI Vision & SFT Titans 🚀",
|
| 46 |
page_icon="🤖",
|
|
|
|
| 53 |
}
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Initialize st.session_state
|
| 57 |
+
if 'history' not in st.session_state:
|
| 58 |
+
st.session_state['history'] = [] # Flat list for history
|
| 59 |
+
if 'builder' not in st.session_state:
|
| 60 |
+
st.session_state['builder'] = None
|
| 61 |
+
if 'model_loaded' not in st.session_state:
|
| 62 |
+
st.session_state['model_loaded'] = False
|
| 63 |
+
if 'processing' not in st.session_state:
|
| 64 |
+
st.session_state['processing'] = {}
|
| 65 |
+
if 'pdf_checkboxes' not in st.session_state:
|
| 66 |
+
st.session_state['pdf_checkboxes'] = {} # Shared cache for PDF checkboxes
|
| 67 |
+
if 'downloaded_pdfs' not in st.session_state:
|
| 68 |
+
st.session_state['downloaded_pdfs'] = {} # Cache for downloaded PDF paths
|
| 69 |
+
|
| 70 |
+
# Model Configuration Classes
|
| 71 |
+
@dataclass
|
|
|
|
|
|
|
| 72 |
class ModelConfig:
|
| 73 |
name: str
|
| 74 |
base_model: str
|
|
|
|
| 76 |
domain: Optional[str] = None
|
| 77 |
model_type: str = "causal_lm"
|
| 78 |
@property
|
| 79 |
+
def model_path(self):
|
| 80 |
return f"models/{self.name}"
|
| 81 |
|
| 82 |
+
@dataclass
|
| 83 |
class DiffusionConfig:
|
| 84 |
name: str
|
| 85 |
base_model: str
|
| 86 |
size: str
|
|
|
|
| 87 |
@property
|
| 88 |
def model_path(self):
|
| 89 |
return f"diffusion_models/{self.name}"
|
| 90 |
|
| 91 |
+
# Datasets
|
| 92 |
+
class SFTDataset(Dataset):
|
| 93 |
+
def __init__(self, data, tokenizer, max_length=128):
|
| 94 |
+
self.data = data
|
| 95 |
+
self.tokenizer = tokenizer
|
| 96 |
+
self.max_length = max_length
|
| 97 |
+
def __len__(self):
|
| 98 |
+
return len(self.data)
|
| 99 |
+
def __getitem__(self, idx):
|
| 100 |
+
prompt = self.data[idx]["prompt"]
|
| 101 |
+
response = self.data[idx]["response"]
|
| 102 |
+
full_text = f"{prompt} {response}"
|
| 103 |
+
full_encoding = self.tokenizer(full_text, max_length=self.max_length, padding="max_length", truncation=True, return_tensors="pt")
|
| 104 |
+
prompt_encoding = self.tokenizer(prompt, max_length=self.max_length, padding=False, truncation=True, return_tensors="pt")
|
| 105 |
+
input_ids = full_encoding["input_ids"].squeeze()
|
| 106 |
+
attention_mask = full_encoding["attention_mask"].squeeze()
|
| 107 |
+
labels = input_ids.clone()
|
| 108 |
+
prompt_len = prompt_encoding["input_ids"].shape[1]
|
| 109 |
+
if prompt_len < self.max_length:
|
| 110 |
+
labels[:prompt_len] = -100
|
| 111 |
+
return {"input_ids": input_ids, "attention_mask": attention_mask, "labels": labels}
|
| 112 |
+
|
| 113 |
+
class DiffusionDataset(Dataset):
|
| 114 |
+
def __init__(self, images, texts):
|
| 115 |
+
self.images = images
|
| 116 |
+
self.texts = texts
|
| 117 |
+
def __len__(self):
|
| 118 |
+
return len(self.images)
|
| 119 |
+
def __getitem__(self, idx):
|
| 120 |
+
return {"image": self.images[idx], "text": self.texts[idx]}
|
| 121 |
+
|
| 122 |
+
class TinyDiffusionDataset(Dataset):
|
| 123 |
+
def __init__(self, images):
|
| 124 |
+
self.images = [torch.tensor(np.array(img.convert("RGB")).transpose(2, 0, 1), dtype=torch.float32) / 255.0 for img in images]
|
| 125 |
+
def __len__(self):
|
| 126 |
+
return len(self.images)
|
| 127 |
+
def __getitem__(self, idx):
|
| 128 |
+
return self.images[idx]
|
| 129 |
+
|
| 130 |
+
# Custom Tiny Diffusion Model
|
| 131 |
+
class TinyUNet(nn.Module):
|
| 132 |
+
def __init__(self, in_channels=3, out_channels=3):
|
| 133 |
+
super(TinyUNet, self).__init__()
|
| 134 |
+
self.down1 = nn.Conv2d(in_channels, 32, 3, padding=1)
|
| 135 |
+
self.down2 = nn.Conv2d(32, 64, 3, padding=1, stride=2)
|
| 136 |
+
self.mid = nn.Conv2d(64, 128, 3, padding=1)
|
| 137 |
+
self.up1 = nn.ConvTranspose2d(128, 64, 3, stride=2, padding=1, output_padding=1)
|
| 138 |
+
self.up2 = nn.Conv2d(64 + 32, 32, 3, padding=1)
|
| 139 |
+
self.out = nn.Conv2d(32, out_channels, 3, padding=1)
|
| 140 |
+
self.time_embed = nn.Linear(1, 64)
|
| 141 |
+
|
| 142 |
+
def forward(self, x, t):
|
| 143 |
+
t_embed = F.relu(self.time_embed(t.unsqueeze(-1)))
|
| 144 |
+
t_embed = t_embed.view(t_embed.size(0), t_embed.size(1), 1, 1)
|
| 145 |
+
|
| 146 |
+
x1 = F.relu(self.down1(x))
|
| 147 |
+
x2 = F.relu(self.down2(x1))
|
| 148 |
+
x_mid = F.relu(self.mid(x2)) + t_embed
|
| 149 |
+
x_up1 = F.relu(self.up1(x_mid))
|
| 150 |
+
x_up2 = F.relu(self.up2(torch.cat([x_up1, x1], dim=1)))
|
| 151 |
+
return self.out(x_up2)
|
| 152 |
+
|
| 153 |
+
class TinyDiffusion:
|
| 154 |
+
def __init__(self, model, timesteps=100):
|
| 155 |
+
self.model = model
|
| 156 |
+
self.timesteps = timesteps
|
| 157 |
+
self.beta = torch.linspace(0.0001, 0.02, timesteps)
|
| 158 |
+
self.alpha = 1 - self.beta
|
| 159 |
+
self.alpha_cumprod = torch.cumprod(self.alpha, dim=0)
|
| 160 |
+
|
| 161 |
+
def train(self, images, epochs=50):
|
| 162 |
+
dataset = TinyDiffusionDataset(images)
|
| 163 |
+
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
|
| 164 |
+
optimizer = torch.optim.Adam(self.model.parameters(), lr=1e-4)
|
| 165 |
+
device = torch.device("cpu")
|
| 166 |
+
self.model.to(device)
|
| 167 |
+
for epoch in range(epochs):
|
| 168 |
+
total_loss = 0
|
| 169 |
+
for x in dataloader:
|
| 170 |
+
x = x.to(device)
|
| 171 |
+
t = torch.randint(0, self.timesteps, (x.size(0),), device=device).float()
|
| 172 |
+
noise = torch.randn_like(x)
|
| 173 |
+
alpha_t = self.alpha_cumprod[t.long()].view(-1, 1, 1, 1)
|
| 174 |
+
x_noisy = torch.sqrt(alpha_t) * x + torch.sqrt(1 - alpha_t) * noise
|
| 175 |
+
pred_noise = self.model(x_noisy, t)
|
| 176 |
+
loss = F.mse_loss(pred_noise, noise)
|
| 177 |
+
optimizer.zero_grad()
|
| 178 |
+
loss.backward()
|
| 179 |
+
optimizer.step()
|
| 180 |
+
total_loss += loss.item()
|
| 181 |
+
logger.info(f"Epoch {epoch + 1}/{epochs}, Loss: {total_loss / len(dataloader):.4f}")
|
| 182 |
+
return self
|
| 183 |
+
|
| 184 |
+
def generate(self, size=(64, 64), steps=100):
|
| 185 |
+
device = torch.device("cpu")
|
| 186 |
+
x = torch.randn(1, 3, size[0], size[1], device=device)
|
| 187 |
+
for t in reversed(range(steps)):
|
| 188 |
+
t_tensor = torch.full((1,), t, device=device, dtype=torch.float32)
|
| 189 |
+
alpha_t = self.alpha_cumprod[t].view(-1, 1, 1, 1)
|
| 190 |
+
pred_noise = self.model(x, t_tensor)
|
| 191 |
+
x = (x - (1 - self.alpha[t]) / torch.sqrt(1 - alpha_t) * pred_noise) / torch.sqrt(self.alpha[t])
|
| 192 |
+
if t > 0:
|
| 193 |
+
x += torch.sqrt(self.beta[t]) * torch.randn_like(x)
|
| 194 |
+
x = torch.clamp(x * 255, 0, 255).byte()
|
| 195 |
+
return Image.fromarray(x.squeeze(0).permute(1, 2, 0).cpu().numpy())
|
| 196 |
+
|
| 197 |
+
def upscale(self, image, scale_factor=2):
|
| 198 |
+
img_tensor = torch.tensor(np.array(image.convert("RGB")).transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0) / 255.0
|
| 199 |
+
upscaled = F.interpolate(img_tensor, scale_factor=scale_factor, mode='bilinear', align_corners=False)
|
| 200 |
+
upscaled = torch.clamp(upscaled * 255, 0, 255).byte()
|
| 201 |
+
return Image.fromarray(upscaled.squeeze(0).permute(1, 2, 0).cpu().numpy())
|
| 202 |
+
|
| 203 |
+
# Model Builders
|
| 204 |
class ModelBuilder:
|
| 205 |
def __init__(self):
|
| 206 |
self.config = None
|
| 207 |
self.model = None
|
| 208 |
self.tokenizer = None
|
| 209 |
+
self.sft_data = None
|
| 210 |
+
self.jokes = ["Why did the AI go to therapy? Too many layers to unpack! 😂", "Training complete! Time for a binary coffee break. ☕"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
|
| 212 |
with st.spinner(f"Loading {model_path}... ⏳"):
|
| 213 |
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
|
|
|
| 219 |
self.model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 220 |
st.success(f"Model loaded! 🎉 {random.choice(self.jokes)}")
|
| 221 |
return self
|
| 222 |
+
def fine_tune_sft(self, csv_path: str, epochs: int = 3, batch_size: int = 4):
|
| 223 |
+
self.sft_data = []
|
| 224 |
+
with open(csv_path, "r") as f:
|
| 225 |
+
reader = csv.DictReader(f)
|
| 226 |
+
for row in reader:
|
| 227 |
+
self.sft_data.append({"prompt": row["prompt"], "response": row["response"]})
|
| 228 |
+
dataset = SFTDataset(self.sft_data, self.tokenizer)
|
| 229 |
+
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
|
| 230 |
+
optimizer = torch.optim.AdamW(self.model.parameters(), lr=2e-5)
|
| 231 |
+
self.model.train()
|
| 232 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 233 |
+
self.model.to(device)
|
| 234 |
+
for epoch in range(epochs):
|
| 235 |
+
with st.spinner(f"Training epoch {epoch + 1}/{epochs}... ⚙️"):
|
| 236 |
+
total_loss = 0
|
| 237 |
+
for batch in dataloader:
|
| 238 |
+
optimizer.zero_grad()
|
| 239 |
+
input_ids = batch["input_ids"].to(device)
|
| 240 |
+
attention_mask = batch["attention_mask"].to(device)
|
| 241 |
+
labels = batch["labels"].to(device)
|
| 242 |
+
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
|
| 243 |
+
loss = outputs.loss
|
| 244 |
+
loss.backward()
|
| 245 |
+
optimizer.step()
|
| 246 |
+
total_loss += loss.item()
|
| 247 |
+
st.write(f"Epoch {epoch + 1} completed. Average loss: {total_loss / len(dataloader):.4f}")
|
| 248 |
+
st.success(f"SFT Fine-tuning completed! 🎉 {random.choice(self.jokes)}")
|
| 249 |
+
return self
|
| 250 |
def save_model(self, path: str):
|
| 251 |
with st.spinner("Saving model... 💾"):
|
| 252 |
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 253 |
self.model.save_pretrained(path)
|
| 254 |
self.tokenizer.save_pretrained(path)
|
| 255 |
st.success(f"Model saved at {path}! ✅")
|
| 256 |
+
def evaluate(self, prompt: str, status_container=None):
|
| 257 |
+
self.model.eval()
|
| 258 |
+
if status_container:
|
| 259 |
+
status_container.write("Preparing to evaluate... 🧠")
|
| 260 |
+
try:
|
| 261 |
+
with torch.no_grad():
|
| 262 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True).to(self.model.device)
|
| 263 |
+
outputs = self.model.generate(**inputs, max_new_tokens=50, do_sample=True, top_p=0.95, temperature=0.7)
|
| 264 |
+
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 265 |
+
except Exception as e:
|
| 266 |
+
if status_container:
|
| 267 |
+
status_container.error(f"Oops! Something broke: {str(e)} 💥")
|
| 268 |
+
return f"Error: {str(e)}"
|
| 269 |
|
| 270 |
class DiffusionBuilder:
|
| 271 |
def __init__(self):
|
|
|
|
| 276 |
self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu")
|
| 277 |
if config:
|
| 278 |
self.config = config
|
| 279 |
+
st.success(f"Diffusion model loaded! 🎨")
|
| 280 |
+
return self
|
| 281 |
+
def fine_tune_sft(self, images, texts, epochs=3):
|
| 282 |
+
dataset = DiffusionDataset(images, texts)
|
| 283 |
+
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
|
| 284 |
+
optimizer = torch.optim.AdamW(self.pipeline.unet.parameters(), lr=1e-5)
|
| 285 |
+
self.pipeline.unet.train()
|
| 286 |
+
for epoch in range(epochs):
|
| 287 |
+
with st.spinner(f"Training diffusion epoch {epoch + 1}/{epochs}... ⚙️"):
|
| 288 |
+
total_loss = 0
|
| 289 |
+
for batch in dataloader:
|
| 290 |
+
optimizer.zero_grad()
|
| 291 |
+
image = batch["image"][0].to(self.pipeline.device)
|
| 292 |
+
text = batch["text"][0]
|
| 293 |
+
latents = self.pipeline.vae.encode(torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0).float().to(self.pipeline.device)).latent_dist.sample()
|
| 294 |
+
noise = torch.randn_like(latents)
|
| 295 |
+
timesteps = torch.randint(0, self.pipeline.scheduler.num_train_timesteps, (latents.shape[0],), device=latents.device)
|
| 296 |
+
noisy_latents = self.pipeline.scheduler.add_noise(latents, noise, timesteps)
|
| 297 |
+
text_embeddings = self.pipeline.text_encoder(self.pipeline.tokenizer(text, return_tensors="pt").input_ids.to(self.pipeline.device))[0]
|
| 298 |
+
pred_noise = self.pipeline.unet(noisy_latents, timesteps, encoder_hidden_states=text_embeddings).sample
|
| 299 |
+
loss = torch.nn.functional.mse_loss(pred_noise, noise)
|
| 300 |
+
loss.backward()
|
| 301 |
+
optimizer.step()
|
| 302 |
+
total_loss += loss.item()
|
| 303 |
+
st.write(f"Epoch {epoch + 1} completed. Average loss: {total_loss / len(dataloader):.4f}")
|
| 304 |
+
st.success("Diffusion SFT Fine-tuning completed! 🎨")
|
| 305 |
return self
|
| 306 |
def save_model(self, path: str):
|
| 307 |
with st.spinner("Saving diffusion model... 💾"):
|
|
|
|
| 311 |
def generate(self, prompt: str):
|
| 312 |
return self.pipeline(prompt, num_inference_steps=20).images[0]
|
| 313 |
|
| 314 |
+
# Utility Functions
|
| 315 |
def generate_filename(sequence, ext="png"):
|
| 316 |
+
timestamp = time.strftime("%d%m%Y%H%M%S")
|
| 317 |
+
return f"{sequence}_{timestamp}.{ext}"
|
| 318 |
|
| 319 |
def pdf_url_to_filename(url):
|
| 320 |
+
# Convert full URL to filename, replacing illegal characters
|
| 321 |
+
safe_name = re.sub(r'[<>:"/\\|?*]', '_', url)
|
| 322 |
+
return f"{safe_name}.pdf"
|
| 323 |
|
| 324 |
def get_download_link(file_path, mime_type="application/pdf", label="Download"):
|
| 325 |
+
with open(file_path, 'rb') as f:
|
| 326 |
+
data = f.read()
|
| 327 |
+
b64 = base64.b64encode(data).decode()
|
| 328 |
+
return f'<a href="data:{mime_type};base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 329 |
|
| 330 |
def zip_directory(directory_path, zip_path):
|
| 331 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 332 |
+
for root, _, files in os.walk(directory_path):
|
| 333 |
+
for file in files:
|
| 334 |
+
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.dirname(directory_path)))
|
| 335 |
|
| 336 |
def get_model_files(model_type="causal_lm"):
|
| 337 |
+
path = "models/*" if model_type == "causal_lm" else "diffusion_models/*"
|
| 338 |
+
return [d for d in glob.glob(path) if os.path.isdir(d)]
|
| 339 |
|
| 340 |
+
def get_gallery_files(file_types=["png"]):
|
| 341 |
+
return sorted([f for ext in file_types for f in glob.glob(f"*.{ext}")])
|
| 342 |
|
| 343 |
def get_pdf_files():
|
| 344 |
return sorted(glob.glob("*.pdf"))
|
|
|
|
| 350 |
with open(output_path, "wb") as f:
|
| 351 |
for chunk in response.iter_content(chunk_size=8192):
|
| 352 |
f.write(chunk)
|
| 353 |
+
return True
|
|
|
|
|
|
|
| 354 |
except requests.RequestException as e:
|
| 355 |
logger.error(f"Failed to download {url}: {e}")
|
| 356 |
+
return False
|
|
|
|
| 357 |
|
| 358 |
+
# Async Processing Functions
|
| 359 |
async def process_pdf_snapshot(pdf_path, mode="single"):
|
| 360 |
start_time = time.time()
|
| 361 |
status = st.empty()
|
|
|
|
| 365 |
output_files = []
|
| 366 |
if mode == "single":
|
| 367 |
page = doc[0]
|
| 368 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0)) # High-res: 200% scale
|
| 369 |
output_file = generate_filename("single", "png")
|
| 370 |
pix.save(output_file)
|
| 371 |
output_files.append(output_file)
|
| 372 |
elif mode == "twopage":
|
| 373 |
for i in range(min(2, len(doc))):
|
| 374 |
page = doc[i]
|
| 375 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0)) # High-res: 200% scale
|
| 376 |
output_file = generate_filename(f"twopage_{i}", "png")
|
| 377 |
pix.save(output_file)
|
| 378 |
output_files.append(output_file)
|
| 379 |
+
elif mode == "allthumbs":
|
| 380 |
for i in range(len(doc)):
|
| 381 |
page = doc[i]
|
| 382 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(0.5, 0.5)) # Thumbnail: 50% scale
|
| 383 |
+
output_file = generate_filename(f"thumb_{i}", "png")
|
| 384 |
pix.save(output_file)
|
| 385 |
output_files.append(output_file)
|
| 386 |
doc.close()
|
| 387 |
elapsed = int(time.time() - start_time)
|
| 388 |
status.text(f"PDF Snapshot ({mode}) completed in {elapsed}s!")
|
| 389 |
+
update_gallery()
|
| 390 |
return output_files
|
| 391 |
except Exception as e:
|
| 392 |
status.error(f"Failed to process PDF: {str(e)}")
|
| 393 |
return []
|
| 394 |
|
|
|
|
| 395 |
async def process_ocr(image, output_file):
|
| 396 |
start_time = time.time()
|
| 397 |
status = st.empty()
|
| 398 |
status.text("Processing GOT-OCR2_0... (0s)")
|
| 399 |
tokenizer = AutoTokenizer.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True)
|
| 400 |
model = AutoModel.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True, torch_dtype=torch.float32).to("cpu").eval()
|
| 401 |
+
result = model.chat(tokenizer, image, ocr_type='ocr')
|
|
|
|
|
|
|
|
|
|
| 402 |
elapsed = int(time.time() - start_time)
|
| 403 |
status.text(f"GOT-OCR2_0 completed in {elapsed}s!")
|
| 404 |
async with aiofiles.open(output_file, "w") as f:
|
| 405 |
await f.write(result)
|
| 406 |
+
update_gallery()
|
| 407 |
return result
|
| 408 |
|
|
|
|
| 409 |
async def process_image_gen(prompt, output_file):
|
| 410 |
start_time = time.time()
|
| 411 |
status = st.empty()
|
| 412 |
status.text("Processing Image Gen... (0s)")
|
| 413 |
+
pipeline = StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu")
|
|
|
|
|
|
|
|
|
|
| 414 |
gen_image = pipeline(prompt, num_inference_steps=20).images[0]
|
| 415 |
elapsed = int(time.time() - start_time)
|
| 416 |
status.text(f"Image Gen completed in {elapsed}s!")
|
| 417 |
gen_image.save(output_file)
|
| 418 |
+
update_gallery()
|
| 419 |
return gen_image
|
| 420 |
|
| 421 |
+
async def process_custom_diffusion(images, output_file, model_name):
|
| 422 |
+
start_time = time.time()
|
| 423 |
+
status = st.empty()
|
| 424 |
+
status.text(f"Training {model_name}... (0s)")
|
| 425 |
+
unet = TinyUNet()
|
| 426 |
+
diffusion = TinyDiffusion(unet)
|
| 427 |
+
diffusion.train(images)
|
| 428 |
+
gen_image = diffusion.generate()
|
| 429 |
+
upscaled_image = diffusion.upscale(gen_image, scale_factor=2)
|
| 430 |
+
elapsed = int(time.time() - start_time)
|
| 431 |
+
status.text(f"{model_name} completed in {elapsed}s!")
|
| 432 |
+
upscaled_image.save(output_file)
|
| 433 |
+
update_gallery()
|
| 434 |
+
return upscaled_image
|
| 435 |
+
|
| 436 |
+
# Mock Search Tool for RAG
|
| 437 |
+
def mock_search(query: str) -> str:
|
| 438 |
+
if "superhero" in query.lower():
|
| 439 |
+
return "Latest trends: Gold-plated Batman statues, VR superhero battles."
|
| 440 |
+
return "No relevant results found."
|
| 441 |
+
|
| 442 |
+
def mock_duckduckgo_search(query: str) -> str:
|
| 443 |
+
if "superhero party trends" in query.lower():
|
| 444 |
+
return """
|
| 445 |
+
Latest trends for 2025:
|
| 446 |
+
- Luxury decorations: Gold-plated Batman statues, holographic Avengers displays.
|
| 447 |
+
- Entertainment: Live stunt shows with Iron Man suits, VR superhero battles.
|
| 448 |
+
- Catering: Gourmet kryptonite-green cocktails, Thor’s hammer-shaped appetizers.
|
| 449 |
+
"""
|
| 450 |
+
return "No relevant results found."
|
| 451 |
+
|
| 452 |
+
# Agent Classes
|
| 453 |
+
class PartyPlannerAgent:
|
| 454 |
+
def __init__(self, model, tokenizer):
|
| 455 |
+
self.model = model
|
| 456 |
+
self.tokenizer = tokenizer
|
| 457 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 458 |
+
self.model.to(self.device)
|
| 459 |
+
def generate(self, prompt: str) -> str:
|
| 460 |
+
self.model.eval()
|
| 461 |
+
with torch.no_grad():
|
| 462 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True).to(self.device)
|
| 463 |
+
outputs = self.model.generate(**inputs, max_new_tokens=100, do_sample=True, top_p=0.95, temperature=0.7)
|
| 464 |
+
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 465 |
+
def plan_party(self, task: str) -> pd.DataFrame:
|
| 466 |
+
search_result = mock_duckduckgo_search("latest superhero party trends")
|
| 467 |
+
prompt = f"Given this context: '{search_result}'\n{task}"
|
| 468 |
+
plan_text = self.generate(prompt)
|
| 469 |
+
locations = {
|
| 470 |
+
"Wayne Manor": (42.3601, -71.0589),
|
| 471 |
+
"New York": (40.7128, -74.0060),
|
| 472 |
+
"Los Angeles": (34.0522, -118.2437),
|
| 473 |
+
"London": (51.5074, -0.1278)
|
| 474 |
+
}
|
| 475 |
+
wayne_coords = locations["Wayne Manor"]
|
| 476 |
+
travel_times = {loc: calculate_cargo_travel_time(coords, wayne_coords) for loc, coords in locations.items() if loc != "Wayne Manor"}
|
| 477 |
+
catchphrases = ["To the Batmobile!", "Avengers, assemble!", "I am Iron Man!", "By the power of Grayskull!"]
|
| 478 |
+
data = [
|
| 479 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gold-plated Batman statues", "Catchphrase": random.choice(catchphrases)},
|
| 480 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Holographic Avengers displays", "Catchphrase": random.choice(catchphrases)},
|
| 481 |
+
{"Location": "London", "Travel Time (hrs)": travel_times["London"], "Luxury Idea": "Live stunt shows with Iron Man suits", "Catchphrase": random.choice(catchphrases)},
|
| 482 |
+
{"Location": "Wayne Manor", "Travel Time (hrs)": 0.0, "Luxury Idea": "VR superhero battles", "Catchphrase": random.choice(catchphrases)},
|
| 483 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gourmet kryptonite-green cocktails", "Catchphrase": random.choice(catchphrases)},
|
| 484 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Thor’s hammer-shaped appetizers", "Catchphrase": random.choice(catchphrases)},
|
| 485 |
]
|
| 486 |
+
return pd.DataFrame(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
+
class CVPartyPlannerAgent:
|
| 489 |
+
def __init__(self, pipeline):
|
| 490 |
+
self.pipeline = pipeline
|
| 491 |
+
def generate(self, prompt: str) -> Image.Image:
|
| 492 |
+
return self.pipeline(prompt, num_inference_steps=20).images[0]
|
| 493 |
+
def plan_party(self, task: str) -> pd.DataFrame:
|
| 494 |
+
search_result = mock_search("superhero party trends")
|
| 495 |
+
prompt = f"Given this context: '{search_result}'\n{task}"
|
| 496 |
+
data = [
|
| 497 |
+
{"Theme": "Batman", "Image Idea": "Gold-plated Batman statue"},
|
| 498 |
+
{"Theme": "Avengers", "Image Idea": "VR superhero battle scene"}
|
| 499 |
+
]
|
| 500 |
+
return pd.DataFrame(data)
|
| 501 |
+
|
| 502 |
+
def calculate_cargo_travel_time(origin_coords: Tuple[float, float], destination_coords: Tuple[float, float], cruising_speed_kmh: float = 750.0) -> float:
|
| 503 |
+
def to_radians(degrees: float) -> float:
|
| 504 |
+
return degrees * (math.pi / 180)
|
| 505 |
+
lat1, lon1 = map(to_radians, origin_coords)
|
| 506 |
+
lat2, lon2 = map(to_radians, destination_coords)
|
| 507 |
+
EARTH_RADIUS_KM = 6371.0
|
| 508 |
+
dlon = lon2 - lon1
|
| 509 |
+
dlat = lat2 - lat1
|
| 510 |
+
a = (math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2)
|
| 511 |
+
c = 2 * math.asin(math.sqrt(a))
|
| 512 |
+
distance = EARTH_RADIUS_KM * c
|
| 513 |
+
actual_distance = distance * 1.1
|
| 514 |
+
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 515 |
+
return round(flight_time, 2)
|
| 516 |
+
|
| 517 |
+
# Main App
|
| 518 |
+
st.title("AI Vision & SFT Titans 🚀")
|
| 519 |
+
|
| 520 |
+
# Sidebar
|
| 521 |
+
st.sidebar.header("Captured Files 📜")
|
| 522 |
+
gallery_size = st.sidebar.slider("Gallery Size", 1, 10, 2) # Default to 2
|
| 523 |
+
def update_gallery():
|
| 524 |
+
media_files = get_gallery_files(["png"])
|
| 525 |
+
pdf_files = get_pdf_files()
|
| 526 |
+
if media_files or pdf_files:
|
| 527 |
+
st.sidebar.subheader("Images 📸")
|
| 528 |
+
cols = st.sidebar.columns(2)
|
| 529 |
+
for idx, file in enumerate(media_files[:gallery_size * 2]): # Limit by gallery size
|
| 530 |
+
with cols[idx % 2]:
|
| 531 |
+
st.image(Image.open(file), caption=os.path.basename(file), use_container_width=True)
|
| 532 |
+
st.sidebar.subheader("PDF Downloads 📖")
|
| 533 |
+
for pdf_file in pdf_files[:gallery_size * 2]: # Limit by gallery size
|
| 534 |
+
st.markdown(get_download_link(pdf_file, "application/pdf", f"📥 Grab {os.path.basename(pdf_file)}"), unsafe_allow_html=True)
|
| 535 |
+
update_gallery()
|
| 536 |
|
| 537 |
+
st.sidebar.subheader("Model Management 🗂️")
|
| 538 |
+
model_type = st.sidebar.selectbox("Model Type", ["Causal LM", "Diffusion"], key="sidebar_model_type")
|
| 539 |
+
model_dirs = get_model_files(model_type)
|
| 540 |
+
selected_model = st.sidebar.selectbox("Select Saved Model", ["None"] + model_dirs, key="sidebar_model_select")
|
| 541 |
+
if selected_model != "None" and st.sidebar.button("Load Model 📂"):
|
| 542 |
+
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 543 |
+
config = (ModelConfig if model_type == "Causal LM" else DiffusionConfig)(name=os.path.basename(selected_model), base_model="unknown", size="small")
|
| 544 |
+
builder.load_model(selected_model, config)
|
| 545 |
+
st.session_state['builder'] = builder
|
| 546 |
+
st.session_state['model_loaded'] = True
|
| 547 |
+
st.rerun()
|
| 548 |
|
| 549 |
+
st.sidebar.subheader("Action Logs 📜")
|
| 550 |
+
log_container = st.sidebar.empty()
|
| 551 |
+
with log_container:
|
| 552 |
+
for record in log_records:
|
| 553 |
+
st.write(f"{record.asctime} - {record.levelname} - {record.message}")
|
| 554 |
|
| 555 |
+
st.sidebar.subheader("History 📜")
|
| 556 |
+
history_container = st.sidebar.empty()
|
| 557 |
+
with history_container:
|
| 558 |
+
for entry in st.session_state['history'][-gallery_size * 2:]: # Limit by gallery size
|
| 559 |
+
st.write(entry)
|
| 560 |
+
|
| 561 |
+
# Tabs
|
| 562 |
+
tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8, tab9 = st.tabs([
|
| 563 |
+
"Camera Snap 📷", "Download PDFs 📥", "Build Titan 🌱", "Fine-Tune Titan 🔧",
|
| 564 |
+
"Test Titan 🧪", "Agentic RAG Party 🌐", "Test OCR 🔍", "Test Image Gen 🎨", "Custom Diffusion 🎨🤓"
|
| 565 |
])
|
|
|
|
| 566 |
|
| 567 |
+
with tab1:
|
|
|
|
| 568 |
st.header("Camera Snap 📷")
|
| 569 |
st.subheader("Single Capture")
|
| 570 |
cols = st.columns(2)
|
|
|
|
| 572 |
cam0_img = st.camera_input("Take a picture - Cam 0", key="cam0")
|
| 573 |
if cam0_img:
|
| 574 |
filename = generate_filename("cam0")
|
|
|
|
|
|
|
| 575 |
with open(filename, "wb") as f:
|
| 576 |
f.write(cam0_img.getvalue())
|
|
|
|
| 577 |
entry = f"Snapshot from Cam 0: {filename}"
|
| 578 |
+
if entry not in st.session_state['history']:
|
| 579 |
+
st.session_state['history'] = [e for e in st.session_state['history'] if not e.startswith("Snapshot from Cam 0:")] + [entry]
|
| 580 |
st.image(Image.open(filename), caption="Camera 0", use_container_width=True)
|
| 581 |
logger.info(f"Saved snapshot from Camera 0: {filename}")
|
| 582 |
+
update_gallery()
|
| 583 |
with cols[1]:
|
| 584 |
cam1_img = st.camera_input("Take a picture - Cam 1", key="cam1")
|
| 585 |
if cam1_img:
|
| 586 |
filename = generate_filename("cam1")
|
|
|
|
|
|
|
| 587 |
with open(filename, "wb") as f:
|
| 588 |
f.write(cam1_img.getvalue())
|
|
|
|
| 589 |
entry = f"Snapshot from Cam 1: {filename}"
|
| 590 |
+
if entry not in st.session_state['history']:
|
| 591 |
+
st.session_state['history'] = [e for e in st.session_state['history'] if not e.startswith("Snapshot from Cam 1:")] + [entry]
|
| 592 |
st.image(Image.open(filename), caption="Camera 1", use_container_width=True)
|
| 593 |
logger.info(f"Saved snapshot from Camera 1: {filename}")
|
| 594 |
+
update_gallery()
|
| 595 |
|
| 596 |
+
with tab2:
|
|
|
|
| 597 |
st.header("Download PDFs 📥")
|
| 598 |
+
# Examples button with arXiv PDF links from README.md
|
| 599 |
if st.button("Examples 📚"):
|
| 600 |
example_urls = [
|
| 601 |
+
"https://arxiv.org/pdf/2308.03892", # Streamlit
|
| 602 |
+
"https://arxiv.org/pdf/1912.01703", # PyTorch
|
| 603 |
+
"https://arxiv.org/pdf/2408.11039", # Qwen2-VL
|
| 604 |
+
"https://arxiv.org/pdf/2109.10282", # TrOCR
|
| 605 |
+
"https://arxiv.org/pdf/2112.10752", # LDM
|
| 606 |
+
"https://arxiv.org/pdf/2308.11236", # OpenCV
|
| 607 |
+
"https://arxiv.org/pdf/1706.03762", # Attention is All You Need
|
| 608 |
+
"https://arxiv.org/pdf/2006.11239", # DDPM
|
| 609 |
+
"https://arxiv.org/pdf/2305.11207", # Pandas
|
| 610 |
+
"https://arxiv.org/pdf/2106.09685", # LoRA
|
| 611 |
+
"https://arxiv.org/pdf/2005.11401", # RAG
|
| 612 |
+
"https://arxiv.org/pdf/2106.10504" # Fine-Tuning Vision Transformers
|
| 613 |
]
|
| 614 |
st.session_state['pdf_urls'] = "\n".join(example_urls)
|
| 615 |
+
|
| 616 |
+
# Robo-Downloader
|
| 617 |
url_input = st.text_area("Enter PDF URLs (one per line)", value=st.session_state.get('pdf_urls', ""), height=200)
|
| 618 |
if st.button("Robo-Download 🤖"):
|
| 619 |
urls = url_input.strip().split("\n")
|
|
|
|
| 630 |
st.session_state['downloaded_pdfs'][url] = output_path
|
| 631 |
logger.info(f"Downloaded PDF from {url} to {output_path}")
|
| 632 |
entry = f"Downloaded PDF: {output_path}"
|
| 633 |
+
if entry not in st.session_state['history']:
|
| 634 |
+
st.session_state['history'].append(entry)
|
| 635 |
else:
|
| 636 |
st.error(f"Failed to nab {url} 😿")
|
| 637 |
else:
|
|
|
|
| 639 |
st.session_state['downloaded_pdfs'][url] = output_path
|
| 640 |
progress_bar.progress((idx + 1) / total_urls)
|
| 641 |
status_text.text("Robo-Download complete! 🚀")
|
| 642 |
+
update_gallery()
|
| 643 |
+
|
| 644 |
+
# PDF Gallery with Thumbnails and Checkboxes
|
| 645 |
+
st.subheader("PDF Gallery 📖")
|
| 646 |
+
downloaded_pdfs = list(st.session_state['downloaded_pdfs'].values())
|
| 647 |
+
if downloaded_pdfs:
|
| 648 |
+
cols_per_row = 3
|
| 649 |
+
for i in range(0, len(downloaded_pdfs), cols_per_row):
|
| 650 |
+
cols = st.columns(cols_per_row)
|
| 651 |
+
for j, pdf_path in enumerate(downloaded_pdfs[i:i + cols_per_row]):
|
| 652 |
+
with cols[j]:
|
| 653 |
+
doc = fitz.open(pdf_path)
|
| 654 |
+
page = doc[0]
|
| 655 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(0.5, 0.5)) # Thumbnail at 50% scale
|
| 656 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 657 |
+
st.image(img, caption=os.path.basename(pdf_path), use_container_width=True)
|
| 658 |
+
# Checkbox for SFT/Input use
|
| 659 |
+
checkbox_key = f"pdf_{pdf_path}"
|
| 660 |
+
st.session_state['pdf_checkboxes'][checkbox_key] = st.checkbox(
|
| 661 |
+
"Use for SFT/Input",
|
| 662 |
+
value=st.session_state['pdf_checkboxes'].get(checkbox_key, False),
|
| 663 |
+
key=checkbox_key
|
| 664 |
+
)
|
| 665 |
+
# Download and Delete Buttons
|
| 666 |
+
st.markdown(get_download_link(pdf_path, "application/pdf", "Snag It! 📥"), unsafe_allow_html=True)
|
| 667 |
+
if st.button("Zap It! 🗑️", key=f"delete_{pdf_path}"):
|
| 668 |
+
os.remove(pdf_path)
|
| 669 |
+
url_key = next((k for k, v in st.session_state['downloaded_pdfs'].items() if v == pdf_path), None)
|
| 670 |
+
if url_key:
|
| 671 |
+
del st.session_state['downloaded_pdfs'][url_key]
|
| 672 |
+
del st.session_state['pdf_checkboxes'][checkbox_key]
|
| 673 |
+
st.success(f"PDF {os.path.basename(pdf_path)} vaporized! 💨")
|
| 674 |
+
st.rerun()
|
| 675 |
+
doc.close()
|
| 676 |
+
else:
|
| 677 |
+
st.info("No PDFs captured yet. Feed the robo-downloader some URLs! 🤖")
|
| 678 |
+
|
| 679 |
+
mode = st.selectbox("Snapshot Mode", ["Single Page (High-Res)", "Two Pages (High-Res)", "All Pages (Thumbnails)"], key="download_mode")
|
| 680 |
if st.button("Snapshot Selected 📸"):
|
| 681 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 682 |
if selected_pdfs:
|
| 683 |
for pdf_path in selected_pdfs:
|
| 684 |
+
mode_key = {"Single Page (High-Res)": "single", "Two Pages (High-Res)": "twopage", "All Pages (Thumbnails)": "allthumbs"}[mode]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
snapshots = asyncio.run(process_pdf_snapshot(pdf_path, mode_key))
|
| 686 |
for snapshot in snapshots:
|
| 687 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
|
|
|
|
|
|
| 688 |
else:
|
| 689 |
+
st.warning("No PDFs selected for snapshotting! Check some boxes first. 📝")
|
| 690 |
|
| 691 |
+
with tab3:
|
| 692 |
+
st.header("Build Titan 🌱")
|
| 693 |
+
model_type = st.selectbox("Model Type", ["Causal LM", "Diffusion"], key="build_type")
|
| 694 |
+
base_model = st.selectbox("Select Tiny Model",
|
| 695 |
+
["HuggingFaceTB/SmolLM-135M", "Qwen/Qwen1.5-0.5B-Chat"] if model_type == "Causal LM" else
|
| 696 |
+
["OFA-Sys/small-stable-diffusion-v0", "stabilityai/stable-diffusion-2-base"])
|
| 697 |
+
model_name = st.text_input("Model Name", f"tiny-titan-{int(time.time())}")
|
| 698 |
+
domain = st.text_input("Target Domain", "general")
|
| 699 |
+
if st.button("Download Model ⬇️"):
|
| 700 |
+
config = (ModelConfig if model_type == "Causal LM" else DiffusionConfig)(name=model_name, base_model=base_model, size="small", domain=domain)
|
| 701 |
+
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 702 |
+
builder.load_model(base_model, config)
|
| 703 |
+
builder.save_model(config.model_path)
|
| 704 |
+
st.session_state['builder'] = builder
|
| 705 |
+
st.session_state['model_loaded'] = True
|
| 706 |
+
entry = f"Built {model_type} model: {model_name}"
|
| 707 |
+
if entry not in st.session_state['history']:
|
| 708 |
+
st.session_state['history'].append(entry)
|
| 709 |
+
st.success(f"Model downloaded and saved to {config.model_path}! 🎉")
|
| 710 |
+
st.rerun()
|
| 711 |
+
|
| 712 |
+
with tab4:
|
| 713 |
+
st.header("Fine-Tune Titan 🔧")
|
| 714 |
+
if 'builder' not in st.session_state or not st.session_state.get('model_loaded', False):
|
| 715 |
+
st.warning("Please build or load a Titan first! ⚠️")
|
| 716 |
+
else:
|
| 717 |
+
if isinstance(st.session_state['builder'], ModelBuilder):
|
| 718 |
+
if st.button("Generate Sample CSV 📝"):
|
| 719 |
+
sample_data = [
|
| 720 |
+
{"prompt": "What is AI?", "response": "AI is artificial intelligence, simulating human smarts in machines."},
|
| 721 |
+
{"prompt": "Explain machine learning", "response": "Machine learning is AI’s gym where models bulk up on data."},
|
| 722 |
+
]
|
| 723 |
+
csv_path = f"sft_data_{int(time.time())}.csv"
|
| 724 |
+
with open(csv_path, "w", newline="") as f:
|
| 725 |
+
writer = csv.DictWriter(f, fieldnames=["prompt", "response"])
|
| 726 |
+
writer.writeheader()
|
| 727 |
+
writer.writerows(sample_data)
|
| 728 |
+
st.markdown(get_download_link(csv_path, "text/csv", "Download Sample CSV"), unsafe_allow_html=True)
|
| 729 |
+
st.success(f"Sample CSV generated as {csv_path}! ✅")
|
| 730 |
+
|
| 731 |
+
uploaded_csv = st.file_uploader("Upload CSV for SFT", type="csv")
|
| 732 |
+
if uploaded_csv and st.button("Fine-Tune with Uploaded CSV 🔄"):
|
| 733 |
+
csv_path = f"uploaded_sft_data_{int(time.time())}.csv"
|
| 734 |
+
with open(csv_path, "wb") as f:
|
| 735 |
+
f.write(uploaded_csv.read())
|
| 736 |
+
new_model_name = f"{st.session_state['builder'].config.name}-sft-{int(time.time())}"
|
| 737 |
+
new_config = ModelConfig(name=new_model_name, base_model=st.session_state['builder'].config.base_model, size="small", domain=st.session_state['builder'].config.domain)
|
| 738 |
+
st.session_state['builder'].config = new_config
|
| 739 |
+
st.session_state['builder'].fine_tune_sft(csv_path)
|
| 740 |
+
st.session_state['builder'].save_model(new_config.model_path)
|
| 741 |
+
zip_path = f"{new_config.model_path}.zip"
|
| 742 |
+
zip_directory(new_config.model_path, zip_path)
|
| 743 |
+
entry = f"Fine-tuned Causal LM: {new_model_name}"
|
| 744 |
+
if entry not in st.session_state['history']:
|
| 745 |
+
st.session_state['history'].append(entry)
|
| 746 |
+
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Titan"), unsafe_allow_html=True)
|
| 747 |
+
st.rerun()
|
| 748 |
+
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 749 |
+
captured_files = get_gallery_files(["png"])
|
| 750 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 751 |
+
if len(captured_files) + len(selected_pdfs) >= 2:
|
| 752 |
+
demo_data = [{"image": img, "text": f"Superhero {os.path.basename(img).split('.')[0]}"} for img in captured_files]
|
| 753 |
+
for pdf_path in selected_pdfs:
|
| 754 |
+
demo_data.append({"image": pdf_path, "text": f"PDF {os.path.basename(pdf_path)}"})
|
| 755 |
+
edited_data = st.data_editor(pd.DataFrame(demo_data), num_rows="dynamic")
|
| 756 |
+
if st.button("Fine-Tune with Dataset 🔄"):
|
| 757 |
+
images = [Image.open(row["image"]) if row["image"].endswith('.png') else Image.frombytes("RGB", fitz.open(row["image"])[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0)).size, fitz.open(row["image"])[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0)).samples) for _, row in edited_data.iterrows()]
|
| 758 |
+
texts = [row["text"] for _, row in edited_data.iterrows()]
|
| 759 |
+
new_model_name = f"{st.session_state['builder'].config.name}-sft-{int(time.time())}"
|
| 760 |
+
new_config = DiffusionConfig(name=new_model_name, base_model=st.session_state['builder'].config.base_model, size="small")
|
| 761 |
+
st.session_state['builder'].config = new_config
|
| 762 |
+
st.session_state['builder'].fine_tune_sft(images, texts)
|
| 763 |
+
st.session_state['builder'].save_model(new_config.model_path)
|
| 764 |
+
zip_path = f"{new_config.model_path}.zip"
|
| 765 |
+
zip_directory(new_config.model_path, zip_path)
|
| 766 |
+
entry = f"Fine-tuned Diffusion: {new_model_name}"
|
| 767 |
+
if entry not in st.session_state['history']:
|
| 768 |
+
st.session_state['history'].append(entry)
|
| 769 |
+
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Diffusion Model"), unsafe_allow_html=True)
|
| 770 |
+
csv_path = f"sft_dataset_{int(time.time())}.csv"
|
| 771 |
+
with open(csv_path, "w", newline="") as f:
|
| 772 |
+
writer = csv.writer(f)
|
| 773 |
+
writer.writerow(["image", "text"])
|
| 774 |
+
for _, row in edited_data.iterrows():
|
| 775 |
+
writer.writerow([row["image"], row["text"]])
|
| 776 |
+
st.markdown(get_download_link(csv_path, "text/csv", "Download SFT Dataset CSV"), unsafe_allow_html=True)
|
| 777 |
+
|
| 778 |
+
with tab5:
|
| 779 |
+
st.header("Test Titan 🧪")
|
| 780 |
+
if 'builder' not in st.session_state or not st.session_state.get('model_loaded', False):
|
| 781 |
+
st.warning("Please build or load a Titan first! ⚠️")
|
| 782 |
+
else:
|
| 783 |
+
if isinstance(st.session_state['builder'], ModelBuilder):
|
| 784 |
+
if st.session_state['builder'].sft_data:
|
| 785 |
+
st.write("Testing with SFT Data:")
|
| 786 |
+
for item in st.session_state['builder'].sft_data[:3]:
|
| 787 |
+
prompt = item["prompt"]
|
| 788 |
+
expected = item["response"]
|
| 789 |
+
status_container = st.empty()
|
| 790 |
+
generated = st.session_state['builder'].evaluate(prompt, status_container)
|
| 791 |
+
st.write(f"**Prompt**: {prompt}")
|
| 792 |
+
st.write(f"**Expected**: {expected}")
|
| 793 |
+
st.write(f"**Generated**: {generated}")
|
| 794 |
+
st.write("---")
|
| 795 |
+
status_container.empty()
|
| 796 |
+
test_prompt = st.text_area("Enter Test Prompt", "What is AI?")
|
| 797 |
+
if st.button("Run Test ▶️"):
|
| 798 |
+
status_container = st.empty()
|
| 799 |
+
result = st.session_state['builder'].evaluate(test_prompt, status_container)
|
| 800 |
+
entry = f"Causal LM Test: {test_prompt} -> {result}"
|
| 801 |
+
if entry not in st.session_state['history']:
|
| 802 |
+
st.session_state['history'].append(entry)
|
| 803 |
+
st.write(f"**Generated Response**: {result}")
|
| 804 |
+
status_container.empty()
|
| 805 |
+
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 806 |
+
test_prompt = st.text_area("Enter Test Prompt", "Neon Batman")
|
| 807 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 808 |
+
if st.button("Run Test ▶️"):
|
| 809 |
+
image = st.session_state['builder'].generate(test_prompt)
|
| 810 |
+
output_file = generate_filename("diffusion_test", "png")
|
| 811 |
+
image.save(output_file)
|
| 812 |
+
entry = f"Diffusion Test: {test_prompt} -> {output_file}"
|
| 813 |
+
if entry not in st.session_state['history']:
|
| 814 |
+
st.session_state['history'].append(entry)
|
| 815 |
+
st.image(image, caption="Generated Image")
|
| 816 |
+
update_gallery()
|
| 817 |
+
|
| 818 |
+
with tab6:
|
| 819 |
+
st.header("Agentic RAG Party 🌐")
|
| 820 |
+
if 'builder' not in st.session_state or not st.session_state.get('model_loaded', False):
|
| 821 |
+
st.warning("Please build or load a Titan first! ⚠️")
|
| 822 |
+
else:
|
| 823 |
+
if isinstance(st.session_state['builder'], ModelBuilder):
|
| 824 |
+
if st.button("Run NLP RAG Demo 🎉"):
|
| 825 |
+
agent = PartyPlannerAgent(st.session_state['builder'].model, st.session_state['builder'].tokenizer)
|
| 826 |
+
task = "Plan a luxury superhero-themed party at Wayne Manor."
|
| 827 |
+
plan_df = agent.plan_party(task)
|
| 828 |
+
entry = f"NLP RAG Demo: Planned party at Wayne Manor"
|
| 829 |
+
if entry not in st.session_state['history']:
|
| 830 |
+
st.session_state['history'].append(entry)
|
| 831 |
+
st.dataframe(plan_df)
|
| 832 |
+
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 833 |
+
if st.button("Run CV RAG Demo 🎉"):
|
| 834 |
+
agent = CVPartyPlannerAgent(st.session_state['builder'].pipeline)
|
| 835 |
+
task = "Generate images for a luxury superhero-themed party."
|
| 836 |
+
plan_df = agent.plan_party(task)
|
| 837 |
+
entry = f"CV RAG Demo: Generated party images"
|
| 838 |
+
if entry not in st.session_state['history']:
|
| 839 |
+
st.session_state['history'].append(entry)
|
| 840 |
+
st.dataframe(plan_df)
|
| 841 |
+
for _, row in plan_df.iterrows():
|
| 842 |
+
image = agent.generate(row["Image Idea"])
|
| 843 |
+
output_file = generate_filename(f"cv_rag_{row['Theme'].lower()}", "png")
|
| 844 |
+
image.save(output_file)
|
| 845 |
+
st.image(image, caption=f"{row['Theme']} - {row['Image Idea']}")
|
| 846 |
+
update_gallery()
|
| 847 |
+
|
| 848 |
+
with tab7:
|
| 849 |
st.header("Test OCR 🔍")
|
| 850 |
+
captured_files = get_gallery_files(["png"])
|
| 851 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 852 |
+
all_files = captured_files + selected_pdfs
|
| 853 |
if all_files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="ocr_select")
|
| 855 |
if selected_file:
|
| 856 |
if selected_file.endswith('.png'):
|
|
|
|
| 866 |
st.session_state['processing']['ocr'] = True
|
| 867 |
result = asyncio.run(process_ocr(image, output_file))
|
| 868 |
entry = f"OCR Test: {selected_file} -> {output_file}"
|
| 869 |
+
if entry not in st.session_state['history']:
|
| 870 |
+
st.session_state['history'].append(entry)
|
| 871 |
st.text_area("OCR Result", result, height=200, key="ocr_result")
|
| 872 |
st.success(f"OCR output saved to {output_file}")
|
| 873 |
st.session_state['processing']['ocr'] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
else:
|
| 875 |
+
st.warning("No images or PDFs captured yet. Use Camera Snap or Download PDFs first!")
|
| 876 |
|
| 877 |
+
with tab8:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
st.header("Test Image Gen 🎨")
|
| 879 |
+
captured_files = get_gallery_files(["png"])
|
| 880 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 881 |
+
all_files = captured_files + selected_pdfs
|
| 882 |
if all_files:
|
| 883 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="gen_select")
|
| 884 |
if selected_file:
|
|
|
|
| 890 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 891 |
doc.close()
|
| 892 |
st.image(image, caption="Reference Image", use_container_width=True)
|
| 893 |
+
prompt = st.text_area("Prompt", "Generate a similar superhero image", key="gen_prompt")
|
| 894 |
if st.button("Run Image Gen 🚀", key="gen_run"):
|
| 895 |
output_file = generate_filename("gen_output", "png")
|
| 896 |
st.session_state['processing']['gen'] = True
|
| 897 |
result = asyncio.run(process_image_gen(prompt, output_file))
|
| 898 |
entry = f"Image Gen Test: {prompt} -> {output_file}"
|
| 899 |
+
if entry not in st.session_state['history']:
|
| 900 |
+
st.session_state['history'].append(entry)
|
| 901 |
st.image(result, caption="Generated Image", use_container_width=True)
|
| 902 |
st.success(f"Image saved to {output_file}")
|
| 903 |
st.session_state['processing']['gen'] = False
|
| 904 |
else:
|
| 905 |
+
st.warning("No images or PDFs captured yet. Use Camera Snap or Download PDFs first WAV!")
|
| 906 |
+
|
| 907 |
+
with tab9:
|
| 908 |
+
st.header("Custom Diffusion 🎨🤓")
|
| 909 |
+
st.write("Unleash your inner artist with our tiny diffusion models!")
|
| 910 |
+
captured_files = get_gallery_files(["png"])
|
| 911 |
+
selected_pdfs = [path for key, path in st.session_state['downloaded_pdfs'].items() if st.session_state['pdf_checkboxes'].get(f"pdf_{path}", False)]
|
| 912 |
+
all_files = captured_files + selected_pdfs
|
| 913 |
+
if all_files:
|
| 914 |
+
st.subheader("Select Images or PDFs to Train")
|
| 915 |
+
selected_files = st.multiselect("Pick Images or PDFs", all_files, key="diffusion_select")
|
| 916 |
+
images = []
|
| 917 |
+
for file in selected_files:
|
| 918 |
+
if file.endswith('.png'):
|
| 919 |
+
images.append(Image.open(file))
|
| 920 |
+
else:
|
| 921 |
+
doc = fitz.open(file)
|
| 922 |
+
pix = doc[0].get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
| 923 |
+
images.append(Image.frombytes("RGB", [pix.width, pix.height], pix.samples))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 924 |
doc.close()
|
| 925 |
+
|
| 926 |
+
model_options = [
|
| 927 |
+
("PixelTickler 🎨✨", "OFA-Sys/small-stable-diffusion-v0"),
|
| 928 |
+
("DreamWeaver 🌙🖌️", "stabilityai/stable-diffusion-2-base"),
|
| 929 |
+
("TinyArtBot 🤖🖼️", "custom")
|
| 930 |
+
]
|
| 931 |
+
model_choice = st.selectbox("Choose Your Diffusion Dynamo", [opt[0] for opt in model_options], key="diffusion_model")
|
| 932 |
+
model_name = next(opt[1] for opt in model_options if opt[0] == model_choice)
|
| 933 |
+
|
| 934 |
+
if st.button("Train & Generate 🚀", key="diffusion_run"):
|
| 935 |
+
output_file = generate_filename("custom_diffusion", "png")
|
| 936 |
+
st.session_state['processing']['diffusion'] = True
|
| 937 |
+
if model_name == "custom":
|
| 938 |
+
result = asyncio.run(process_custom_diffusion(images, output_file, model_choice))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 939 |
else:
|
| 940 |
+
builder = DiffusionBuilder()
|
| 941 |
+
builder.load_model(model_name)
|
| 942 |
+
result = builder.generate("A superhero scene inspired by captured images")
|
| 943 |
+
result.save(output_file)
|
| 944 |
+
entry = f"Custom Diffusion: {model_choice} -> {output_file}"
|
| 945 |
+
if entry not in st.session_state['history']:
|
| 946 |
+
st.session_state['history'].append(entry)
|
| 947 |
+
st.image(result, caption=f"{model_choice} Masterpiece", use_container_width=True)
|
| 948 |
+
st.success(f"Image saved to {output_file}")
|
| 949 |
+
st.session_state['processing']['diffusion'] = False
|
| 950 |
else:
|
| 951 |
+
st.warning("No images or PDFs captured yet. Use Camera Snap or Download PDFs first!")
|
| 952 |
|
| 953 |
+
# Initial Gallery Update
|
| 954 |
+
update_gallery()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|