fizzarif7 commited on
Commit
fb4628b
·
verified ·
1 Parent(s): cef3655

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -24
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
- prompt = f"""You're helping a writer generate a new scene for a story set in: "{theme}".
148
- Please respond with:
149
- 1. Character names (2-3 max),
150
- 2. A short dialogue line,
151
- 3. Who says the dialogue,
152
- 4. Their outfits,
153
- 5. Their mood,
154
- 6. Background style (one of: Realistic, Cartoon, Fantasy, Dark Fantasy)."""
155
-
156
- response = text_model.generate_content(prompt).text
157
- lines = response.splitlines()
158
-
159
- char_names = next((l.split(":", 1)[1].strip() for l in lines if "Character" in l), "")
160
- dialogue = next((l.split(":", 1)[1].strip() for l in lines if "dialogue" in l.lower()), "")
161
- speaker = next((l.split(":", 1)[1].strip() for l in lines if "says" in l.lower()), "")
162
- outfits = next((l.split(":", 1)[1].strip() for l in lines if "outfits" in l.lower()), "")
163
- moods = next((l.split(":", 1)[1].strip() for l in lines if "mood" in l.lower()), "")
164
- bg_style = next((l.split(":", 1)[1].strip() for l in lines if "background" in l.lower()), "Fantasy")
165
-
166
- return char_names, dialogue, speaker, outfits, moods, bg_style
 
 
 
 
 
 
 
 
 
 
 
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],