awacke1 commited on
Commit
1a2a001
Β·
verified Β·
1 Parent(s): 191766c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -117
app.py CHANGED
@@ -1,4 +1,7 @@
1
  #!/usr/bin/env python3
 
 
 
2
  import os
3
  import glob
4
  import time
@@ -17,252 +20,277 @@ from dataclasses import dataclass
17
  from typing import Optional
18
  import gradio as gr
19
 
 
20
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
21
  logger = logging.getLogger(__name__)
22
- log_records = []
23
 
 
24
  class LogCaptureHandler(logging.Handler):
 
25
  def emit(self, record):
26
  log_records.append(record)
27
 
28
- logger.addHandler(LogCaptureHandler())
29
 
 
30
  @dataclass
31
  class ModelConfig:
32
- name: str
33
- base_model: str
34
- size: str
35
- domain: Optional[str] = None
36
- model_type: str = "causal_lm"
 
37
  @property
38
  def model_path(self):
39
  return f"models/{self.name}"
40
 
 
41
  @dataclass
42
  class DiffusionConfig:
43
- name: str
44
- base_model: str
45
- size: str
46
- domain: Optional[str] = None
 
47
  @property
48
  def model_path(self):
49
  return f"diffusion_models/{self.name}"
50
 
 
51
  class ModelBuilder:
 
52
  def __init__(self):
53
- self.config = None
54
- self.model = None
55
- self.tokenizer = None
 
56
  def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
57
- self.model = AutoModelForCausalLM.from_pretrained(model_path)
58
- self.tokenizer = AutoTokenizer.from_pretrained(model_path)
59
  if self.tokenizer.pad_token is None:
60
- self.tokenizer.pad_token = self.tokenizer.eos_token
61
  if config:
62
- self.config = config
63
- self.model.to("cuda" if torch.cuda.is_available() else "cpu")
64
  return self
 
65
  def save_model(self, path: str):
66
- os.makedirs(os.path.dirname(path), exist_ok=True)
67
- self.model.save_pretrained(path)
68
- self.tokenizer.save_pretrained(path)
69
 
 
70
  class DiffusionBuilder:
 
71
  def __init__(self):
72
- self.config = None
73
- self.pipeline = None
 
74
  def load_model(self, model_path: str, config: Optional[DiffusionConfig] = None):
75
- self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu")
76
  if config:
77
- self.config = config
78
  return self
 
79
  def save_model(self, path: str):
80
- os.makedirs(os.path.dirname(path), exist_ok=True)
81
- self.pipeline.save_pretrained(path)
 
82
  def generate(self, prompt: str):
83
- return self.pipeline(prompt, num_inference_steps=20).images[0]
84
 
 
85
  def generate_filename(sequence, ext):
86
- timestamp = time.strftime("%d%m%Y%H%M%S")
87
  return f"{sequence}_{timestamp}.{ext}"
88
 
 
89
  def get_gallery_files(file_types):
90
- return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")]))) # Deduplicate files
91
 
 
92
  async def process_image_gen(prompt, output_file, builder):
93
  if builder and isinstance(builder, DiffusionBuilder) and builder.pipeline:
94
- pipeline = builder.pipeline
95
  else:
96
- pipeline = StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu")
97
- gen_image = pipeline(prompt, num_inference_steps=20).images[0]
98
- gen_image.save(output_file)
99
  return gen_image
100
 
101
- # Upload Functions
102
  def upload_images(files, history, selected_files):
103
  if not files:
104
- return "No files uploaded", history, selected_files
105
  uploaded = []
106
  for file in files:
107
- ext = file.name.split('.')[-1].lower()
108
  if ext in ["jpg", "png"]:
109
- output_path = f"img_{int(time.time())}_{os.path.basename(file.name)}"
110
  with open(output_path, "wb") as f:
111
- f.write(file.read())
112
  uploaded.append(output_path)
113
- history.append(f"Uploaded Image: {output_path}")
114
- selected_files[output_path] = False
115
  return f"Uploaded {len(uploaded)} images", history, selected_files
116
 
 
117
  def upload_videos(files, history, selected_files):
118
  if not files:
119
- return "No files uploaded", history, selected_files
120
  uploaded = []
121
  for file in files:
122
- ext = file.name.split('.')[-1].lower()
123
  if ext == "mp4":
124
- output_path = f"vid_{int(time.time())}_{os.path.basename(file.name)}"
125
  with open(output_path, "wb") as f:
126
- f.write(file.read())
127
  uploaded.append(output_path)
128
- history.append(f"Uploaded Video: {output_path}")
129
- selected_files[output_path] = False
130
  return f"Uploaded {len(uploaded)} videos", history, selected_files
131
 
 
132
  def upload_documents(files, history, selected_files):
133
  if not files:
134
- return "No files uploaded", history, selected_files
135
  uploaded = []
136
  for file in files:
137
- ext = file.name.split('.')[-1].lower()
138
  if ext in ["md", "pdf", "docx"]:
139
- output_path = f"doc_{int(time.time())}_{os.path.basename(file.name)}"
140
  with open(output_path, "wb") as f:
141
- f.write(file.read())
142
  uploaded.append(output_path)
143
- history.append(f"Uploaded Document: {output_path}")
144
- selected_files[output_path] = False
145
  return f"Uploaded {len(uploaded)} documents", history, selected_files
146
 
 
147
  def upload_datasets(files, history, selected_files):
148
  if not files:
149
- return "No files uploaded", history, selected_files
150
  uploaded = []
151
  for file in files:
152
- ext = file.name.split('.')[-1].lower()
153
  if ext in ["csv", "xlsx"]:
154
- output_path = f"data_{int(time.time())}_{os.path.basename(file.name)}"
155
  with open(output_path, "wb") as f:
156
- f.write(file.read())
157
  uploaded.append(output_path)
158
- history.append(f"Uploaded Dataset: {output_path}")
159
- selected_files[output_path] = False
160
  return f"Uploaded {len(uploaded)} datasets", history, selected_files
161
 
 
162
  def upload_links(links_title, links_url, history, selected_files):
163
  if not links_title or not links_url:
164
- return "No links provided", history, selected_files
165
- links = list(zip(links_title.split('\n'), links_url.split('\n')))
166
  uploaded = []
167
  for title, url in links:
168
  if title and url:
169
- link_entry = f"[{title}]({url})"
170
  uploaded.append(link_entry)
171
- history.append(f"Added Link: {link_entry}")
172
- selected_files[link_entry] = False
173
  return f"Added {len(uploaded)} links", history, selected_files
174
 
175
- # Gallery Update
176
  def update_galleries(history, selected_files):
177
  galleries = {
178
- "images": get_gallery_files(["jpg", "png"]),
179
- "videos": get_gallery_files(["mp4"]),
180
- "documents": get_gallery_files(["md", "pdf", "docx"]),
181
- "datasets": get_gallery_files(["csv", "xlsx"]),
182
- "links": [f for f in selected_files.keys() if f.startswith('[') and '](' in f and f.endswith(')')]
183
  }
184
  gallery_outputs = {
185
- "images": [(Image.open(f), os.path.basename(f)) for f in galleries["images"]],
186
- "videos": [(f, os.path.basename(f)) for f in galleries["videos"]], # File path as preview
187
- "documents": [(Image.frombytes("RGB", fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).size, fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).samples) if f.endswith('.pdf') else f, os.path.basename(f)) for f in galleries["documents"]],
188
- "datasets": [(f, os.path.basename(f)) for f in galleries["datasets"]],
189
- "links": [(f, f.split(']')[0][1:]) for f in galleries["links"]]
190
  }
191
- history.append(f"Updated galleries: {sum(len(g) for g in galleries.values())} files")
192
  return gallery_outputs, history, selected_files
193
 
194
- # Sidebar Update
195
  def update_sidebar(history, selected_files):
196
- all_files = get_gallery_files(["jpg", "png", "mp4", "md", "pdf", "docx", "csv", "xlsx"]) + [f for f in selected_files.keys() if f.startswith('[') and '](' in f and f.endswith(')')]
197
- file_list = [gr.File(label=os.path.basename(f) if not f.startswith('[') else f.split(']')[0][1:], value=f) for f in all_files]
198
  return file_list, history
