from flask import Flask, render_template, request, jsonify from duckduckgo_search import DDGS import json import os app = Flask(__name__) STORY_PROMPT = os.getenv("PROMPT") @app.route('/') def home(): return render_template('index.html') @app.route('/generate', methods=['POST']) def generate_story(): topic = request.json.get('topic', '') if not topic: return jsonify({'error': 'No topic provided'}), 400 try: # Generate story using DuckDuckGo Chat prompt = STORY_PROMPT.replace('{$TOPIC}', topic) story = DDGS().chat(prompt, model="gpt-4o-mini") return jsonify({'story': story}) except Exception as e: print(f"Error details: {str(e)}") return jsonify({'error': str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)