Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -77,10 +77,31 @@ def add_text_to_image(image_path, text):
|
|
77 |
|
78 |
return [img for _, img in sorted(images_with_text)]
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
def visualize_story_images(story):
|
81 |
if not story.strip():
|
82 |
return []
|
83 |
-
return
|
84 |
|
85 |
with gr.Blocks() as iface:
|
86 |
gr.Markdown("# Story Visualizer")
|
|
|
77 |
|
78 |
return [img for _, img in sorted(images_with_text)]
|
79 |
|
80 |
+
def process_story_lines(story, progress=gr.Progress()):
|
81 |
+
lines = [line.strip() for line in story.split('\n') if line.strip()]
|
82 |
+
images_with_text = []
|
83 |
+
|
84 |
+
def process_line(idx, line):
|
85 |
+
prompt = line.replace(" ", "_")
|
86 |
+
img_file = download_image(prompt, idx)
|
87 |
+
if img_file:
|
88 |
+
return add_text_to_image(img_file, line)
|
89 |
+
return None
|
90 |
+
|
91 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
92 |
+
future_to_idx = {executor.submit(process_line, idx, line): idx for idx, line in enumerate(lines)}
|
93 |
+
for future in progress.tqdm(as_completed(future_to_idx), total=len(lines), desc="Processing images"):
|
94 |
+
idx = future_to_idx[future]
|
95 |
+
result = future.result()
|
96 |
+
if result:
|
97 |
+
images_with_text.append((idx, result))
|
98 |
+
|
99 |
+
return [img for _, img in sorted(images_with_text)]
|
100 |
+
|
101 |
def visualize_story_images(story):
|
102 |
if not story.strip():
|
103 |
return []
|
104 |
+
return process_story_lines(story)
|
105 |
|
106 |
with gr.Blocks() as iface:
|
107 |
gr.Markdown("# Story Visualizer")
|