Andre commited on
Commit
a282ad7
·
1 Parent(s): f69fcea
Files changed (3) hide show
  1. app old.py → app copy1.py +0 -0
  2. app copy2.py +166 -0
  3. app.py +12 -6
app old.py → app copy1.py RENAMED
File without changes
app copy2.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ from huggingface_hub import InferenceClient
4
+ from PIL import Image
5
+ import gradio as gr
6
+ from datetime import datetime
7
+
8
+ # Retrieve the Hugging Face token from environment variables
9
+ api_token = os.getenv("HF_CTB_TOKEN")
10
+
11
+ # Debugging: Check if the Hugging Face token is available
12
+ if not api_token:
13
+ print("ERROR: Hugging Face token (HF_CTB_TOKEN) is missing. Please set it as an environment variable.")
14
+ else:
15
+ print("Hugging Face token loaded successfully.")
16
+
17
+ # List of models with aliases
18
+ models = [
19
+ {"alias": "FLUX.1-dev", "name": "black-forest-labs/FLUX.1-dev"},
20
+ #{"alias": "Stable Diffusion 3.5 turbo", "name": "stabilityai/stable-diffusion-3.5-large-turbo"},
21
+ {"alias": "Midjourney", "name": "strangerzonehf/Flux-Midjourney-Mix2-LoRA"}
22
+ ]
23
+
24
+ # List of prompts with intense combat
25
+ prompts = [
26
+ {
27
+ "alias": "Castle Siege",
28
+ "text": "A medieval castle under siege, with archers firing arrows from the walls, knights charging on horses, and catapults launching fireballs. The enemy army, dressed in {enemy_color} armor, is fiercely attacking the castle, with soldiers scaling ladders and clashing swords with the defenders. Arrows fly through the air, explosions light up the battlefield, and injured knights lie on the ground. Fire engulfs parts of the castle, and the air is thick with smoke and chaos. Unreal Engine render style, photorealistic, realistic fantasy style."
29
+ },
30
+ {
31
+ "alias": "Forest Battle",
32
+ "text": "A fierce battle between two armies in a dense forest, with knights wielding swords and axes, horses rearing, and the ground covered in mud and blood. The enemy army, dressed in {enemy_color} armor, is locked in brutal combat, with soldiers fighting hand-to-hand amidst the trees. Arrows whiz past, and the sounds of clashing steel echo through the forest. Injured soldiers scream in pain, and the forest is littered with broken weapons and shields. Unreal Engine render style, photorealistic, realistic fantasy style."
33
+ },
34
+ {
35
+ "alias": "Boiling Oil Defense",
36
+ "text": "A dramatic moment in a medieval siege, with a knight leading a charge against a castle gate, while defenders pour boiling oil from the walls. The enemy army, dressed in {enemy_color} armor, is relentlessly attacking, with soldiers screaming as they are hit by the oil. Knights clash swords at the gate, and arrows rain down from above. The ground is littered with the bodies of fallen soldiers, and the air is filled with the smell of burning flesh. Unreal Engine render style, photorealistic, realistic fantasy style."
37
+ },
38
+ {
39
+ "alias": "Burning Castle Battle",
40
+ "text": "A chaotic battlefield with knights on horseback clashing with infantry, archers firing volleys of arrows, and a castle burning in the background. The enemy army, dressed in {enemy_color} armor, is fighting fiercely, with soldiers engaging in brutal melee combat. Flames light up the scene as knights charge through the chaos. Injured soldiers crawl on the ground, and the air is filled with the sounds of clashing steel and screams of pain. Unreal Engine render style, photorealistic, realistic fantasy style."
41
+ },
42
+ {
43
+ "alias": "Heroic Last Stand",
44
+ "text": "A heroic last stand of a small group of knights defending a bridge against a massive army, with arrows flying and swords clashing. The enemy army, dressed in {enemy_color} armor, is overwhelming the defenders, but the knights fight bravely, cutting down enemy soldiers as they advance. The bridge is littered with bodies and broken weapons. Blood stains the ground, and the air is thick with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
45
+ },
46
+ {
47
+ "alias": "Siege Tower Attack",
48
+ "text": "A medieval siege tower approaching a castle wall, with knights scaling ladders and defenders throwing rocks and shooting arrows. The enemy army, dressed in {enemy_color} armor, is fighting desperately to breach the walls, with soldiers clashing swords on the battlements. Arrows fly in all directions, and the siege tower is engulfed in flames. Injured soldiers fall from the ladders, and the ground is littered with the bodies of the fallen. Unreal Engine render style, photorealistic, realistic fantasy style."
49
+ },
50
+ {
51
+ "alias": "Knight Duel",
52
+ "text": "A dramatic duel between two knights in the middle of a battlefield, with their armies watching and the castle in the background. The enemy army, dressed in {enemy_color} armor, is engaged in fierce combat all around, with soldiers clashing swords and firing arrows. The duelists fight with skill and determination, their blades flashing in the sunlight. Injured soldiers lie on the ground, and the air is filled with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
53
+ },
54
+ {
55
+ "alias": "Night Battle",
56
+ "text": "A night battle during a medieval siege, with torches lighting the scene, knights fighting in the shadows, and the castle walls looming in the background. The enemy army, dressed in {enemy_color} armor, is locked in brutal combat, with soldiers clashing swords and firing arrows in the dim light. Flames from burning siege equipment illuminate the chaos. Injured soldiers scream in pain, and the ground is littered with the bodies of the fallen. Unreal Engine render style, photorealistic, realistic fantasy style."
57
+ },
58
+ {
59
+ "alias": "Marching Army",
60
+ "text": "A massive army of knights and infantry marching towards a distant castle, with banners flying and the sun setting behind them. The enemy army, dressed in {enemy_color} armor, is engaging in skirmishes along the way, with soldiers clashing swords and firing arrows. The battlefield is alive with the sounds of combat and the clash of steel. Injured soldiers lie on the ground, and the air is thick with the smell of blood and smoke. Unreal Engine render style, photorealistic, realistic fantasy style."
61
+ },
62
+ {
63
+ "alias": "Snowy Battlefield",
64
+ "text": "A medieval battle in a snowy landscape, with knights in heavy armor fighting on a frozen lake, and the castle visible in the distance. The enemy army, dressed in {enemy_color} armor, is locked in fierce combat, with soldiers slipping on the ice as they clash swords. Arrows fly through the air, and the snow is stained red with blood. Injured soldiers crawl on the ground, and the air is filled with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
65
+ }
66
+ ]
67
+
68
+ # Debugging: Print prompt and model options
69
+ print("Prompt Options:", [p["alias"] for p in prompts])
70
+ print("Model Options:", [m["alias"] for m in models])
71
+
72
+ # Function to generate images
73
+ def generate_image(prompt_alias, team, model_alias, height, width, num_inference_steps, guidance_scale, seed):
74
+ # Debugging: Check if the token is available
75
+ if not api_token:
76
+ return None, "ERROR: Hugging Face token (HF_CTB_TOKEN) is missing. Please set it as an environment variable."
77
+
78
+ # Find the selected prompt and model
79
+ try:
80
+ prompt = next(p for p in prompts if p["alias"] == prompt_alias)["text"]
81
+ model_name = next(m for m in models if m["alias"] == model_alias)["name"]
82
+ except StopIteration:
83
+ return None, "ERROR: Invalid prompt or model selected."
84
+
85
+ # Determine the enemy color
86
+ enemy_color = "blue" if team.lower() == "red" else "red"
87
+ prompt = prompt.format(enemy_color=enemy_color)
88
+
89
+ if team.lower() == "red":
90
+ prompt += " The winning army is dressed in red armor and banners."
91
+ elif team.lower() == "blue":
92
+ prompt += " The winning army is dressed in blue armor and banners."
93
+
94
+ # Randomize the seed if needed
95
+ if seed == -1:
96
+ seed = random.randint(0, 1000000)
97
+
98
+ # Initialize the InferenceClient
99
+ try:
100
+ client = InferenceClient(model_name, token=api_token)
101
+ except Exception as e:
102
+ return None, f"ERROR: Failed to initialize InferenceClient. Details: {e}"
103
+
104
+ # Generate the image
105
+ try:
106
+ image = client.text_to_image(
107
+ prompt,
108
+ guidance_scale=guidance_scale,
109
+ num_inference_steps=num_inference_steps,
110
+ width=width,
111
+ height=height,
112
+ seed=seed
113
+ )
114
+ except Exception as e:
115
+ return None, f"ERROR: Failed to generate image. Details: {e}"
116
+
117
+ # Save the image with a timestamped filename
118
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
119
+ output_filename = f"{timestamp}_{model_alias.replace(' ', '_').lower()}_{prompt_alias.replace(' ', '_').lower()}_{team.lower()}.png"
120
+ try:
121
+ image.save(output_filename)
122
+ except Exception as e:
123
+ return None, f"ERROR: Failed to save image. Details: {e}"
124
+
125
+ return output_filename, "Image generated successfully!"
126
+
127
+ # Gradio Interface
128
+ with gr.Blocks() as demo:
129
+ gr.Markdown("# CtB AI Image Generator")
130
+ with gr.Row():
131
+ # Set default values for dropdowns
132
+ prompt_dropdown = gr.Dropdown(choices=[p["alias"] for p in prompts], label="Select Prompt", value=prompts[0]["alias"])
133
+ team_dropdown = gr.Dropdown(choices=["Red", "Blue"], label="Select Team", value="Red")
134
+ model_dropdown = gr.Dropdown(choices=[m["alias"] for m in models], label="Select Model", value=models[0]["alias"])
135
+ #with gr.Row():
136
+ # Commented-out dialog boxes (can be re-enabled later)
137
+ # height_input = gr.Number(value=360, label="Height")
138
+ # width_input = gr.Number(value=640, label="Width")
139
+ # num_inference_steps_input = gr.Slider(minimum=10, maximum=100, value=20, label="Inference Steps")
140
+ # guidance_scale_input = gr.Slider(minimum=1.0, maximum=20.0, value=2.0, step=0.5, label="Guidance Scale")
141
+ # seed_input = gr.Number(value=-1, label="Seed (-1 for random)")
142
+ with gr.Row():
143
+ generate_button = gr.Button("Generate Image")
144
+ with gr.Row():
145
+ output_image = gr.Image(label="Generated Image")
146
+ with gr.Row():
147
+ status_text = gr.Textbox(label="Status", placeholder="Waiting for input...", interactive=False)
148
+
149
+ # Function to handle button click
150
+ def generate(prompt_alias, team, model_alias, height=360, width=640, num_inference_steps=20, guidance_scale=2.0, seed=-1):
151
+ try:
152
+ # Generate the image
153
+ image_path, message = generate_image(prompt_alias, team, model_alias, height, width, num_inference_steps, guidance_scale, seed)
154
+ return image_path, message
155
+ except Exception as e:
156
+ return None, f"An error occurred: {e}"
157
+
158
+ # Connect the button to the function
159
+ generate_button.click(
160
+ generate,
161
+ inputs=[prompt_dropdown, team_dropdown, model_dropdown], # Removed commented-out inputs
162
+ outputs=[output_image, status_text]
163
+ )
164
+
165
+ # Launch the Gradio app p
166
+ demo.launch()
app.py CHANGED
@@ -17,7 +17,6 @@ else:
17
  # List of models with aliases
