throaway2854 commited on
Commit
db9ae99
1 Parent(s): 8ca7af9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -17
app.py CHANGED
@@ -1,5 +1,33 @@
1
  import gradio as gr
2
  import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def generate_prompt(scene_tags, num_people, position_tags, characters, outfit_tags, camera_tags, concept_tags, lora_tags, num_tags=3):
5
  all_tags = {
@@ -10,67 +38,85 @@ def generate_prompt(scene_tags, num_people, position_tags, characters, outfit_ta
10
  "concept": concept_tags.split(','),
11
  }
12
 
13
- # Remove empty strings and strip whitespace
14
  for category in all_tags:
15
  all_tags[category] = [tag.strip() for tag in all_tags[category] if tag.strip()]
16
 
17
- # Process characters
18
  character_list = [char.strip() for char in characters.split(';') if char.strip()]
19
  character_prompts = []
20
- for i, character in enumerate(character_list):
21
  char_tags = character.split(',')
22
  char_name = char_tags[0].strip()
23
  char_traits = [tag.strip() for tag in char_tags[1:] if tag.strip()]
24
  char_prompt = f"{char_name}, " + ", ".join(random.sample(char_traits, min(num_tags, len(char_traits))))
25
  character_prompts.append(char_prompt)
26
 
27
- # Generate main prompt
28
  selected_tags = []
29
  for category, tags in all_tags.items():
30
  if tags:
31
  selected_tags.extend(f"{tag}:{random.uniform(0.8, 1.2):.2f}" for tag in random.sample(tags, min(num_tags, len(tags))))
32
 
33
- # Add number of people
34
  if num_people.strip():
35
  selected_tags.append(f"{num_people} people:1.1")
36
 
37
- # Combine all parts of the prompt
38
  prompt_parts = character_prompts + selected_tags
39
  random.shuffle(prompt_parts)
40
  main_prompt = ", ".join(prompt_parts)
41
 
42
- # Add LORA tags
43
  lora_list = [lora.strip() for lora in lora_tags.split(',') if lora.strip()]
44
  lora_prompt = " ".join(f"<lora:{lora}:1>" for lora in lora_list)
45
 
46
- # Add fixed tags at the end
47
  fixed_tags = "source_anime, score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres, anime artwork, anime style, vibrant, studio anime, highly detailed"
48
 
49
  full_prompt = f"{main_prompt}, {fixed_tags} {lora_prompt}".strip()
50
  return full_prompt
51
 
 
 
 
 
 
 
 
 
 
 
 
52
  def create_ui():
 
 
53
  with gr.Blocks() as demo:
54
- gr.Markdown("# Advanced Pony SDXL Prompt Generator")
55
 
56
  with gr.Row():
57
  with gr.Column():
58
- scene_input = gr.Textbox(label="Scene Tags (comma-separated)")
59
  num_people_input = gr.Textbox(label="Number of People")
60
- position_input = gr.Textbox(label="Position Tags (comma-separated)")
61
- characters_input = gr.Textbox(label="Characters (Format: Name, Trait1, Trait2; Name2, Trait1, Trait2)")
62
- outfit_input = gr.Textbox(label="Outfit Tags (comma-separated)")
63
- camera_input = gr.Textbox(label="Camera View/Angle Tags (comma-separated)")
64
- concept_input = gr.Textbox(label="Concept Tags (comma-separated)")
65
- lora_input = gr.Textbox(label="LORA Tags (comma-separated)")
 
66
  num_tags_slider = gr.Slider(minimum=1, maximum=5, step=1, value=3, label="Number of tags per category")
67
  generate_button = gr.Button("Generate Prompt")
68
 
69
  with gr.Column():
70
  output = gr.Textbox(label="Generated Prompt", lines=5)
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  generate_button.click(
73
- generate_prompt,
74
  inputs=[scene_input, num_people_input, position_input, characters_input, outfit_input, camera_input, concept_input, lora_input, num_tags_slider],
75
  outputs=[output]
76
  )
 
1
  import gradio as gr
2
  import random
3
+ import json
4
+ from huggingface_hub import HfApi, hf_hub_download, upload_file
5
+
6
+ REPO_ID = "throaway2854/promptGen" # Replace with your actual Space name
7
+ FILE_NAME = "saved_data.json"
8
+
9
+ def load_data():
10
+ try:
11
+ file_path = hf_hub_download(repo_id=REPO_ID, filename=FILE_NAME)
12
+ with open(file_path, 'r') as f:
13
+ return json.load(f)
14
+ except:
15
+ return {
16
+ "scene_tags": [], "position_tags": [], "outfit_tags": [],
17
+ "camera_tags": [], "concept_tags": [], "lora_tags": [],
18
+ "characters": {}
19
+ }
20
+
21
+ def save_data(data):
22
+ with open(FILE_NAME, 'w') as f:
23
+ json.dump(data, f)
24
+ api = HfApi()
25
+ api.upload_file(
26
+ path_or_fileobj=FILE_NAME,
27
+ path_in_repo=FILE_NAME,
28
+ repo_id=REPO_ID,
29
+ repo_type="space"
30
+ )
31
 
32
  def generate_prompt(scene_tags, num_people, position_tags, characters, outfit_tags, camera_tags, concept_tags, lora_tags, num_tags=3):
33
  all_tags = {
 
38
  "concept": concept_tags.split(','),
39
  }
40
 
 
41
  for category in all_tags:
42
  all_tags[category] = [tag.strip() for tag in all_tags[category] if tag.strip()]
43
 
 
44
  character_list = [char.strip() for char in characters.split(';') if char.strip()]
45
  character_prompts = []
46
+ for character in character_list:
47
  char_tags = character.split(',')
48
  char_name = char_tags[0].strip()
49
  char_traits = [tag.strip() for tag in char_tags[1:] if tag.strip()]
50
  char_prompt = f"{char_name}, " + ", ".join(random.sample(char_traits, min(num_tags, len(char_traits))))
51
  character_prompts.append(char_prompt)
52
 
 
53
  selected_tags = []
54
  for category, tags in all_tags.items():
55
  if tags:
56
  selected_tags.extend(f"{tag}:{random.uniform(0.8, 1.2):.2f}" for tag in random.sample(tags, min(num_tags, len(tags))))
57
 
 
58
  if num_people.strip():
59
  selected_tags.append(f"{num_people} people:1.1")
60
 
 
61
  prompt_parts = character_prompts + selected_tags
62
  random.shuffle(prompt_parts)
63
  main_prompt = ", ".join(prompt_parts)
64
 
 
65
  lora_list = [lora.strip() for lora in lora_tags.split(',') if lora.strip()]
66
  lora_prompt = " ".join(f"<lora:{lora}:1>" for lora in lora_list)
67
 
 
68
  fixed_tags = "source_anime, score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres, anime artwork, anime style, vibrant, studio anime, highly detailed"
69
 
70
  full_prompt = f"{main_prompt}, {fixed_tags} {lora_prompt}".strip()
71
  return full_prompt
72
 
73
+ def update_data(data, key, value):
74
+ if isinstance(data[key], list):
75
+ data[key] = list(set(data[key] + [v.strip() for v in value.split(',') if v.strip()]))
76
+ elif isinstance(data[key], dict):
77
+ for char in [c.strip() for c in value.split(';') if c.strip()]:
78
+ char_name, *char_traits = [t.strip() for t in char.split(',')]
79
+ if char_name:
80
+ data[key][char_name] = list(set(data[key].get(char_name, []) + char_traits))
81
+ save_data(data)
82
+ return data
83
+
84
  def create_ui():
85
+ data = load_data()
86
+
87
  with gr.Blocks() as demo:
88
+ gr.Markdown("# Advanced Pony SDXL Prompt Generator with Persistent Storage")
89
 
90
  with gr.Row():
91
  with gr.Column():
92
+ scene_input = gr.Textbox(label="Scene Tags (comma-separated)", value=", ".join(data["scene_tags"]))
93
  num_people_input = gr.Textbox(label="Number of People")
94
+ position_input = gr.Textbox(label="Position Tags (comma-separated)", value=", ".join(data["position_tags"]))
95
+ characters_input = gr.Textbox(label="Characters (Format: Name, Trait1, Trait2; Name2, Trait1, Trait2)",
96
+ value="; ".join([f"{name}, {', '.join(traits)}" for name, traits in data["characters"].items()]))
97
+ outfit_input = gr.Textbox(label="Outfit Tags (comma-separated)", value=", ".join(data["outfit_tags"]))
98
+ camera_input = gr.Textbox(label="Camera View/Angle Tags (comma-separated)", value=", ".join(data["camera_tags"]))
99
+ concept_input = gr.Textbox(label="Concept Tags (comma-separated)", value=", ".join(data["concept_tags"]))
100
+ lora_input = gr.Textbox(label="LORA Tags (comma-separated)", value=", ".join(data["lora_tags"]))
101
  num_tags_slider = gr.Slider(minimum=1, maximum=5, step=1, value=3, label="Number of tags per category")
102
  generate_button = gr.Button("Generate Prompt")
103
 
104
  with gr.Column():
105
  output = gr.Textbox(label="Generated Prompt", lines=5)
106
 
107
+ def update_and_generate(*args):
108
+ nonlocal data
109
+ data = update_data(data, "scene_tags", args[0])
110
+ data = update_data(data, "position_tags", args[2])
111
+ data = update_data(data, "characters", args[3])
112
+ data = update_data(data, "outfit_tags", args[4])
113
+ data = update_data(data, "camera_tags", args[5])
114
+ data = update_data(data, "concept_tags", args[6])
115
+ data = update_data(data, "lora_tags", args[7])
116
+ return generate_prompt(*args)
117
+
118
  generate_button.click(
119
+ update_and_generate,
120
  inputs=[scene_input, num_people_input, position_input, characters_input, outfit_input, camera_input, concept_input, lora_input, num_tags_slider],
121
  outputs=[output]
122
  )