from flask import Flask, Blueprint, request, jsonify, current_app import openai import random import os from flask_cors import CORS # --- Blueprint --- reading_bp = Blueprint("reading", __name__) # app = Flask(__name__) app = Flask(__name__) CORS(app) _OPENAI_API_KEY_FALLBACK = os.getenv("OPENAI_API_KEY", "") # Set up your OpenAI API key (replace this with your own API key) # openai.api_key = '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' # Replace with your actual OpenAI API key def _ensure_openai_key(): """Set openai.api_key from app config or env before each API call.""" api_key = (current_app.config.get("OPENAI_API_KEY") if current_app else None) or _OPENAI_API_KEY_FALLBACK if api_key: openai.api_key = api_key # Function to generate content dynamically based on the topic and difficulty level def generate_content(topic, difficulty): _ensure_openai_key() try: # Define instructions based on difficulty level if difficulty == "easy": instruction = f"Write a very simple and basic explanation about {topic} for children aged 6-8. Use very simple words and short sentences." elif difficulty == "medium": instruction = f"Write a detailed and engaging explanation about {topic} for children aged 9-12. Use simple words but include more details." else: # Hard difficulty instruction = f"Write an in-depth explanation about {topic} for children aged 13-16. Use more complex words and provide deeper insights into the topic." # Call OpenAI API to generate the content response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a friendly teacher explaining concepts to students."}, {"role": "user", "content": instruction} ], max_tokens=700, temperature=0.7 ) content = response.choices[0].message.content.strip() return content except Exception as e: return f"Error generating content: {str(e)}" # Function to generate multiple-choice questions from content based on difficulty level def generate_questions(content, difficulty): _ensure_openai_key() try: # Split the content into sentences or key points and shuffle them content_sentences = content.split(".") # Assuming content is in sentence form. If not, modify accordingly. random.shuffle(content_sentences) # Adjust question complexity based on difficulty if difficulty == "easy": question_instruction = "Generate 3 very simple multiple-choice questions based on the content. The questions should be very easy to understand." elif difficulty == "medium": question_instruction = "Generate 3 multiple-choice questions with moderate difficulty based on the content." else: # Hard difficulty question_instruction = "Generate 3 challenging multiple-choice questions that require deep understanding of the content." # prompt = f"{question_instruction}\nContent:\n{content}\n\nFormat the output like this:\n\n1. Question: What is XYZ?\nOptions: [Option 1, Option 2, Option 3, Option 4]\nCorrect Answer: Option 1\n\n2. Question: Why does XYZ happen?\nOptions: [Option 1, Option 2, Option 3, Option 4]\nCorrect Answer: Option 2" prompt = f"{question_instruction}\nContent:\n{'. '.join(content_sentences[:3])}\n\nFormat the output like this:\n\n1. Question: What is XYZ?\nOptions: [Option 1, Option 2, Option 3, Option 4]\nCorrect Answer: Option 1\n\n2. Question: Why does XYZ happen?\nOptions: [Option 1, Option 2, Option 3, Option 4]\nCorrect Answer: Option 2" response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant who generates educational multiple-choice questions."}, {"role": "user", "content": prompt} ], max_tokens=700, temperature=0.7 ) questions = response.choices[0].message.content.strip() return questions except Exception as e: return f"Error generating questions: {str(e)}" @reading_bp.route('/generate_content', methods=['POST']) # @app.route('/generate_content', methods=['POST']) def generate_content_route(): data = request.json topic = data.get('topic') difficulty = data.get('difficulty', 'medium') # Default to medium if not provided if not topic: return jsonify({"error": "Topic is required"}), 400 if difficulty not in ["easy", "medium", "hard"]: return jsonify({"error": "Invalid difficulty level. Choose 'easy', 'medium', or 'hard'."}), 400 content = generate_content(topic, difficulty) return jsonify({"content": content}) @reading_bp.route('/generate_questions', methods=['POST']) # @app.route('/generate_questions', methods=['POST']) def generate_questions_route(): data = request.json content = data.get('content') difficulty = data.get('difficulty', 'medium') # Default to medium if not provided if not content: return jsonify({"error": "Content is required"}), 400 if difficulty not in ["easy", "medium", "hard"]: return jsonify({"error": "Invalid difficulty level. Choose 'easy', 'medium', or 'hard'."}), 400 questions = generate_questions(content, difficulty) return jsonify({"questions": questions}) @reading_bp.route('/validate_answer', methods=['POST']) # @app.route('/validate_answer', methods=['POST']) def validate_answer(): question = request.json.get('question') selected_answer = request.json.get('selected_answer') if not question or not selected_answer: return jsonify({"error": "Question and answer are required"}), 400 # Ensure both answers are stripped of leading/trailing spaces before comparison correct_answer = question["correct_answer"].strip() selected_answer = selected_answer.strip() # Print the correct answer to the backend console for debugging print(f"Correct Answer: {correct_answer}") is_correct = selected_answer == correct_answer return jsonify({"is_correct": is_correct, "correct_answer": correct_answer}) # if __name__ == '__main__': # app.run(debug=True) # if __name__ == '__main__': # app.run(host='0.0.0.0', port=5001) # --- Optional: allow this file to run standalone locally while still using the blueprint --- if __name__ == '__main__': app = Flask(__name__) CORS(app) # For local runs, pull key from env; no hard-coding app.config["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "") app.register_blueprint(reading_bp, url_prefix='') app.run(host='0.0.0.0', port=5001, debug=True)