File size: 2,744 Bytes
901ec26
 
d2829ee
28d0527
d2829ee
 
 
1e72595
d2829ee
 
28d0527
d2829ee
28d0527
d2829ee
 
 
 
 
 
 
28d0527
1e72595
 
 
 
 
d2829ee
 
1e72595
 
 
 
 
d2829ee
1e72595
d2829ee
 
1e72595
d2829ee
 
28d0527
d2829ee
 
 
28d0527
d2829ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e72595
28d0527
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
from transformers import pipeline
import PyPDF2

# Load Hugging Face Models
@st.cache_resource
def load_models():
    question_generator = pipeline("text2text-generation", model="valhalla/t5-small-qg-prepend")
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
    return question_generator, summarizer

question_generator, summarizer = load_models()

# Extract text from a PDF file
def extract_text_from_pdf(pdf_file):
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    extracted_text = ""
    for page in pdf_reader.pages:
        extracted_text += page.extract_text()
    return extracted_text

# Summarize text
def summarize_text(text):
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
    return summary[0]["summary_text"]

# Generate MCQs
def generate_questions(text, num_questions=5):
    clean_text = text[:500]  # Limit input to first 500 characters
    prompt = (
        f"Create {num_questions} diverse multiple-choice questions focusing on key concepts. "
        f"Use the text: {clean_text}"
    )
    questions = question_generator(
        prompt,
        max_length=128,
        num_beams=5,
        num_return_sequences=min(num_questions, 3),  # Generate up to 3 per request
    )
    return [q["generated_text"] for q in questions]

# Streamlit App
st.title("AI-Powered Quiz Generator")
st.subheader("Create quizzes and summaries from any text or PDF file using AI.")

# Input options
st.write("### Step 1: Provide Input")
input_option = st.radio("Choose input method:", ("Text Input", "Upload PDF"))
input_text = ""

if input_option == "Text Input":
    input_text = st.text_area("Enter your text:", height=200)
elif input_option == "Upload PDF":
    pdf_file = st.file_uploader("Upload a PDF file:", type=["pdf"])
    if pdf_file:
        input_text = extract_text_from_pdf(pdf_file)
        st.write("Extracted Text Preview:")
        st.write(input_text[:1000])  # Show a preview of the extracted text

# Generate quiz and summary
if st.button("Generate Quiz"):
    if input_text.strip():
        # Summarize content
        st.write("### Summary")
        with st.spinner("Summarizing content..."):
            summary = summarize_text(input_text)
            st.write(summary)

        # Generate MCQs
        st.write("### Generated Questions")
        with st.spinner("Generating questions..."):
            questions = generate_questions(summary, num_questions=5)
            for i, question in enumerate(questions):
                st.write(f"**{i+1}. {question}**")
    else:
        st.error("Please enter some text or upload a PDF to generate a quiz!")

# Footer
st.markdown("---")
st.markdown("**Created by Engr Muhammad Arsalan**")