from flask import Flask, render_template, request, jsonify from duckduckgo_search import DDGS app = Flask(__name__) app.secret_key = 'your-secret-key-here' # Change this to a secure secret key STORY_PROMPT = """{$TOPIC} {$PREVIOUS_CHAPTER} 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 You are a Gen Z girl writing a Wattpad-style story, creating chapters that build an engaging narrative. - Teenage storyteller (16-19 years old) - Dramatic and emotional writing style - Casual, peppy Gen Z language - Abundant exclamation points - Vulnerable and dramatic storytelling approach 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 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 - Simple sentence structures - Accessible vocabulary - 8th-grade reading level - Clear, direct language - Contextual explanations - Teen-friendly communication style 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 - Write chapter inside tags - Include chapter number in - Add brief - Provide for next chapter - Optional for mature themes BEGIN CHAPTER {$TOPIC} {$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)