199
 
200
- # Operations
201
  def toggle_selection(file_list, selected_files):
202
  for file in file_list:
203
- selected_files[file] = not selected_files.get(file, False)
204
  return selected_files
205
 
 
206
  def image_gen(prompt, builder, history, selected_files):
207
- selected = [f for f, sel in selected_files.items() if sel and f.endswith(('.jpg', '.png'))]
208
  if not selected:
209
- return "No images selected", None, history, selected_files
210
- output_file = generate_filename("gen_output", "png")
211
- gen_image = asyncio.run(process_image_gen(prompt, output_file, builder))
212
- history.append(f"Image Gen: {prompt} -> {output_file}")
213
- selected_files[output_file] = True
214
  return f"Image saved to {output_file}", gen_image, history, selected_files
215
 
216
- # Gradio UI
217
  with gr.Blocks(title="AI Vision & SFT Titans πŸš€") as demo:
218
- gr.Markdown("# AI Vision & SFT Titans πŸš€")
219
- history = gr.State(value=[])
220
- builder = gr.State(value=None)
221
- selected_files = gr.State(value={})
222
 
223
  with gr.Row():
224
  with gr.Column(scale=1):
225
- gr.Markdown("## πŸ“ Files")
226
- sidebar_files = gr.Files(label="Downloads", height=300)
227
 
228
  with gr.Column(scale=3):
229
  with gr.Row():
230
- gr.Markdown("## πŸ› οΈ Toolbar")
231
- select_btn = gr.Button("βœ… Select")
232
- gen_btn = gr.Button("🎨 Generate")
233
 
234
  with gr.Tabs():
235
- with gr.TabItem("πŸ“€ Upload"):
236
  with gr.Row():
237
- img_upload = gr.File(label="πŸ–ΌοΈ Images (jpg/png)", file_count="multiple", accept=["image/jpeg", "image/png"])
238
- vid_upload = gr.File(label="πŸŽ₯ Videos (mp4)", file_count="multiple", accept=["video/mp4"])
239
  with gr.Row():
