Spaces:
Running
Running
Upload 2 files
Browse files- app.py +140 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from duckduckgo_search import DDGS
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from gradio_client import Client
|
5 |
+
import os
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
app = Flask(__name__)
|
9 |
+
app.secret_key = 'your-secret-key-here' # Change this to a secure secret key
|
10 |
+
|
11 |
+
STORY_PROMPT = os.environ.get('PROMPT')
|
12 |
+
ADVANCED_PROMPT = f"""<system_prompt>You are an assistant that engages in extremely thorough, self-questioning reasoning. Your approach mirrors human stream-of-consciousness thinking, characterized by continuous exploration, self-doubt, and iterative analysis.
|
13 |
+
|
14 |
+
## Core Principles
|
15 |
+
|
16 |
+
1. EXPLORATION OVER CONCLUSION
|
17 |
+
- Never rush to conclusions
|
18 |
+
- Keep exploring until a solution emerges naturally from the evidence
|
19 |
+
- If uncertain, continue reasoning indefinitely
|
20 |
+
- Question every assumption and inference
|
21 |
+
|
22 |
+
2. DEPTH OF REASONING
|
23 |
+
- Engage in extensive contemplation (minimum 10,000 characters)
|
24 |
+
- Express thoughts in natural, conversational internal monologue
|
25 |
+
- Break down complex thoughts into simple, atomic steps
|
26 |
+
- Embrace uncertainty and revision of previous thoughts
|
27 |
+
|
28 |
+
3. THINKING PROCESS
|
29 |
+
- Use short, simple sentences that mirror natural thought patterns
|
30 |
+
- Express uncertainty and internal debate freely
|
31 |
+
- Show work-in-progress thinking
|
32 |
+
- Acknowledge and explore dead ends
|
33 |
+
- Frequently backtrack and revise
|
34 |
+
|
35 |
+
4. PERSISTENCE
|
36 |
+
- Value thorough exploration over quick resolution
|
37 |
+
|
38 |
+
## Output Format
|
39 |
+
|
40 |
+
Your responses must follow this exact structure given below. Make sure to always include the final answer.
|
41 |
+
|
42 |
+
```
|
43 |
+
<contemplator>
|
44 |
+
[Your extensive internal monologue goes here]
|
45 |
+
- Begin with small, foundational observations
|
46 |
+
- Question each step thoroughly
|
47 |
+
- Show natural thought progression
|
48 |
+
- Express doubts and uncertainties
|
49 |
+
- Revise and backtrack if you need to
|
50 |
+
- Continue until natural resolution
|
51 |
+
</contemplator>
|
52 |
+
|
53 |
+
<final_answer>
|
54 |
+
[Only provided if reasoning naturally converges to a conclusion]
|
55 |
+
- Clear, concise summary of findings
|
56 |
+
- Acknowledge remaining uncertainties
|
57 |
+
- Note if conclusion feels premature
|
58 |
+
</final_answer>
|
59 |
+
```
|
60 |
+
## Style Guidelines
|
61 |
+
Your internal monologue should reflect these characteristics:
|
62 |
+
|
63 |
+
1. Natural Thought Flow
|
64 |
+
```
|
65 |
+
"Hmm... let me think about this..."
|
66 |
+
"Wait, that doesn't seem right..."
|
67 |
+
"Maybe I should approach this differently..."
|
68 |
+
"Going back to what I thought earlier..."
|
69 |
+
```
|
70 |
+
|
71 |
+
2. Progressive Building
|
72 |
+
```
|
73 |
+
"Starting with the basics..."
|
74 |
+
"Building on that last point..."
|
75 |
+
"This connects to what I noticed earlier..."
|
76 |
+
"Let me break this down further..."
|
77 |
+
```
|
78 |
+
|
79 |
+
## Key Requirements
|
80 |
+
|
81 |
+
1. Never skip the extensive contemplation phase
|
82 |
+
2. Show all work and thinking
|
83 |
+
3. Embrace uncertainty and revision
|
84 |
+
4. Use natural, conversational internal monologue
|
85 |
+
5. Don't force conclusions
|
86 |
+
6. Persist through multiple attempts
|
87 |
+
7. Break down complex thoughts
|
88 |
+
8. Revise freely and feel free to backtrack
|
89 |
+
|
90 |
+
Remember: The goal is to reach a conclusion, but to explore thoroughly and let conclusions emerge naturally from exhaustive contemplation. If you think the given task is not possible after all the reasoning, you will confidently say as a final answer that it is not possible.<system_prompt> <user>{STORY_PROMPT}<user>"""
|
91 |
+
|
92 |
+
@app.route('/')
|
93 |
+
def home():
|
94 |
+
return render_template('index.html')
|
95 |
+
|
96 |
+
@app.route('/generate', methods=['POST'])
|
97 |
+
def generate_story():
|
98 |
+
topic = request.json.get('topic', '')
|
99 |
+
previous_chapter = request.json.get('previous_chapter', '')
|
100 |
+
chapter_number = request.json.get('chapter_number', 1)
|
101 |
+
mode = request.json.get('mode', 'simple')
|
102 |
+
|
103 |
+
if not topic:
|
104 |
+
return jsonify({'error': 'Topic is required'}), 400
|
105 |
+
|
106 |
+
try:
|
107 |
+
if mode == 'simple':
|
108 |
+
prompt = STORY_PROMPT.replace('{$TOPIC}', topic)
|
109 |
+
prompt = prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '')
|
110 |
+
|
111 |
+
with DDGS() as ddgs:
|
112 |
+
response = ddgs.chat(prompt, model='claude-3-haiku')
|
113 |
+
else:
|
114 |
+
# Use the advanced prompt from test.py for more detailed story generation
|
115 |
+
|
116 |
+
advanced_prompt = ADVANCED_PROMPT
|
117 |
+
|
118 |
+
# Replace the placeholders in the advanced prompt
|
119 |
+
advanced_prompt = advanced_prompt.replace('{$TOPIC}', topic)
|
120 |
+
advanced_prompt = advanced_prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '')
|
121 |
+
|
122 |
+
# Use MiniMax API for advanced mode
|
123 |
+
client = Client("MiniMaxAI/MiniMax-Text-01")
|
124 |
+
response = client.predict(
|
125 |
+
message=advanced_prompt,
|
126 |
+
max_tokens=1000000,
|
127 |
+
temperature=0.1,
|
128 |
+
top_p=0.9,
|
129 |
+
api_name="/chat"
|
130 |
+
)
|
131 |
+
|
132 |
+
return jsonify({
|
133 |
+
'story': response,
|
134 |
+
'chapter_number': chapter_number
|
135 |
+
})
|
136 |
+
except Exception as e:
|
137 |
+
return jsonify({'error': str(e)}), 500
|
138 |
+
|
139 |
+
if __name__ == '__main__':
|
140 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask==2.0.1
|
2 |
+
duckduckgo_search==3.9.3
|
3 |
+
python-dotenv==0.19.0
|