realambuj commited on
Commit
e63b866
1 Parent(s): fd163f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from io import StringIO
3
+ import streamlit as st
4
+ import joblib
5
+ from transformers import pipeline
6
+ from lmqg import TransformersQG
7
+
8
+ #util func starts here
9
+ dummyData = [
10
+ {
11
+ "question": "When was the foundation of Gojoseon believed to have occurred?",
12
+ "answer": "The foundation of Gojoseon is believed to have occurred in 2333 BC by the mythic king Dangun."
13
+ },
14
+ {
15
+ "question": "Which dynasty ruled Korea after the unification of the Three Kingdoms under Unified Silla in AD 668?",
16
+ "answer": "Korea was subsequently ruled by the Goryeo dynasty (918–1392) and the Joseon dynasty (1392–1897)."
17
+ },
18
+ {
19
+ "question": "In 1897, what proclamation did King Gojong make, leading to the annexation of Korea by the Empire of Japan in 1910?",
20
+ "answer": "King Gojong proclaimed the Korean Empire in 1897, which was later annexed by the Empire of Japan in 1910."
21
+ },
22
+ {
23
+ "question": "How was Korea divided after World War II in 1945?",
24
+ "answer": "After World War II in 1945, Korea was divided into two zones along the 38th parallel, with the north occupied by the Soviet Union and the south occupied by the United States."
25
+ },
26
+ {
27
+ "question": "What event marked the beginning of the Korean War in 1950?",
28
+ "answer": "The Korean War began when North Korean forces invaded South Korea in 1950."
29
+ },
30
+ {
31
+ "question": "What agreement brought about a ceasefire in the Korean War in 1953, establishing a demilitarized zone (DMZ)?",
32
+ "answer": "The Korean Armistice Agreement brought about a ceasefire in 1953 and established a demilitarized zone (DMZ)."
33
+ },
34
+ {
35
+ "question": "Which philosophy did North Korea's first leader, Kim Il Sung, promote as the state ideology, purging pro-Soviet and pro-Chinese elements?",
36
+ "answer": "Kim Il Sung promoted his personal philosophy of Juche as the state ideology, purging both pro-Soviet and pro-Chinese elements."
37
+ },
38
+ {
39
+ "question": "What contributed to North Korea's international isolation in the 1980s?",
40
+ "answer": "North Korea's international isolation sharply accelerated from the 1980s onwards as the Cold War came to an end."
41
+ },
42
+ {
43
+ "question": "How did the fall of the Soviet Union in 1991 impact North Korea's economy?",
44
+ "answer": "The fall of the Soviet Union in 1991 brought about a sharp decline in the North Korean economy."
45
+ },
46
+ {
47
+ "question": "What significant event occurred in North Korea from 1994 to 1998, resulting in a large number of casualties?",
48
+ "answer": "From 1994 to 1998, North Korea suffered a famine that resulted in the deaths of between 240,000 and 420,000 people."
49
+ }
50
+ ]
51
+
52
+
53
+ def break_paragraph_into_parts(paragraph, max_length):
54
+ sentences = paragraph.split(". ") # Assuming that the sentences end with a period and a space
55
+ temp_parts = []
56
+ part = ''
57
+ for sentence in sentences:
58
+ if len(part) + len(sentence) <= max_length:
59
+ part += sentence + ". " # Add the sentence and the period back
60
+ else:
61
+ temp_parts.append(part)
62
+ part = sentence + ". " # Start a new part with the remaining sentence
63
+ temp_parts.append(part) # Add the last part
64
+
65
+ parts = [part.strip() for part in temp_parts] # Remove any leading/trailing spaces
66
+
67
+ return parts
68
+
69
+
70
+ def util(NumQues,Input):
71
+
72
+ context = break_paragraph_into_parts(Input,512)
73
+ context = context[:NumQues]
74
+
75
+ #Question generation
76
+ generatedQuestions = []
77
+ Question_Generator=joblib.load("QGenerator.sav")
78
+
79
+ for part in context:
80
+ question = Question_Generator.generate_q(list_context=part, list_answer="")
81
+ generatedQuestions.append(question)
82
+
83
+
84
+ #Answer Generation
85
+ load_pipeline=joblib.load('AGenerator.sav')
86
+
87
+ generatedAnswers=[]
88
+ for Q in generatedQuestions:
89
+ print(Q,'\n')
90
+ gen_answer=load_pipeline(question=Q, context=Input)
91
+ generatedAnswers.append(gen_answer['answer'])
92
+
93
+ for i in range(len(generatedAnswers)):
94
+ entry = dummyData[i]
95
+ code = f'Ques: "{generatedQuestions[i]}"\nAns: "{generatedAnswers[i]}"'
96
+ st.code(code, language='python')
97
+
98
+ #util func ends here
99
+
100
+
101
+
102
+
103
+
104
+
105
+ st.title('Question Answer Pair Generation from Documents')
106
+
107
+ tab1, tab2 = st.tabs(["Enter Text", "Choose Document"])
108
+
109
+ flag='None'
110
+
111
+ with st.sidebar:
112
+ st.image('Pic.png')
113
+ st.title("Final Year Project")
114
+ st.divider()
115
+ code = '''Team Members CSE(20-37):
116
+ \nPrateek Niket BT20CSE211 \nSmriti Singh BT20CSE156 \nAmbuj Raj BT20CSE054 \nSrishti Pandey BT20CSE068'''
117
+ st.code(code, language='JAVA')
118
+ code = '''Mentored By: \nDr. Amol Bhopale'''
119
+ st.code(code, language='JAVA')
120
+
121
+
122
+ with tab1:
123
+ txt = st.text_area(
124
+ "Enter Text to Generate Question-Answer"
125
+ )
126
+ flag='text'
127
+
128
+ with tab2:
129
+ uploaded_file = st.file_uploader("Choose a file", type=['txt'], accept_multiple_files=False)
130
+ if uploaded_file is not None:
131
+ # To convert to a string based IO:
132
+ stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
133
+ txt = stringio.read()
134
+ flag='file'
135
+
136
+ NumQues = st.slider('No. of Questions to Generate: ', 1, 5, 1)
137
+ print(NumQues)
138
+
139
+ if st.button('Generate',type="primary"):
140
+ with st.spinner('Question Answer pair Generation in Progress....'):
141
+ util(NumQues,txt)
142
+ st.success('Question Answer pair Generated Successfully!')
143
+
144
+
145
+