Spaces:
Build error
Build error
Commit
·
61c9e21
1
Parent(s):
f2ad269
Delete quiz_gen_new3.py
Browse files- quiz_gen_new3.py +0 -124
quiz_gen_new3.py
DELETED
@@ -1,124 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from textwrap3 import wrap
|
3 |
-
from flashtext import KeywordProcessor
|
4 |
-
import torch, random, nltk, string, traceback, sys, os, requests, datetime
|
5 |
-
import numpy as np
|
6 |
-
import pandas as pd
|
7 |
-
from transformers import T5ForConditionalGeneration,T5Tokenizer
|
8 |
-
import pke
|
9 |
-
from helper import postprocesstext, summarizer, get_nouns_multipartite, get_keywords,\
|
10 |
-
get_question, get_related_word, get_final_option_list, load_raw_text
|
11 |
-
|
12 |
-
|
13 |
-
def set_seed(seed: int):
|
14 |
-
random.seed(seed)
|
15 |
-
np.random.seed(seed)
|
16 |
-
torch.manual_seed(seed)
|
17 |
-
torch.cuda.manual_seed_all(seed)
|
18 |
-
|
19 |
-
set_seed(42)
|
20 |
-
|
21 |
-
@st.cache(allow_output_mutation = True)
|
22 |
-
def load_model():
|
23 |
-
nltk.download('punkt')
|
24 |
-
nltk.download('brown')
|
25 |
-
nltk.download('wordnet')
|
26 |
-
nltk.download('stopwords')
|
27 |
-
nltk.download('wordnet')
|
28 |
-
nltk.download('omw-1.4')
|
29 |
-
summary_mod_name = os.environ["summary_mod_name"]
|
30 |
-
question_mod_name = os.environ["question_mod_name"]
|
31 |
-
summary_model = T5ForConditionalGeneration.from_pretrained(summary_mod_name)
|
32 |
-
summary_tokenizer = T5Tokenizer.from_pretrained(summary_mod_name)
|
33 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
-
summary_model = summary_model.to(device)
|
35 |
-
question_model = T5ForConditionalGeneration.from_pretrained(question_mod_name)
|
36 |
-
question_tokenizer = T5Tokenizer.from_pretrained(question_mod_name)
|
37 |
-
question_model = question_model.to(device)
|
38 |
-
return summary_model, summary_tokenizer, question_tokenizer, question_model
|
39 |
-
|
40 |
-
from nltk.corpus import wordnet as wn
|
41 |
-
from nltk.tokenize import sent_tokenize
|
42 |
-
from nltk.corpus import stopwords
|
43 |
-
|
44 |
-
def csv_downloader(df):
|
45 |
-
res = df.to_csv(index=False,sep="\t").encode('utf-8')
|
46 |
-
st.download_button(
|
47 |
-
label="Download logs data as CSV separated by tab",
|
48 |
-
data=res,
|
49 |
-
file_name='df_quiz_log_file_v1.csv',
|
50 |
-
mime='text/csv')
|
51 |
-
|
52 |
-
def load_file():
|
53 |
-
"""Load text from file"""
|
54 |
-
uploaded_file = st.file_uploader("Upload Files",type=['txt'])
|
55 |
-
if uploaded_file is not None:
|
56 |
-
if uploaded_file.type == "text/plain":
|
57 |
-
raw_text = str(uploaded_file.read(),"utf-8")
|
58 |
-
return raw_text
|
59 |
-
|
60 |
-
st.markdown('')
|
61 |
-
|
62 |
-
# Loading Model
|
63 |
-
summary_model, summary_tokenizer, question_tokenizer, question_model =load_model()
|
64 |
-
|
65 |
-
# App title and description
|
66 |
-
st.title("Exam Assistant")
|
67 |
-
st.write("Upload text, Get ready for answering autogenerated questions")
|
68 |
-
|
69 |
-
# Load file
|
70 |
-
st.text("Disclaimer: This app stores user's input for model improvement purposes !!")
|
71 |
-
|
72 |
-
# Load file
|
73 |
-
|
74 |
-
default_text = load_raw_text()
|
75 |
-
raw_text = st.text_area("Enter text here", default_text, height=250, max_chars=1000000, )
|
76 |
-
|
77 |
-
# raw_text = load_file()
|
78 |
-
start_time = str(datetime.datetime.now())
|
79 |
-
if raw_text != None and raw_text != '':
|
80 |
-
summary_text = summarizer(raw_text,summary_model,summary_tokenizer)
|
81 |
-
ans_list = get_keywords(raw_text,summary_text)
|
82 |
-
#print("Ans list: {}".format(ans_list))
|
83 |
-
questions = []
|
84 |
-
option1=[]
|
85 |
-
option2=[]
|
86 |
-
option3=[]
|
87 |
-
option4=[]
|
88 |
-
for idx,ans in enumerate(ans_list):
|
89 |
-
#print("IDX: {}, ANS: {}".format(idx, ans))
|
90 |
-
ques = get_question(summary_text,ans,question_model,question_tokenizer)
|
91 |
-
other_options = get_related_word(ans)
|
92 |
-
final_options, ans_index = get_final_option_list(ans,other_options)
|
93 |
-
option1.append(final_options[0])
|
94 |
-
option2.append(final_options[1])
|
95 |
-
option3.append(final_options[2])
|
96 |
-
option4.append(final_options[3])
|
97 |
-
if ques not in questions:
|
98 |
-
html_str = f"""
|
99 |
-
<div>
|
100 |
-
<p>
|
101 |
-
{idx+1}: <b> {ques} </b>
|
102 |
-
</p>
|
103 |
-
</div>
|
104 |
-
"""
|
105 |
-
html_str += f' <p style="color:Green;"><b> {final_options[0]} </b></p> ' if ans_index == 0 else f' <p><b> {final_options[0]} </b></p> '
|
106 |
-
html_str += f' <p style="color:Green;"><b> {final_options[1]} </b></p> ' if ans_index == 1 else f' <p><b> {final_options[1]} </b></p> '
|
107 |
-
html_str += f' <p style="color:Green;"><b> {final_options[2]} </b></p> ' if ans_index == 2 else f' <p><b> {final_options[2]} </b></p> '
|
108 |
-
html_str += f' <p style="color:Green;"><b> {final_options[3]} </b></p> ' if ans_index == 3 else f' <p><b> {final_options[3]} </b></p> '
|
109 |
-
html_str += f"""
|
110 |
-
"""
|
111 |
-
st.markdown(html_str , unsafe_allow_html=True)
|
112 |
-
st.markdown("-----")
|
113 |
-
questions.append(ques)
|
114 |
-
output_path = "results/df_quiz_log_file_v1.csv"
|
115 |
-
res_df = pd.DataFrame({"TimeStamp":[start_time]*len(ans_list),\
|
116 |
-
"Input":[str(raw_text)]*len(ans_list),\
|
117 |
-
"Question":questions,"Option1":option1,\
|
118 |
-
"Option2":option2,\
|
119 |
-
"Option3":option3,\
|
120 |
-
"Option4":option4,\
|
121 |
-
"Correct Answer":ans_list})
|
122 |
-
res_df.to_csv(output_path, mode='a', index=False, sep="\t", header= not os.path.exists(output_path))
|
123 |
-
# st.dataframe(pd.read_csv(output_path,sep="\t").tail(5))
|
124 |
-
csv_downloader(pd.read_csv(output_path,sep="\t"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|