18
  models = [
19
  {"alias": "FLUX.1-dev", "name": "black-forest-labs/FLUX.1-dev"},
20
- #{"alias": "Stable Diffusion 3.5 turbo", "name": "stabilityai/stable-diffusion-3.5-large-turbo"},
21
  {"alias": "Midjourney", "name": "strangerzonehf/Flux-Midjourney-Mix2-LoRA"}
22
  ]
23
 
@@ -70,7 +69,7 @@ print("Prompt Options:", [p["alias"] for p in prompts])
70
  print("Model Options:", [m["alias"] for m in models])
71
 
72
  # Function to generate images
73
- def generate_image(prompt_alias, team, model_alias, height, width, num_inference_steps, guidance_scale, seed):
74
  # Debugging: Check if the token is available
75
  if not api_token:
76
  return None, "ERROR: Hugging Face token (HF_CTB_TOKEN) is missing. Please set it as an environment variable."
@@ -91,6 +90,10 @@ def generate_image(prompt_alias, team, model_alias, height, width, num_inference
91
  elif team.lower() == "blue":
92
  prompt += " The winning army is dressed in blue armor and banners."
93
 
 
 
 
 
94
  # Randomize the seed if needed
95
  if seed == -1:
96
  seed = random.randint(0, 1000000)
@@ -132,6 +135,9 @@ with gr.Blocks() as demo:
132
  prompt_dropdown = gr.Dropdown(choices=[p["alias"] for p in prompts], label="Select Prompt", value=prompts[0]["alias"])
133
  team_dropdown = gr.Dropdown(choices=["Red", "Blue"], label="Select Team", value="Red")
134
  model_dropdown = gr.Dropdown(choices=[m["alias"] for m in models], label="Select Model", value=models[0]["alias"])
 
 
 
135
  #with gr.Row():
136
  # Commented-out dialog boxes (can be re-enabled later)
137
  # height_input = gr.Number(value=360, label="Height")
@@ -147,10 +153,10 @@ with gr.Blocks() as demo:
147
  status_text = gr.Textbox(label="Status", placeholder="Waiting for input...", interactive=False)
148
 
149
  # Function to handle button click
150
- def generate(prompt_alias, team, model_alias, height=360, width=640, num_inference_steps=20, guidance_scale=2.0, seed=-1):
151
  try:
152
  # Generate the image
153
- image_path, message = generate_image(prompt_alias, team, model_alias, height, width, num_inference_steps, guidance_scale, seed)
154
  return image_path, message
155
  except Exception as e:
156
  return None, f"An error occurred: {e}"
@@ -158,9 +164,9 @@ with gr.Blocks() as demo:
158
  # Connect the button to the function
159
  generate_button.click(
160
  generate,
161
- inputs=[prompt_dropdown, team_dropdown, model_dropdown], # Removed commented-out inputs
162
  outputs=[output_image, status_text]
163
  )
164
 
165
- # Launch the Gradio app p
166
  demo.launch()
 
17
  # List of models with aliases
18
  models = [
19
  {"alias": "FLUX.1-dev", "name": "black-forest-labs/FLUX.1-dev"},
 
20
  {"alias": "Midjourney", "name": "strangerzonehf/Flux-Midjourney-Mix2-LoRA"}
21
  ]
22
 
 
69
  print("Model Options:", [m["alias"] for m in models])
70
 
71
  # Function to generate images
72
+ def generate_image(prompt_alias, team, model_alias, custom_prompt, height, width, num_inference_steps, guidance_scale, seed):
73
  # Debugging: Check if the token is available
74
  if not api_token:
75
  return None, "ERROR: Hugging Face token (HF_CTB_TOKEN) is missing. Please set it as an environment variable."
 
90
  elif team.lower() == "blue":
91
  prompt += " The winning army is dressed in blue armor and banners."
92
 
93
+ # Append the custom prompt (if provided)
94
+ if custom_prompt and len(custom_prompt.strip()) > 0:
95
+ prompt += " " + custom_prompt.strip()
96
+
97
  # Randomize the seed if needed
98
  if seed == -1:
99
  seed = random.randint(0, 1000000)
 
135
  prompt_dropdown = gr.Dropdown(choices=[p["alias"] for p in prompts], label="Select Prompt", value=prompts[0]["alias"])
136
  team_dropdown = gr.Dropdown(choices=["Red", "Blue"], label="Select Team", value="Red")
137
  model_dropdown = gr.Dropdown(choices=[m["alias"] for m in models], label="Select Model", value=models[0]["alias"])
138
+ with gr.Row():
139
+ # Add a text box for custom user input (max 200 characters)
140
+ custom_prompt_input = gr.Textbox(label="Custom Prompt (Optional)", placeholder="Enter additional details (max 200 chars)...", max_lines=1, max_length=200)
141
  #with gr.Row():
142
  # Commented-out dialog boxes (can be re-enabled later)
143
  # height_input = gr.Number(value=360, label="Height")
 
153
  status_text = gr.Textbox(label="Status", placeholder="Waiting for input...", interactive=False)
154
 
155
  # Function to handle button click
156
+ def generate(prompt_alias, team, model_alias, custom_prompt, height=360, width=640, num_inference_steps=20, guidance_scale=2.0, seed=-1):
157
  try:
158
  # Generate the image
159
+ image_path, message = generate_image(prompt_alias, team, model_alias, custom_prompt, height, width, num_inference_steps, guidance_scale, seed)
160
  return image_path, message
161
  except Exception as e:
162
  return None, f"An error occurred: {e}"
 
164
  # Connect the button to the function
165
  generate_button.click(
166
  generate,
167
+ inputs=[prompt_dropdown, team_dropdown, model_dropdown, custom_prompt_input], # Added custom_prompt_input
168
  outputs=[output_image, status_text]
169
  )
170
 
171
+ # Launch the Gradio app
172
  demo.launch()