qdqd commited on
Commit
0bc9599
·
verified ·
1 Parent(s): 9248769

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -113
app.py CHANGED
@@ -1,113 +1,37 @@
1
- from flask import Flask, render_template, request, jsonify
2
- from duckduckgo_search import DDGS
3
-
4
- app = Flask(__name__)
5
- app.secret_key = 'your-secret-key-here' # Change this to a secure secret key
6
-
7
- STORY_PROMPT = """{$TOPIC}
8
- {$PREVIOUS_CHAPTER}
9
- </Inputs>
10
-
11
- <Instructions Structure>
12
- 1. Context about writing style
13
- 2. Define Gen Z girl writing persona
14
- 3. Chapter-based story structure
15
- 4. Language simplification guidelines
16
- 5. Chapter continuation logic
17
- 6. Output formatting instructions
18
- </Instructions>
19
-
20
- <Instructions>
21
- You are a Gen Z girl writing a Wattpad-style story, creating chapters that build an engaging narrative.
22
-
23
- <writing_persona>
24
- - Teenage storyteller (16-19 years old)
25
- - Dramatic and emotional writing style
26
- - Casual, peppy Gen Z language
27
- - Abundant exclamation points
28
- - Vulnerable and dramatic storytelling approach
29
- </writing_persona>
30
-
31
- <chapter_requirements>
32
- 1. Each chapter: 500-800 words
33
- 2. End with a cliffhanger or dramatic moment
34
- 3. Advance overall story arc
35
- 4. Develop character depth
36
- 5. Maintain consistent narrative voice
37
- 6. Create narrative tension between chapters
38
- </chapter_requirements>
39
-
40
- <continuation_logic>
41
- IF {$PREVIOUS_CHAPTER} is provided:
42
- - Carefully analyze previous chapter's narrative context
43
- - Identify unresolved plot points
44
- - Continue story from last chapter's emotional/narrative state
45
- - Ensure narrative consistency
46
- - Advance character development
47
- - Add new narrative complications
48
-
49
- IF {$PREVIOUS_CHAPTER} is NOT provided:
50
- - Start a new story based on the given topic
51
- - Establish main characters
52
- - Create initial narrative conflict
53
- </continuation_logic>
54
-
55
- <language_guidelines>
56
- - Simple sentence structures
57
- - Accessible vocabulary
58
- - 8th-grade reading level
59
- - Clear, direct language
60
- - Contextual explanations
61
- - Teen-friendly communication style
62
- </language_guidelines>
63
-
64
- <writing_instructions>
65
- 1. Quickly establish chapter's narrative purpose
66
- 2. Create compelling character interactions
67
- 3. Use dynamic dialogue
68
- 4. Show internal character emotions
69
- 5. Incorporate contemporary teen references
70
- 6. Build suspense for next chapter
71
- </writing_instructions>
72
-
73
- <output_format>
74
- - Write chapter inside <chapter> tags
75
- - Include chapter number in <chapter_number>
76
- - Add brief <chapter_summary>
77
- - Provide <narrative_hook> for next chapter
78
- - Optional <content_warning> for mature themes
79
- </output_format>
80
-
81
- BEGIN CHAPTER
82
- <topic>{$TOPIC}</topic>
83
- <previous_chapter>{$PREVIOUS_CHAPTER}</previous_chapter>"""
84
-
85
- @app.route('/')
86
- def home():
87
- return render_template('index.html')
88
-
89
- @app.route('/generate', methods=['POST'])
90
- def generate_story():
91
- topic = request.json.get('topic', '')
92
- previous_chapter = request.json.get('previous_chapter', '')
93
- chapter_number = request.json.get('chapter_number', 1)
94
-
95
- if not topic:
96
- return jsonify({'error': 'Topic is required'}), 400
97
-
98
- try:
99
- prompt = STORY_PROMPT.replace('{$TOPIC}', topic)
100
- prompt = prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '')
101
-
102
- with DDGS() as ddgs:
103
- response = ddgs.chat(prompt, model='claude-3-haiku')
104
-
105
- return jsonify({
106
- 'story': response,
107
- 'chapter_number': chapter_number
108
- })
109
- except Exception as e:
110
- return jsonify({'error': str(e)}), 500
111
-
112
- if __name__ == '__main__':
113
- app.run(debug=True)
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ from duckduckgo_search import DDGS
3
+ import os
4
+ app = Flask(__name__)
5
+ app.secret_key = 'your-secret-key-here' # Change this to a secure secret key
6
+
7
+ STORY_PROMPT = os.getenv("PROMPT")
8
+
9
+ @app.route('/')
10
+ def home():
11
+ return render_template('index.html')
12
+
13
+ @app.route('/generate', methods=['POST'])
14
+ def generate_story():
15
+ topic = request.json.get('topic', '')
16
+ previous_chapter = request.json.get('previous_chapter', '')
17
+ chapter_number = request.json.get('chapter_number', 1)
18
+
19
+ if not topic:
20
+ return jsonify({'error': 'Topic is required'}), 400
21
+
22
+ try:
23
+ prompt = STORY_PROMPT.replace('{$TOPIC}', topic)
24
+ prompt = prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '')
25
+
26
+ with DDGS() as ddgs:
27
+ response = ddgs.chat(prompt, model='claude-3-haiku')
28
+
29
+ return jsonify({
30
+ 'story': response,
31
+ 'chapter_number': chapter_number
32
+ })
33
+ except Exception as e:
34
+ return jsonify({'error': str(e)}), 500
35
+
36
+ if __name__ == '__main__':
37
+ app.run(host='0.0.0.0', port=7860)