throaway2854
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import zipfile
|
5 |
+
from datasets import Dataset, DatasetDict
|
6 |
+
|
7 |
+
# Directory to store datasets
|
8 |
+
DATASETS_DIR = "datasets"
|
9 |
+
|
10 |
+
# Ensure the datasets directory exists
|
11 |
+
os.makedirs(DATASETS_DIR, exist_ok=True)
|
12 |
+
|
13 |
+
# Function to save dataset to persistent storage
|
14 |
+
def save_dataset(dataset_name, data):
|
15 |
+
dataset_path = os.path.join(DATASETS_DIR, f"{dataset_name}.jsonl")
|
16 |
+
with open(dataset_path, 'w') as f:
|
17 |
+
for item in data:
|
18 |
+
f.write(json.dumps(item) + "\n")
|
19 |
+
|
20 |
+
# Function to load dataset from persistent storage
|
21 |
+
def load_dataset(dataset_name):
|
22 |
+
dataset_path = os.path.join(DATASETS_DIR, f"{dataset_name}.jsonl")
|
23 |
+
if os.path.exists(dataset_path):
|
24 |
+
with open(dataset_path, 'r') as f:
|
25 |
+
data = [json.loads(line) for line in f]
|
26 |
+
return data
|
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 |
+
# Create a zip file
|
36 |
+
zip_filename = f"{dataset_name}.zip"
|
37 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
38 |
+
# Add images and JSONL file to the zip
|
39 |
+
with open(dataset_path, 'r') as f:
|
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 |
+
data.append({"image": image_path, "prompt": prompt})
|
55 |
+
save_dataset(dataset_name, data)
|
56 |
+
return data
|
57 |
+
|
58 |
+
def edit_image_prompt(dataset_name, index, new_prompt):
|
59 |
+
data = load_dataset(dataset_name)
|
60 |
+
if 0 <= index < len(data):
|
61 |
+
data[index]['prompt'] = new_prompt
|
62 |
+
save_dataset(dataset_name, data)
|
63 |
+
return data
|
64 |
+
|
65 |
+
def delete_image_prompt(dataset_name, index):
|
66 |
+
data = load_dataset(dataset_name)
|
67 |
+
if 0 <= index < len(data):
|
68 |
+
del data[index]
|
69 |
+
save_dataset(dataset_name, data)
|
70 |
+
return data
|
71 |
+
|
72 |
+
def get_datasets():
|
73 |
+
return [f.split('.')[0] for f in os.listdir(DATASETS_DIR) if f.endswith('.jsonl')]
|
74 |
+
|
75 |
+
# Gradio UI
|
76 |
+
with gr.Blocks() as demo:
|
77 |
+
dataset_name = gr.Textbox(label="Dataset Name")
|
78 |
+
image = gr.Image(label="Upload Image")
|
79 |
+
prompt = gr.Textbox(label="Prompt")
|
80 |
+
add_button = gr.Button("Add Image/Prompt")
|
81 |
+
edit_index = gr.Number(label="Edit Index", value=0)
|
82 |
+
new_prompt = gr.Textbox(label="New Prompt")
|
83 |
+
edit_button = gr.Button("Edit Prompt")
|
84 |
+
delete_index = gr.Number(label="Delete Index", value=0)
|
85 |
+
delete_button = gr.Button("Delete Image/Prompt")
|
86 |
+
generate_button = gr.Button("Generate Dataset")
|
87 |
+
download_button = gr.Button("Download Dataset")
|
88 |
+
dataset_list = gr.Dropdown(label="Select Dataset", choices=get_datasets())
|
89 |
+
dataset_display = gr.Dataframe(headers=["Image", "Prompt"])
|
90 |
+
|
91 |
+
add_button.click(add_image_prompt, [dataset_name, image, prompt], dataset_display)
|
92 |
+
edit_button.click(edit_image_prompt, [dataset_name, edit_index, new_prompt], dataset_display)
|
93 |
+
delete_button.click(delete_image_prompt, [dataset_name, delete_index], dataset_display)
|
94 |
+
generate_button.click(generate_dataset, [dataset_name], None)
|
95 |
+
download_button.click(generate_dataset, [dataset_name], None, file_name=f"{dataset_name}.zip")
|
96 |
+
dataset_list.change(load_dataset, [dataset_list], dataset_display)
|
97 |
+
|
98 |
+
demo.launch()
|