willsh1997 commited on
Commit
4edde26
·
1 Parent(s): 0c4a319

:bug: incorrect obj method - removed status printout

Browse files
Files changed (1) hide show
  1. gradio_neutral_input_func.py +46 -46
gradio_neutral_input_func.py CHANGED
@@ -6,7 +6,7 @@ import json
6
  import uuid
7
  import os
8
  from stable_diffusion_demo import StableDiffusion
9
- from datasets import Dataset, Features, Value, Image as HFImage, load_dataset
10
  import tempfile
11
 
12
  # Setup directories
@@ -47,51 +47,51 @@ def load_dataset_from_hf():
47
 
48
  def save_to_hf_dataset(image, description):
49
  """Save new image and description to HuggingFace dataset"""
 
 
 
 
 
50
  try:
51
- # Generate UUID for the new entry
52
- image_id = str(uuid.uuid4())
 
 
 
 
 
 
 
 
 
 
53
 
54
- # Load existing dataset
55
- try:
56
- existing_dataset = load_dataset(DATASET_REPO, split="train")
57
- except:
58
- # Create empty dataset if it doesn't exist
59
- existing_dataset = Dataset.from_dict({
60
- "image": [],
61
- "description": [],
62
- "uuid": []
63
- }).cast_column("image", HFImage())
64
 
65
- # Create temporary file for the image
66
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
67
- image.save(tmp_file.name, format='PNG')
68
-
69
- # Create new entry
70
- new_entry = {
71
- "image": [tmp_file.name],
72
- "description": [description],
73
- "uuid": [image_id]
74
- }
75
-
76
- # Create new dataset from the entry
77
- new_dataset = Dataset.from_dict(new_entry).cast_column("image", HFImage())
78
-
79
- # Concatenate with existing dataset
80
- if len(existing_dataset) > 0:
81
- combined_dataset = existing_dataset.concatenate(new_dataset)
82
- else:
83
- combined_dataset = new_dataset
84
-
85
- # Push to HuggingFace Hub
86
- combined_dataset.push_to_hub(DATASET_REPO, private=False, token=HF_TOKEN)
87
-
88
- # Clean up temporary file
89
- os.unlink(tmp_file.name)
90
-
91
- return True, "Successfully saved to HuggingFace dataset!"
92
 
93
- except Exception as e:
94
- return False, f"Error saving to HuggingFace: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  def save_image_and_description(image, description):
97
  """Save the generated image and its description to HuggingFace dataset"""
@@ -118,9 +118,9 @@ def save_image_and_description(image, description):
118
  except:
119
  pass # Local save is just backup, don't fail if it doesn't work
120
 
121
- return message, None, load_previous_examples()
122
  else:
123
- return message, None, None
124
 
125
  def load_previous_examples():
126
  """Load examples from HuggingFace dataset"""
@@ -221,7 +221,7 @@ with gr.Blocks(title="Neutral Image App") as demo:
221
  image_output = gr.Image(type="pil", label="Generated Image", interactive=False)
222
  description_input = gr.Textbox(label="Describe the image", lines=3)
223
  save_btn = gr.Button("Save Image and Description")
224
- status_output = gr.Textbox(label="Status")
225
 
226
  with gr.Accordion("Previous Examples", open=False):
227
  gallery = gr.Gallery(
@@ -240,7 +240,7 @@ with gr.Blocks(title="Neutral Image App") as demo:
240
  save_btn.click(
241
  fn=save_image_and_description,
242
  inputs=[image_output, description_input],
243
- outputs=[status_output, image_output, gallery]
244
  )
245
 
246
  refresh_btn.click(
 
6
  import uuid
7
  import os
8
  from stable_diffusion_demo import StableDiffusion
9
+ from datasets import Dataset, Features, Value, Image as HFImage, load_dataset, concatenate_datasets
10
  import tempfile
11
 
12
  # Setup directories
 
47
 
48
  def save_to_hf_dataset(image, description):
49
  """Save new image and description to HuggingFace dataset"""
50
+ # try:
51
+ # Generate UUID for the new entry
52
+ image_id = str(uuid.uuid4())
53
+
54
+ # Load existing dataset
55
  try:
56
+ existing_dataset = load_dataset(DATASET_REPO, split="train")
57
+ except:
58
+ # Create empty dataset if it doesn't exist
59
+ existing_dataset = Dataset.from_dict({
60
+ "image": [],
61
+ "description": [],
62
+ "uuid": []
63
+ }).cast_column("image", HFImage())
64
+
65
+ # Create temporary file for the image
66
+ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
67
+ image.save(tmp_file.name, format='PNG')
68
 
69
+ # Create new entry
70
+ new_entry = {
71
+ "image": [tmp_file.name],
72
+ "description": [description],
73
+ "uuid": [image_id]
74
+ }
 
 
 
 
75
 
76
+ # Create new dataset from the entry
77
+ new_dataset = Dataset.from_dict(new_entry).cast_column("image", HFImage())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ # Concatenate with existing dataset
80
+ if len(existing_dataset) > 0:
81
+ combined_dataset = concatenate_datasets([existing_dataset, new_dataset])
82
+ else:
83
+ combined_dataset = new_dataset
84
+
85
+ # Push to HuggingFace Hub
86
+ combined_dataset.push_to_hub(DATASET_REPO, private=False, token=HF_TOKEN)
87
+
88
+ # Clean up temporary file
89
+ os.unlink(tmp_file.name)
90
+
91
+ return True, "Successfully saved to HuggingFace dataset!"
92
+
93
+ # except Exception as e:
94
+ # return False, f"Error saving to HuggingFace: {str(e)}"
95
 
96
  def save_image_and_description(image, description):
97
  """Save the generated image and its description to HuggingFace dataset"""
 
118
  except:
119
  pass # Local save is just backup, don't fail if it doesn't work
120
 
121
+ return None, load_previous_examples()
122
  else:
123
+ return None, None
124
 
125
  def load_previous_examples():
126
  """Load examples from HuggingFace dataset"""
 
221
  image_output = gr.Image(type="pil", label="Generated Image", interactive=False)
222
  description_input = gr.Textbox(label="Describe the image", lines=3)
223
  save_btn = gr.Button("Save Image and Description")
224
+ # status_output = gr.Textbox(label="Status")
225
 
226
  with gr.Accordion("Previous Examples", open=False):
227
  gallery = gr.Gallery(
 
240
  save_btn.click(
241
  fn=save_image_and_description,
242
  inputs=[image_output, description_input],
243
+ outputs=[image_output, gallery]
244
  )
245
 
246
  refresh_btn.click(