File size: 4,159 Bytes
e4c9cfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33f1328
 
 
982c7d5
 
33f1328
ba459a9
 
33f1328
 
 
 
 
e4c9cfd
 
 
 
 
 
 
 
 
33f1328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4c9cfd
 
 
 
 
 
 
179644c
 
b38f6a1
e4c9cfd
 
 
 
b38f6a1
e4c9cfd
 
 
 
 
45889c6
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 questions using OpenAI GPT-3
def generate_questions(text, num_questions):
    prompt = f"Generate {num_questions} questions from the given text:\n{text}"
    response = OpenAI.Completion.create(
        engine="text-davinci-003",  # You can use another engine if needed
        prompt=prompt,
        max_tokens=200,
        temperature=0.7
    )
    questions = [choice['text'].strip() for choice in response['choices']]
    return questions

# Modified generate_quiz function
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 using OpenAI GPT-3.5
    if st.session_state.vectorDB:
        raw_text = get_pdf_text(pdf_content)
        generated_questions = generate_questions(raw_text, num_questions)

        # Display and collect user input for each generated question
        for i, generated_question in enumerate(generated_questions):
            st.subheader(f"Question {i + 1}")
            question = st.text_input(f"Generated Question: {generated_question}", key=f"question_{i + 1}")

            # Collect options and correct answer
            options = []
            for j in range(1, 5):
                option = st.text_input(f"Option {j}:", key=f"option_{i + 1}_{j}")
                options.append(option)

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

            # Save question, options, and correct answer in vector database
            # (Replace the following line with your logic to store in the vector database)
            if st.button(f'Save Question {i + 1}'):
                st.success(f'Question {i + 1} Saved!')

    # 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('Powered By 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=5, 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)