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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import glob
|
| 4 |
+
import base64
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
from torch.utils.data import Dataset, DataLoader
|
| 10 |
+
import csv
|
| 11 |
+
import time
|
| 12 |
+
from dataclasses import dataclass
|
| 13 |
+
from typing import Optional, Tuple
|
| 14 |
+
import zipfile
|
| 15 |
+
import math
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import random
|
| 18 |
+
import logging
|
| 19 |
+
import numpy as np
|
| 20 |
+
import cv2
|
| 21 |
+
import sounddevice as sd
|
| 22 |
+
|
| 23 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
log_records = []
|
| 26 |
+
|
| 27 |
+
class LogCaptureHandler(logging.Handler):
|
| 28 |
+
def emit(self, record):
|
| 29 |
+
log_records.append(record)
|
| 30 |
+
|
| 31 |
+
logger.addHandler(LogCaptureHandler())
|
| 32 |
+
|
| 33 |
+
st.set_page_config(
|
| 34 |
+
page_title="SFT Tiny Titans 🚀",
|
| 35 |
+
page_icon="🤖",
|
| 36 |
+
layout="wide",
|
| 37 |
+
initial_sidebar_state="expanded",
|
| 38 |
+
menu_items={
|
| 39 |
+
'Get Help': 'https://huggingface.co/awacke1',
|
| 40 |
+
'Report a Bug': 'https://huggingface.co/spaces/awacke1',
|
| 41 |
+
'About': "Tiny Titans: Small models, big dreams, and a sprinkle of chaos! 🌌"
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if 'captured_images' not in st.session_state:
|
| 46 |
+
st.session_state['captured_images'] = []
|
| 47 |
+
if 'nlp_builder' not in st.session_state:
|
| 48 |
+
st.session_state['nlp_builder'] = None
|
| 49 |
+
if 'cv_builder' not in st.session_state:
|
| 50 |
+
st.session_state['cv_builder'] = None
|
| 51 |
+
if 'nlp_loaded' not in st.session_state:
|
| 52 |
+
st.session_state['nlp_loaded'] = False
|
| 53 |
+
if 'cv_loaded' not in st.session_state:
|
| 54 |
+
st.session_state['cv_loaded'] = False
|
| 55 |
+
if 'active_tab' not in st.session_state:
|
| 56 |
+
st.session_state['active_tab'] = "Build Titan 🌱"
|
| 57 |
+
|
| 58 |
+
@dataclass
|
| 59 |
+
class ModelConfig:
|
| 60 |
+
name: str
|
| 61 |
+
base_model: str
|
| 62 |
+
size: str
|
| 63 |
+
domain: Optional[str] = None
|
| 64 |
+
model_type: str = "causal_lm"
|
| 65 |
+
@property
|
| 66 |
+
def model_path(self):
|
| 67 |
+
return f"models/{self.name}"
|
| 68 |
+
|
| 69 |
+
@dataclass
|
| 70 |
+
class DiffusionConfig:
|
| 71 |
+
name: str
|
| 72 |
+
base_model: str
|
| 73 |
+
size: str
|
| 74 |
+
@property
|
| 75 |
+
def model_path(self):
|
| 76 |
+
return f"diffusion_models/{self.name}"
|
| 77 |
+
|
| 78 |
+
class SFTDataset(Dataset):
|
| 79 |
+
def __init__(self, data, tokenizer, max_length=128):
|
| 80 |
+
self.data = data
|
| 81 |
+
self.tokenizer = tokenizer
|
| 82 |
+
self.max_length = max_length
|
| 83 |
+
def __len__(self):
|
| 84 |
+
return len(self.data)
|
| 85 |
+
def __getitem__(self, idx):
|
| 86 |
+
prompt = self.data[idx]["prompt"]
|
| 87 |
+
response = self.data[idx]["response"]
|
| 88 |
+
full_text = f"{prompt} {response}"
|
| 89 |
+
full_encoding = self.tokenizer(full_text, max_length=self.max_length, padding="max_length", truncation=True, return_tensors="pt")
|
| 90 |
+
prompt_encoding = self.tokenizer(prompt, max_length=self.max_length, padding=False, truncation=True, return_tensors="pt")
|
| 91 |
+
input_ids = full_encoding["input_ids"].squeeze()
|
| 92 |
+
attention_mask = full_encoding["attention_mask"].squeeze()
|
| 93 |
+
labels = input_ids.clone()
|
| 94 |
+
prompt_len = prompt_encoding["input_ids"].shape[1]
|
| 95 |
+
if prompt_len < self.max_length:
|
| 96 |
+
labels[:prompt_len] = -100
|
| 97 |
+
return {"input_ids": input_ids, "attention_mask": attention_mask, "labels": labels}
|
| 98 |
+
|
| 99 |
+
class DiffusionDataset(Dataset):
|
| 100 |
+
def __init__(self, images, texts):
|
| 101 |
+
self.images = images
|
| 102 |
+
self.texts = texts
|
| 103 |
+
def __len__(self):
|
| 104 |
+
return len(self.images)
|
| 105 |
+
def __getitem__(self, idx):
|
| 106 |
+
return {"image": self.images[idx], "text": self.texts[idx]}
|
| 107 |
+
|
| 108 |
+
class ModelBuilder:
|
| 109 |
+
def __init__(self):
|
| 110 |
+
self.config = None
|
| 111 |
+
self.model = None
|
| 112 |
+
self.tokenizer = None
|
| 113 |
+
self.sft_data = None
|
| 114 |
+
self.jokes = ["Why did the AI go to therapy? Too many layers to unpack! 😂", "Training complete! Time for a binary coffee break. ☕"]
|
| 115 |
+
def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
|
| 116 |
+
try:
|
| 117 |
+
with st.spinner(f"Loading {model_path}... ⏳ (Patience, young padawan!)"):
|
| 118 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 119 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 120 |
+
if self.tokenizer.pad_token is None:
|
| 121 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 122 |
+
if config:
|
| 123 |
+
self.config = config
|
| 124 |
+
self.model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 125 |
+
st.success(f"Model loaded! 🎉 {random.choice(self.jokes)}")
|
| 126 |
+
logger.info(f"Successfully loaded Causal LM model: {model_path}")
|
| 127 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 128 |
+
st.error(f"GPU memory error loading {model_path}: {str(e)} 💥 (Out of GPU juice!)")
|
| 129 |
+
logger.error(f"GPU memory error loading {model_path}: {str(e)}")
|
| 130 |
+
raise
|
| 131 |
+
except MemoryError as e:
|
| 132 |
+
st.error(f"CPU memory error loading {model_path}: {str(e)} 💥 (RAM ran away!)")
|
| 133 |
+
logger.error(f"CPU memory error loading {model_path}: {str(e)}")
|
| 134 |
+
raise
|
| 135 |
+
except Exception as e:
|
| 136 |
+
st.error(f"Failed to load {model_path}: {str(e)} 💥 (Something broke—check the logs!)")
|
| 137 |
+
logger.error(f"Failed to load {model_path}: {str(e)}")
|
| 138 |
+
raise
|
| 139 |
+
return self
|
| 140 |
+
def fine_tune_sft(self, csv_path: str, epochs: int = 3, batch_size: int = 4):
|
| 141 |
+
try:
|
| 142 |
+
self.sft_data = []
|
| 143 |
+
with open(csv_path, "r") as f:
|
| 144 |
+
reader = csv.DictReader(f)
|
| 145 |
+
for row in reader:
|
| 146 |
+
self.sft_data.append({"prompt": row["prompt"], "response": row["response"]})
|
| 147 |
+
dataset = SFTDataset(self.sft_data, self.tokenizer)
|
| 148 |
+
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
|
| 149 |
+
optimizer = torch.optim.AdamW(self.model.parameters(), lr=2e-5)
|
| 150 |
+
self.model.train()
|
| 151 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 152 |
+
self.model.to(device)
|
| 153 |
+
for epoch in range(epochs):
|
| 154 |
+
with st.spinner(f"Training epoch {epoch + 1}/{epochs}... ⚙️ (The AI is lifting weights!)"):
|
| 155 |
+
total_loss = 0
|
| 156 |
+
for batch in dataloader:
|
| 157 |
+
optimizer.zero_grad()
|
| 158 |
+
input_ids = batch["input_ids"].to(device)
|
| 159 |
+
attention_mask = batch["attention_mask"].to(device)
|
| 160 |
+
labels = batch["labels"].to(device)
|
| 161 |
+
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
|
| 162 |
+
loss = outputs.loss
|
| 163 |
+
loss.backward()
|
| 164 |
+
optimizer.step()
|
| 165 |
+
total_loss += loss.item()
|
| 166 |
+
st.write(f"Epoch {epoch + 1} completed. Average loss: {total_loss / len(dataloader):.4f}")
|
| 167 |
+
st.success(f"SFT Fine-tuning completed! 🎉 {random.choice(self.jokes)}")
|
| 168 |
+
logger.info(f"Successfully fine-tuned Causal LM model: {self.config.name}")
|
| 169 |
+
except Exception as e:
|
| 170 |
+
st.error(f"Fine-tuning failed: {str(e)} 💥 (Training hit a snag!)")
|
| 171 |
+
logger.error(f"Fine-tuning failed: {str(e)}")
|
| 172 |
+
raise
|
| 173 |
+
return self
|
| 174 |
+
def save_model(self, path: str):
|
| 175 |
+
try:
|
| 176 |
+
with st.spinner("Saving model... 💾 (Packing the AI’s suitcase!)"):
|
| 177 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 178 |
+
self.model.save_pretrained(path)
|
| 179 |
+
self.tokenizer.save_pretrained(path)
|
| 180 |
+
st.success(f"Model saved at {path}! ✅ May the force be with it.")
|
| 181 |
+
logger.info(f"Model saved at {path}")
|
| 182 |
+
except Exception as e:
|
| 183 |
+
st.error(f"Failed to save model: {str(e)} 💥 (Save operation crashed!)")
|
| 184 |
+
logger.error(f"Failed to save model: {str(e)}")
|
| 185 |
+
raise
|
| 186 |
+
def evaluate(self, prompt: str, status_container=None):
|
| 187 |
+
self.model.eval()
|
| 188 |
+
if status_container:
|
| 189 |
+
status_container.write("Preparing to evaluate... 🧠 (Titan’s warming up its circuits!)")
|
| 190 |
+
logger.info(f"Evaluating prompt: {prompt}")
|
| 191 |
+
try:
|
| 192 |
+
with torch.no_grad():
|
| 193 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True).to(self.model.device)
|
| 194 |
+
outputs = self.model.generate(**inputs, max_new_tokens=50, do_sample=True, top_p=0.95, temperature=0.7)
|
| 195 |
+
result = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 196 |
+
logger.info(f"Generated response: {result}")
|
| 197 |
+
return result
|
| 198 |
+
except Exception as e:
|
| 199 |
+
logger.error(f"Evaluation error: {str(e)}")
|
| 200 |
+
if status_container:
|
| 201 |
+
status_container.error(f"Oops! Something broke: {str(e)} 💥 (Titan tripped over a wire!)")
|
| 202 |
+
return f"Error: {str(e)}"
|
| 203 |
+
|
| 204 |
+
class DiffusionBuilder:
|
| 205 |
+
def __init__(self):
|
| 206 |
+
self.config = None
|
| 207 |
+
self.pipeline = None
|
| 208 |
+
def load_model(self, model_path: str, config: Optional[DiffusionConfig] = None):
|
| 209 |
+
from diffusers import StableDiffusionPipeline
|
| 210 |
+
try:
|
| 211 |
+
with st.spinner(f"Loading diffusion model {model_path}... ⏳"):
|
| 212 |
+
self.pipeline = StableDiffusionPipeline.from_pretrained(model_path)
|
| 213 |
+
self.pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 214 |
+
if config:
|
| 215 |
+
self.config = config
|
| 216 |
+
st.success(f"Diffusion model loaded! 🎨")
|
| 217 |
+
logger.info(f"Successfully loaded Diffusion model: {model_path}")
|
| 218 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 219 |
+
st.error(f"GPU memory error loading {model_path}: {str(e)} 💥 (Out of GPU juice!)")
|
| 220 |
+
logger.error(f"GPU memory error loading {model_path}: {str(e)}")
|
| 221 |
+
raise
|
| 222 |
+
except MemoryError as e:
|
| 223 |
+
st.error(f"CPU memory error loading {model_path}: {str(e)} 💥 (RAM ran away!)")
|
| 224 |
+
logger.error(f"CPU memory error loading {model_path}: {str(e)}")
|
| 225 |
+
raise
|
| 226 |
+
except Exception as e:
|
| 227 |
+
st.error(f"Failed to load {model_path}: {str(e)} 💥 (Something broke—check the logs!)")
|
| 228 |
+
logger.error(f"Failed to load {model_path}: {str(e)}")
|
| 229 |
+
raise
|
| 230 |
+
return self
|
| 231 |
+
def fine_tune_sft(self, images, texts, epochs=3):
|
| 232 |
+
try:
|
| 233 |
+
dataset = DiffusionDataset(images, texts)
|
| 234 |
+
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
|
| 235 |
+
optimizer = torch.optim.AdamW(self.pipeline.unet.parameters(), lr=1e-5)
|
| 236 |
+
self.pipeline.unet.train()
|
| 237 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 238 |
+
for epoch in range(epochs):
|
| 239 |
+
with st.spinner(f"Training diffusion epoch {epoch + 1}/{epochs}... ⚙️"):
|
| 240 |
+
total_loss = 0
|
| 241 |
+
for batch in dataloader:
|
| 242 |
+
optimizer.zero_grad()
|
| 243 |
+
image = batch["image"][0].to(device)
|
| 244 |
+
text = batch["text"][0]
|
| 245 |
+
latents = self.pipeline.vae.encode(torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0).float().to(device)).latent_dist.sample()
|
| 246 |
+
noise = torch.randn_like(latents)
|
| 247 |
+
timesteps = torch.randint(0, self.pipeline.scheduler.num_train_timesteps, (latents.shape[0],), device=latents.device)
|
| 248 |
+
noisy_latents = self.pipeline.scheduler.add_noise(latents, noise, timesteps)
|
| 249 |
+
text_embeddings = self.pipeline.text_encoder(self.pipeline.tokenizer(text, return_tensors="pt").input_ids.to(device))[0]
|
| 250 |
+
pred_noise = self.pipeline.unet(noisy_latents, timesteps, encoder_hidden_states=text_embeddings).sample
|
| 251 |
+
loss = torch.nn.functional.mse_loss(pred_noise, noise)
|
| 252 |
+
loss.backward()
|
| 253 |
+
optimizer.step()
|
| 254 |
+
total_loss += loss.item()
|
| 255 |
+
st.write(f"Epoch {epoch + 1} completed. Average loss: {total_loss / len(dataloader):.4f}")
|
| 256 |
+
st.success("Diffusion SFT Fine-tuning completed! 🎨")
|
| 257 |
+
logger.info(f"Successfully fine-tuned Diffusion model: {self.config.name}")
|
| 258 |
+
except Exception as e:
|
| 259 |
+
st.error(f"Fine-tuning failed: {str(e)} 💥 (Training hit a snag!)")
|
| 260 |
+
logger.error(f"Fine-tuning failed: {str(e)}")
|
| 261 |
+
raise
|
| 262 |
+
return self
|
| 263 |
+
def save_model(self, path: str):
|
| 264 |
+
try:
|
| 265 |
+
with st.spinner("Saving diffusion model... 💾"):
|
| 266 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 267 |
+
self.pipeline.save_pretrained(path)
|
| 268 |
+
st.success(f"Diffusion model saved at {path}! ✅")
|
| 269 |
+
logger.info(f"Diffusion model saved at {path}")
|
| 270 |
+
except Exception as e:
|
| 271 |
+
st.error(f"Failed to save model: {str(e)} 💥 (Save operation crashed!)")
|
| 272 |
+
logger.error(f"Failed to save model: {str(e)}")
|
| 273 |
+
raise
|
| 274 |
+
def generate(self, prompt: str):
|
| 275 |
+
try:
|
| 276 |
+
return self.pipeline(prompt, num_inference_steps=50).images[0]
|
| 277 |
+
except Exception as e:
|
| 278 |
+
st.error(f"Image generation failed: {str(e)} 💥 (Pixel party pooper!)")
|
| 279 |
+
logger.error(f"Image generation failed: {str(e)}")
|
| 280 |
+
raise
|
| 281 |
+
|
| 282 |
+
def generate_filename(sequence, ext="png"):
|
| 283 |
+
from datetime import datetime
|
| 284 |
+
import pytz
|
| 285 |
+
central = pytz.timezone('US/Central')
|
| 286 |
+
dt = datetime.now(central)
|
| 287 |
+
return f"{dt.strftime('%m-%d-%Y-%I-%M-%S-%p')}.{ext}"
|
| 288 |
+
|
| 289 |
+
def get_download_link(file_path, mime_type="text/plain", label="Download"):
|
| 290 |
+
try:
|
| 291 |
+
with open(file_path, 'rb') as f:
|
| 292 |
+
data = f.read()
|
| 293 |
+
b64 = base64.b64encode(data).decode()
|
| 294 |
+
return f'<a href="data:{mime_type};base64,{b64}" download="{os.path.basename(file_path)}">{label} 📥</a>'
|
| 295 |
+
except Exception as e:
|
| 296 |
+
logger.error(f"Failed to generate download link for {file_path}: {str(e)}")
|
| 297 |
+
return f"Error: Could not generate link for {file_path}"
|
| 298 |
+
|
| 299 |
+
def zip_files(files, zip_path):
|
| 300 |
+
try:
|
| 301 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 302 |
+
for file in files:
|
| 303 |
+
zipf.write(file, os.path.basename(file))
|
| 304 |
+
logger.info(f"Created ZIP file: {zip_path}")
|
| 305 |
+
except Exception as e:
|
| 306 |
+
logger.error(f"Failed to create ZIP file {zip_path}: {str(e)}")
|
| 307 |
+
raise
|
| 308 |
+
|
| 309 |
+
def delete_files(files):
|
| 310 |
+
try:
|
| 311 |
+
for file in files:
|
| 312 |
+
os.remove(file)
|
| 313 |
+
logger.info(f"Deleted file: {file}")
|
| 314 |
+
st.session_state['captured_images'] = [f for f in st.session_state['captured_images'] if f not in files]
|
| 315 |
+
except Exception as e:
|
| 316 |
+
logger.error(f"Failed to delete files: {str(e)}")
|
| 317 |
+
raise
|
| 318 |
+
|
| 319 |
+
def get_model_files(model_type="causal_lm"):
|
| 320 |
+
path = "models/*" if model_type == "causal_lm" else "diffusion_models/*"
|
| 321 |
+
return [d for d in glob.glob(path) if os.path.isdir(d)]
|
| 322 |
+
|
| 323 |
+
def get_gallery_files(file_types):
|
| 324 |
+
return sorted(list(set(f for ext in file_types for f in glob.glob(f"*.{ext}"))))
|
| 325 |
+
|
| 326 |
+
def update_gallery():
|
| 327 |
+
media_files = get_gallery_files(["png"])
|
| 328 |
+
if media_files:
|
| 329 |
+
cols = st.sidebar.columns(2)
|
| 330 |
+
for idx, file in enumerate(media_files[:gallery_size * 2]):
|
| 331 |
+
with cols[idx % 2]:
|
| 332 |
+
st.image(Image.open(file), caption=file, use_container_width=True)
|
| 333 |
+
st.markdown(get_download_link(file, "image/png", "Download Image"), unsafe_allow_html=True)
|
| 334 |
+
|
| 335 |
+
def get_available_devices():
|
| 336 |
+
video_devices = []
|
| 337 |
+
for i in range(10):
|
| 338 |
+
cap = cv2.VideoCapture(i)
|
| 339 |
+
if cap.isOpened():
|
| 340 |
+
video_devices.append(f"Camera {i}")
|
| 341 |
+
cap.release()
|
| 342 |
+
audio_devices = sd.query_devices()
|
| 343 |
+
audio_list = [f"{device['name']} (ID: {i})" for i, device in enumerate(audio_devices) if device['max_input_channels'] > 0]
|
| 344 |
+
return video_devices, audio_list
|
| 345 |
+
|
| 346 |
+
def mock_search(query: str) -> str:
|
| 347 |
+
if "superhero" in query.lower():
|
| 348 |
+
return "Latest trends: Gold-plated Batman statues, VR superhero battles."
|
| 349 |
+
return "No relevant results found."
|
| 350 |
+
|
| 351 |
+
class PartyPlannerAgent:
|
| 352 |
+
def __init__(self, model, tokenizer):
|
| 353 |
+
self.model = model
|
| 354 |
+
self.tokenizer = tokenizer
|
| 355 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 356 |
+
self.model.to(self.device)
|
| 357 |
+
def generate(self, prompt: str) -> str:
|
| 358 |
+
self.model.eval()
|
| 359 |
+
with torch.no_grad():
|
| 360 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True).to(self.device)
|
| 361 |
+
outputs = self.model.generate(**inputs, max_new_tokens=100, do_sample=True, top_p=0.95, temperature=0.7)
|
| 362 |
+
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 363 |
+
def plan_party(self, task: str) -> pd.DataFrame:
|
| 364 |
+
search_result = mock_search("superhero party trends")
|
| 365 |
+
prompt = f"Given this context: '{search_result}'\n{task}"
|
| 366 |
+
plan_text = self.generate(prompt)
|
| 367 |
+
locations = {"Wayne Manor": (42.3601, -71.0589), "New York": (40.7128, -74.0060)}
|
| 368 |
+
wayne_coords = locations["Wayne Manor"]
|
| 369 |
+
travel_times = {loc: calculate_cargo_travel_time(coords, wayne_coords) for loc, coords in locations.items() if loc != "Wayne Manor"}
|
| 370 |
+
data = [
|
| 371 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gold-plated Batman statues"},
|
| 372 |
+
{"Location": "Wayne Manor", "Travel Time (hrs)": 0.0, "Luxury Idea": "VR superhero battles"}
|
| 373 |
+
]
|
| 374 |
+
return pd.DataFrame(data)
|
| 375 |
+
|
| 376 |
+
class CVPartyPlannerAgent:
|
| 377 |
+
def __init__(self, pipeline):
|
| 378 |
+
self.pipeline = pipeline
|
| 379 |
+
def generate(self, prompt: str) -> Image.Image:
|
| 380 |
+
return self.pipeline(prompt, num_inference_steps=50).images[0]
|
| 381 |
+
def plan_party(self, task: str) -> pd.DataFrame:
|
| 382 |
+
search_result = mock_search("superhero party trends")
|
| 383 |
+
prompt = f"Given this context: '{search_result}'\n{task}"
|
| 384 |
+
data = [
|
| 385 |
+
{"Theme": "Batman", "Image Idea": "Gold-plated Batman statue"},
|
| 386 |
+
{"Theme": "Avengers", "Image Idea": "VR superhero battle scene"}
|
| 387 |
+
]
|
| 388 |
+
return pd.DataFrame(data)
|
| 389 |
+
|
| 390 |
+
def calculate_cargo_travel_time(origin_coords: Tuple[float, float], destination_coords: Tuple[float, float], cruising_speed_kmh: float = 750.0) -> float:
|
| 391 |
+
def to_radians(degrees: float) -> float:
|
| 392 |
+
return degrees * (math.pi / 180)
|
| 393 |
+
lat1, lon1 = map(to_radians, origin_coords)
|
| 394 |
+
lat2, lon2 = map(to_radians, destination_coords)
|
| 395 |
+
EARTH_RADIUS_KM = 6371.0
|
| 396 |
+
dlon = lon2 - lon1
|
| 397 |
+
dlat = lat2 - lat1
|
| 398 |
+
a = (math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2)
|
| 399 |
+
c = 2 * math.asin(math.sqrt(a))
|
| 400 |
+
distance = EARTH_RADIUS_KM * c
|
| 401 |
+
actual_distance = distance * 1.1
|
| 402 |
+
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 403 |
+
return round(flight_time, 2)
|
| 404 |
+
|
| 405 |
+
st.title("SFT Tiny Titans 🚀 (Small but Mighty!)")
|
| 406 |
+
|
| 407 |
+
st.sidebar.header("Media Gallery 🎨")
|
| 408 |
+
gallery_size = st.sidebar.slider("Gallery Size 📸", 1, 10, 4, help="Adjust how many epic captures you see! 🌟")
|
| 409 |
+
update_gallery()
|
| 410 |
+
|
| 411 |
+
col1, col2 = st.sidebar.columns(2)
|
| 412 |
+
with col1:
|
| 413 |
+
if st.button("Download All 📦"):
|
| 414 |
+
media_files = get_gallery_files(["png"])
|
| 415 |
+
if media_files:
|
| 416 |
+
zip_path = f"snapshot_collection_{int(time.time())}.zip"
|
| 417 |
+
zip_files(media_files, zip_path)
|
| 418 |
+
st.sidebar.markdown(get_download_link(zip_path, "application/zip", "Download All Snapshots"), unsafe_allow_html=True)
|
| 419 |
+
st.sidebar.success("Snapshots zipped and ready! 🎉 Grab your loot!")
|
| 420 |
+
else:
|
| 421 |
+
st.sidebar.warning("No snapshots to zip! 📸 Snap some pics first!")
|
| 422 |
+
with col2:
|
| 423 |
+
if st.button("Delete All 🗑️"):
|
| 424 |
+
media_files = get_gallery_files(["png"])
|
| 425 |
+
if media_files:
|
| 426 |
+
delete_files(media_files)
|
| 427 |
+
st.sidebar.success("All snapshots vanquished! 🧹 Gallery cleared!")
|
| 428 |
+
update_gallery()
|
| 429 |
+
else:
|
| 430 |
+
st.sidebar.warning("Nothing to delete! 📸 Snap some pics to clear later!")
|
| 431 |
+
|
| 432 |
+
uploaded_files = st.sidebar.file_uploader("Upload Files 🎵🎥🖼️📝��", type=["mp3", "mp4", "png", "jpeg", "md", "pdf", "docx"], accept_multiple_files=True)
|
| 433 |
+
if uploaded_files:
|
| 434 |
+
for uploaded_file in uploaded_files:
|
| 435 |
+
filename = uploaded_file.name
|
| 436 |
+
with open(filename, "wb") as f:
|
| 437 |
+
f.write(uploaded_file.getvalue())
|
| 438 |
+
logger.info(f"Uploaded file: {filename}")
|
| 439 |
+
|
| 440 |
+
st.sidebar.subheader("Audio Gallery 🎵")
|
| 441 |
+
audio_files = get_gallery_files(["mp3"])
|
| 442 |
+
if audio_files:
|
| 443 |
+
for file in audio_files[:gallery_size]:
|
| 444 |
+
st.sidebar.audio(file, format="audio/mp3")
|
| 445 |
+
st.sidebar.markdown(get_download_link(file, "audio/mp3", f"Download {file}"), unsafe_allow_html=True)
|
| 446 |
+
|
| 447 |
+
st.sidebar.subheader("Video Gallery 🎥")
|
| 448 |
+
video_files = get_gallery_files(["mp4"])
|
| 449 |
+
if video_files:
|
| 450 |
+
for file in video_files[:gallery_size]:
|
| 451 |
+
st.sidebar.video(file, format="video/mp4")
|
| 452 |
+
st.sidebar.markdown(get_download_link(file, "video/mp4", f"Download {file}"), unsafe_allow_html=True)
|
| 453 |
+
|
| 454 |
+
st.sidebar.subheader("Image Gallery 🖼️")
|
| 455 |
+
image_files = get_gallery_files(["png", "jpeg"])
|
| 456 |
+
if image_files:
|
| 457 |
+
cols = st.sidebar.columns(2)
|
| 458 |
+
for idx, file in enumerate(image_files[:gallery_size * 2]):
|
| 459 |
+
with cols[idx % 2]:
|
| 460 |
+
st.image(Image.open(file), caption=file, use_container_width=True)
|
| 461 |
+
st.markdown(get_download_link(file, "image/png" if file.endswith(".png") else "image/jpeg", f"Download {file}"), unsafe_allow_html=True)
|
| 462 |
+
|
| 463 |
+
st.sidebar.subheader("Markdown Gallery 📝")
|
| 464 |
+
md_files = get_gallery_files(["md"])
|
| 465 |
+
if md_files:
|
| 466 |
+
for file in md_files[:gallery_size]:
|
| 467 |
+
with open(file, "r") as f:
|
| 468 |
+
st.sidebar.markdown(f.read())
|
| 469 |
+
st.sidebar.markdown(get_download_link(file, "text/markdown", f"Download {file}"), unsafe_allow_html=True)
|
| 470 |
+
|
| 471 |
+
st.sidebar.subheader("Document Gallery 📜")
|
| 472 |
+
doc_files = get_gallery_files(["pdf", "docx"])
|
| 473 |
+
if doc_files:
|
| 474 |
+
for file in doc_files[:gallery_size]:
|
| 475 |
+
mime_type = "application/pdf" if file.endswith(".pdf") else "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 476 |
+
st.sidebar.markdown(get_download_link(file, mime_type, f"Download {file}"), unsafe_allow_html=True)
|
| 477 |
+
|
| 478 |
+
st.sidebar.subheader("Model Management 🗂️")
|
| 479 |
+
model_type = st.sidebar.selectbox("Model Type", ["Causal LM", "Diffusion"])
|
| 480 |
+
model_dirs = get_model_files("causal_lm" if model_type == "Causal LM" else "diffusion")
|
| 481 |
+
selected_model = st.sidebar.selectbox("Select Saved Model", ["None"] + model_dirs)
|
| 482 |
+
if selected_model != "None" and st.sidebar.button("Load Model 📂"):
|
| 483 |
+
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 484 |
+
config = (ModelConfig if model_type == "Causal LM" else DiffusionConfig)(name=os.path.basename(selected_model), base_model="unknown", size="small")
|
| 485 |
+
try:
|
| 486 |
+
builder.load_model(selected_model, config)
|
| 487 |
+
if model_type == "Causal LM":
|
| 488 |
+
st.session_state['nlp_builder'] = builder
|
| 489 |
+
st.session_state['nlp_loaded'] = True
|
| 490 |
+
else:
|
| 491 |
+
st.session_state['cv_builder'] = builder
|
| 492 |
+
st.session_state['cv_loaded'] = True
|
| 493 |
+
st.rerun()
|
| 494 |
+
except Exception as e:
|
| 495 |
+
st.error(f"Model load failed: {str(e)} 💥 (Check logs for details!)")
|
| 496 |
+
|
| 497 |
+
st.sidebar.subheader("Model Status 🚦")
|
| 498 |
+
st.sidebar.write(f"**NLP Model**: {'Loaded' if st.session_state['nlp_loaded'] else 'Not Loaded'} {'(Active)' if st.session_state['nlp_loaded'] and isinstance(st.session_state.get('nlp_builder'), ModelBuilder) else ''}")
|
| 499 |
+
st.sidebar.write(f"**CV Model**: {'Loaded' if st.session_state['cv_loaded'] else 'Not Loaded'} {'(Active)' if st.session_state['cv_loaded'] and isinstance(st.session_state.get('cv_builder'), DiffusionBuilder) else ''}")
|
| 500 |
+
|
| 501 |
+
tabs = [
|
| 502 |
+
"Build Titan 🌱", "Camera Snap 📷",
|
| 503 |
+
"Fine-Tune Titan (NLP) 🔧", "Test Titan (NLP) 🧪", "Agentic RAG Party (NLP) 🌐",
|
| 504 |
+
"Fine-Tune Titan (CV) 🔧", "Test Titan (CV) 🧪", "Agentic RAG Party (CV) 🌐"
|
| 505 |
+
]
|
| 506 |
+
tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8 = st.tabs(tabs)
|
| 507 |
+
|
| 508 |
+
for i, tab in enumerate(tabs):
|
| 509 |
+
if st.session_state['active_tab'] != tab and st.session_state.get(f'tab{i}_active', False):
|
| 510 |
+
logger.info(f"Switched to tab: {tab}")
|
| 511 |
+
st.session_state['active_tab'] = tab
|
| 512 |
+
st.session_state[f'tab{i}_active'] = (st.session_state['active_tab'] == tab)
|
| 513 |
+
|
| 514 |
+
with tab1:
|
| 515 |
+
st.header("Build Titan 🌱")
|
| 516 |
+
model_type = st.selectbox("Model Type", ["Causal LM", "Diffusion"], key="build_type")
|
| 517 |
+
base_model = st.selectbox("Select Tiny Model",
|
| 518 |
+
["HuggingFaceTB/SmolLM-135M", "HuggingFaceTB/SmolLM-360M", "Qwen/Qwen1.5-0.5B-Chat"] if model_type == "Causal LM" else
|
| 519 |
+
["stabilityai/stable-diffusion-2-base", "runwayml/stable-diffusion-v1-5"])
|
| 520 |
+
model_name = st.text_input("Model Name", f"tiny-titan-{int(time.time())}")
|
| 521 |
+
domain = st.text_input("Target Domain", "general", help="Where will your Titan flex its muscles? 💪") if model_type == "Causal LM" else None
|
| 522 |
+
if st.button("Download Model ⬇️"):
|
| 523 |
+
config = ModelConfig(name=model_name, base_model=base_model, size="small", domain=domain) if model_type == "Causal LM" else DiffusionConfig(name=model_name, base_model=base_model, size="small")
|
| 524 |
+
builder = ModelBuilder() if model_type == "Causal LM" else DiffusionBuilder()
|
| 525 |
+
try:
|
| 526 |
+
builder.load_model(base_model, config)
|
| 527 |
+
builder.save_model(config.model_path)
|
| 528 |
+
if model_type == "Causal LM":
|
| 529 |
+
st.session_state['nlp_builder'] = builder
|
| 530 |
+
st.session_state['nlp_loaded'] = True
|
| 531 |
+
else:
|
| 532 |
+
st.session_state['cv_builder'] = builder
|
| 533 |
+
st.session_state['cv_loaded'] = True
|
| 534 |
+
st.rerun()
|
| 535 |
+
except Exception as e:
|
| 536 |
+
st.error(f"Model build failed: {str(e)} 💥 (Check logs for details!)")
|
| 537 |
+
|
| 538 |
+
with tab2:
|
| 539 |
+
st.header("Camera Snap 📷 (Dual Capture!)")
|
| 540 |
+
video_devices, audio_devices = get_available_devices()
|
| 541 |
+
st.subheader("Device Settings ⚙️")
|
| 542 |
+
if not video_devices:
|
| 543 |
+
st.warning("No video devices detected! 📷 Please connect a camera.")
|
| 544 |
+
else:
|
| 545 |
+
st.write(f"Detected Video Devices: {', '.join(video_devices)}")
|
| 546 |
+
if not audio_devices:
|
| 547 |
+
st.warning("No audio input devices detected! 🎙️ Please connect a microphone.")
|
| 548 |
+
else:
|
| 549 |
+
st.write(f"Detected Audio Devices: {', '.join(audio_devices)}")
|
| 550 |
+
default_cam0 = video_devices[0] if video_devices else None
|
| 551 |
+
default_cam1 = video_devices[1] if len(video_devices) > 1 else default_cam0
|
| 552 |
+
selected_cam0 = st.selectbox("Select Camera 0", video_devices, index=0 if video_devices else -1, key="cam0_select")
|
| 553 |
+
selected_cam1 = st.selectbox("Select Camera 1", video_devices, index=1 if len(video_devices) > 1 else 0, key="cam1_select")
|
| 554 |
+
selected_audio = st.selectbox("Select Audio Device", audio_devices, key="audio_select") if audio_devices else st.write("No audio devices available.")
|
| 555 |
+
slice_count = st.number_input("Image Slice Count 🎞️", min_value=1, max_value=20, value=10, help="How many snaps to dream of? (Automation’s on vacation! 😜)")
|
| 556 |
+
video_length = st.number_input("Video Dream Length (seconds) 🎥", min_value=1, max_value=30, value=10, help="Imagine a vid this long—sadly, we’re stuck with pics for now! 😂")
|
| 557 |
+
cols = st.columns(2)
|
| 558 |
+
with cols[0]:
|
| 559 |
+
st.subheader(f"Camera 0 ({selected_cam0}) 🎬")
|
| 560 |
+
cam0_img = st.camera_input("Snap a Shot - Cam 0 📸", key="cam0", help="Click to capture a heroic moment! 🦸♂️")
|
| 561 |
+
if cam0_img:
|
| 562 |
+
filename = generate_filename(0)
|
| 563 |
+
with open(filename, "wb") as f:
|
| 564 |
+
f.write(cam0_img.getvalue())
|
| 565 |
+
st.image(Image.open(filename), caption=filename, use_container_width=True)
|
| 566 |
+
logger.info(f"Saved snapshot from Camera 0: {filename}")
|
| 567 |
+
st.session_state['captured_images'].append(filename)
|
| 568 |
+
update_gallery()
|
| 569 |
+
st.info("🚨 Multi-frame capture’s on strike! Snap one at a time—your Titan’s too cool for automation glitches! 😎")
|
| 570 |
+
with cols[1]:
|
| 571 |
+
st.subheader(f"Camera 1 ({selected_cam1}) 🎥")
|
| 572 |
+
cam1_img = st.camera_input("Snap a Shot - Cam 1 📸", key="cam1", help="Grab another epic frame! 🌟")
|
| 573 |
+
if cam1_img:
|
| 574 |
+
filename = generate_filename(1)
|
| 575 |
+
with open(filename, "wb") as f:
|
| 576 |
+
f.write(cam1_img.getvalue())
|
| 577 |
+
st.image(Image.open(filename), caption=filename, use_container_width=True)
|
| 578 |
+
logger.info(f"Saved snapshot from Camera 1: {filename}")
|
| 579 |
+
st.session_state['captured_images'].append(filename)
|
| 580 |
+
update_gallery()
|
| 581 |
+
st.info("🚨 Frame bursts? Nope, manual snaps only! One click, one masterpiece! 🎨")
|
| 582 |
+
|
| 583 |
+
with tab3:
|
| 584 |
+
st.header("Fine-Tune Titan (NLP) 🔧 (Teach Your Word Wizard Some Tricks!)")
|
| 585 |
+
if not st.session_state['nlp_loaded'] or not isinstance(st.session_state['nlp_builder'], ModelBuilder):
|
| 586 |
+
st.warning("Please build or load an NLP Titan first! ⚠️ (No word wizard, no magic!)")
|
| 587 |
+
else:
|
| 588 |
+
if st.button("Generate Sample CSV 📝"):
|
| 589 |
+
sample_data = [
|
| 590 |
+
{"prompt": "What is AI?", "response": "AI is artificial intelligence, simulating human smarts in machines."},
|
| 591 |
+
{"prompt": "Explain machine learning", "response": "Machine learning is AI’s gym where models bulk up on data."},
|
| 592 |
+
{"prompt": "What is a neural network?", "response": "A neural network is a brainy AI mimicking human noggins."},
|
| 593 |
+
]
|
| 594 |
+
csv_path = f"sft_data_{int(time.time())}.csv"
|
| 595 |
+
with open(csv_path, "w", newline="") as f:
|
| 596 |
+
writer = csv.DictWriter(f, fieldnames=["prompt", "response"])
|
| 597 |
+
writer.writeheader()
|
| 598 |
+
writer.writerows(sample_data)
|
| 599 |
+
st.markdown(get_download_link(csv_path, "text/csv", "Download Sample CSV"), unsafe_allow_html=True)
|
| 600 |
+
st.success(f"Sample CSV generated as {csv_path}! ✅ (Fresh from the data oven!)")
|
| 601 |
+
uploaded_csv = st.file_uploader("Upload CSV for SFT 📜", type="csv", help="Feed your Titan some tasty prompt-response pairs! 🍽️")
|
| 602 |
+
if uploaded_csv and st.button("Fine-Tune with Uploaded CSV 🔄"):
|
| 603 |
+
csv_path = f"uploaded_sft_data_{int(time.time())}.csv"
|
| 604 |
+
with open(csv_path, "wb") as f:
|
| 605 |
+
f.write(uploaded_csv.read())
|
| 606 |
+
new_model_name = f"{st.session_state['nlp_builder'].config.name}-sft-{int(time.time())}"
|
| 607 |
+
new_config = ModelConfig(name=new_model_name, base_model=st.session_state['nlp_builder'].config.base_model, size="small", domain=st.session_state['nlp_builder'].config.domain)
|
| 608 |
+
st.session_state['nlp_builder'].config = new_config
|
| 609 |
+
with st.status("Fine-tuning NLP Titan... ⏳ (Whipping words into shape!)", expanded=True) as status:
|
| 610 |
+
st.session_state['nlp_builder'].fine_tune_sft(csv_path)
|
| 611 |
+
st.session_state['nlp_builder'].save_model(new_config.model_path)
|
| 612 |
+
status.update(label="Fine-tuning completed! 🎉 (Wordsmith Titan unleashed!)", state="complete")
|
| 613 |
+
zip_path = f"{new_config.model_path}.zip"
|
| 614 |
+
zip_files([new_config.model_path], zip_path)
|
| 615 |
+
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned NLP Titan"), unsafe_allow_html=True)
|
| 616 |
+
|
| 617 |
+
with tab4:
|
| 618 |
+
st.header("Test Titan (NLP) 🧪 (Put Your Word Wizard to the Test!)")
|
| 619 |
+
if not st.session_state['nlp_loaded'] or not isinstance(st.session_state['nlp_builder'], ModelBuilder):
|
| 620 |
+
st.warning("Please build or load an NLP Titan first! ⚠️ (No word wizard, no test drive!)")
|
| 621 |
+
else:
|
| 622 |
+
if st.session_state['nlp_builder'].sft_data:
|
| 623 |
+
st.write("Testing with SFT Data:")
|
| 624 |
+
with st.spinner("Running SFT data tests... ⏳ (Titan’s flexing its word muscles!)"):
|
| 625 |
+
for item in st.session_state['nlp_builder'].sft_data[:3]:
|
| 626 |
+
prompt = item["prompt"]
|
| 627 |
+
expected = item["response"]
|
| 628 |
+
status_container = st.empty()
|
| 629 |
+
generated = st.session_state['nlp_builder'].evaluate(prompt, status_container)
|
| 630 |
+
st.write(f"**Prompt**: {prompt}")
|
| 631 |
+
st.write(f"**Expected**: {expected}")
|
| 632 |
+
st.write(f"**Generated**: {generated} (Titan says: '{random.choice(['Bleep bloop!', 'I am groot!', '42!'])}')")
|
| 633 |
+
st.write("---")
|
| 634 |
+
status_container.empty()
|
| 635 |
+
test_prompt = st.text_area("Enter Test Prompt 🗣️", "What is AI?", help="Ask your Titan anything—it’s ready to chat! 😜")
|
| 636 |
+
if st.button("Run Test ▶️"):
|
| 637 |
+
with st.spinner("Testing your prompt... ⏳ (Titan’s pondering deeply!)"):
|
| 638 |
+
status_container = st.empty()
|
| 639 |
+
result = st.session_state['nlp_builder'].evaluate(test_prompt, status_container)
|
| 640 |
+
st.write(f"**Generated Response**: {result} (Titan’s wisdom unleashed!)")
|
| 641 |
+
status_container.empty()
|
| 642 |
+
|
| 643 |
+
with tab5:
|
| 644 |
+
st.header("Agentic RAG Party (NLP) 🌐 (Party Like It’s 2099!)")
|
| 645 |
+
st.write("This demo uses your SFT-tuned NLP Titan to plan a superhero party with mock retrieval!")
|
| 646 |
+
if not st.session_state['nlp_loaded'] or not isinstance(st.session_state['nlp_builder'], ModelBuilder):
|
| 647 |
+
st.warning("Please build or load an NLP Titan first! ⚠️ (No word wizard, no party!)")
|
| 648 |
+
else:
|
| 649 |
+
if st.button("Run NLP RAG Demo 🎉"):
|
| 650 |
+
with st.spinner("Loading your SFT-tuned NLP Titan... ⏳ (Titan’s suiting up!)"):
|
| 651 |
+
agent = PartyPlannerAgent(st.session_state['nlp_builder'].model, st.session_state['nlp_builder'].tokenizer)
|
| 652 |
+
st.write("Agent ready! 🦸♂️ (Time to plan an epic bash!)")
|
| 653 |
+
task = """
|
| 654 |
+
Plan a luxury superhero-themed party at Wayne Manor (42.3601° N, 71.0589° W).
|
| 655 |
+
Use mock search results for the latest superhero party trends, refine for luxury elements
|
| 656 |
+
(decorations, entertainment, catering), and calculate cargo travel times from key locations
|
| 657 |
+
(New York: 40.7128° N, 74.0060° W; LA: 34.0522° N, 118.2437° W; London: 51.5074° N, 0.1278° W)
|
| 658 |
+
to Wayne Manor. Create a plan with at least 6 entries in a pandas dataframe.
|
| 659 |
+
"""
|
| 660 |
+
with st.spinner("Planning the ultimate superhero bash... ⏳ (Calling all caped crusaders!)"):
|
| 661 |
+
try:
|
| 662 |
+
locations = {
|
| 663 |
+
"Wayne Manor": (42.3601, -71.0589),
|
| 664 |
+
"New York": (40.7128, -74.0060),
|
| 665 |
+
"Los Angeles": (34.0522, -118.2437),
|
| 666 |
+
"London": (51.5074, -0.1278)
|
| 667 |
+
}
|
| 668 |
+
wayne_coords = locations["Wayne Manor"]
|
| 669 |
+
travel_times = {loc: calculate_cargo_travel_time(coords, wayne_coords) for loc, coords in locations.items() if loc != "Wayne Manor"}
|
| 670 |
+
search_result = mock_search("superhero party trends")
|
| 671 |
+
prompt = f"""
|
| 672 |
+
Given this context from a search: "{search_result}"
|
| 673 |
+
Plan a luxury superhero-themed party at Wayne Manor. Suggest luxury decorations, entertainment, and catering ideas.
|
| 674 |
+
"""
|
| 675 |
+
plan_text = agent.generate(prompt)
|
| 676 |
+
catchphrases = ["To the Batmobile!", "Avengers, assemble!", "I am Iron Man!", "By the power of Grayskull!"]
|
| 677 |
+
data = [
|
| 678 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gold-plated Batman statues", "Catchphrase": random.choice(catchphrases)},
|
| 679 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Holographic Avengers displays", "Catchphrase": random.choice(catchphrases)},
|
| 680 |
+
{"Location": "London", "Travel Time (hrs)": travel_times["London"], "Luxury Idea": "Live stunt shows with Iron Man suits", "Catchphrase": random.choice(catchphrases)},
|
| 681 |
+
{"Location": "Wayne Manor", "Travel Time (hrs)": 0.0, "Luxury Idea": "VR superhero battles", "Catchphrase": random.choice(catchphrases)},
|
| 682 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gourmet kryptonite-green cocktails", "Catchphrase": random.choice(catchphrases)},
|
| 683 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Thor’s hammer-shaped appetizers", "Catchphrase": random.choice(catchphrases)},
|
| 684 |
+
]
|
| 685 |
+
plan_df = pd.DataFrame(data)
|
| 686 |
+
st.write("Agentic RAG Party Plan:")
|
| 687 |
+
st.dataframe(plan_df)
|
| 688 |
+
st.write("Party on, Wayne! 🦸♂️🎉")
|
| 689 |
+
except Exception as e:
|
| 690 |
+
st.error(f"Error planning party: {str(e)} (Even Superman has kryptonite days!)")
|
| 691 |
+
logger.error(f"Error in NLP RAG demo: {str(e)}")
|
| 692 |
+
|
| 693 |
+
with tab6:
|
| 694 |
+
st.header("Fine-Tune Titan (CV) 🔧 (Paint Your Titan’s Masterpiece!)")
|
| 695 |
+
if not st.session_state['cv_loaded'] or not isinstance(st.session_state['cv_builder'], DiffusionBuilder):
|
| 696 |
+
st.warning("Please build or load a CV Titan first! ⚠️ (No artist, no canvas!)")
|
| 697 |
+
else:
|
| 698 |
+
captured_images = get_gallery_files(["png"])
|
| 699 |
+
if len(captured_images) >= 2:
|
| 700 |
+
demo_data = [{"image": img, "text": f"Superhero {os.path.basename(img).split('.')[0]}"} for img in captured_images[:min(len(captured_images), 10)]]
|
| 701 |
+
edited_data = st.data_editor(pd.DataFrame(demo_data), num_rows="dynamic", help="Craft your image-text pairs like a superhero artist! 🎨")
|
| 702 |
+
if st.button("Fine-Tune with Dataset 🔄"):
|
| 703 |
+
images = [Image.open(row["image"]) for _, row in edited_data.iterrows()]
|
| 704 |
+
texts = [row["text"] for _, row in edited_data.iterrows()]
|
| 705 |
+
new_model_name = f"{st.session_state['cv_builder'].config.name}-sft-{int(time.time())}"
|
| 706 |
+
new_config = DiffusionConfig(name=new_model_name, base_model=st.session_state['cv_builder'].config.base_model, size="small")
|
| 707 |
+
st.session_state['cv_builder'].config = new_config
|
| 708 |
+
with st.status("Fine-tuning CV Titan... ⏳ (Brushing up those pixels!)", expanded=True) as status:
|
| 709 |
+
st.session_state['cv_builder'].fine_tune_sft(images, texts)
|
| 710 |
+
st.session_state['cv_builder'].save_model(new_config.model_path)
|
| 711 |
+
status.update(label="Fine-tuning completed! 🎉 (Pixel Titan unleashed!)", state="complete")
|
| 712 |
+
zip_path = f"{new_config.model_path}.zip"
|
| 713 |
+
zip_files([new_config.model_path], zip_path)
|
| 714 |
+
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned CV Titan"), unsafe_allow_html=True)
|
| 715 |
+
csv_path = f"sft_dataset_{int(time.time())}.csv"
|
| 716 |
+
with open(csv_path, "w", newline="") as f:
|
| 717 |
+
writer = csv.writer(f)
|
| 718 |
+
writer.writerow(["image", "text"])
|
| 719 |
+
for _, row in edited_data.iterrows():
|
| 720 |
+
writer.writerow([row["image"], row["text"]])
|
| 721 |
+
st.markdown(get_download_link(csv_path, "text/csv", "Download SFT Dataset CSV"), unsafe_allow_html=True)
|
| 722 |
+
|
| 723 |
+
with tab7:
|
| 724 |
+
st.header("Test Titan (CV) 🧪 (Unleash Your Pixel Power!)")
|
| 725 |
+
if not st.session_state['cv_loaded'] or not isinstance(st.session_state['cv_builder'], DiffusionBuilder):
|
| 726 |
+
st.warning("Please build or load a CV Titan first! ⚠️ (No artist, no masterpiece!)")
|
| 727 |
+
else:
|
| 728 |
+
test_prompt = st.text_area("Enter Test Prompt 🎨", "Neon Batman", help="Dream up a wild image—your Titan’s got the brush! 🖌️")
|
| 729 |
+
if st.button("Run Test ▶️"):
|
| 730 |
+
with st.spinner("Painting your masterpiece... ⏳ (Titan’s mixing colors!)"):
|
| 731 |
+
image = st.session_state['cv_builder'].generate(test_prompt)
|
| 732 |
+
st.image(image, caption="Generated Image", use_container_width=True)
|
| 733 |
+
|
| 734 |
+
with tab8:
|
| 735 |
+
st.header("Agentic RAG Party (CV) 🌐 (Party with Pixels!)")
|
| 736 |
+
st.write("This demo uses your SFT-tuned CV Titan to generate superhero party images with mock retrieval!")
|
| 737 |
+
if not st.session_state['cv_loaded'] or not isinstance(st.session_state['cv_builder'], DiffusionBuilder):
|
| 738 |
+
st.warning("Please build or load a CV Titan first! ⚠️ (No artist, no party!)")
|
| 739 |
+
else:
|
| 740 |
+
if st.button("Run CV RAG Demo 🎉"):
|
| 741 |
+
with st.spinner("Loading your SFT-tuned CV Titan... ⏳ (Titan’s grabbing its paintbrush!)"):
|
| 742 |
+
agent = CVPartyPlannerAgent(st.session_state['cv_builder'].pipeline)
|
| 743 |
+
st.write("Agent ready! 🎨 (Time to paint an epic bash!)")
|
| 744 |
+
task = "Generate images for a luxury superhero-themed party."
|
| 745 |
+
with st.spinner("Crafting superhero party visuals... ⏳ (Pixels assemble!)"):
|
| 746 |
+
try:
|
| 747 |
+
plan_df = agent.plan_party(task)
|
| 748 |
+
st.dataframe(plan_df)
|
| 749 |
+
for _, row in plan_df.iterrows():
|
| 750 |
+
image = agent.generate(row["Image Idea"])
|
| 751 |
+
st.image(image, caption=f"{row['Theme']} - {row['Image Idea']}", use_container_width=True)
|
| 752 |
+
except Exception as e:
|
| 753 |
+
st.error(f"Error in CV RAG demo: {str(e)} 💥 (Pixel party crashed!)")
|
| 754 |
+
logger.error(f"Error in CV RAG demo: {str(e)}")
|
| 755 |
+
|
| 756 |
+
st.sidebar.subheader("Action Logs 📜")
|
| 757 |
+
log_container = st.sidebar.empty()
|
| 758 |
+
with log_container:
|
| 759 |
+
for record in log_records:
|
| 760 |
+
st.write(f"{record.asctime} - {record.levelname} - {record.message}")
|
| 761 |
+
|
| 762 |
+
update_gallery()
|