Spaces:
Sleeping
Sleeping
| 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.vectorstores import FAISS | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_community.llms import OpenAI | |
| import openai | |
| import random | |
| # Load environment variables | |
| load_dotenv() | |
| openai_api_key = os.getenv('OPENAI_API_KEY') | |
| openai.api_key = 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 | |
| def get_text_chunks(text: str): | |
| # This function will split the text into smaller chunks | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=1000, | |
| chunk_overlap=100, | |
| length_function=len, | |
| is_separator_regex=False, | |
| ) | |
| chunks = text_splitter.split_text(text) | |
| return chunks | |
| def get_vectorstore(text_chunks): | |
| # This function will create a vector database as well as create and store the embedding of the text chunks into the VectorDB | |
| embeddings = OpenAIEmbeddings() | |
| vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings) | |
| return vectorstore | |
| def search_vectorstore(vectorDB, query): | |
| # This function searches for specific data or content in the vector database | |
| # Specify the search type | |
| search_type = 'similarity' | |
| search_results = vectorDB.search(query, search_type=search_type) | |
| return search_results | |
| def generate_quiz_questions(search_results, num_questions): | |
| # Generate quiz questions with options using GPT-3.5-turbo-16k based on the search results | |
| 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'] = (pdf_content) | |
| # Placeholder for quiz questions | |
| quiz_questions = [] | |
| # Generate questions using GPT-3.5-turbo-16k with the correct endpoint | |
| for i in range(num_questions): | |
| prompt = f"As a highly intelligent and powerful quiz generator, your task is to Generate a different multiple-choice question and correct answer in a comprehensive and accurate format for the same related to:\n- {random.choice(search_results)} and the response generated by you should be comprehensive and relevant to the topic specified, you should not generate the same content multiple times in a single response, generate the explaination for each correct answer" | |
| # Prepare messages | |
| messages = [ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": prompt}, | |
| ] | |
| # Generate questions using GPT-3.5-turbo-16k with the correct endpoint | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo-16k", | |
| messages=messages, | |
| max_tokens=200, | |
| n=num_questions, | |
| stop=None, | |
| temperature=0.7, | |
| ) | |
| # Extract the generated question from the response | |
| question = response['choices'][0]['message']['content'].strip() | |
| correct_answer = f"Correct Answer: {random.choice(search_results)}" | |
| # Generate options (3 incorrect + 1 correct) | |
| correct_option = f"Correct: {random.choice(search_results)}" | |
| incorrect_options = [f"Incorrect: {random.choice(search_results)}" for _ in range(3)] | |
| options = incorrect_options + [correct_option] | |
| # Shuffle the options to randomize the order | |
| random.shuffle(options) | |
| # Append question and options to the quiz_questions list | |
| quiz_questions.append((question, options, correct_answer)) | |
| # Display quiz questions with options | |
| st.write('Quiz Questions:') | |
| for i, (question, options, correct_answer) in enumerate(quiz_questions, 1): | |
| st.write(f"{i}. {question}") | |
| correct_option = [opt for opt in options if opt.startswith('Correct')][0] | |
| 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=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: | |
| # Process PDF and create vector database | |
| vectorDB = get_vectorstore(get_text_chunks(get_pdf_text(pdf_content))) | |
| # Searching for a specific query in the vector database | |
| if quiz_topic: | |
| search_results = search_vectorstore(vectorDB, quiz_topic) | |
| # Generate quiz questions with options using GPT-3.5-turbo-16k based on the search results | |
| generate_quiz_questions(search_results, num_questions) | |
| else: | |
| st.error('Please provide a topic to search for!') | |