Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,9 +96,19 @@ def generate_pdf(images, explanations):
|
|
| 96 |
doc.build(story)
|
| 97 |
return tmp.name
|
| 98 |
|
| 99 |
-
# --------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
def generate_scene(prompt, images, summaries, explanations):
|
| 102 |
image = generate_image_from_text(prompt)
|
| 103 |
summary = summarize_scene(prompt)
|
| 104 |
explanation = explain_scene(image) if image else "Explanation unavailable."
|
|
@@ -107,91 +117,84 @@ def generate_scene(prompt, images, summaries, explanations):
|
|
| 107 |
summaries.append(summary)
|
| 108 |
explanations.append(explanation)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def edit_scene(index, theme, char_count, character_names, dialogue, images, summaries, explanations):
|
| 113 |
-
prompt = f"Scene {index+1} set in {theme}. Characters: {character_names}. Dialogue: '{dialogue}'"
|
| 114 |
-
image = generate_image_from_text(prompt)
|
| 115 |
-
summary = summarize_scene(prompt)
|
| 116 |
-
explanation = explain_scene(image)
|
| 117 |
-
|
| 118 |
-
images[index] = image
|
| 119 |
-
summaries[index] = summary
|
| 120 |
-
explanations[index] = explanation
|
| 121 |
-
|
| 122 |
-
return images, summaries, explanations
|
| 123 |
|
| 124 |
-
|
| 125 |
-
if 0 <= index < len(images):
|
| 126 |
-
del images[index]
|
| 127 |
-
del summaries[index]
|
| 128 |
-
del explanations[index]
|
| 129 |
-
return images, summaries, explanations
|
| 130 |
|
| 131 |
def finalize_story(images, explanations):
|
| 132 |
if not images or not explanations:
|
| 133 |
return None, None
|
|
|
|
| 134 |
pdf = generate_pdf(images, explanations)
|
|
|
|
| 135 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as txt:
|
| 136 |
for i, exp in enumerate(explanations):
|
| 137 |
txt.write(f"Scene {i+1}:\n{exp}\n\n")
|
| 138 |
txt_path = txt.name
|
|
|
|
| 139 |
return txt_path, pdf
|
| 140 |
|
| 141 |
-
# -------------------- UI
|
| 142 |
|
| 143 |
-
with gr.Blocks(title="
|
| 144 |
-
gr.Markdown("## π¬ AI
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
character_names = gr.Textbox(label="Character Names")
|
| 151 |
-
dialogue = gr.Textbox(label="Dialogue")
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
scene_images = gr.State([])
|
| 165 |
scene_explanations = gr.State([])
|
| 166 |
scene_summaries = gr.State([])
|
| 167 |
|
| 168 |
-
def build_prompt(index, theme, character_names, dialogue):
|
| 169 |
-
return f"Scene {index+1} set in {theme}. Characters: {character_names}. Dialogue: '{dialogue}'"
|
| 170 |
-
|
| 171 |
generate_btn.click(
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
outputs=[scene_images, scene_summaries, scene_explanations, thumbnail_gallery]
|
| 183 |
)
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
inputs=[index_to_edit, scene_images, scene_summaries, scene_explanations],
|
| 188 |
-
outputs=[scene_images, scene_summaries, scene_explanations, thumbnail_gallery]
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
finalize_btn.click(
|
| 192 |
-
finalize_story,
|
| 193 |
inputs=[scene_images, scene_explanations],
|
| 194 |
outputs=[txt_file, pdf_file]
|
| 195 |
)
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
doc.build(story)
|
| 97 |
return tmp.name
|
| 98 |
|
| 99 |
+
# -------------------- Core Logic --------------------
|
| 100 |
+
|
| 101 |
+
def generate_scene(num_scenes, theme, char_count, character_names, dialogue,
|
| 102 |
+
images, summaries, explanations):
|
| 103 |
+
|
| 104 |
+
if len(images) >= int(num_scenes):
|
| 105 |
+
return gr.update(), gr.update(), gr.update(), images, summaries, explanations, \
|
| 106 |
+
f"β
All {num_scenes} scenes have been generated.", gr.update(visible=True)
|
| 107 |
+
|
| 108 |
+
prompt = f"Scene {len(images)+1} set in {theme}. Number of characters: {char_count}. Characters: {character_names}."
|
| 109 |
+
if dialogue:
|
| 110 |
+
prompt += f' Include this dialogue: "{dialogue}".'
|
| 111 |
|
|
|
|
| 112 |
image = generate_image_from_text(prompt)
|
| 113 |
summary = summarize_scene(prompt)
|
| 114 |
explanation = explain_scene(image) if image else "Explanation unavailable."
|
|
|
|
| 117 |
summaries.append(summary)
|
| 118 |
explanations.append(explanation)
|
| 119 |
|
| 120 |
+
status = f"β
Scene {len(images)} of {int(num_scenes)} generated."
|
| 121 |
+
done_visible = len(images) == int(num_scenes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
return image, summary, explanation, images, summaries, explanations, status, gr.update(visible=done_visible)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
def finalize_story(images, explanations):
|
| 126 |
if not images or not explanations:
|
| 127 |
return None, None
|
| 128 |
+
|
| 129 |
pdf = generate_pdf(images, explanations)
|
| 130 |
+
|
| 131 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as txt:
|
| 132 |
for i, exp in enumerate(explanations):
|
| 133 |
txt.write(f"Scene {i+1}:\n{exp}\n\n")
|
| 134 |
txt_path = txt.name
|
| 135 |
+
|
| 136 |
return txt_path, pdf
|
| 137 |
|
| 138 |
+
# -------------------- UI --------------------
|
| 139 |
|
| 140 |
+
with gr.Blocks(title="Scene-by-Scene Story Generator") as demo:
|
| 141 |
+
gr.Markdown("## π¬ AI Scene-by-Scene Story Creator")
|
| 142 |
+
gr.Markdown("Describe your story one scene at a time, with AI-generated images, summaries, and explanations.")
|
| 143 |
|
| 144 |
+
with gr.Accordion("π§© Story Setup", open=True):
|
| 145 |
+
scene_total = gr.Number(label="π’ Number of Scenes", precision=0, value=3)
|
| 146 |
+
theme = gr.Textbox(label="π Global Theme", placeholder="e.g. A magical forest full of glowing creatures")
|
| 147 |
|
| 148 |
+
gr.Markdown("### β¨ Describe Your Next Scene")
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
with gr.Row():
|
| 151 |
+
char_count = gr.Number(label="π₯ Number of Characters", precision=0, value=2)
|
| 152 |
+
character_names = gr.Textbox(label="π§ββοΈ Character Names", placeholder="e.g. Elora, Bramble the Bear")
|
| 153 |
+
dialogue = gr.Textbox(label="π¬ Dialogue (optional)", placeholder="e.g. 'Protect the forest!'")
|
| 154 |
|
| 155 |
+
generate_btn = gr.Button("β Generate This Scene")
|
| 156 |
+
status = gr.Markdown()
|
| 157 |
|
| 158 |
+
image_output = gr.Image(label="πΌοΈ Scene Image", type="pil")
|
| 159 |
+
summary_output = gr.Markdown(label="π Scene Summary")
|
| 160 |
+
explanation_output = gr.Textbox(label="π Scene Explanation", lines=6)
|
| 161 |
|
| 162 |
+
with gr.Row():
|
| 163 |
+
tts_btn = gr.Button("π Read Aloud")
|
| 164 |
+
tts_audio = gr.Audio(label="Audio", autoplay=False)
|
| 165 |
+
|
| 166 |
+
done_btn = gr.Button("β
Finalize Story & Export", visible=False)
|
| 167 |
+
txt_file = gr.File(label="π Explanations (.txt)")
|
| 168 |
+
pdf_file = gr.File(label="π Scene PDF")
|
| 169 |
+
|
| 170 |
+
# Persistent session states
|
| 171 |
scene_images = gr.State([])
|
| 172 |
scene_explanations = gr.State([])
|
| 173 |
scene_summaries = gr.State([])
|
| 174 |
|
|
|
|
|
|
|
|
|
|
| 175 |
generate_btn.click(
|
| 176 |
+
fn=generate_scene,
|
| 177 |
+
inputs=[
|
| 178 |
+
scene_total, theme, char_count, character_names, dialogue,
|
| 179 |
+
scene_images, scene_summaries, scene_explanations
|
| 180 |
+
],
|
| 181 |
+
outputs=[
|
| 182 |
+
image_output, summary_output, explanation_output,
|
| 183 |
+
scene_images, scene_summaries, scene_explanations,
|
| 184 |
+
status, done_btn
|
| 185 |
+
]
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
+
done_btn.click(
|
| 189 |
+
fn=finalize_story,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
inputs=[scene_images, scene_explanations],
|
| 191 |
outputs=[txt_file, pdf_file]
|
| 192 |
)
|
| 193 |
|
| 194 |
+
tts_btn.click(
|
| 195 |
+
fn=text_to_speech,
|
| 196 |
+
inputs=[explanation_output],
|
| 197 |
+
outputs=[tts_audio]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
demo.launch()
|