Spaces:
Sleeping
Sleeping
Krishnan Palanisami
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import wikipedia
|
3 |
+
from haystack.document_stores import InMemoryDocumentStore
|
4 |
+
from haystack.utils import clean_wiki_text, convert_files_to_docs
|
5 |
+
from haystack.nodes import TfidfRetriever, FARMReader
|
6 |
+
from haystack.pipelines import ExtractiveQAPipeline
|
7 |
+
from main import print_qa, QuestionGenerator
|
8 |
+
|
9 |
+
def main():
|
10 |
+
# Set the Streamlit app title
|
11 |
+
st.title("Question Generation using Haystack and Streamlit")
|
12 |
+
|
13 |
+
# Select the input type
|
14 |
+
inputs = ["Input Paragraph", "Wikipedia Examples"]
|
15 |
+
input_type = st.selectbox("Select an input type:", inputs)
|
16 |
+
|
17 |
+
# Initialize wiki_text as an empty string
|
18 |
+
wiki_text = ""
|
19 |
+
|
20 |
+
# Handle different input types
|
21 |
+
if input_type == "Input Paragraph":
|
22 |
+
# Allow user to input text paragraph
|
23 |
+
wiki_text = st.text_area("Input paragraph:", height=200)
|
24 |
+
|
25 |
+
elif input_type == "Wikipedia Examples":
|
26 |
+
# Define topics for selection
|
27 |
+
topics = ["Deep Learning", "Machine Learning"]
|
28 |
+
selected_topic = st.selectbox("Select a topic:", topics)
|
29 |
+
|
30 |
+
# Retrieve Wikipedia content based on the selected topic
|
31 |
+
if selected_topic:
|
32 |
+
wiki = wikipedia.page(selected_topic)
|
33 |
+
wiki_text = wiki.content
|
34 |
+
|
35 |
+
# Display the retrieved Wikipedia content (optional)
|
36 |
+
st.text_area("Retrieved Wikipedia content:", wiki_text, height=200)
|
37 |
+
|
38 |
+
# Preprocess the input text
|
39 |
+
wiki_text = clean_wiki_text(wiki_text)
|
40 |
+
|
41 |
+
# Allow user to specify the number of questions to generate
|
42 |
+
num_questions = st.slider("Number of questions to generate:", min_value=1, max_value=20, value=5)
|
43 |
+
|
44 |
+
# Allow user to specify the model to use
|
45 |
+
model_options = ["deepset/roberta-base-squad2", "deepset/roberta-base-squad2-distilled", "bert-large-uncased-whole-word-masking-squad2", "deepset/flan-t5-xl-squad2"]
|
46 |
+
model_name = st.selectbox("Select model:", model_options)
|
47 |
+
|
48 |
+
# Button to generate questions
|
49 |
+
if st.button("Generate Questions"):
|
50 |
+
document_store = InMemoryDocumentStore()
|
51 |
+
|
52 |
+
# Convert the preprocessed text into a document
|
53 |
+
document = {"content": wiki_text}
|
54 |
+
document_store.write_documents([document])
|
55 |
+
|
56 |
+
# Initialize a TfidfRetriever
|
57 |
+
retriever = TfidfRetriever(document_store=document_store)
|
58 |
+
|
59 |
+
# Initialize a FARMReader with the selected model
|
60 |
+
reader = FARMReader(model_name_or_path=model_name, use_gpu=False)
|
61 |
+
|
62 |
+
# Initialize the question generation pipeline
|
63 |
+
pipe = ExtractiveQAPipeline(reader, retriever)
|
64 |
+
|
65 |
+
# Initialize the QuestionGenerator
|
66 |
+
qg = QuestionGenerator()
|
67 |
+
|
68 |
+
# Generate multiple-choice questions
|
69 |
+
qa_list = qg.generate(
|
70 |
+
wiki_text,
|
71 |
+
num_questions=num_questions,
|
72 |
+
answer_style='multiple_choice'
|
73 |
+
)
|
74 |
+
|
75 |
+
# Display the generated questions and answers
|
76 |
+
st.header("Generated Questions and Answers:")
|
77 |
+
for idx, qa in enumerate(qa_list):
|
78 |
+
# Display the question
|
79 |
+
st.write(f"Question {idx + 1}: {qa['question']}")
|
80 |
+
|
81 |
+
# Display the answer options
|
82 |
+
if 'answer' in qa:
|
83 |
+
for i, option in enumerate(qa['answer']):
|
84 |
+
correct_marker = "(correct)" if option["correct"] else ""
|
85 |
+
st.write(f"Option {i + 1}: {option['answer']} {correct_marker}")
|
86 |
+
|
87 |
+
# Add a separator after each question-answer pair
|
88 |
+
st.write("-" * 40)
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
# Run the Streamlit app
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|
99 |
+
|
100 |
+
|