from flask import request, jsonify from transformers import pipeline print("Loading Question Generation model (iarfmoose/t5-base-question-generator)...") # Initialize the pipeline for text2text-generation with the specified model qg_model = pipeline("text2text-generation", model="iarfmoose/t5-base-question-generator") print("Question Generation model loaded.") def handle_generate_questions(): data = request.get_json() if not data or 'text' not in data: return jsonify({'error': 'Invalid request. "text" field is required.'}), 400 text = data['text'] # Prepend the text with "generate questions: " as required by this model input_text = f"generate questions: {text}" try: # Generate questions results = qg_model(input_text, max_length=64, num_beams=4, early_stopping=True) # The result is a single string with questions separated by '' generated_text = results[0]['generated_text'] questions = [q.strip() for q in generated_text.split('') if q.strip()] print(f"Generated questions for text: '{text[:50]}...' -> {questions}") return jsonify({'questions': questions}) except Exception as e: print(f"Error during question generation: {e}") return jsonify({'error': str(e)}), 500