Spaces:
Running
Running
herodevcode commited on
Commit ·
6070720
1
Parent(s): b6cd6c8
Add Gradio app with RunwayML image generation
Browse files- app.py +209 -0
- generate_image.py +506 -0
- requirements.txt +4 -0
- text_handler.py +24 -0
app.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from text_handler import process_text
|
| 6 |
+
from generate_image import generate_and_wait_for_result
|
| 7 |
+
import concurrent.futures
|
| 8 |
+
|
| 9 |
+
def create_markdown_with_images(prompt, image_paths, batch_folder, reference_image_paths=None):
|
| 10 |
+
"""
|
| 11 |
+
Create a markdown file with the prompt, reference images, and generated image links.
|
| 12 |
+
"""
|
| 13 |
+
markdown_content = f"# Image Generation Results\n\n"
|
| 14 |
+
markdown_content += f"**Prompt:** {prompt}\n\n"
|
| 15 |
+
markdown_content += f"**Generated on:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
| 16 |
+
markdown_content += f"**Batch:** {batch_folder}\n\n"
|
| 17 |
+
|
| 18 |
+
if reference_image_paths:
|
| 19 |
+
markdown_content += "## Reference Images\n\n"
|
| 20 |
+
markdown_content += "| Character | Scene | Style |\n"
|
| 21 |
+
markdown_content += "|-----------|-------|-------|\n"
|
| 22 |
+
ref_cells = []
|
| 23 |
+
ref_labels = ["Character", "Scene", "Style"]
|
| 24 |
+
for i, ref_path in enumerate(reference_image_paths):
|
| 25 |
+
if ref_path and os.path.exists(ref_path):
|
| 26 |
+
ref_filename = os.path.basename(ref_path)
|
| 27 |
+
import shutil
|
| 28 |
+
ref_dest = os.path.join("output", batch_folder, f"ref_{ref_filename}")
|
| 29 |
+
shutil.copy2(ref_path, ref_dest)
|
| 30 |
+
ref_cells.append(f"![{ref_labels[i] if i < len(ref_labels) else 'Reference'}](ref_{ref_filename})<br>*{ref_filename}*")
|
| 31 |
+
else:
|
| 32 |
+
ref_cells.append("*No image*")
|
| 33 |
+
while len(ref_cells) < 3:
|
| 34 |
+
ref_cells.append("*No image*")
|
| 35 |
+
markdown_content += f"| {ref_cells[0]} | {ref_cells[1]} | {ref_cells[2]} |\n\n"
|
| 36 |
+
|
| 37 |
+
markdown_content += "## Generated Images\n\n"
|
| 38 |
+
for i, image_path in enumerate(image_paths, 1):
|
| 39 |
+
if image_path and os.path.exists(image_path):
|
| 40 |
+
filename = os.path.basename(image_path)
|
| 41 |
+
markdown_content += f"- **Image {i}:** ``\n"
|
| 42 |
+
else:
|
| 43 |
+
markdown_content += f"- **Image {i}:** *Generation failed*\n"
|
| 44 |
+
|
| 45 |
+
output_dir = os.path.join("output", batch_folder)
|
| 46 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 47 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 48 |
+
markdown_filename = f"generation_report_{timestamp}.md"
|
| 49 |
+
markdown_path = os.path.join(output_dir, markdown_filename)
|
| 50 |
+
|
| 51 |
+
with open(markdown_path, 'w', encoding='utf-8') as f:
|
| 52 |
+
f.write(markdown_content)
|
| 53 |
+
|
| 54 |
+
return markdown_path
|
| 55 |
+
|
| 56 |
+
def save_uploaded_image(image, folder_name):
|
| 57 |
+
"""
|
| 58 |
+
Save uploaded image to the appropriate assets folder.
|
| 59 |
+
"""
|
| 60 |
+
if image is None:
|
| 61 |
+
return image
|
| 62 |
+
assets_path = os.path.join("assets", folder_name)
|
| 63 |
+
os.makedirs(assets_path, exist_ok=True)
|
| 64 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 65 |
+
filename = f"uploaded_{timestamp}.jpg"
|
| 66 |
+
filepath = os.path.join(assets_path, filename)
|
| 67 |
+
image.save(filepath, "JPEG", quality=95)
|
| 68 |
+
print(f"Saved uploaded image to: {filepath}")
|
| 69 |
+
return image
|
| 70 |
+
|
| 71 |
+
def process_images(prompt, character_image, scene_image, style_image, num_images):
|
| 72 |
+
"""
|
| 73 |
+
Process text prompt and input images, generate specified number of images using RunwayML.
|
| 74 |
+
"""
|
| 75 |
+
num_images = int(num_images)
|
| 76 |
+
try:
|
| 77 |
+
processed_prompt = process_text(prompt)
|
| 78 |
+
|
| 79 |
+
reference_images = []
|
| 80 |
+
temp_paths = []
|
| 81 |
+
|
| 82 |
+
if character_image:
|
| 83 |
+
char_path = os.path.join("assets", "characters", f"temp_char_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
| 84 |
+
os.makedirs(os.path.dirname(char_path), exist_ok=True)
|
| 85 |
+
character_image.save(char_path, "JPEG", quality=95)
|
| 86 |
+
reference_images.append(char_path)
|
| 87 |
+
temp_paths.append(char_path)
|
| 88 |
+
|
| 89 |
+
if scene_image:
|
| 90 |
+
scene_path = os.path.join("assets", "scenes", f"temp_scene_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
| 91 |
+
os.makedirs(os.path.dirname(scene_path), exist_ok=True)
|
| 92 |
+
scene_image.save(scene_path, "JPEG", quality=95)
|
| 93 |
+
reference_images.append(scene_path)
|
| 94 |
+
temp_paths.append(scene_path)
|
| 95 |
+
|
| 96 |
+
if style_image:
|
| 97 |
+
style_path = os.path.join("assets", "styles", f"temp_style_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
| 98 |
+
os.makedirs(os.path.dirname(style_path), exist_ok=True)
|
| 99 |
+
style_image.save(style_path, "JPEG", quality=95)
|
| 100 |
+
reference_images.append(style_path)
|
| 101 |
+
temp_paths.append(style_path)
|
| 102 |
+
|
| 103 |
+
if not reference_images:
|
| 104 |
+
return "No reference images provided.", []
|
| 105 |
+
|
| 106 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 107 |
+
batch_folder = f"batch_{timestamp}"
|
| 108 |
+
batch_folder_path = os.path.join("output", batch_folder)
|
| 109 |
+
os.makedirs(batch_folder_path, exist_ok=True)
|
| 110 |
+
|
| 111 |
+
image_paths = [os.path.join(batch_folder_path, f"generated_{i+1}_{timestamp}.jpg") for i in range(num_images)]
|
| 112 |
+
|
| 113 |
+
def generate_single_image(index):
|
| 114 |
+
filename = os.path.basename(image_paths[index])
|
| 115 |
+
task_id, generated_image_path = generate_and_wait_for_result(
|
| 116 |
+
prompt_text=processed_prompt,
|
| 117 |
+
reference_image_paths=reference_images,
|
| 118 |
+
auto_tag_prompt=False,
|
| 119 |
+
filename=filename,
|
| 120 |
+
batch_folder=batch_folder
|
| 121 |
+
)
|
| 122 |
+
return index, task_id, generated_image_path
|
| 123 |
+
|
| 124 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=num_images) as executor:
|
| 125 |
+
futures = [executor.submit(generate_single_image, i) for i in range(num_images)]
|
| 126 |
+
completed_image_paths = [None] * num_images
|
| 127 |
+
for future in concurrent.futures.as_completed(futures):
|
| 128 |
+
index, task_id, generated_image_path = future.result()
|
| 129 |
+
completed_image_paths[index] = generated_image_path
|
| 130 |
+
|
| 131 |
+
markdown_path = create_markdown_with_images(
|
| 132 |
+
prompt=prompt,
|
| 133 |
+
image_paths=completed_image_paths,
|
| 134 |
+
batch_folder=batch_folder,
|
| 135 |
+
reference_image_paths=reference_images
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
for temp_path in temp_paths:
|
| 139 |
+
if os.path.exists(temp_path):
|
| 140 |
+
os.remove(temp_path)
|
| 141 |
+
|
| 142 |
+
existing_image_paths = [path for path in completed_image_paths if path and os.path.exists(path)]
|
| 143 |
+
status_msg = f"{processed_prompt}\n\nGenerated {len(existing_image_paths)}/{num_images} images in batch: {batch_folder}\nMarkdown report: {markdown_path}"
|
| 144 |
+
|
| 145 |
+
return status_msg, existing_image_paths
|
| 146 |
+
|
| 147 |
+
except Exception as e:
|
| 148 |
+
return f"Error: {str(e)}", []
|
| 149 |
+
|
| 150 |
+
# Gradio Interface
|
| 151 |
+
with gr.Blocks(title="RunwayML Image Generation") as demo:
|
| 152 |
+
gr.Markdown("# RunwayML Image Generation")
|
| 153 |
+
gr.Markdown("Generate images using RunwayML with reference images. Choose how many images to generate (1-4).")
|
| 154 |
+
|
| 155 |
+
with gr.Tab("RunwayML with References"):
|
| 156 |
+
with gr.Row():
|
| 157 |
+
num_images = gr.Dropdown(
|
| 158 |
+
choices=["1", "2", "3", "4"],
|
| 159 |
+
value="4",
|
| 160 |
+
label="Number of Images to Generate"
|
| 161 |
+
)
|
| 162 |
+
runway_prompt_input = gr.Textbox(
|
| 163 |
+
label="Text Prompt",
|
| 164 |
+
placeholder="Enter your prompt here...",
|
| 165 |
+
value="@character is in @scene with @style style"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
with gr.Row():
|
| 169 |
+
character_input = gr.Image(label="Character", type="pil")
|
| 170 |
+
scene_input = gr.Image(label="Scene", type="pil")
|
| 171 |
+
style_input = gr.Image(label="Style", type="pil")
|
| 172 |
+
|
| 173 |
+
runway_process_btn = gr.Button("Generate Images", variant="primary")
|
| 174 |
+
|
| 175 |
+
runway_output_text = gr.Textbox(label="Status", interactive=False)
|
| 176 |
+
runway_gallery = gr.Gallery(
|
| 177 |
+
label="Generated Images",
|
| 178 |
+
show_label=True,
|
| 179 |
+
elem_id="gallery",
|
| 180 |
+
columns=2,
|
| 181 |
+
rows=2
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# Event handlers
|
| 185 |
+
character_input.upload(
|
| 186 |
+
fn=lambda img: save_uploaded_image(img, "characters"),
|
| 187 |
+
inputs=[character_input],
|
| 188 |
+
outputs=[character_input]
|
| 189 |
+
)
|
| 190 |
+
scene_input.upload(
|
| 191 |
+
fn=lambda img: save_uploaded_image(img, "scenes"),
|
| 192 |
+
inputs=[scene_input],
|
| 193 |
+
outputs=[scene_input]
|
| 194 |
+
)
|
| 195 |
+
style_input.upload(
|
| 196 |
+
fn=lambda img: save_uploaded_image(img, "styles"),
|
| 197 |
+
inputs=[style_input],
|
| 198 |
+
outputs=[style_input]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
runway_process_btn.click(
|
| 202 |
+
fn=process_images,
|
| 203 |
+
inputs=[runway_prompt_input, character_input, scene_input, style_input, num_images],
|
| 204 |
+
outputs=[runway_output_text, runway_gallery]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
demo.queue(default_concurrency_limit=2, max_size=20)
|
| 209 |
+
demo.launch()
|
generate_image.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
import time
|
| 4 |
+
import requests
|
| 5 |
+
from typing import List, Optional, Tuple
|
| 6 |
+
from runwayml import RunwayML
|
| 7 |
+
import mimetypes
|
| 8 |
+
from urllib.parse import urlparse
|
| 9 |
+
import replicate
|
| 10 |
+
|
| 11 |
+
def encode_image_to_data_uri(image_path: str) -> str:
|
| 12 |
+
"""Convert a local image file to a data URI."""
|
| 13 |
+
# Get the MIME type
|
| 14 |
+
mime_type, _ = mimetypes.guess_type(image_path)
|
| 15 |
+
if not mime_type or not mime_type.startswith('image/'):
|
| 16 |
+
raise ValueError(f"Unsupported image type for {image_path}")
|
| 17 |
+
|
| 18 |
+
# Read and encode the image
|
| 19 |
+
with open(image_path, 'rb') as image_file:
|
| 20 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 21 |
+
|
| 22 |
+
return f"data:{mime_type};base64,{encoded_string}"
|
| 23 |
+
|
| 24 |
+
def save_generated_image(image_url: str, filename: str = None, batch_folder: str = None) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Download and save the generated image to a timestamped batch folder.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
image_url: URL of the generated image
|
| 30 |
+
filename: Optional filename (auto-generated if not provided)
|
| 31 |
+
batch_folder: Optional batch folder name (auto-generated with timestamp if not provided)
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
Path to the saved image file
|
| 35 |
+
"""
|
| 36 |
+
# Create batch folder if not provided
|
| 37 |
+
if not batch_folder:
|
| 38 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 39 |
+
batch_folder = f"batch_{timestamp}"
|
| 40 |
+
|
| 41 |
+
# Create directory structure
|
| 42 |
+
output_dir = os.path.join("output", batch_folder)
|
| 43 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 44 |
+
|
| 45 |
+
# Generate filename if not provided
|
| 46 |
+
if not filename:
|
| 47 |
+
timestamp = int(time.time())
|
| 48 |
+
filename = f"generated_{timestamp}.jpg"
|
| 49 |
+
|
| 50 |
+
# Ensure filename has extension
|
| 51 |
+
if not os.path.splitext(filename)[1]:
|
| 52 |
+
filename += ".jpg"
|
| 53 |
+
|
| 54 |
+
output_path = os.path.join(output_dir, filename)
|
| 55 |
+
|
| 56 |
+
# Download and save the image
|
| 57 |
+
response = requests.get(image_url)
|
| 58 |
+
response.raise_for_status()
|
| 59 |
+
|
| 60 |
+
with open(output_path, 'wb') as f:
|
| 61 |
+
f.write(response.content)
|
| 62 |
+
|
| 63 |
+
return output_path
|
| 64 |
+
|
| 65 |
+
def generate_image_with_references(
|
| 66 |
+
prompt_text: str,
|
| 67 |
+
reference_image_paths: List[str],
|
| 68 |
+
ratio: str = "1920:1080",
|
| 69 |
+
model: str = "gen4_image",
|
| 70 |
+
seed: Optional[int] = None,
|
| 71 |
+
api_key: Optional[str] = None,
|
| 72 |
+
auto_tag_prompt: bool = True
|
| 73 |
+
) -> str:
|
| 74 |
+
"""
|
| 75 |
+
Generate an image using RunwayML API with reference images.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
prompt_text: Description of the image to generate (max 1000 characters)
|
| 79 |
+
reference_image_paths: List of local image file paths to use as references
|
| 80 |
+
ratio: Output image resolution (default: "1920:1080")
|
| 81 |
+
model: Model to use (default: "gen4_image")
|
| 82 |
+
seed: Optional seed for reproducible results
|
| 83 |
+
api_key: Optional API key (uses RUNWAYML_API_SECRET env var if not provided)
|
| 84 |
+
auto_tag_prompt: Whether to automatically append tags to prompt (default: True)
|
| 85 |
+
When False, expects user to manually include @character, @scene, @style in prompt
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
Task ID for the generation request
|
| 89 |
+
"""
|
| 90 |
+
# Initialize client
|
| 91 |
+
client = RunwayML(api_key=api_key or os.environ.get("RUNWAYML_API_SECRET"))
|
| 92 |
+
|
| 93 |
+
# Validate inputs
|
| 94 |
+
if len(reference_image_paths) > 3:
|
| 95 |
+
raise ValueError("Maximum 3 reference images allowed")
|
| 96 |
+
|
| 97 |
+
if len(prompt_text) > 1000:
|
| 98 |
+
raise ValueError("Prompt text must be 1000 characters or less")
|
| 99 |
+
|
| 100 |
+
# Prepare reference images with standardized tags
|
| 101 |
+
reference_images = []
|
| 102 |
+
tags = []
|
| 103 |
+
|
| 104 |
+
# Keep track of used standard tags to avoid duplicates
|
| 105 |
+
used_standard_tags = set()
|
| 106 |
+
|
| 107 |
+
for i, image_path in enumerate(reference_image_paths):
|
| 108 |
+
if not os.path.exists(image_path):
|
| 109 |
+
raise FileNotFoundError(f"Image file not found: {image_path}")
|
| 110 |
+
|
| 111 |
+
# Create tag based on path structure, prioritizing standard categories
|
| 112 |
+
filename = os.path.splitext(os.path.basename(image_path))[0]
|
| 113 |
+
path_parts = image_path.split(os.sep)
|
| 114 |
+
|
| 115 |
+
# Look for standard category directories
|
| 116 |
+
tag = None
|
| 117 |
+
for part in path_parts:
|
| 118 |
+
if part == 'characters' and 'character' not in used_standard_tags:
|
| 119 |
+
tag = 'character'
|
| 120 |
+
used_standard_tags.add('character')
|
| 121 |
+
break
|
| 122 |
+
elif part == 'scenes' and 'scene' not in used_standard_tags:
|
| 123 |
+
tag = 'scene'
|
| 124 |
+
used_standard_tags.add('scene')
|
| 125 |
+
break
|
| 126 |
+
elif part == 'styles' and 'style' not in used_standard_tags:
|
| 127 |
+
tag = 'style'
|
| 128 |
+
used_standard_tags.add('style')
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
# If no standard category found, create a custom tag from filename
|
| 132 |
+
if not tag:
|
| 133 |
+
tag = f"ref_{filename}".replace('-', '_').replace(' ', '_')[:16]
|
| 134 |
+
# Ensure tag starts with letter and is alphanumeric + underscore
|
| 135 |
+
tag = ''.join(c for c in tag if c.isalnum() or c == '_')
|
| 136 |
+
if not tag[0].isalpha():
|
| 137 |
+
tag = f"img_{tag}"
|
| 138 |
+
tag = tag[:16] # Ensure max 16 characters
|
| 139 |
+
|
| 140 |
+
tags.append(tag)
|
| 141 |
+
|
| 142 |
+
# Convert to data URI
|
| 143 |
+
data_uri = encode_image_to_data_uri(image_path)
|
| 144 |
+
|
| 145 |
+
reference_images.append({
|
| 146 |
+
"uri": data_uri,
|
| 147 |
+
"tag": tag
|
| 148 |
+
})
|
| 149 |
+
|
| 150 |
+
# Handle prompt modification based on auto_tag_prompt setting
|
| 151 |
+
final_prompt = prompt_text
|
| 152 |
+
if auto_tag_prompt and tags:
|
| 153 |
+
# Auto-append tags to prompt
|
| 154 |
+
tag_mentions = " ".join([f"@{tag}" for tag in tags])
|
| 155 |
+
final_prompt = f"{prompt_text} using references: {tag_mentions}"
|
| 156 |
+
|
| 157 |
+
# Ensure we don't exceed character limit
|
| 158 |
+
if len(final_prompt) > 1000:
|
| 159 |
+
# Try without the descriptive text
|
| 160 |
+
tag_mentions = " ".join([f"@{tag}" for tag in tags])
|
| 161 |
+
final_prompt = f"{prompt_text} {tag_mentions}"
|
| 162 |
+
|
| 163 |
+
# If still too long, truncate prompt text
|
| 164 |
+
if len(final_prompt) > 1000:
|
| 165 |
+
available_chars = 1000 - len(tag_mentions) - 1
|
| 166 |
+
final_prompt = f"{prompt_text[:available_chars]} {tag_mentions}"
|
| 167 |
+
|
| 168 |
+
print(f"Using tags: {tags}")
|
| 169 |
+
if auto_tag_prompt:
|
| 170 |
+
print(f"Auto-tagged prompt: {final_prompt}")
|
| 171 |
+
else:
|
| 172 |
+
print(f"Manual tagging mode - use @{', @'.join(tags)} in your prompt")
|
| 173 |
+
print(f"Original prompt: {final_prompt}")
|
| 174 |
+
|
| 175 |
+
# Prepare the request parameters
|
| 176 |
+
create_params = {
|
| 177 |
+
"model": model,
|
| 178 |
+
"prompt_text": final_prompt,
|
| 179 |
+
"ratio": ratio,
|
| 180 |
+
"reference_images": reference_images
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
# Only include seed if it's not None
|
| 184 |
+
if seed is not None:
|
| 185 |
+
create_params["seed"] = seed
|
| 186 |
+
|
| 187 |
+
# Create the generation task
|
| 188 |
+
task = client.text_to_image.create(**create_params)
|
| 189 |
+
|
| 190 |
+
return task.id
|
| 191 |
+
|
| 192 |
+
def check_task_status(task_id: str, api_key: Optional[str] = None):
|
| 193 |
+
"""
|
| 194 |
+
Check the status of a generation task.
|
| 195 |
+
|
| 196 |
+
Args:
|
| 197 |
+
task_id: The task ID returned from generate_image_with_references
|
| 198 |
+
api_key: Optional API key (uses RUNWAYML_API_SECRET env var if not provided)
|
| 199 |
+
|
| 200 |
+
Returns:
|
| 201 |
+
Task details including status and output URLs if completed
|
| 202 |
+
"""
|
| 203 |
+
client = RunwayML(api_key=api_key or os.environ.get("RUNWAYML_API_SECRET"))
|
| 204 |
+
return client.tasks.retrieve(id=task_id)
|
| 205 |
+
|
| 206 |
+
def generate_and_wait_for_result(
|
| 207 |
+
prompt_text: str,
|
| 208 |
+
reference_image_paths: List[str],
|
| 209 |
+
ratio: str = "1920:1080",
|
| 210 |
+
model: str = "gen4_image",
|
| 211 |
+
seed: Optional[int] = None,
|
| 212 |
+
api_key: Optional[str] = None,
|
| 213 |
+
filename: str = None,
|
| 214 |
+
batch_folder: str = None,
|
| 215 |
+
max_retries: int = 8,
|
| 216 |
+
wait_interval: int = 15,
|
| 217 |
+
auto_tag_prompt: bool = True
|
| 218 |
+
) -> Tuple[str, str]:
|
| 219 |
+
"""
|
| 220 |
+
Generate an image and wait for completion with automatic retries.
|
| 221 |
+
|
| 222 |
+
Args:
|
| 223 |
+
prompt_text: Description of the image to generate
|
| 224 |
+
reference_image_paths: List of local image file paths to use as references
|
| 225 |
+
ratio: Output image resolution
|
| 226 |
+
model: Model to use
|
| 227 |
+
seed: Optional seed for reproducible results
|
| 228 |
+
api_key: Optional API key
|
| 229 |
+
filename: Optional filename for saved image
|
| 230 |
+
max_retries: Maximum number of status checks (default: 8)
|
| 231 |
+
wait_interval: Seconds to wait between checks (default: 15)
|
| 232 |
+
auto_tag_prompt: Whether to automatically append tags to prompt
|
| 233 |
+
|
| 234 |
+
Returns:
|
| 235 |
+
Tuple of (task_id, saved_image_path)
|
| 236 |
+
"""
|
| 237 |
+
# Start the generation task
|
| 238 |
+
task_id = generate_image_with_references(
|
| 239 |
+
prompt_text=prompt_text,
|
| 240 |
+
reference_image_paths=reference_image_paths,
|
| 241 |
+
ratio=ratio,
|
| 242 |
+
model=model,
|
| 243 |
+
seed=seed,
|
| 244 |
+
api_key=api_key,
|
| 245 |
+
auto_tag_prompt=auto_tag_prompt
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
print(f"Image generation started. Task ID: {task_id}")
|
| 249 |
+
print(f"Checking status every {wait_interval} seconds (max {max_retries} attempts)...")
|
| 250 |
+
|
| 251 |
+
# Wait and check status
|
| 252 |
+
for attempt in range(max_retries):
|
| 253 |
+
print(f"Attempt {attempt + 1}/{max_retries} - Waiting {wait_interval} seconds...")
|
| 254 |
+
time.sleep(wait_interval)
|
| 255 |
+
|
| 256 |
+
try:
|
| 257 |
+
status = check_task_status(task_id, api_key)
|
| 258 |
+
print(f"Status: {status.status}")
|
| 259 |
+
|
| 260 |
+
if status.status == "SUCCEEDED":
|
| 261 |
+
if hasattr(status, 'output') and status.output:
|
| 262 |
+
image_url = status.output[0]
|
| 263 |
+
print(f"Generation completed! Image URL: {image_url}")
|
| 264 |
+
|
| 265 |
+
# Save the image
|
| 266 |
+
saved_path = save_generated_image(image_url, filename, batch_folder)
|
| 267 |
+
print(f"Image saved to: {saved_path}")
|
| 268 |
+
|
| 269 |
+
return task_id, saved_path
|
| 270 |
+
else:
|
| 271 |
+
print("Task succeeded but no output found")
|
| 272 |
+
return task_id, None
|
| 273 |
+
|
| 274 |
+
elif status.status == "FAILED":
|
| 275 |
+
print("Task failed")
|
| 276 |
+
return task_id, None
|
| 277 |
+
|
| 278 |
+
elif status.status in ["PENDING", "RUNNING"]:
|
| 279 |
+
print("Task still in progress...")
|
| 280 |
+
continue
|
| 281 |
+
|
| 282 |
+
except Exception as e:
|
| 283 |
+
print(f"Error checking status: {e}")
|
| 284 |
+
if attempt == max_retries - 1:
|
| 285 |
+
print("Max retries reached. Task may still be processing.")
|
| 286 |
+
return task_id, None
|
| 287 |
+
|
| 288 |
+
print(f"Timeout after {max_retries} attempts. Task may still be processing.")
|
| 289 |
+
print(f"You can manually check status later using task ID: {task_id}")
|
| 290 |
+
return task_id, None
|
| 291 |
+
|
| 292 |
+
def generate_image_with_replicate_imagen(
|
| 293 |
+
prompt: str,
|
| 294 |
+
aspect_ratio: str = "1:1",
|
| 295 |
+
output_format: str = "jpg",
|
| 296 |
+
model: str = "google/imagen-4-fast",
|
| 297 |
+
safety_filter_level: str = "block_only_high",
|
| 298 |
+
filename: str = None,
|
| 299 |
+
api_token: Optional[str] = None
|
| 300 |
+
) -> str:
|
| 301 |
+
"""
|
| 302 |
+
Generate an image using Replicate's Google Imagen models.
|
| 303 |
+
|
| 304 |
+
Args:
|
| 305 |
+
prompt: Text prompt for image generation
|
| 306 |
+
aspect_ratio: Aspect ratio of the generated image (default: "1:1")
|
| 307 |
+
output_format: Format of the output image (default: "jpg")
|
| 308 |
+
model: Imagen model to use (default: "google/imagen-4-fast")
|
| 309 |
+
safety_filter_level: Safety filter level (default: "block_only_high")
|
| 310 |
+
filename: Optional filename for saved image
|
| 311 |
+
api_token: Optional API token (uses REPLICATE_API_TOKEN env var if not provided)
|
| 312 |
+
|
| 313 |
+
Returns:
|
| 314 |
+
Path to the saved image file
|
| 315 |
+
"""
|
| 316 |
+
# Set API token
|
| 317 |
+
if api_token:
|
| 318 |
+
os.environ["REPLICATE_API_TOKEN"] = api_token
|
| 319 |
+
elif not os.environ.get("REPLICATE_API_TOKEN"):
|
| 320 |
+
raise ValueError("REPLICATE_API_TOKEN environment variable must be set or api_token must be provided")
|
| 321 |
+
|
| 322 |
+
print(f"Generating image with model: {model}")
|
| 323 |
+
print(f"Prompt: {prompt}")
|
| 324 |
+
print(f"Aspect ratio: {aspect_ratio}, Format: {output_format}")
|
| 325 |
+
|
| 326 |
+
# Run the model
|
| 327 |
+
try:
|
| 328 |
+
output = replicate.run(
|
| 329 |
+
model,
|
| 330 |
+
input={
|
| 331 |
+
"prompt": prompt,
|
| 332 |
+
"aspect_ratio": aspect_ratio,
|
| 333 |
+
"output_format": output_format,
|
| 334 |
+
"safety_filter_level": safety_filter_level
|
| 335 |
+
}
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
# The output is a URL string
|
| 339 |
+
image_url = output
|
| 340 |
+
print(f"Image generated successfully: {image_url}")
|
| 341 |
+
|
| 342 |
+
# Save the image
|
| 343 |
+
saved_path = save_generated_image(image_url, filename)
|
| 344 |
+
print(f"Image saved to: {saved_path}")
|
| 345 |
+
|
| 346 |
+
return saved_path
|
| 347 |
+
|
| 348 |
+
except Exception as e:
|
| 349 |
+
print(f"Error generating image with Replicate Imagen: {e}")
|
| 350 |
+
raise
|
| 351 |
+
|
| 352 |
+
def main():
|
| 353 |
+
"""Example usage with model selection between runway and imagen-fast."""
|
| 354 |
+
print("=== Image Generation Model Selection ===")
|
| 355 |
+
print("Available models:")
|
| 356 |
+
print("1. runway - RunwayML with reference images")
|
| 357 |
+
print("2. imagen-fast - Replicate's Google Imagen 4 Fast")
|
| 358 |
+
|
| 359 |
+
model_choice = input("Enter model choice (runway/imagen-fast): ").strip().lower()
|
| 360 |
+
|
| 361 |
+
if model_choice == "runway":
|
| 362 |
+
print("\n=== Testing RunwayML with Reference Images ===")
|
| 363 |
+
# Example reference images
|
| 364 |
+
reference_images = [
|
| 365 |
+
"assets/characters/japanese_guy.jpg",
|
| 366 |
+
"assets/scenes/f1-fields.jpg",
|
| 367 |
+
"assets/styles/f1-cockpit.jpg"
|
| 368 |
+
]
|
| 369 |
+
|
| 370 |
+
print("=== Manual Tagging Mode (Default for Testing) ===")
|
| 371 |
+
# Example with manual tagging (auto_tag_prompt=False)
|
| 372 |
+
manual_prompt = "@character in a @scene with @style composition, cinematic lighting, high detail"
|
| 373 |
+
|
| 374 |
+
try:
|
| 375 |
+
task_id, saved_path = generate_and_wait_for_result(
|
| 376 |
+
prompt_text=manual_prompt,
|
| 377 |
+
reference_image_paths=reference_images,
|
| 378 |
+
ratio="1920:1080",
|
| 379 |
+
filename="f1_driver_manual_tags.jpg",
|
| 380 |
+
auto_tag_prompt=False # Manual tagging mode
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
if saved_path:
|
| 384 |
+
print(f"Manual tagging success! Image saved to: {saved_path}")
|
| 385 |
+
else:
|
| 386 |
+
print(f"Manual tagging incomplete. Task ID: {task_id}")
|
| 387 |
+
|
| 388 |
+
except Exception as e:
|
| 389 |
+
print(f"Manual tagging error: {e}")
|
| 390 |
+
|
| 391 |
+
print("\n" + "="*50)
|
| 392 |
+
print("=== Auto Tagging Mode Example ===")
|
| 393 |
+
# Example with automatic tagging (auto_tag_prompt=True)
|
| 394 |
+
auto_prompt = "A Japanese F1 driver in a cockpit style setting on a racing field, cinematic lighting, high detail"
|
| 395 |
+
|
| 396 |
+
try:
|
| 397 |
+
task_id, saved_path = generate_and_wait_for_result(
|
| 398 |
+
prompt_text=auto_prompt,
|
| 399 |
+
reference_image_paths=reference_images,
|
| 400 |
+
ratio="1920:1080",
|
| 401 |
+
filename="f1_driver_auto_tags.jpg",
|
| 402 |
+
auto_tag_prompt=True # Auto tagging mode
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
if saved_path:
|
| 406 |
+
print(f"Auto tagging success! Image saved to: {saved_path}")
|
| 407 |
+
else:
|
| 408 |
+
print(f"Auto tagging incomplete. Task ID: {task_id}")
|
| 409 |
+
|
| 410 |
+
except Exception as e:
|
| 411 |
+
print(f"Auto tagging error: {e}")
|
| 412 |
+
|
| 413 |
+
elif model_choice == "imagen-fast":
|
| 414 |
+
print("\n=== Testing Replicate's Google Imagen 4 Fast ===")
|
| 415 |
+
|
| 416 |
+
# Get prompt from user or use default
|
| 417 |
+
prompt = input("Enter image prompt (or press Enter for default): ").strip()
|
| 418 |
+
if not prompt:
|
| 419 |
+
prompt = "A cinematic shot of a futuristic sports car racing through a neon-lit cyberpunk city at night, high detail, dramatic lighting"
|
| 420 |
+
|
| 421 |
+
# Get aspect ratio
|
| 422 |
+
aspect_ratio = input("Enter aspect ratio (default 16:9): ").strip() or "16:9"
|
| 423 |
+
|
| 424 |
+
# Get model version
|
| 425 |
+
model_version = input("Enter model version (fast/ultra, default fast): ").strip().lower() or "fast"
|
| 426 |
+
model_name = "google/imagen-4-fast" if model_version == "fast" else "google/imagen-4-ultra"
|
| 427 |
+
|
| 428 |
+
try:
|
| 429 |
+
saved_path = generate_image_with_replicate_imagen(
|
| 430 |
+
prompt=prompt,
|
| 431 |
+
aspect_ratio=aspect_ratio,
|
| 432 |
+
model=model_name,
|
| 433 |
+
filename="imagen_test.jpg"
|
| 434 |
+
)
|
| 435 |
+
print(f"Imagen generation success! Image saved to: {saved_path}")
|
| 436 |
+
|
| 437 |
+
except Exception as e:
|
| 438 |
+
print(f"Imagen generation error: {e}")
|
| 439 |
+
|
| 440 |
+
else:
|
| 441 |
+
print(f"Invalid model choice: {model_choice}")
|
| 442 |
+
print("Please choose either 'runway' or 'imagen-fast'")
|
| 443 |
+
|
| 444 |
+
def example_manual_tagging():
|
| 445 |
+
"""
|
| 446 |
+
Example function demonstrating manual tagging mode.
|
| 447 |
+
When auto_tag_prompt=False, users must include @character, @scene, @style in their prompts.
|
| 448 |
+
"""
|
| 449 |
+
reference_images = [
|
| 450 |
+
"assets/characters/anime_girl.jpg",
|
| 451 |
+
"assets/scenes/cyberpunk_city.jpg",
|
| 452 |
+
"assets/styles/neon_art.jpg"
|
| 453 |
+
]
|
| 454 |
+
|
| 455 |
+
# Manual prompt with explicit tag references
|
| 456 |
+
prompt_with_tags = """
|
| 457 |
+
A futuristic @character standing in a @cyberpunk @scene
|
| 458 |
+
with @style aesthetic, glowing neon lights, 4k resolution
|
| 459 |
+
""".strip()
|
| 460 |
+
|
| 461 |
+
print("Manual Tagging Example:")
|
| 462 |
+
print(f"Prompt: {prompt_with_tags}")
|
| 463 |
+
|
| 464 |
+
try:
|
| 465 |
+
task_id, saved_path = generate_and_wait_for_result(
|
| 466 |
+
prompt_text=prompt_with_tags,
|
| 467 |
+
reference_image_paths=reference_images,
|
| 468 |
+
auto_tag_prompt=False, # Disabled - expects manual @tags
|
| 469 |
+
filename="cyberpunk_manual.jpg"
|
| 470 |
+
)
|
| 471 |
+
return task_id, saved_path
|
| 472 |
+
except Exception as e:
|
| 473 |
+
print(f"Error in manual tagging example: {e}")
|
| 474 |
+
return None, None
|
| 475 |
+
|
| 476 |
+
def example_auto_tagging():
|
| 477 |
+
"""
|
| 478 |
+
Example function demonstrating auto tagging mode.
|
| 479 |
+
When auto_tag_prompt=True, tags are automatically appended to the prompt.
|
| 480 |
+
"""
|
| 481 |
+
reference_images = [
|
| 482 |
+
"assets/characters/warrior.jpg",
|
| 483 |
+
"assets/scenes/medieval_castle.jpg",
|
| 484 |
+
"assets/styles/oil_painting.jpg"
|
| 485 |
+
]
|
| 486 |
+
|
| 487 |
+
# Simple prompt without tag references
|
| 488 |
+
simple_prompt = "A brave warrior defending a castle, epic fantasy art"
|
| 489 |
+
|
| 490 |
+
print("Auto Tagging Example:")
|
| 491 |
+
print(f"Original prompt: {simple_prompt}")
|
| 492 |
+
|
| 493 |
+
try:
|
| 494 |
+
task_id, saved_path = generate_and_wait_for_result(
|
| 495 |
+
prompt_text=simple_prompt,
|
| 496 |
+
reference_image_paths=reference_images,
|
| 497 |
+
auto_tag_prompt=True, # Enabled - automatically adds @tags
|
| 498 |
+
filename="fantasy_auto.jpg"
|
| 499 |
+
)
|
| 500 |
+
return task_id, saved_path
|
| 501 |
+
except Exception as e:
|
| 502 |
+
print(f"Error in auto tagging example: {e}")
|
| 503 |
+
return None, None
|
| 504 |
+
|
| 505 |
+
if __name__ == "__main__":
|
| 506 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pillow
|
| 3 |
+
requests
|
| 4 |
+
runwayml
|
text_handler.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def process_text(input_text):
|
| 2 |
+
"""
|
| 3 |
+
Function that takes text input and returns it to terminal
|
| 4 |
+
|
| 5 |
+
Args:
|
| 6 |
+
input_text (str): The text to be processed and returned
|
| 7 |
+
|
| 8 |
+
Returns:
|
| 9 |
+
str: The same text that was input
|
| 10 |
+
"""
|
| 11 |
+
print(f"Input received: {input_text}")
|
| 12 |
+
return input_text
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
"""
|
| 16 |
+
Main function to demonstrate the text processing
|
| 17 |
+
"""
|
| 18 |
+
# Example usage
|
| 19 |
+
user_input = input("Enter some text: ")
|
| 20 |
+
result = process_text(user_input)
|
| 21 |
+
print(f"Returned text: {result}")
|
| 22 |
+
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
main()
|