Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,98 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
import json
|
|
|
4 |
import zipfile
|
5 |
-
from
|
6 |
-
|
7 |
-
# Directory to store datasets
|
8 |
-
DATASETS_DIR = "datasets"
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
|
13 |
-
# Function to save dataset to
|
14 |
-
def save_dataset(dataset_name
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
# Function to load dataset from
|
21 |
-
def load_dataset(dataset_name):
|
22 |
-
|
23 |
-
|
24 |
-
with open(
|
25 |
-
|
26 |
-
return
|
27 |
-
return
|
28 |
-
|
29 |
-
# Function to generate and download dataset as a zip file
|
30 |
-
def generate_dataset(dataset_name):
|
31 |
-
dataset_path = os.path.join(DATASETS_DIR, f"{dataset_name}.jsonl")
|
32 |
-
if not os.path.exists(dataset_path):
|
33 |
-
return None
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
data = [json.loads(line) for line in f]
|
41 |
-
jsonl_path = os.path.join(DATASETS_DIR, f"{dataset_name}.jsonl")
|
42 |
-
zipf.write(jsonl_path, arcname=f"{dataset_name}.jsonl")
|
43 |
-
for item in data:
|
44 |
-
image_path = item['image']
|
45 |
-
zipf.write(image_path, arcname=os.path.join('images', os.path.basename(image_path)))
|
46 |
-
|
47 |
-
return zip_filename
|
48 |
-
|
49 |
-
# Gradio interface functions
|
50 |
-
def add_image_prompt(dataset_name, image, prompt):
|
51 |
-
data = load_dataset(dataset_name)
|
52 |
-
image_path = os.path.join(DATASETS_DIR, os.path.basename(image.name))
|
53 |
image.save(image_path)
|
54 |
-
|
55 |
-
|
56 |
-
return data
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
if 0 <= index < len(
|
61 |
-
|
62 |
-
|
63 |
-
return
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
if 0 <= index < len(
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
71 |
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
# Gradio
|
76 |
with gr.Blocks() as demo:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import json
|
3 |
+
import os
|
4 |
import zipfile
|
5 |
+
from datetime import datetime
|
|
|
|
|
|
|
6 |
|
7 |
+
# Initialize dataset storage
|
8 |
+
datasets = {}
|
9 |
|
10 |
+
# Function to save dataset to JSONL file
|
11 |
+
def save_dataset(dataset_name):
|
12 |
+
if dataset_name in datasets:
|
13 |
+
dataset = datasets[dataset_name]
|
14 |
+
os.makedirs(f"{dataset_name}/images", exist_ok=True)
|
15 |
+
with open(f"{dataset_name}/dataset.jsonl", "w") as f:
|
16 |
+
for item in dataset:
|
17 |
+
f.write(json.dumps(item) + "\n")
|
18 |
|
19 |
+
# Function to load dataset from JSONL file
|
20 |
+
def load_dataset(dataset_name, file):
|
21 |
+
if file:
|
22 |
+
os.makedirs(dataset_name, exist_ok=True)
|
23 |
+
with open(f"{dataset_name}/dataset.jsonl", "r") as f:
|
24 |
+
datasets[dataset_name] = [json.loads(line) for line in f]
|
25 |
+
return f"Dataset '{dataset_name}' loaded successfully."
|
26 |
+
return "No file uploaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Function to add image/prompt pair to dataset
|
29 |
+
def add_to_dataset(dataset_name, image, prompt):
|
30 |
+
if dataset_name not in datasets:
|
31 |
+
datasets[dataset_name] = []
|
32 |
+
image_path = f"{dataset_name}/images/{datetime.now().timestamp()}.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
image.save(image_path)
|
34 |
+
datasets[dataset_name].append({"image": image_path, "prompt": prompt})
|
35 |
+
return f"Added to dataset '{dataset_name}'."
|
|
|
36 |
|
37 |
+
# Function to edit image/prompt pair in dataset
|
38 |
+
def edit_dataset(dataset_name, index, new_prompt):
|
39 |
+
if dataset_name in datasets and 0 <= index < len(datasets[dataset_name]):
|
40 |
+
datasets[dataset_name][index]["prompt"] = new_prompt
|
41 |
+
return f"Edited prompt at index {index} in dataset '{dataset_name}'."
|
42 |
+
return "Invalid index."
|
43 |
|
44 |
+
# Function to delete image/prompt pair from dataset
|
45 |
+
def delete_from_dataset(dataset_name, index):
|
46 |
+
if dataset_name in datasets and 0 <= index < len(datasets[dataset_name]):
|
47 |
+
os.remove(datasets[dataset_name][index]["image"])
|
48 |
+
datasets[dataset_name].pop(index)
|
49 |
+
return f"Deleted item at index {index} from dataset '{dataset_name}'."
|
50 |
+
return "Invalid index."
|
51 |
|
52 |
+
# Function to generate and download dataset as zip file
|
53 |
+
def generate_dataset(dataset_name):
|
54 |
+
if dataset_name in datasets:
|
55 |
+
save_dataset(dataset_name)
|
56 |
+
zip_filename = f"{dataset_name}.zip"
|
57 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
58 |
+
for root, _, files in os.walk(dataset_name):
|
59 |
+
for file in files:
|
60 |
+
zipf.write(os.path.join(root, file))
|
61 |
+
return zip_filename
|
62 |
+
return "Dataset not found."
|
63 |
|
64 |
+
# Gradio interface
|
65 |
with gr.Blocks() as demo:
|
66 |
+
gr.Markdown("# Dataset Builder")
|
67 |
+
|
68 |
+
with gr.Tab("Create/Edit Dataset"):
|
69 |
+
dataset_name = gr.Textbox(label="Dataset Name")
|
70 |
+
image = gr.Image(label="Upload Image")
|
71 |
+
prompt = gr.Textbox(label="Prompt")
|
72 |
+
add_button = gr.Button("Add to Dataset")
|
73 |
+
add_button.click(add_to_dataset, inputs=[dataset_name, image, prompt], outputs="text")
|
74 |
+
|
75 |
+
index = gr.Number(label="Index to Edit/Delete", precision=0)
|
76 |
+
new_prompt = gr.Textbox(label="New Prompt")
|
77 |
+
edit_button = gr.Button("Edit Prompt")
|
78 |
+
edit_button.click(edit_dataset, inputs=[dataset_name, index, new_prompt], outputs="text")
|
79 |
+
|
80 |
+
delete_button = gr.Button("Delete from Dataset")
|
81 |
+
delete_button.click(delete_from_dataset, inputs=[dataset_name, index], outputs="text")
|
82 |
+
|
83 |
+
with gr.Tab("Upload Dataset"):
|
84 |
+
upload_dataset_name = gr.Textbox(label="Dataset Name")
|
85 |
+
upload_file = gr.File(label="Upload JSONL File")
|
86 |
+
upload_button = gr.Button("Upload Dataset")
|
87 |
+
upload_button.click(load_dataset, inputs=[upload_dataset_name, upload_file], outputs="text")
|
88 |
+
|
89 |
+
with gr.Tab("Generate/Download Dataset"):
|
90 |
+
generate_button = gr.Button("Generate Dataset")
|
91 |
+
generate_button.click(generate_dataset, inputs=[dataset_name], outputs="file")
|
92 |
+
|
93 |
+
with gr.Tab("View Dataset"):
|
94 |
+
view_dataset_name = gr.Textbox(label="Dataset Name")
|
95 |
+
view_button = gr.Button("View Dataset")
|
96 |
+
view_button.click(lambda name: datasets.get(name, []), inputs=[view_dataset_name], outputs="json")
|
97 |
|
98 |
demo.launch()
|