File size: 4,434 Bytes
80c9d39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask, request, jsonify
import tensorflow as tf
from transformers import AutoTokenizer, TFT5ForConditionalGeneration
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
import os
import re
import spacy
from nltk.corpus import wordnet as wn
import random
import nltk
nltk.download('wordnet')

nlp = spacy.load("en_core_web_sm")

app = Flask(__name__)

# Model uploaded configuration
LOCAL_QG_MODEL_PATH = "blaxx14/t5-question-generation"  

"""string into dictionary"""
def parse_to_dict(input_string):
    try:
        question_part, answer_part = input_string.split('Answer: ')
        question = question_part.replace('Question: ', '').strip()  
        answer = answer_part.strip()  
        
        result_dict = {
            "Question": question,
            "Answer": answer
        }
        
        return result_dict
    
    except ValueError:
        print("Format input string tidak sesuai")
        return None


"""Find sinonim"""
def get_synonyms(word):
    synonyms = set()
    for syn in wn.synsets(word):
        for lemma in syn.lemmas():
            synonyms.add(lemma.name())
    return list(synonyms)


"""Create distractor"""
def generate_distractors(question, correct_answer):
    doc = nlp(question)
    
    keywords = [token.text for token in doc if token.pos_ in ['NOUN', 'PROPN']]
    
    distractors = []
    
    for keyword in keywords:
        synonyms = get_synonyms(keyword)
        synonyms = [word for word in synonyms if word.lower() != correct_answer.lower()]
        distractors.extend(synonyms)
        
    distractors = random.sample(distractors, min(3, len(distractors)))
    
    return distractors

"""Load question generator model and tokenizer"""
print("Loading model...")
model = TFT5ForConditionalGeneration.from_pretrained(LOCAL_QG_MODEL_PATH, from_pt=False)
tokenizer = AutoTokenizer.from_pretrained("t5-small")
print("Model loaded successfully.")

"""Function for generate question"""
def generate_question(text, max_length=4096):
    input_text = f"Generate question answer: {text}"
    input_ids = tokenizer.encode(input_text, return_tensors="tf", max_length=512, truncation=True)

    output = model.generate(
        input_ids,
        max_length=max_length,
        num_beams=10,
        top_k=0,
        top_p=0.8,
        temperature=1.5,
        do_sample=True,
        early_stopping=True
    )

    output_text = tokenizer.decode(output[0], skip_special_tokens=True)
    return output_text

"""Cleaning input"""
def clean_text(text):
    cleaned_text = text.replace("translit.", "")
    cleaned_text = re.sub(r'\[.*?\]', '', cleaned_text)
    return cleaned_text

def split_text_into_sentences(paragraph):
    text = clean_text(paragraph)
    sentences = re.split(r'(?<=[.?!])\s+', text)
    return sentences

def split_into_parts(sentences, num_parts=5):
    if len(sentences) <= num_parts:
        return sentences
    else:
        part_size = len(sentences) // num_parts
        parts = [sentences[i:i + part_size] for i in range(0, len(sentences), part_size)]

        if len(parts) > num_parts:
            parts[-2].extend(parts[-1])
            parts = parts[:-1]

        return parts

"""Route for run generator and save the results in cloud"""
@app.route('/generate-question', methods=['POST'])
def api_generate_question():
    try:
        data = request.json
        text = data.get('text', '')

        if not text:
            return jsonify({'error': 'Text tidak boleh kosong'}), 400



        """Run cleaning input"""
        formatted_sentences = split_text_into_sentences(text)
        parts = split_into_parts(formatted_sentences)


        """Just for checking"""
        #print(parts) 
        
        """Generate question"""
        question_list = []
        
        for sentence in parts:
            combined_input = ' '.join(sentence)
            result = generate_question(combined_input)
            result_dict = parse_to_dict(result)
            # print(result_dict)
            distractors = generate_distractors(result_dict["Question"], result_dict["Answer"])
            result_dict["distractor"] = distractors
            question_list.append(result_dict)

        print(question_list)
        return jsonify({'generated_question': question_list})
    except Exception as e:
        return jsonify({'error': str(e)}), 500


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)