File size: 3,755 Bytes
ac04b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
import io
import os
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_community.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain_community.vectorstores import FAISS
import openai

# Load environment variables
load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')

# Initialize Streamlit session states
if 'vectorDB' not in st.session_state:
    st.session_state.vectorDB = None

# Function to extract text from a PDF file
def get_pdf_text(pdf):
    text = ""
    pdf_reader = PdfReader(pdf)
    for page in pdf_reader.pages:
        text += page.extract_text()
    return text

# Function to create a vector database
def get_vectorstore(text_chunks):
    embeddings = OpenAIEmbeddings(api_key=openai_api_key)
    vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
    return vectorstore

# Function to split text into chunks
def get_text_chunks(text):
    text_chunks = text.split('\n\n')  # Modify this based on your text splitting requirements
    return text_chunks

# Function to retrieve quiz data from the vector database
def retrieve_quiz_data(vectorDB, num_questions):
    # Retrieve stored quiz data from the vector database
    # You need to implement the logic to query the vector database and get quiz data
    # For illustration purposes, assuming you have a function named 'query_vector_database'
    quiz_data = query_vector_database(vectorDB, num_questions)
    return quiz_data

# Function to generate quiz questions
def generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content):
    st.header(f"Quiz Generator: {quiz_name}")
    st.subheader(f"Topic: {quiz_topic}")

    # Process PDF and create vector database
    if st.button('Generate Quiz'):
        st.session_state['vectorDB'] = processing(pdf_content)
        st.success('PDF Processed and Vector Database Created')

    # Generate Quiz Questions
    if st.session_state.vectorDB:
        # Retrieve quiz data from the vector database
        generated_quiz_data = retrieve_quiz_data(st.session_state.vectorDB, num_questions)

        # Display retrieved questions, options, and correct answers
        for i, (question, options, correct_answer) in enumerate(generated_quiz_data):
            st.subheader(f"Question {i + 1}")
            st.write(f"Retrieved Question: {question}")
            st.write(f"Options: {', '.join(options)}")
            st.write(f"Correct Answer: {correct_answer}")

    # Save button to store vector database
    if st.session_state.vectorDB:
        if st.button('Save Vector Database'):
            st.success('Vector Database Saved')

# Replace this function with your logic to query the vector database and get quiz data
def query_vector_database(vectorDB, num_questions):
    # Implement your logic to query the vector database and retrieve quiz data
    # This is a placeholder, replace it with your actual implementation
    return [(f"Question {i + 1}", [f"Option {j}" for j in range(1, 5)], f"Option {i % 4 + 1}") for i in range(num_questions)]

if __name__ =='__main__':
    st.set_page_config(page_title="CB Quiz Generator", page_icon="📝")
    st.title('CB Quiz Generator')

    # User inputs
    quiz_name = st.text_input('Enter Quiz Name:')
    quiz_topic = st.text_input('Enter Quiz Topic:')
    num_questions = st.number_input('Enter Number of Questions:', min_value=1, value=1, step=1)
    pdf_content = st.file_uploader("Upload PDF Content for Questions:", type='pdf')

    # Generate quiz if all inputs are provided
    if quiz_name and quiz_topic and num_questions and pdf_content:
        generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content)