File size: 2,959 Bytes
b1c343c 79b274c b1c343c e377d5f 79b274c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c 7cf4a2b b1c343c 7cf4a2b b1c343c d64dc21 b1c343c d64dc21 b1c343c d64dc21 b1c343c f21bf1a b1c343c f21bf1a b1c343c 00039a6 b1c343c 00039a6 b1c343c 79ea3d6 b1c343c f21bf1a b1c343c f21bf1a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
># Import necessary libraries
import gradio as gr
import json
import os
import zipfile
# Define helper functions
def create_dataset(dataset_name):
dataset_path = f'{dataset_name}.zip'
if not os.path.exists(dataset_path):
with zipfile.ZipFile(dataset_path, 'w') as zip_file:
zip_file.writestr('images/', '')
zip_file.writestr('data.jsonl', '')
return dataset_path
def upload_pair(dataset_path, image, prompt):
with zipfile.ZipFile(dataset_path, 'a') as zip_file:
image_path = f'images/{image.name}'
zip_file.writestr(image_path, image.read())
data = {'image': image_path, 'prompt': prompt}
zip_file.writestr('data.jsonl', json.dumps(data) + '\n')
def edit_prompt(dataset_path, image_path, new_prompt):
with zipfile.ZipFile(dataset_path, 'r') as zip_file:
data = json.load(zip_file.open('data.jsonl'))
for item in data:
if item['image'] == image_path:
item['prompt'] = new_prompt
break
with zipfile.ZipFile(dataset_path, 'w') as zip_file:
zip_file.writestr('data.jsonl', json.dumps(data))
def delete_pair(dataset_path, image_path):
with zipfile.ZipFile(dataset_path, 'r') as zip_file:
data = json.load(zip_file.open('data.jsonl'))
data = [item for item in data if item['image'] != image_path]
with zipfile.ZipFile(dataset_path, 'w') as zip_file:
zip_file.writestr('data.jsonl', json.dumps(data))
def download_dataset(dataset_path):
return dataset_path
# Define Gradio application
demo = gr.Blocks()
with demo:
# Create dataset
dataset_name = gr.Textbox(label='Dataset Name')
create_button = gr.Button('Create Dataset')
create_button.click(create_dataset, inputs=[dataset_name], outputs=[])
# Upload pair
image_upload = gr.File(label='Image')
prompt = gr.Textbox(label='Prompt')
upload_button = gr.Button('Upload Pair')
upload_button.click(upload_pair, inputs=[dataset_name, image_upload, prompt], outputs=[])
# Edit prompt
image_path = gr.Textbox(label='Image Path')
new_prompt = gr.Textbox(label='New Prompt')
edit_button = gr.Button('Edit Prompt')
edit_button.click(edit_prompt, inputs=[dataset_name, image_path, new_prompt], outputs=[])
# Delete pair
delete_button = gr.Button('Delete Pair')
delete_button.click(delete_pair, inputs=[dataset_name, image_path], outputs=[])
# Download dataset
download_button = gr.Button('Download Dataset')
download_button.click(download_dataset, inputs=[dataset_name], outputs=[])
# Upload dataset
dataset_upload = gr.File(label='Dataset')
upload_dataset_button = gr.Button('Upload Dataset')
upload_dataset_button.click(create_dataset, inputs=[dataset_upload], outputs=[])
# Horizontal gallery
gallery = gr.Gallery(label='Dataset Gallery')
demo.append(gallery)
# Launch Gradio application
demo.launch() |