Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
8 |
-
import base64
|
9 |
import pandas as pd
|
10 |
-
import torch
|
11 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
12 |
-
from diffusers import StableDiffusionPipeline
|
13 |
-
import fitz
|
14 |
-
from PIL import Image
|
15 |
-
import logging
|
16 |
-
import asyncio
|
17 |
-
import aiofiles
|
18 |
-
from io import BytesIO
|
19 |
-
from dataclasses import dataclass
|
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")
|
@@ -33,74 +35,6 @@ class LogCaptureHandler(logging.Handler):
|
|
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!”
|
@@ -110,16 +44,6 @@ def generate_filename(sequence, ext):
|
|
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:
|
@@ -136,22 +60,6 @@ def upload_images(files, history, selected_files):
|
|
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:
|
@@ -159,7 +67,7 @@ def upload_documents(files, history, selected_files):
|
|
159 |
uploaded = []
|
160 |
for file in files:
|
161 |
ext = file.name.split('.')[-1].lower() # 🕵️ Peeking at the paper type!
|
162 |
-
if ext in ["
|
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!
|
@@ -168,14 +76,14 @@ def upload_documents(files, history, selected_files):
|
|
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
|
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
|
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!
|
@@ -184,43 +92,25 @@ def upload_datasets(files, history, selected_files):
|
|
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 |
-
"
|
206 |
-
"
|
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 |
-
"
|
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", "
|
223 |
-
file_list = [gr.File(label=os.path.basename(f)
|
224 |
return file_list, history
|
225 |
|
226 |
# ✅ Check it or wreck it—toggle those selections like a pro!
|
@@ -229,68 +119,79 @@ def toggle_selection(file_list, selected_files):
|
|
229 |
selected_files[file] = not selected_files.get(file, False) # ✅ Flip the switch, baby!
|
230 |
return selected_files
|
231 |
|
232 |
-
#
|
233 |
-
def
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
demo.launch()
|
|
|
1 |
+
Apologies for the breakup! Here’s the complete, unbroken code listing for app.py, incorporating the Gradio 5.23.0 features demo without the accept parameter, all in one cohesive block:
|
2 |
+
|
3 |
+
python
|
4 |
+
|
5 |
+
Collapse
|
6 |
+
|
7 |
+
Wrap
|
8 |
+
|
9 |
+
Copy
|
10 |
#!/usr/bin/env python3
|
11 |
# 😂 Shebangin’ it like it’s 1999—Python 3, let’s roll!
|
12 |
|
13 |
# 🧳 Importing the whole circus—get ready for a wild ride!
|
14 |
import os
|
|
|
15 |
import time
|
|
|
16 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
import gradio as gr
|
18 |
+
from gradio import DeepLinkButton # 🔥 Deep links from 5.23.0!
|
19 |
+
import pkg_resources # 🕵️♂️ Sneaky version checker!
|
20 |
+
import logging
|
21 |
+
import glob
|
22 |
+
from PIL import Image
|
23 |
+
import fitz
|
24 |
|
25 |
# 📜 Logging setup—because even AIs need a diary!
|
26 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
35 |
|
36 |
logger.addHandler(LogCaptureHandler()) # 🐟 Adding the hook to the logger—catch ‘em all!
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# 😂 Time to stamp files like a boss—unique names incoming!
|
39 |
def generate_filename(sequence, ext):
|
40 |
timestamp = time.strftime("%d%m%Y%H%M%S") # ⏰ Clock says “name me now!”
|
|
|
44 |
def get_gallery_files(file_types):
|
45 |
return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")]))) # 🗃️ Deduped treasure hunt!
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# 🖼️ Snap those pics like a paparazzi—upload images with flair!
|
48 |
def upload_images(files, history, selected_files):
|
49 |
if not files:
|
|
|
60 |
selected_files[output_path] = False # ✅ Unchecked for now!
|
61 |
return f"Uploaded {len(uploaded)} images", history, selected_files
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# 📜 Scribble some docs—PDFs and more, oh what a bore!
|
64 |
def upload_documents(files, history, selected_files):
|
65 |
if not files:
|
|
|
67 |
uploaded = []
|
68 |
for file in files:
|
69 |
ext = file.name.split('.')[-1].lower() # 🕵️ Peeking at the paper type!
|
70 |
+
if ext in ["pdf"]: # Limiting to PDF for demo simplicity
|
71 |
output_path = f"doc_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Stamping the scroll!
|
72 |
with open(output_path, "wb") as f:
|
73 |
f.write(file.read()) # 📜 Scroll secured!
|
|
|
76 |
selected_files[output_path] = False # ✅ Still on the bench!
|
77 |
return f"Uploaded {len(uploaded)} documents", history, selected_files
|
78 |
|
79 |
+
# 📊 Data nerd alert—CSV uploads for the win!
|
80 |
def upload_datasets(files, history, selected_files):
|
81 |
if not files:
|
82 |
return "No files uploaded", history, selected_files # 📈 No data, no geek-out!
|
83 |
uploaded = []
|
84 |
for file in files:
|
85 |
ext = file.name.split('.')[-1].lower() # 🕵️ Cracking the data code!
|
86 |
+
if ext == "csv":
|
87 |
output_path = f"data_{int(time.time())}_{os.path.basename(file.name)}" # 🏷️ Labeling the stats!
|
88 |
with open(output_path, "wb") as f:
|
89 |
f.write(file.read()) # 📊 Stats stashed!
|
|
|
92 |
selected_files[output_path] = False # ✅ Not picked yet!
|
93 |
return f"Uploaded {len(uploaded)} datasets", history, selected_files
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
# 🖼️ Gallery glow-up—show off all your files in style!
|
96 |
def update_galleries(history, selected_files):
|
97 |
galleries = {
|
98 |
"images": get_gallery_files(["jpg", "png"]), # 🖼️ Picture parade!
|
99 |
+
"documents": get_gallery_files(["pdf"]), # 📜 Doc depot!
|
100 |
+
"datasets": get_gallery_files(["csv"]), # 📊 Data den!
|
|
|
|
|
101 |
}
|
102 |
gallery_outputs = {
|
103 |
"images": [(Image.open(f), os.path.basename(f)) for f in galleries["images"]], # 🖼️ Picture perfect!
|
104 |
+
"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), os.path.basename(f)) for f in galleries["documents"]], # 📜 Doc dazzle!
|
|
|
105 |
"datasets": [(f, os.path.basename(f)) for f in galleries["datasets"]], # 📊 Data delight!
|
|
|
106 |
}
|
107 |
history.append(f"Updated galleries: {sum(len(g) for g in galleries.values())} files") # 📜 Gallery grand total!
|
108 |
return gallery_outputs, history, selected_files
|
109 |
|
110 |
# 📂 Sidebar swagger—download links that scream “take me home!”
|
111 |
def update_sidebar(history, selected_files):
|
112 |
+
all_files = get_gallery_files(["jpg", "png", "pdf", "csv"])
|
113 |
+
file_list = [gr.File(label=os.path.basename(f), value=f) for f in all_files] # 📥 Download goodies!
|
114 |
return file_list, history
|
115 |
|
116 |
# ✅ Check it or wreck it—toggle those selections like a pro!
|
|
|
119 |
selected_files[file] = not selected_files.get(file, False) # ✅ Flip the switch, baby!
|
120 |
return selected_files
|
121 |
|
122 |
+
# 📊 Dataframe demo—showing off Gradio 5.21.0+ dataframe mastery!
|
123 |
+
def get_dataframe():
|
124 |
+
df = pd.DataFrame({
|
125 |
+
"Name": ["Alice", "Bob", "Charlie"],
|
126 |
+
"Age": [25, 30, 35],
|
127 |
+
"Score": [95.5, 87.0, 92.3]
|
128 |
+
})
|
129 |
+
return df
|
130 |
+
|
131 |
+
# 📜 Mermaid.js demo—flowchart fun from 5.23.0!
|
132 |
+
def get_mermaid_chart():
|
133 |
+
return """
|
134 |
+
```mermaid
|
135 |
+
graph TD
|
136 |
+
A[Upload Files] --> B[View Gallery]
|
137 |
+
B --> C[Select Files]
|
138 |
+
C --> D[Generate Output]
|
139 |
+
D --> E[Deep Link to Result]
|
140 |
+
"""
|
141 |
+
|
142 |
+
🎨 Code editor demo—Jedi completion from 5.23.0!
|
143 |
+
def get_code_snippet():
|
144 |
+
return "def hello(name):\n return f'Hello, {name}!'"
|
145 |
+
|
146 |
+
🎪 Gradio UI—step right up to the AI circus!
|
147 |
+
with gr.Blocks(title="Gradio 5.23.0 Mastery Demo 🚀") as demo:
|
148 |
+
gr.Markdown(f"# Gradio 5.23.0 Mastery Demo 🚀\nRunning Gradio version: {pkg_resources.get_distribution('gradio').version}") # 🎉 Welcome to the big top with version check!
|
149 |
+
history = gr.State(value=[]) # 📜 The ringmaster’s logbook!
|
150 |
+
selected_files = gr.State(value={}) # ✅ The chosen ones, ready to perform!
|
151 |
+
|
152 |
+
with gr.Row():
|
153 |
+
with gr.Column(scale=1):
|
154 |
+
gr.Markdown("## 📁 Files") # 🗃️ The file circus tent!
|
155 |
+
sidebar_files = gr.Files(label="Downloads", height=300) # 📥 Grab your souvenirs here!
|
156 |
+
|
157 |
+
with gr.Column(scale=3):
|
158 |
+
with gr.Row():
|
159 |
+
gr.Markdown("## 🛠️ Toolbar") # 🔧 The circus control panel!
|
160 |
+
select_btn = gr.Button("✅ Select") # ✅ Pick your performers!
|
161 |
+
|
162 |
+
with gr.Tabs():
|
163 |
+
with gr.TabItem("📤 Upload"): # 📤 The upload trapeze!
|
164 |
+
with gr.Row():
|
165 |
+
img_upload = gr.File(label="🖼️ Images (jpg/png)", file_count="multiple") # 🖼️ Picture trapeze!
|
166 |
+
doc_upload = gr.File(label="📜 Docs (pdf)", file_count="multiple") # 📜 Doc drop!
|
167 |
+
with gr.Row():
|
168 |
+
data_upload = gr.File(label="📊 Data (csv)", file_count="multiple") # 📊 Data dive!
|
169 |
+
upload_status = gr.Textbox(label="Status") # 📢 Ringmaster’s update!
|
170 |
+
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(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
|
171 |
+
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(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
|
172 |
+
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(), history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
|
173 |
+
|
174 |
+
with gr.TabItem("🖼️ Gallery"): # 🖼️ The big top showcase!
|
175 |
+
img_gallery = gr.Gallery(label="🖼️ Images (jpg/png)", columns=4, height="auto") # 🖼️ Picture parade!
|
176 |
+
doc_gallery = gr.Gallery(label="📜 Docs (pdf)", columns=4, height="auto") # 📜 Doc depot!
|
177 |
+
data_gallery = gr.Gallery(label="📊 Data (csv)", columns=4, height="auto") # 📊 Data den!
|
178 |
+
gr.Button("🔄 Refresh").click(update_galleries, inputs=[history, selected_files], outputs=[img_gallery, doc_gallery, data_gallery, history, selected_files]).then(update_sidebar, inputs=[history, selected_files], outputs=[sidebar_files, history])
|
179 |
+
|
180 |
+
with gr.TabItem("🔍 Features"): # 🔍 The magic trick tent!
|
181 |
+
gr.Markdown("### 📊 Dataframe Mastery (5.21.0)") # 📊 Flexing new dataframe tricks!
|
182 |
+
df_output = gr.Dataframe(value=get_dataframe, interactive=True, static_columns=["Name"], wrap=True) # 🔥 Static columns, drag selection from 5.21.0!
|
183 |
+
gr.Markdown("### 📜 Mermaid.js Flowchart (5.23.0)") # 📜 Mermaid.js from 5.23.0!
|
184 |
+
mermaid_output = gr.Markdown(value=get_mermaid_chart) # 🌐 Flowchart fun!
|
185 |
+
gr.Markdown("### 🎨 Code Editor with Jedi Completion (5.23.0)") # 🎨 Jedi power from 5.23.0!
|
186 |
+
code_output = gr.Code(value=get_code_snippet, language="python", interactive=True) # ✍️ Code with autocompletion!
|
187 |
+
gr.Markdown("### 💥 Deep Link Button (5.23.0)") # 💥 Deep links from 5.23.0!
|
188 |
+
DeepLinkButton(label="Link to Latest Output", variant="secondary", deep_link="/gallery/images") # 🔥 Secondary variant from 5.23.0!
|
189 |
+
|
190 |
+
with gr.TabItem("📜 History"): # 📜 The logbook showcase!
|
191 |
+
history_output = gr.Textbox(label="History", lines=5, value="\n".join(history.value), interactive=False) # 📜 What’s been cooking?
|
192 |
+
|
193 |
+
🎉 Auto-update history on load—Gradio 5.20.1 event listener vibes!
|
194 |
+
demo.load(lambda h: "\n".join(h[-5:]), inputs=[history], outputs=[history_output])
|
195 |
+
|
196 |
+
🎉 Launch the circus—step right up, folks!
|
197 |
demo.launch()
|