quiz / app.py
mohammed3536's picture
Update app.py
982c7d5 verified
raw
history blame contribute delete
No virus
4.16 kB
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)