240
- doc_upload = gr.File(label="πŸ“œ Docs (md/pdf/docx)", file_count="multiple", accept=["text/markdown", "application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"])
241
- data_upload = gr.File(label="πŸ“Š Data (csv/xlsx)", file_count="multiple", accept=["text/csv", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"])
242
  with gr.Row():
243
- links_title = gr.Textbox(label="πŸ”— Link Titles", lines=3)
244
- links_url = gr.Textbox(label="πŸ”— Link URLs", lines=3)
245
- upload_status = gr.Textbox(label="Status")
246
  gr.Button("πŸ“€ Upload Images").click(upload_images, inputs=[img_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
247
  gr.Button("πŸ“€ Upload Videos").click(upload_videos, inputs=[vid_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
248
  gr.Button("πŸ“€ Upload Docs").click(upload_documents, inputs=[doc_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
249
  gr.Button("πŸ“€ Upload Data").click(upload_datasets, inputs=[data_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
250
  gr.Button("πŸ“€ Upload Links").click(upload_links, inputs=[links_title, links_url, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
251
 
252
- with gr.TabItem("πŸ–ΌοΈ Gallery"):
253
- img_gallery = gr.Gallery(label="πŸ–ΌοΈ Images (jpg/png)", columns=4, height="auto")
254
- vid_gallery = gr.Gallery(label="πŸŽ₯ Videos (mp4)", columns=4, height="auto")
255
- doc_gallery = gr.Gallery(label="πŸ“œ Docs (md/pdf/docx)", columns=4, height="auto")
256
- data_gallery = gr.Gallery(label="πŸ“Š Data (csv/xlsx)", columns=4, height="auto")
257
- link_gallery = gr.Gallery(label="πŸ”— Links", columns=4, height="auto")
258
  gr.Button("πŸ”„ Refresh").click(update_galleries, inputs=[history, selected_files], outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
259
 
260
- with gr.TabItem("πŸ” Operations"):
261
- prompt = gr.Textbox(label="Image Gen Prompt", value="Generate a neon version")
262
- op_status = gr.Textbox(label="Status")
263
- op_output = gr.Image(label="Output")
264
- select_files = gr.Dropdown(choices=list(selected_files.value.keys()), multiselect=True, label="Select Files")
265
  select_btn.click(toggle_selection, inputs=[select_files, selected_files], outputs=[selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
266
  gen_btn.click(image_gen, inputs=[prompt, builder, history, selected_files], outputs=[op_status, op_output, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
267
 
 
268
  demo.launch()
 
1
  #!/usr/bin/env python3
2
+ # πŸ˜‚ Shebangin’ it like it’s 1999β€”Python 3, let’s roll!
3
+
4
+ # 🧳 Importing the whole circusβ€”get ready for a wild ride!
5
  import os
6
  import glob
7
  import time
 
20
  from typing import Optional
21
  import gradio as gr
22
 
23
+ # πŸ“œ Logging setupβ€”because even AIs need a diary!
24
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
25
  logger = logging.getLogger(__name__)
26
+ log_records = [] # πŸ—’οΈ Dear diary, today I logged a thing...
27
 
28
+ # πŸ€“ LogCaptureHandler classβ€”catching logs like a pro fisherman!
29
  class LogCaptureHandler(logging.Handler):
30
+ # 🎣 Hooking those logs right outta the stream!
31
  def emit(self, record):
32
  log_records.append(record)
33
 
34
+ logger.addHandler(LogCaptureHandler()) # 🐟 Adding the hook to the loggerβ€”catch β€˜em all!
35
 
36
+ # 🏰 ModelConfig dataclassβ€”building castles for our AI kings!
37
  @dataclass
38
  class ModelConfig:
39
+ name: str # 🏷️ Naming our royal model
40
+ base_model: str # πŸ›οΈ The foundation it stands on
41
+ size: str # πŸ“ How big’s this beast?
42
+ domain: Optional[str] = None # 🌍 Where does it rule? Optional kingdom!
43
+ model_type: str = "causal_lm" # βš™οΈ What kind of magic does it wield?
44
+ # πŸ—ΊοΈ Property to map the pathβ€”where the king resides!
45
  @property
46
  def model_path(self):
47
  return f"models/{self.name}"
48
 
49
+ # 🎨 DiffusionConfig dataclassβ€”art school for diffusion models!
50
  @dataclass
51
  class DiffusionConfig:
52
+ name: str # πŸ–ŒοΈ What’s this masterpiece called?
53
+ base_model: str # πŸ–ΌοΈ The canvas it starts with
54
+ size: str # πŸ“ Size of the artwork, big or small?
55
+ domain: Optional[str] = None # 🎨 Optional styleβ€”abstract or realism?
56
+ # πŸ—ΊοΈ Property to find the galleryβ€”where the art hangs!
57
  @property
58
  def model_path(self):
59
  return f"diffusion_models/{self.name}"
60
 
61
+ # πŸ€– ModelBuilder classβ€”assembling AI like Lego bricks!
62
  class ModelBuilder:
63
+ # πŸ› οΈ Initβ€”setting up the workshop!
64
  def __init__(self):
65
+ self.config = None # πŸ—ΊοΈ Blueprint? Not yet!
66
+ self.model = None # πŸ€– The robot’s still in pieces
67
+ self.tokenizer = None # πŸ“ No word-chopper yet
68
+ # πŸš€ Load_modelβ€”blast off with a pre-trained brain!
69
  def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
70
+ self.model = AutoModelForCausalLM.from_pretrained(model_path) # 🧠 Brain downloaded!
71
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path) # βœ‚οΈ Word-slicer ready!
72
  if self.tokenizer.pad_token is None:
73
+ self.tokenizer.pad_token = self.tokenizer.eos_token # 🩹 Patching up the tokenizer
74
  if config:
75
+ self.config = config # πŸ“œ Got the blueprint now!
76
+ self.model.to("cuda" if torch.cuda.is_available() else "cpu") # ⚑ GPU or bust!
77
  return self
78
+ # πŸ’Ύ Save_modelβ€”stashing the AI for later glory!
79
  def save_model(self, path: str):
80
+ os.makedirs(os.path.dirname(path), exist_ok=True) # 🏠 Making room for the save
81
+ self.model.save_pretrained(path) # 🧠 Brain archived!
82
+ self.tokenizer.save_pretrained(path) # βœ‚οΈ Slicer stored!
83
 
84
+ # 🎨 DiffusionBuilder classβ€”crafting diffusion dreams one pixel at a time!
85
  class DiffusionBuilder:
86
+ # πŸ–ŒοΈ Initβ€”prepping the easel for some art!
87
  def __init__(self):
88
+ self.config = None # πŸ—ΊοΈ No art plan yet
89
+ self.pipeline = None # 🎨 No paintbrush in hand
90
+ # πŸ–ΌοΈ Load_modelβ€”grabbing a pre-painted masterpiece!
91
  def load_model(self, model_path: str, config: Optional[DiffusionConfig] = None):
92
+ self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu") # πŸ–ŒοΈ Brush loaded, CPU style!
93
  if config:
94
+ self.config = config # πŸ“œ Art plan acquired!
95
  return self
96
+ # πŸ’Ύ Save_modelβ€”framing the artwork for the gallery!
97
  def save_model(self, path: str):
98
+ os.makedirs(os.path.dirname(path), exist_ok=True) # 🏠 Prepping the gallery wall
99
+ self.pipeline.save_pretrained(path) # πŸ–ΌοΈ Hung up for all to see!
100
+ # 🌈 Generateβ€”spinning pixels into gold, Picasso-style!
101
  def generate(self, prompt: str):
102
+ return self.pipeline(prompt, num_inference_steps=20).images[0] # 🎨 Art in 20 steps or less!
103
 
104
+ # πŸ˜‚ Time to stamp files like a bossβ€”unique names incoming!
105
  def generate_filename(sequence, ext):
106
+ timestamp = time.strftime("%d%m%Y%H%M%S") # ⏰ Clock says β€œname me now!”
107
  return f"{sequence}_{timestamp}.{ext}"
108
 
109
+ # πŸ•΅οΈβ€β™‚οΈ Sherlocking the filesystem for your precious files!
110
  def get_gallery_files(file_types):
111
+ return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")]))) # πŸ—ƒοΈ Deduped treasure hunt!
112
 
113
+ # 🎨 Paint the town neonβ€”async image gen magic ahead!
114
  async def process_image_gen(prompt, output_file, builder):
115
  if builder and isinstance(builder, DiffusionBuilder) and builder.pipeline:
116
+ pipeline = builder.pipeline # πŸ–ŒοΈ Using the pro’s brush!
117
  else:
118
+ pipeline = StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu") # 🎨 Default brush, CPU vibes!
119
+ gen_image = pipeline(prompt, num_inference_steps=20).images[0] # 🌟 20 steps to brilliance!
120
+ gen_image.save(output_file) # πŸ–ΌοΈ Saving the neon dream!
121
  return gen_image
122
 
123
+ # πŸ–ΌοΈ Snap those pics like a paparazziβ€”upload images with flair!
124
  def upload_images(files, history, selected_files):
125
  if not files:
126
+ return "No files uploaded", history, selected_files # 😒 No pics, no party!
127
  uploaded = []
128
  for file in files:
129
+ ext = file.name.split('.')[-1].lower() # πŸ•΅οΈ Sniffing out the file type!
130
  if ext in ["jpg", "png"]:
131
+ output_path = f"img_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Tagging it fresh!
132
  with open(output_path, "wb") as f:
133
+ f.write(file.read()) # πŸ“Έ Snap saved!
134
  uploaded.append(output_path)
135
+ history.append(f"Uploaded Image: {output_path}") # πŸ“œ Logging the fame!
136
+ selected_files[output_path] = False # βœ… Unchecked for now!
137
  return f"Uploaded {len(uploaded)} images", history, selected_files
138
 
139
+ # πŸŽ₯ Roll cameraβ€”video uploads that’ll make Spielberg jealous!
140
  def upload_videos(files, history, selected_files):
141
  if not files:
142
+ return "No files uploaded", history, selected_files # 🎬 No footage, no Oscar!
143
  uploaded = []
144
  for file in files:
145
+ ext = file.name.split('.')[-1].lower() # πŸ•΅οΈ Checking the reel type!
146
  if ext == "mp4":
147
+ output_path = f"vid_{int(time.time())}_{os.path.basename(file.name)}" # 🎞️ Reel name ready!
148
  with open(output_path, "wb") as f:
149
+ f.write(file.read()) # πŸŽ₯ Action, saved!
150
  uploaded.append(output_path)
151
+ history.append(f"Uploaded Video: {output_path}") # πŸ“œ Cue the credits!
152
+ selected_files[output_path] = False # βœ… Not selected yet!
153
  return f"Uploaded {len(uploaded)} videos", history, selected_files
154
 
155
+ # πŸ“œ Scribble some docsβ€”PDFs and more, oh what a bore!
156
  def upload_documents(files, history, selected_files):
157
  if not files:
158
+ return "No files uploaded", history, selected_files # πŸ“ No docs, no drama!
159
  uploaded = []
160
  for file in files:
161
+ ext = file.name.split('.')[-1].lower() # πŸ•΅οΈ Peeking at the paper type!
162
  if ext in ["md", "pdf", "docx"]:
163
+ output_path = f"doc_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Stamping the scroll!
164
  with open(output_path, "wb") as f:
165
+ f.write(file.read()) # πŸ“œ Scroll secured!
166
  uploaded.append(output_path)
167
+ history.append(f"Uploaded Document: {output_path}") # πŸ“œ Noted in history!
168
+ selected_files[output_path] = False # βœ… Still on the bench!
169
  return f"Uploaded {len(uploaded)} documents", history, selected_files
170
 
171
+ # πŸ“Š Data nerd alertβ€”CSV and Excel uploads for the win!
172
  def upload_datasets(files, history, selected_files):
173
  if not files:
174
+ return "No files uploaded", history, selected_files # πŸ“ˆ No data, no geek-out!
175
  uploaded = []
176
  for file in files:
177
+ ext = file.name.split('.')[-1].lower() # πŸ•΅οΈ Cracking the data code!
178
  if ext in ["csv", "xlsx"]:
179
+ output_path = f"data_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Labeling the stats!
180
  with open(output_path, "wb") as f:
181
+ f.write(file.read()) # πŸ“Š Stats stashed!
182
  uploaded.append(output_path)
183
+ history.append(f"Uploaded Dataset: {output_path}") # πŸ“œ Data’s in the books!
184
+ selected_files[output_path] = False # βœ… Not picked yet!
185
  return f"Uploaded {len(uploaded)} datasets", history, selected_files
186
 
187
+ # πŸ”— Link it upβ€”URLs and titles, the web’s wild child!
188
  def upload_links(links_title, links_url, history, selected_files):
189
  if not links_title or not links_url:
190
+ return "No links provided", history, selected_files # 🌐 No links, no surf!
191
+ links = list(zip(links_title.split('\n'), links_url.split('\n'))) # 🧩 Pairing titles and URLs!
192
  uploaded = []
193
  for title, url in links:
194
  if title and url:
195
+ link_entry = f"[{title}]({url})" # πŸ”— Crafting the web gem!
196
  uploaded.append(link_entry)
197
+ history.append(f"Added Link: {link_entry}") # πŸ“œ Linking history!
198
+ selected_files[link_entry] = False # βœ… Surf’s not up yet!
199
  return f"Added {len(uploaded)} links", history, selected_files
200
 
201
+ # πŸ–ΌοΈ Gallery glow-upβ€”show off all your files in style!
202
  def update_galleries(history, selected_files):
203
  galleries = {
204
+ "images": get_gallery_files(["jpg", "png"]), # πŸ–ΌοΈ Picture parade!
205
+ "videos": get_gallery_files(["mp4"]), # πŸŽ₯ Video vault!
206
+ "documents": get_gallery_files(["md", "pdf", "docx"]), # πŸ“œ Doc depot!
207
+ "datasets": get_gallery_files(["csv", "xlsx"]), # πŸ“Š Data den!
208
+ "links": [f for f in selected_files.keys() if f.startswith('[') and '](' in f and f.endswith(')')] # πŸ”— Link lounge!
209
  }
210
  gallery_outputs = {
211
+ "images": [(Image.open(f), os.path.basename(f)) for f in galleries["images"]], # πŸ–ΌοΈ Picture perfect!
212
+ "videos": [(f, os.path.basename(f)) for f in galleries["videos"]], # πŸŽ₯ Reel deal!
213
+ "documents": [(Image.frombytes("RGB", fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).size, fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).samples) if f.endswith('.pdf') else f, os.path.basename(f)) for f in galleries["documents"]], # πŸ“œ Doc dazzle!
214
+ "datasets": [(f, os.path.basename(f)) for f in galleries["datasets"]], # πŸ“Š Data delight!
215
+ "links": [(f, f.split(']')[0][1:]) for f in galleries["links"]] # πŸ”— Link love!
216
  }
217
+ history.append(f"Updated galleries: {sum(len(g) for g in galleries.values())} files") # πŸ“œ Gallery grand total!
218
  return gallery_outputs, history, selected_files
219
 
220
+ # πŸ“‚ Sidebar swaggerβ€”download links that scream β€œtake me home!”
221
  def update_sidebar(history, selected_files):
222
+ all_files = get_gallery_files(["jpg", "png", "mp4", "md", "pdf", "docx", "csv", "xlsx"]) + [f for f in selected_files.keys() if f.startswith('[') and '](' in f and f.endswith(')')] # πŸ—ƒοΈ All the loot!
223
+ file_list = [gr.File(label=os.path.basename(f) if not f.startswith('[') else f.split(']')[0][1:], value=f) for f in all_files] # πŸ“₯ Download goodies!
224
  return file_list, history
225
 
226
+ # βœ… Check it or wreck itβ€”toggle those selections like a pro!
227
  def toggle_selection(file_list, selected_files):
228
  for file in file_list:
229
+ selected_files[file] = not selected_files.get(file, False) # βœ… Flip the switch, baby!
230
  return selected_files
231
 
232
+ # 🎨 Neon dreams unleashedβ€”generate images that pop!
233
  def image_gen(prompt, builder, history, selected_files):
234
+ selected = [f for f, sel in selected_files.items() if sel and f.endswith(('.jpg', '.png'))] # πŸ–ΌοΈ Picking the canvas!
235
  if not selected:
236
+ return "No images selected", None, history, selected_files # 😒 No art, no party!
237
+ output_file = generate_filename("gen_output", "png") # 🏷️ New masterpiece name!
238
+ gen_image = asyncio.run(process_image_gen(prompt, output_file, builder)) # 🌈 Paint it neon!
239
+ history.append(f"Image Gen: {prompt} -> {output_file}") # πŸ“œ Art history in the making!
240
+ selected_files[output_file] = True # βœ… Auto-select the new star!
241
  return f"Image saved to {output_file}", gen_image, history, selected_files
242
 
243
+ # πŸŽͺ Gradio UIβ€”step right up to the AI circus!
244
  with gr.Blocks(title="AI Vision & SFT Titans πŸš€") as demo:
245
+ gr.Markdown("# AI Vision & SFT Titans πŸš€") # πŸŽ‰ Welcome to the big top!
246
+ history = gr.State(value=[]) # πŸ“œ The ringmaster’s logbook!
247
+ builder = gr.State(value=None) # πŸ€– The AI acrobat, waiting in the wings!
248
+ selected_files = gr.State(value={}) # βœ… The chosen ones, ready to perform!
249
 
250
  with gr.Row():
251
  with gr.Column(scale=1):
252
+ gr.Markdown("## πŸ“ Files") # πŸ—ƒοΈ The file circus tent!
253
+ sidebar_files = gr.Files(label="Downloads", height=300) # πŸ“₯ Grab your souvenirs here!
254
 
255
  with gr.Column(scale=3):
256
  with gr.Row():
257
+ gr.Markdown("## πŸ› οΈ Toolbar") # πŸ”§ The circus control panel!
258
+ select_btn = gr.Button("βœ… Select") # βœ… Pick your performers!
259
+ gen_btn = gr.Button("🎨 Generate") # 🎨 Unleash the art clowns!
260
 
261
  with gr.Tabs():
262
+ with gr.TabItem("πŸ“€ Upload"): # πŸ“€ The upload trapeze!
263
  with gr.Row():
264
+ img_upload = gr.File(label="πŸ–ΌοΈ Images (jpg/png)", file_count="multiple", accept=["image/jpeg", "image/png"]) # πŸ–ΌοΈ Picture trapeze!
265
+ vid_upload = gr.File(label="πŸŽ₯ Videos (mp4)", file_count="multiple", accept=["video/mp4"]) # πŸŽ₯ Video vault!
266
  with gr.Row():
267
+ doc_upload = gr.File(label="πŸ“œ Docs (md/pdf/docx)", file_count="multiple", accept=["text/markdown", "application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]) # πŸ“œ Doc drop!
268
+ data_upload = gr.File(label="πŸ“Š Data (csv/xlsx)", file_count="multiple", accept=["text/csv", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]) # πŸ“Š Data dive!
269
  with gr.Row():
270
+ links_title = gr.Textbox(label="πŸ”— Link Titles", lines=3) # πŸ”— Title tightrope!
271
+ links_url = gr.Textbox(label="πŸ”— Link URLs", lines=3) # πŸ”— URL unicycle!
272
+ upload_status = gr.Textbox(label="Status") # πŸ“’ Ringmaster’s update!
273
  gr.Button("πŸ“€ Upload Images").click(upload_images, inputs=[img_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
274
  gr.Button("πŸ“€ Upload Videos").click(upload_videos, inputs=[vid_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
275
  gr.Button("πŸ“€ Upload Docs").click(upload_documents, inputs=[doc_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
276
  gr.Button("πŸ“€ Upload Data").click(upload_datasets, inputs=[data_upload, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
277
  gr.Button("πŸ“€ Upload Links").click(upload_links, inputs=[links_title, links_url, history, selected_files], outputs=[upload_status, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), gr.Gallery(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
278
 
279
+ with gr.TabItem("πŸ–ΌοΈ Gallery"): # πŸ–ΌοΈ The big top showcase!
280
+ img_gallery = gr.Gallery(label="πŸ–ΌοΈ Images (jpg/png)", columns=4, height="auto") # πŸ–ΌοΈ Picture parade!
281
+ vid_gallery = gr.Gallery(label="πŸŽ₯ Videos (mp4)", columns=4, height="auto") # πŸŽ₯ Video vault!
282
+ doc_gallery = gr.Gallery(label="πŸ“œ Docs (md/pdf/docx)", columns=4, height="auto") # πŸ“œ Doc depot!
283
+ data_gallery = gr.Gallery(label="πŸ“Š Data (csv/xlsx)", columns=4, height="auto") # πŸ“Š Data den!
284
+ link_gallery = gr.Gallery(label="πŸ”— Links", columns=4, height="auto") # πŸ”— Link lounge!
285
  gr.Button("πŸ”„ Refresh").click(update_galleries, inputs=[history, selected_files], outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
286
 
287
+ with gr.TabItem("πŸ” Operations"): # πŸ” The magic trick tent!
288
+ prompt = gr.Textbox(label="Image Gen Prompt", value="Generate a neon version") # 🎨 Art spellbook!
289
+ op_status = gr.Textbox(label="Status") # πŸ“’ Trick status!
290
+ op_output = gr.Image(label="Output") # 🎨 The big reveal!
291
+ select_files = gr.Dropdown(choices=list(selected_files.value.keys()), multiselect=True, label="Select Files") # βœ… Pick your props!
292
  select_btn.click(toggle_selection, inputs=[select_files, selected_files], outputs=[selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
293
  gen_btn.click(image_gen, inputs=[prompt, builder, history, selected_files], outputs=[op_status, op_output, history, selected_files]).then(update_galleries, inputs=[history, selected_files], outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
294
 
295
+ # πŸŽ‰ Launch the circusβ€”step right up, folks!
296
  demo.launch()