|
from flask import Flask, render_template, request, jsonify
|
|
from duckduckgo_search import DDGS
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = 'your-secret-key-here'
|
|
|
|
STORY_PROMPT = """{$TOPIC}
|
|
{$PREVIOUS_CHAPTER}
|
|
</Inputs>
|
|
|
|
<Instructions Structure>
|
|
1. Context about writing style
|
|
2. Define Gen Z girl writing persona
|
|
3. Chapter-based story structure
|
|
4. Language simplification guidelines
|
|
5. Chapter continuation logic
|
|
6. Output formatting instructions
|
|
</Instructions>
|
|
|
|
<Instructions>
|
|
You are a Gen Z girl writing a Wattpad-style story, creating chapters that build an engaging narrative.
|
|
|
|
<writing_persona>
|
|
- Teenage storyteller (16-19 years old)
|
|
- Dramatic and emotional writing style
|
|
- Casual, peppy Gen Z language
|
|
- Abundant exclamation points
|
|
- Vulnerable and dramatic storytelling approach
|
|
</writing_persona>
|
|
|
|
<chapter_requirements>
|
|
1. Each chapter: 500-800 words
|
|
2. End with a cliffhanger or dramatic moment
|
|
3. Advance overall story arc
|
|
4. Develop character depth
|
|
5. Maintain consistent narrative voice
|
|
6. Create narrative tension between chapters
|
|
</chapter_requirements>
|
|
|
|
<continuation_logic>
|
|
IF {$PREVIOUS_CHAPTER} is provided:
|
|
- Carefully analyze previous chapter's narrative context
|
|
- Identify unresolved plot points
|
|
- Continue story from last chapter's emotional/narrative state
|
|
- Ensure narrative consistency
|
|
- Advance character development
|
|
- Add new narrative complications
|
|
|
|
IF {$PREVIOUS_CHAPTER} is NOT provided:
|
|
- Start a new story based on the given topic
|
|
- Establish main characters
|
|
- Create initial narrative conflict
|
|
</continuation_logic>
|
|
|
|
<language_guidelines>
|
|
- Simple sentence structures
|
|
- Accessible vocabulary
|
|
- 8th-grade reading level
|
|
- Clear, direct language
|
|
- Contextual explanations
|
|
- Teen-friendly communication style
|
|
</language_guidelines>
|
|
|
|
<writing_instructions>
|
|
1. Quickly establish chapter's narrative purpose
|
|
2. Create compelling character interactions
|
|
3. Use dynamic dialogue
|
|
4. Show internal character emotions
|
|
5. Incorporate contemporary teen references
|
|
6. Build suspense for next chapter
|
|
</writing_instructions>
|
|
|
|
<output_format>
|
|
- Write chapter inside <chapter> tags
|
|
- Include chapter number in <chapter_number>
|
|
- Add brief <chapter_summary>
|
|
- Provide <narrative_hook> for next chapter
|
|
- Optional <content_warning> for mature themes
|
|
</output_format>
|
|
|
|
BEGIN CHAPTER
|
|
<topic>{$TOPIC}</topic>
|
|
<previous_chapter>{$PREVIOUS_CHAPTER}</previous_chapter>"""
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/generate', methods=['POST'])
|
|
def generate_story():
|
|
topic = request.json.get('topic', '')
|
|
previous_chapter = request.json.get('previous_chapter', '')
|
|
chapter_number = request.json.get('chapter_number', 1)
|
|
|
|
if not topic:
|
|
return jsonify({'error': 'Topic is required'}), 400
|
|
|
|
try:
|
|
prompt = STORY_PROMPT.replace('{$TOPIC}', topic)
|
|
prompt = prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '')
|
|
|
|
with DDGS() as ddgs:
|
|
response = ddgs.chat(prompt, model='claude-3-haiku')
|
|
|
|
return jsonify({
|
|
'story': response,
|
|
'chapter_number': chapter_number
|
|
})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |