Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -142,31 +142,43 @@ def finalize_story(images, explanations):
|
|
| 142 |
|
| 143 |
return txt_path, pdf
|
| 144 |
|
| 145 |
-
def ai_write_scene(theme):
|
| 146 |
try:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
except Exception as e:
|
| 168 |
print("AI write error:", e)
|
| 169 |
-
return "", "", "", "", "", "Fantasy"
|
|
|
|
| 170 |
|
| 171 |
# -------------------- UI --------------------
|
| 172 |
|
|
@@ -229,10 +241,11 @@ with gr.Blocks(title="Scene-by-Scene Story Generator") as demo:
|
|
| 229 |
|
| 230 |
ai_coauthor_btn.click(
|
| 231 |
fn=ai_write_scene,
|
| 232 |
-
inputs=[theme],
|
| 233 |
-
outputs=[character_names, dialogue, dialogue_speaker, char_styles, char_moods, bg_style]
|
| 234 |
)
|
| 235 |
|
|
|
|
| 236 |
done_btn.click(
|
| 237 |
fn=finalize_story,
|
| 238 |
inputs=[scene_images, scene_explanations],
|
|
|
|
| 142 |
|
| 143 |
return txt_path, pdf
|
| 144 |
|
| 145 |
+
def ai_write_scene(theme, total_scenes, scene_summaries):
|
| 146 |
try:
|
| 147 |
+
scene_index = len(scene_summaries) + 1
|
| 148 |
+
if scene_index > int(total_scenes):
|
| 149 |
+
return "", "", "", "", "", "Fantasy", f"✅ All {total_scenes} scenes completed."
|
| 150 |
+
|
| 151 |
+
story_so_far = "\n".join([f"Scene {i+1}: {s}" for i, s in enumerate(scene_summaries)]) if scene_summaries else ""
|
| 152 |
+
prompt = f"""
|
| 153 |
+
You are co-writing a story set in this theme: "{theme}".
|
| 154 |
+
Generate Scene {scene_index} of {total_scenes}.
|
| 155 |
+
Continue the story logically based on previous scenes (if any):
|
| 156 |
+
|
| 157 |
+
{story_so_far}
|
| 158 |
+
|
| 159 |
+
Return:
|
| 160 |
+
1. Character names (existing or new),
|
| 161 |
+
2. A single dialogue line,
|
| 162 |
+
3. The speaker of that dialogue,
|
| 163 |
+
4. Outfits worn,
|
| 164 |
+
5. Characters' emotional moods,
|
| 165 |
+
6. Background style (choose from: Realistic, Cartoon, Fantasy, Dark Fantasy).
|
| 166 |
+
Only provide the raw values, no headers.
|
| 167 |
+
"""
|
| 168 |
+
response = text_model.generate_content(prompt).text.strip()
|
| 169 |
+
parts = [line.strip() for line in response.split("\n") if line.strip()]
|
| 170 |
+
char_names = parts[0] if len(parts) > 0 else ""
|
| 171 |
+
dialogue = parts[1] if len(parts) > 1 else ""
|
| 172 |
+
speaker = parts[2] if len(parts) > 2 else ""
|
| 173 |
+
outfits = parts[3] if len(parts) > 3 else ""
|
| 174 |
+
moods = parts[4] if len(parts) > 4 else ""
|
| 175 |
+
bg_style = parts[5] if len(parts) > 5 else "Fantasy"
|
| 176 |
+
|
| 177 |
+
return char_names, dialogue, speaker, outfits, moods, bg_style, f"📝 Scene {scene_index} ready to generate."
|
| 178 |
except Exception as e:
|
| 179 |
print("AI write error:", e)
|
| 180 |
+
return "", "", "", "", "", "Fantasy", "⚠️ AI scene generation failed."
|
| 181 |
+
|
| 182 |
|
| 183 |
# -------------------- UI --------------------
|
| 184 |
|
|
|
|
| 241 |
|
| 242 |
ai_coauthor_btn.click(
|
| 243 |
fn=ai_write_scene,
|
| 244 |
+
inputs=[theme, scene_total, scene_summaries],
|
| 245 |
+
outputs=[character_names, dialogue, dialogue_speaker, char_styles, char_moods, bg_style, status]
|
| 246 |
)
|
| 247 |
|
| 248 |
+
|
| 249 |
done_btn.click(
|
| 250 |
fn=finalize_story,
|
| 251 |
inputs=[scene_images, scene_explanations],
|