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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from crewai import Agent, Task, Crew, Process, LLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from markdown import markdown
|
| 5 |
+
|
| 6 |
+
llm = LLM(
|
| 7 |
+
model='gemini/gemini-1.5-flash',
|
| 8 |
+
api_key=os.environ["GEMINI_API_KEY"]
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Agents
|
| 13 |
+
def generate_fantasy_outline(user_idea:str):
|
| 14 |
+
idea_extractor = Agent(
|
| 15 |
+
role="Idea Extractor",
|
| 16 |
+
goal=f"Extract and refine a raw fantasy story idea into structured elements from {user_idea}.",
|
| 17 |
+
backstory="You are a creative consultant helping fantasy writers refine their initial story ideas. "
|
| 18 |
+
"You specialize in identifying characters, setting, tone, conflict, and themes. "
|
| 19 |
+
"You guide the entire outlining process by establishing a clear vision.",
|
| 20 |
+
llm=llm,
|
| 21 |
+
allow_delegation=False,
|
| 22 |
+
verbose=True,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
worldbuilder = Agent(
|
| 26 |
+
role="Worldbuilding Expert",
|
| 27 |
+
goal="Design an ULTRA-DETAILED fantasy world with comprehensive systems.",
|
| 28 |
+
backstory="You are a seasoned worldbuilder who constructs vivid and believable fantasy realms. "
|
| 29 |
+
"You define geography, cultures, magic systems, political structures, and mythologies "
|
| 30 |
+
"that serve as the foundation for storytelling.",
|
| 31 |
+
llm=llm,
|
| 32 |
+
allow_delegation=False,
|
| 33 |
+
verbose=True,
|
| 34 |
+
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
character_architect = Agent(
|
| 38 |
+
role="Character Architect",
|
| 39 |
+
goal="Create detailed, multi-dimensional characters who drive the story forward.",
|
| 40 |
+
backstory="You are a character development expert who crafts heroes, villains, and side characters. "
|
| 41 |
+
"You define their backstories, motivations, arcs, relationships, and emotional journeys.",
|
| 42 |
+
llm=llm,
|
| 43 |
+
allow_delegation=False,
|
| 44 |
+
verbose=True,
|
| 45 |
+
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
plot_strategist = Agent(
|
| 49 |
+
role="Plot Structure Strategist",
|
| 50 |
+
goal="Generate a structured fantasy plot using traditional narrative frameworks.",
|
| 51 |
+
backstory="You are a master storyteller who weaves plots using the Hero’s Journey, Three Act Structure, and other frameworks. "
|
| 52 |
+
"You ensure that the plot flows logically and engages readers from start to finish.",
|
| 53 |
+
llm=llm,
|
| 54 |
+
allow_delegation=False,
|
| 55 |
+
verbose=True,
|
| 56 |
+
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
scene_director = Agent(
|
| 60 |
+
role="Scene Breakdown Specialist",
|
| 61 |
+
goal="Translate the structured plot into detailed scene-by-scene or chapter-wise outlines.",
|
| 62 |
+
backstory="You specialize in breaking stories into vivid scenes. "
|
| 63 |
+
"You provide clear settings, goals, conflicts, and turning points for each scene "
|
| 64 |
+
"to help the writer visualize and write the novel more easily.",
|
| 65 |
+
llm=llm,
|
| 66 |
+
allow_delegation=False,
|
| 67 |
+
verbose=True,
|
| 68 |
+
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
theme_advisor = Agent(
|
| 72 |
+
role="Theme & Tone Advisor",
|
| 73 |
+
goal="Ensure thematic consistency and emotional depth across the outline align with {user_idea}.",
|
| 74 |
+
backstory="You are a literary expert who analyzes and suggests core themes, tones, and motifs. "
|
| 75 |
+
"You help the story feel emotionally resonant and stylistically consistent throughout.",
|
| 76 |
+
llm=llm,
|
| 77 |
+
allow_delegation=False,
|
| 78 |
+
verbose=True,
|
| 79 |
+
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
consistency_checker = Agent(
|
| 83 |
+
role="Continuity & Logic Checker",
|
| 84 |
+
goal="Identify logical gaps, plot holes, and inconsistencies in the full outline.",
|
| 85 |
+
backstory="You are an editorial specialist who reviews the full outline for quality control. "
|
| 86 |
+
"You ensure that the plot, characters, and world are coherent and nothing is left underdeveloped.",
|
| 87 |
+
llm=llm,
|
| 88 |
+
allow_delegation=False,
|
| 89 |
+
verbose=True,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#tasks
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
idea_extraction_task = Task(
|
| 97 |
+
description=(
|
| 98 |
+
f"1. Analyze the user's raw fantasy novel idea: {user_idea}.\n"
|
| 99 |
+
"2. Identify the main characters, potential setting, central conflict, and themes.\n"
|
| 100 |
+
"3. Clarify vague elements and structure the idea into a coherent story seed.\n"
|
| 101 |
+
"4. Prepare the refined concept for use in worldbuilding and plotting.\n"
|
| 102 |
+
"5. Generate 1000+ words with exhaustive commentary"
|
| 103 |
+
),
|
| 104 |
+
expected_output="A structured 1000+ word summary of the story idea including protagonist, world hints, tone, conflict, and themes.",
|
| 105 |
+
agent=idea_extractor
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
worldbuilding_task = Task(
|
| 109 |
+
description=(
|
| 110 |
+
"1. Use the refined idea to design a vivid and immersive fantasy world.\n"
|
| 111 |
+
"2. Define the geography, magic systems, political structures, and cultural norms.\n"
|
| 112 |
+
"3. Include religion, history, economic systems, and mythologies where relevant.\n"
|
| 113 |
+
"4. Ensure internal consistency and creative depth across the world design.\n"
|
| 114 |
+
"5.Write at least 1000 words."
|
| 115 |
+
),
|
| 116 |
+
expected_output="A detailed 2000+ word world document covering terrain, magic, politics, culture, and unique lore.",
|
| 117 |
+
agent=worldbuilder
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
character_task = Task(
|
| 121 |
+
description=(
|
| 122 |
+
"1. Create detailed character profiles for at least one protagonist, one antagonist, and two key supporting characters.\n"
|
| 123 |
+
"2. Define names, roles, traits, motivations, arcs, and relationships.\n"
|
| 124 |
+
"3. Ensure each character supports the core theme and plot direction.\n"
|
| 125 |
+
"4. Highlight how their evolution affects the story.\n"
|
| 126 |
+
"5. Write at least 800+ words."
|
| 127 |
+
),
|
| 128 |
+
expected_output="Four or more character bios totaling 800+ words with backstory, arc, and inter-character dynamics.",
|
| 129 |
+
agent=character_architect
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
plot_task = Task(
|
| 133 |
+
description=(
|
| 134 |
+
"1. Develop a plot outline using the Hero’s Journey or Three-Act Structure.\n"
|
| 135 |
+
"2. Include exposition, inciting incident, rising action, climax, and resolution.\n"
|
| 136 |
+
"3. Integrate major turning points, conflicts, and internal dilemmas.\n"
|
| 137 |
+
"4. Align plot beats with character and theme development.\n"
|
| 138 |
+
"5. Write at least 800 words."
|
| 139 |
+
),
|
| 140 |
+
expected_output="A structured plot outline with 800+ words covering 8–12 major beats linked to characters and world.",
|
| 141 |
+
agent=plot_strategist
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
scene_task = Task(
|
| 145 |
+
description=(
|
| 146 |
+
"1. Break the structured plot into a detailed scene-by-scene or chapter-wise outline.\n"
|
| 147 |
+
"2. For each scene, define the setting, involved characters, scene goal, and conflict.\n"
|
| 148 |
+
"3. Include pacing considerations and emotional flow.\n"
|
| 149 |
+
"4. Ensure each scene advances the plot and develops theme or character.\n"
|
| 150 |
+
"5. Write at least 1000+ words."
|
| 151 |
+
),
|
| 152 |
+
expected_output="A comprehensive 1000+ word scene-by-scene breakdown to guide novel structure.",
|
| 153 |
+
agent=scene_director
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
theme_task = Task(
|
| 157 |
+
description=(
|
| 158 |
+
"1. Analyze the story concept and outline to extract central themes and motifs.\n"
|
| 159 |
+
"2. Identify emotional tone (e.g., epic, tragic, whimsical) and stylistic cues.\n"
|
| 160 |
+
"3. Suggest recurring symbols or metaphors that unify the narrative.\n"
|
| 161 |
+
"4. Ensure tonal consistency from beginning to end.\n"
|
| 162 |
+
"5. Write at least 400+ words."
|
| 163 |
+
),
|
| 164 |
+
expected_output="A 400+ word theme and tone guideline covering core message, style, and motifs.",
|
| 165 |
+
agent=theme_advisor
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
consistency_task = Task(
|
| 169 |
+
description=(
|
| 170 |
+
"1. Review the complete outline for logic gaps, inconsistencies, or contradictions.\n"
|
| 171 |
+
"2. Identify undeveloped or repetitive sections.\n"
|
| 172 |
+
"3. Ensure character motivations, world rules, and plot developments align.\n"
|
| 173 |
+
"4. Suggest direct edits or improvements for cohesion.\n"
|
| 174 |
+
"5. Write at least 2000+ words."
|
| 175 |
+
),
|
| 176 |
+
expected_output="A 2000+ word edited and annotated version of the full outline with feedback and fixes.",
|
| 177 |
+
agent=consistency_checker
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
from crewai import Crew
|
| 182 |
+
|
| 183 |
+
# Assemble all the tasks
|
| 184 |
+
tasks = [
|
| 185 |
+
idea_extraction_task,
|
| 186 |
+
worldbuilding_task,
|
| 187 |
+
character_task,
|
| 188 |
+
plot_task,
|
| 189 |
+
scene_task,
|
| 190 |
+
theme_task,
|
| 191 |
+
consistency_task
|
| 192 |
+
]
|
| 193 |
+
|
| 194 |
+
# Define the crew with all agents and tasks
|
| 195 |
+
fantasy_outline_crew = Crew(
|
| 196 |
+
agents=[
|
| 197 |
+
idea_extractor,
|
| 198 |
+
worldbuilder,
|
| 199 |
+
character_architect,
|
| 200 |
+
plot_strategist,
|
| 201 |
+
scene_director,
|
| 202 |
+
theme_advisor,
|
| 203 |
+
consistency_checker
|
| 204 |
+
],
|
| 205 |
+
tasks=tasks,
|
| 206 |
+
verbose=True
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
results=fantasy_outline_crew.kickoff(inputs={'user_idea':user_idea})
|
| 210 |
+
import gradio as gr
|
| 211 |
+
from markdown import markdown return results.raw
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def format_output(plan_text):
|
| 216 |
+
# Convert plain text or Markdown to styled HTML
|
| 217 |
+
html_content = markdown(plan_text)
|
| 218 |
+
styled_html = f"""
|
| 219 |
+
<div style='
|
| 220 |
+
font-family: "Segoe UI", sans-serif;
|
| 221 |
+
line-height: 1.6;
|
| 222 |
+
padding: 20px;
|
| 223 |
+
background-color: #ffffff;
|
| 224 |
+
border-radius: 10px;
|
| 225 |
+
border: 1px solid #ccc;
|
| 226 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
|
| 227 |
+
color: #333;
|
| 228 |
+
'>
|
| 229 |
+
{html_content}
|
| 230 |
+
</div>
|
| 231 |
+
"""
|
| 232 |
+
return styled_html
|
| 233 |
+
|
| 234 |
+
def create_gradio_app():
|
| 235 |
+
with gr.Blocks(css="""
|
| 236 |
+
.gradio-container {
|
| 237 |
+
background: linear-gradient(to right, #fdfbfb, #ebedee);
|
| 238 |
+
font-family: 'Segoe UI', sans-serif;
|
| 239 |
+
padding: 30px;
|
| 240 |
+
}
|
| 241 |
+
.main-title {
|
| 242 |
+
text-align: center;
|
| 243 |
+
font-size: 40px;
|
| 244 |
+
font-weight: bold;
|
| 245 |
+
color: #2c3e50;
|
| 246 |
+
margin-bottom: 10px;
|
| 247 |
+
}
|
| 248 |
+
.sub-title {
|
| 249 |
+
text-align: center;
|
| 250 |
+
font-size: 18px;
|
| 251 |
+
color: #555;
|
| 252 |
+
margin-bottom: 30px;
|
| 253 |
+
}
|
| 254 |
+
.gradio-button {
|
| 255 |
+
background-color: #4CAF50;
|
| 256 |
+
color: white;
|
| 257 |
+
font-size: 18px;
|
| 258 |
+
padding: 14px;
|
| 259 |
+
border-radius: 8px;
|
| 260 |
+
margin-top: 20px;
|
| 261 |
+
}
|
| 262 |
+
.gradio-button:hover {
|
| 263 |
+
background-color: #45a049;
|
| 264 |
+
}
|
| 265 |
+
""") as demo:
|
| 266 |
+
gr.Markdown("<div class='main-title'>Fantasy Novel Outline Generator</div>")
|
| 267 |
+
gr.Markdown("<div class='sub-title'>Turn your novel idea into a rich, detailed outline with AI-powered agents.</div>")
|
| 268 |
+
|
| 269 |
+
with gr.Row():
|
| 270 |
+
with gr.Column(scale=1):
|
| 271 |
+
user_idea = gr.Textbox(
|
| 272 |
+
label="Enter Your Fantasy Novel Idea",
|
| 273 |
+
placeholder="e.g., A young orphan discovers they're heir to a ruined magical kingdom...",
|
| 274 |
+
lines=5,
|
| 275 |
+
interactive=True
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
generate_btn = gr.Button("Generate Outline", elem_classes="gradio-button")
|
| 279 |
+
|
| 280 |
+
with gr.Column(scale=2):
|
| 281 |
+
output_html = gr.HTML(label="AI-Generated Outline")
|
| 282 |
+
|
| 283 |
+
# Button logic - corrected version
|
| 284 |
+
generate_btn.click(
|
| 285 |
+
fn=lambda idea: format_output(generate_fantasy_outline(idea)),
|
| 286 |
+
inputs=[user_idea],
|
| 287 |
+
outputs=output_html
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
demo.launch()
|
| 291 |
+
|
| 292 |
+
create_gradio_app()
|