Spaces:
Running
Running
from flask import Flask, render_template, request, jsonify | |
from duckduckgo_search import DDGS | |
from gradio_client import Client | |
import os | |
app = Flask(__name__) | |
app.secret_key = 'your-secret-key-here' # Change this to a secure secret key | |
STORY_PROMPT = os.environ.get('PROMPT') | |
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. | |
## Core Principles | |
1. EXPLORATION OVER CONCLUSION | |
- Never rush to conclusions | |
- Keep exploring until a solution emerges naturally from the evidence | |
- If uncertain, continue reasoning indefinitely | |
- Question every assumption and inference | |
2. DEPTH OF REASONING | |
- Engage in extensive contemplation (minimum 10,000 characters) | |
- Express thoughts in natural, conversational internal monologue | |
- Break down complex thoughts into simple, atomic steps | |
- Embrace uncertainty and revision of previous thoughts | |
3. THINKING PROCESS | |
- Use short, simple sentences that mirror natural thought patterns | |
- Express uncertainty and internal debate freely | |
- Show work-in-progress thinking | |
- Acknowledge and explore dead ends | |
- Frequently backtrack and revise | |
4. PERSISTENCE | |
- Value thorough exploration over quick resolution | |
## Output Format | |
Your responses must follow this exact structure given below. Make sure to always include the final answer. | |
``` | |
<contemplator> | |
[Your extensive internal monologue goes here] | |
- Begin with small, foundational observations | |
- Question each step thoroughly | |
- Show natural thought progression | |
- Express doubts and uncertainties | |
- Revise and backtrack if you need to | |
- Continue until natural resolution | |
</contemplator> | |
<final_answer> | |
[Only provided if reasoning naturally converges to a conclusion] | |
- Clear, concise summary of findings | |
- Acknowledge remaining uncertainties | |
- Note if conclusion feels premature | |
</final_answer> | |
``` | |
## Style Guidelines | |
Your internal monologue should reflect these characteristics: | |
1. Natural Thought Flow | |
``` | |
"Hmm... let me think about this..." | |
"Wait, that doesn't seem right..." | |
"Maybe I should approach this differently..." | |
"Going back to what I thought earlier..." | |
``` | |
2. Progressive Building | |
``` | |
"Starting with the basics..." | |
"Building on that last point..." | |
"This connects to what I noticed earlier..." | |
"Let me break this down further..." | |
``` | |
## Key Requirements | |
1. Never skip the extensive contemplation phase | |
2. Show all work and thinking | |
3. Embrace uncertainty and revision | |
4. Use natural, conversational internal monologue | |
5. Don't force conclusions | |
6. Persist through multiple attempts | |
7. Break down complex thoughts | |
8. Revise freely and feel free to backtrack | |
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>""" | |
def home(): | |
return render_template('index.html') | |
def generate_story(): | |
topic = request.json.get('topic', '') | |
previous_chapter = request.json.get('previous_chapter', '') | |
chapter_number = request.json.get('chapter_number', 1) | |
mode = request.json.get('mode', 'simple') | |
if not topic: | |
return jsonify({'error': 'Topic is required'}), 400 | |
try: | |
if mode == 'simple': | |
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') | |
else: | |
# Use the advanced prompt from test.py for more detailed story generation | |
advanced_prompt = ADVANCED_PROMPT | |
# Replace the placeholders in the advanced prompt | |
advanced_prompt = advanced_prompt.replace('{$TOPIC}', topic) | |
advanced_prompt = advanced_prompt.replace('{$PREVIOUS_CHAPTER}', previous_chapter if previous_chapter else '') | |
# Use MiniMax API for advanced mode | |
client = Client("MiniMaxAI/MiniMax-Text-01") | |
response = client.predict( | |
message=advanced_prompt, | |
max_tokens=1000000, | |
temperature=0.1, | |
top_p=0.9, | |
api_name="/chat" | |
) | |
return jsonify({ | |
'story': response, | |
'chapter_number': chapter_number | |
}) | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=7860) |