Spaces:
Runtime error
Runtime error
fixing issues with session_id states, adding visualizations
Browse filesquestions were not getting printed in the states so fix the issue (code still had session_state.generated_questions in it there so.. :) )
app.py
CHANGED
|
@@ -36,7 +36,8 @@ st.set_page_config(
|
|
| 36 |
"About" : "#Hi this our project."
|
| 37 |
}
|
| 38 |
)
|
| 39 |
-
|
|
|
|
| 40 |
# Initialize Wikipedia API with a user agent
|
| 41 |
user_agent = 'QGen/1.0 (channingfisher7@gmail.com)'
|
| 42 |
wiki_wiki = wikipediaapi.Wikipedia(user_agent= user_agent,language='en')
|
|
@@ -65,7 +66,7 @@ def set_state(session_id, key, value):
|
|
| 65 |
|
| 66 |
@st.cache_resource
|
| 67 |
def load_model():
|
| 68 |
-
model_name = "DevBM/t5-
|
| 69 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 70 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 71 |
return model, tokenizer
|
|
@@ -198,7 +199,6 @@ def get_synonyms(word, n=3):
|
|
| 198 |
def generate_options(answer, context, n=3):
|
| 199 |
options = [answer]
|
| 200 |
|
| 201 |
-
|
| 202 |
# Add contextually relevant words using a pre-trained model
|
| 203 |
context_embedding = context_model.encode(context)
|
| 204 |
answer_embedding = context_model.encode(answer)
|
|
@@ -337,9 +337,6 @@ def main():
|
|
| 337 |
st.title(":blue[Question Generator System]")
|
| 338 |
session_id = get_session_id()
|
| 339 |
state = initialize_state(session_id)
|
| 340 |
-
# Initialize session state
|
| 341 |
-
if 'generated_questions' not in st.session_state:
|
| 342 |
-
st.session_state.generated_questions = []
|
| 343 |
|
| 344 |
with st.sidebar:
|
| 345 |
st.subheader("Customization Options")
|
|
@@ -359,6 +356,7 @@ def main():
|
|
| 359 |
extract_all_keywords = st.toggle("Extract Max Keywords",value=False)
|
| 360 |
with col2:
|
| 361 |
enable_feedback_mode = st.toggle("Enable Feedback Mode",False)
|
|
|
|
| 362 |
text = None
|
| 363 |
if input_type == "Text Input":
|
| 364 |
text = st.text_area("Enter text here:", value="Joe Biden, the current US president is on a weak wicket going in for his reelection later this November against former President Donald Trump.")
|
|
@@ -370,7 +368,8 @@ def main():
|
|
| 370 |
text = clean_text(text)
|
| 371 |
segments = segment_text(text)
|
| 372 |
generate_questions_button = st.button("Generate Questions")
|
| 373 |
-
|
|
|
|
| 374 |
state['generated_questions'] = []
|
| 375 |
# st.session_state.generated_questions = []
|
| 376 |
for text in segments:
|
|
@@ -380,6 +379,8 @@ def main():
|
|
| 380 |
for i, (keyword, context) in enumerate(keyword_sentence_mapping.items()):
|
| 381 |
if i >= num_questions:
|
| 382 |
break
|
|
|
|
|
|
|
| 383 |
question = generate_question(context, keyword, num_beams=num_beams)
|
| 384 |
options = generate_options(keyword,context)
|
| 385 |
overall_score, relevance_score, complexity_score, spelling_correctness = assess_question_quality(context,question,keyword)
|
|
@@ -395,20 +396,22 @@ def main():
|
|
| 395 |
"complexity_score" : complexity_score,
|
| 396 |
"spelling_correctness" : spelling_correctness,
|
| 397 |
}
|
|
|
|
| 398 |
# st.session_state.generated_questions.append(tpl)
|
| 399 |
state['generated_questions'].append(tpl)
|
| 400 |
-
|
|
|
|
|
|
|
|
|
|
| 401 |
set_state(session_id, 'generated_questions', state['generated_questions'])
|
|
|
|
| 402 |
|
| 403 |
# sort question based on their quality score
|
| 404 |
-
# st.session_state.generated_questions = sorted(st.session_state.generated_questions,key = lambda x: x['overall_score'], reverse=True)
|
| 405 |
state['generated_questions'] = sorted(state['generated_questions'],key = lambda x: x['overall_score'], reverse=True)
|
| 406 |
# Display generated questions
|
| 407 |
-
# if st.session_state.generated_questions:
|
| 408 |
if state['generated_questions']:
|
| 409 |
st.header("Generated Questions:",divider='blue')
|
| 410 |
-
for i, q in enumerate(
|
| 411 |
-
# with st.expander(f"Question {i+1}"):
|
| 412 |
st.subheader(body=f":orange[Q{i+1}:] {q['question']}")
|
| 413 |
|
| 414 |
if show_context is True:
|
|
@@ -430,7 +433,7 @@ def main():
|
|
| 430 |
m3.metric("Complexity Score", value=f"{q['complexity_score']:,.2f}")
|
| 431 |
m4.metric("Spelling Correctness", value=f"{q['spelling_correctness']:,.2f}")
|
| 432 |
|
| 433 |
-
|
| 434 |
if enable_feedback_mode:
|
| 435 |
q['question'] = st.text_input(f"Edit Question {i+1}:", value=q['question'], key=f"question_{i}")
|
| 436 |
q['rating'] = st.selectbox(f"Rate this question (1-5)", options=[1, 2, 3, 4, 5], key=f"rating_{i}")
|
|
@@ -443,43 +446,51 @@ def main():
|
|
| 443 |
# if st.session_state.generated_questions:
|
| 444 |
if state['generated_questions']:
|
| 445 |
with st.sidebar:
|
| 446 |
-
csv_data = export_to_csv(
|
| 447 |
st.download_button(label="Download CSV", data=csv_data, file_name='questions.csv', mime='text/csv')
|
| 448 |
|
| 449 |
-
pdf_data = export_to_pdf(
|
| 450 |
st.download_button(label="Download PDF", data=pdf_data, file_name='questions.pdf', mime='application/pdf')
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
print("********************************************************************************")
|
| 483 |
|
| 484 |
if __name__ == '__main__':
|
| 485 |
main()
|
|
|
|
| 36 |
"About" : "#Hi this our project."
|
| 37 |
}
|
| 38 |
)
|
| 39 |
+
# st.set_option(deprecation.showPyplotGlobalUse=False)
|
| 40 |
+
# st.set_option('base','dark')
|
| 41 |
# Initialize Wikipedia API with a user agent
|
| 42 |
user_agent = 'QGen/1.0 (channingfisher7@gmail.com)'
|
| 43 |
wiki_wiki = wikipediaapi.Wikipedia(user_agent= user_agent,language='en')
|
|
|
|
| 66 |
|
| 67 |
@st.cache_resource
|
| 68 |
def load_model():
|
| 69 |
+
model_name = "DevBM/t5-small-squad"
|
| 70 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 71 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 72 |
return model, tokenizer
|
|
|
|
| 199 |
def generate_options(answer, context, n=3):
|
| 200 |
options = [answer]
|
| 201 |
|
|
|
|
| 202 |
# Add contextually relevant words using a pre-trained model
|
| 203 |
context_embedding = context_model.encode(context)
|
| 204 |
answer_embedding = context_model.encode(answer)
|
|
|
|
| 337 |
st.title(":blue[Question Generator System]")
|
| 338 |
session_id = get_session_id()
|
| 339 |
state = initialize_state(session_id)
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
with st.sidebar:
|
| 342 |
st.subheader("Customization Options")
|
|
|
|
| 356 |
extract_all_keywords = st.toggle("Extract Max Keywords",value=False)
|
| 357 |
with col2:
|
| 358 |
enable_feedback_mode = st.toggle("Enable Feedback Mode",False)
|
| 359 |
+
# set_state(session_id, 'generated_questions', state['generated_questions'])
|
| 360 |
text = None
|
| 361 |
if input_type == "Text Input":
|
| 362 |
text = st.text_area("Enter text here:", value="Joe Biden, the current US president is on a weak wicket going in for his reelection later this November against former President Donald Trump.")
|
|
|
|
| 368 |
text = clean_text(text)
|
| 369 |
segments = segment_text(text)
|
| 370 |
generate_questions_button = st.button("Generate Questions")
|
| 371 |
+
q_count = 0
|
| 372 |
+
if generate_questions_button:
|
| 373 |
state['generated_questions'] = []
|
| 374 |
# st.session_state.generated_questions = []
|
| 375 |
for text in segments:
|
|
|
|
| 379 |
for i, (keyword, context) in enumerate(keyword_sentence_mapping.items()):
|
| 380 |
if i >= num_questions:
|
| 381 |
break
|
| 382 |
+
if q_count>num_questions:
|
| 383 |
+
break
|
| 384 |
question = generate_question(context, keyword, num_beams=num_beams)
|
| 385 |
options = generate_options(keyword,context)
|
| 386 |
overall_score, relevance_score, complexity_score, spelling_correctness = assess_question_quality(context,question,keyword)
|
|
|
|
| 396 |
"complexity_score" : complexity_score,
|
| 397 |
"spelling_correctness" : spelling_correctness,
|
| 398 |
}
|
| 399 |
+
print("\n\n",tpl,"\n\n")
|
| 400 |
# st.session_state.generated_questions.append(tpl)
|
| 401 |
state['generated_questions'].append(tpl)
|
| 402 |
+
q_count += 1
|
| 403 |
+
print("\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n")
|
| 404 |
+
data = get_state(session_id)
|
| 405 |
+
print(data)
|
| 406 |
set_state(session_id, 'generated_questions', state['generated_questions'])
|
| 407 |
+
a = get_state(session_id)
|
| 408 |
|
| 409 |
# sort question based on their quality score
|
|
|
|
| 410 |
state['generated_questions'] = sorted(state['generated_questions'],key = lambda x: x['overall_score'], reverse=True)
|
| 411 |
# Display generated questions
|
|
|
|
| 412 |
if state['generated_questions']:
|
| 413 |
st.header("Generated Questions:",divider='blue')
|
| 414 |
+
for i, q in enumerate(state['generated_questions']):
|
|
|
|
| 415 |
st.subheader(body=f":orange[Q{i+1}:] {q['question']}")
|
| 416 |
|
| 417 |
if show_context is True:
|
|
|
|
| 433 |
m3.metric("Complexity Score", value=f"{q['complexity_score']:,.2f}")
|
| 434 |
m4.metric("Spelling Correctness", value=f"{q['spelling_correctness']:,.2f}")
|
| 435 |
|
| 436 |
+
# q['context'] = st.text_area(f"Edit Context {i+1}:", value=q['context'], key=f"context_{i}")
|
| 437 |
if enable_feedback_mode:
|
| 438 |
q['question'] = st.text_input(f"Edit Question {i+1}:", value=q['question'], key=f"question_{i}")
|
| 439 |
q['rating'] = st.selectbox(f"Rate this question (1-5)", options=[1, 2, 3, 4, 5], key=f"rating_{i}")
|
|
|
|
| 446 |
# if st.session_state.generated_questions:
|
| 447 |
if state['generated_questions']:
|
| 448 |
with st.sidebar:
|
| 449 |
+
csv_data = export_to_csv(state['generated_questions'])
|
| 450 |
st.download_button(label="Download CSV", data=csv_data, file_name='questions.csv', mime='text/csv')
|
| 451 |
|
| 452 |
+
pdf_data = export_to_pdf(state['generated_questions'])
|
| 453 |
st.download_button(label="Download PDF", data=pdf_data, file_name='questions.pdf', mime='application/pdf')
|
| 454 |
+
with st.expander("View Visualizations"):
|
| 455 |
+
questions = [tpl['question'] for tpl in state['generated_questions']]
|
| 456 |
+
overall_scores = [tpl['overall_score'] for tpl in state['generated_questions']]
|
| 457 |
+
st.subheader('WordCloud of Questions',divider='rainbow')
|
| 458 |
+
display_word_cloud(questions)
|
| 459 |
+
st.subheader('Overall Scores',divider='violet')
|
| 460 |
+
overall_scores = pd.DataFrame(overall_scores,columns=['Overall Scores'])
|
| 461 |
+
st.line_chart(overall_scores)
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
# View Feedback Statistics
|
| 465 |
+
with st.expander("View Feedback Statistics"):
|
| 466 |
+
feedback_file = 'question_feedback.json'
|
| 467 |
+
if os.path.exists(feedback_file):
|
| 468 |
+
with open(feedback_file, 'r') as f:
|
| 469 |
+
feedback_data = json.load(f)
|
| 470 |
+
|
| 471 |
+
st.subheader("Feedback Statistics")
|
| 472 |
+
|
| 473 |
+
# Calculate average rating
|
| 474 |
+
ratings = [feedback['rating'] for feedback in feedback_data]
|
| 475 |
+
avg_rating = sum(ratings) / len(ratings) if ratings else 0
|
| 476 |
+
st.write(f"Average Question Rating: {avg_rating:.2f}")
|
| 477 |
+
|
| 478 |
+
# Show distribution of ratings
|
| 479 |
+
rating_counts = {i: ratings.count(i) for i in range(1, 6)}
|
| 480 |
+
st.bar_chart(rating_counts)
|
| 481 |
|
| 482 |
+
# Show some highly rated questions
|
| 483 |
+
st.subheader("Highly Rated Questions")
|
| 484 |
+
sorted_feedback = sorted(feedback_data, key=lambda x: x['rating'], reverse=True)
|
| 485 |
+
top_questions = sorted_feedback[:5]
|
| 486 |
+
for feedback in top_questions:
|
| 487 |
+
st.write(f"Question: {feedback['question']}")
|
| 488 |
+
st.write(f"Answer: {feedback['answer']}")
|
| 489 |
+
st.write(f"Rating: {feedback['rating']}")
|
| 490 |
+
st.write("---")
|
| 491 |
+
else:
|
| 492 |
+
st.write("No feedback data available yet.")
|
| 493 |
+
print("********************************************************************************")
|
|
|
|
| 494 |
|
| 495 |
if __name__ == '__main__':
|
| 496 |
main()
|