File size: 3,532 Bytes
e4c9cfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179644c
 
 
e4c9cfd
 
 
 
 
 
 
 
 
 
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 os
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from langchain_openai import OpenAI, OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_community.vectorstores import FAISS

# 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 process PDF and create vector database
def processing(pdf):
    raw_text = get_pdf_text(pdf)
    text_chunks = get_text_chunks(raw_text)
    vectorDB = get_vectorstore(text_chunks)
    return vectorDB

# 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('Process PDF'):
        st.session_state['vectorDB'] = processing(pdf_content)
        st.success('PDF Processed and Vector Database Created')

    # Generate Quiz Questions
    for i in range(1, num_questions + 1):
        st.subheader(f"Question {i}")
        question = st.text_input(f"Enter Question {i}:", key=f"question_{i}")
        options = []
        for j in range(1, 5):
            option = st.text_input(f"Option {j}:", key=f"option_{i}_{j}")
            options.append(option)

        correct_answer = st.selectbox(f"Correct Answer for Question {i}:", options=options, key=f"correct_answer_{i}")

        # Save question, options, and correct answer in vector database
        if st.session_state.vectorDB:
            # Create a prompt template for question and options
            template = f"Quiz: {quiz_name}\nTopic: {quiz_topic}\nQuestion: {question}\nOptions: {', '.join(options)}\nCorrect Answer: {correct_answer}"
            prompt = PromptTemplate(template=template)

            # Store question data in vector database
            st.session_state.vectorDB.add(prompt.generate(), embedding=None)

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

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

    # 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)