VoiceQ / qgen_server.py
yolloo's picture
Upload 4 files
784c877 verified
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