nomadic07 commited on
Commit
6f5e844
·
1 Parent(s): f7aa651
Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +105 -0
  3. requirements.txt +5 -0
  4. setup.sh +2 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ s2v_reddit_2015_md
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sense2vec import Sense2Vec
3
+ import spacy
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+
6
+ # Load models
7
+ tokenizer = T5Tokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
8
+ model = T5ForConditionalGeneration.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
9
+
10
+ # Load SpaCy model and Sense2Vec
11
+ nlp = spacy.load("en_core_web_sm")
12
+ s2v = Sense2Vec().from_disk('s2v_reddit_2015_md/s2v_old')
13
+
14
+ # Function to get important keywords
15
+ def get_imp_keywords(content):
16
+ out = []
17
+ doc = nlp(content)
18
+ for token in doc:
19
+ if token.pos_ in ["NOUN", "PROPN", "VERB", "NUM"]:
20
+ out.append(token.text)
21
+ return out
22
+
23
+ # Function to generate question
24
+ def get_question(context, answer):
25
+ sentence = extract_relevant_sentence(context, answer)
26
+ prompt = f"answer: {answer} context: {context} </s>"
27
+ max_len = 256
28
+ encoding = tokenizer.encode_plus(prompt, max_length=max_len, pad_to_max_length=False, truncation=True, return_tensors="pt")
29
+
30
+ input_ids = encoding["input_ids"]
31
+ attention_mask = encoding["attention_mask"]
32
+
33
+ try:
34
+ outs = model.generate(
35
+ input_ids=input_ids,
36
+ attention_mask=attention_mask,
37
+ early_stopping=True,
38
+ num_beams=5,
39
+ num_return_sequences=1,
40
+ no_repeat_ngram_size=2,
41
+ max_length=300
42
+ )
43
+ dec = [tokenizer.decode(ids, skip_special_tokens=True) for ids in outs]
44
+ question = dec[0].replace("question:", "").strip()
45
+ return question
46
+ except Exception as e:
47
+ return f"Error generating question: {str(e)}"
48
+
49
+ # Extract relevant sentence
50
+ def extract_relevant_sentence(context, answer):
51
+ sentences = context.split(". ")
52
+ for sentence in sentences:
53
+ if answer in sentence:
54
+ return sentence
55
+ return context
56
+
57
+ # Function to get similar words using Sense2Vec
58
+ def sense2vec_get_words(word):
59
+ output = []
60
+ word = word.lower().replace(" ", "_")
61
+ sense = s2v.get_best_sense(word)
62
+ if not sense:
63
+ return output
64
+
65
+ most_similar = s2v.most_similar(sense, n=20)
66
+ for each_word in most_similar:
67
+ append_word = each_word[0].split("|")[0].replace("_", " ").lower()
68
+ if append_word.lower() != word:
69
+ output.append(append_word.title())
70
+ return output
71
+
72
+ # Generate multiple questions
73
+ def generate_question(context):
74
+ np = get_imp_keywords(context)
75
+ output = ""
76
+ cnt = 0
77
+ for answer in np:
78
+ if cnt >= 5:
79
+ break
80
+ distractors = sense2vec_get_words(answer.capitalize())
81
+ if len(distractors) > 0:
82
+ ques = get_question(context, answer)
83
+ output += f"Question: {ques}\n"
84
+ output += f"Answer: {answer.capitalize()}\n"
85
+ output += "Options:\n"
86
+ options = [answer.capitalize()] + distractors[:3]
87
+ for idx, option in enumerate(options, 1):
88
+ output += f"{idx}. {option}\n"
89
+ cnt += 1
90
+ output += "\n"
91
+ return output
92
+
93
+ # Streamlit Interface
94
+ st.title("Automatic Question Generator")
95
+ st.write("Generate multiple-choice questions based on the provided context.")
96
+
97
+ context = st.text_area("Enter the context text here:")
98
+
99
+ if st.button("Generate Questions"):
100
+ if context:
101
+ with st.spinner("Generating questions..."):
102
+ questions_output = generate_question(context)
103
+ st.text_area("Generated Questions", questions_output, height=300)
104
+ else:
105
+ st.warning("Please enter the context text to generate questions.")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ sense2vec
2
+ spacy
3
+ transformers
4
+ torch
5
+ streamlit
setup.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #!/bin/bash
2
+ python -m spacy download en_core_web_sm