File size: 1,615 Bytes
784c877
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

import os
from flask import request, jsonify
from transformers import pipeline

# Define a writable directory for the model cache.
# This now respects the HF_HOME environment variable set in the Dockerfile.
cache_dir = os.environ.get("HF_HOME", "/tmp/huggingface")
os.makedirs(cache_dir, exist_ok=True)

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", model_kwargs={"cache_dir": cache_dir})
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 '<sep>'
        generated_text = results[0]['generated_text']
        questions = [q.strip() for q in generated_text.split('<sep>') 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