mohammed3536 commited on
Commit
ac04b1c
1 Parent(s): 84b6716

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ import streamlit as st
4
+ from dotenv import load_dotenv
5
+ from PyPDF2 import PdfReader
6
+ from langchain_community.embeddings import OpenAIEmbeddings
7
+ from langchain_community.llms import OpenAI
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain_community.vectorstores import FAISS
10
+ import openai
11
+
12
+ # Load environment variables
13
+ load_dotenv()
14
+ openai_api_key = os.getenv('OPENAI_API_KEY')
15
+
16
+ # Initialize Streamlit session states
17
+ if 'vectorDB' not in st.session_state:
18
+ st.session_state.vectorDB = None
19
+
20
+ # Function to extract text from a PDF file
21
+ def get_pdf_text(pdf):
22
+ text = ""
23
+ pdf_reader = PdfReader(pdf)
24
+ for page in pdf_reader.pages:
25
+ text += page.extract_text()
26
+ return text
27
+
28
+ # Function to create a vector database
29
+ def get_vectorstore(text_chunks):
30
+ embeddings = OpenAIEmbeddings(api_key=openai_api_key)
31
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
32
+ return vectorstore
33
+
34
+ # Function to split text into chunks
35
+ def get_text_chunks(text):
36
+ text_chunks = text.split('\n\n') # Modify this based on your text splitting requirements
37
+ return text_chunks
38
+
39
+ # Function to retrieve quiz data from the vector database
40
+ def retrieve_quiz_data(vectorDB, num_questions):
41
+ # Retrieve stored quiz data from the vector database
42
+ # You need to implement the logic to query the vector database and get quiz data
43
+ # For illustration purposes, assuming you have a function named 'query_vector_database'
44
+ quiz_data = query_vector_database(vectorDB, num_questions)
45
+ return quiz_data
46
+
47
+ # Function to generate quiz questions
48
+ def generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content):
49
+ st.header(f"Quiz Generator: {quiz_name}")
50
+ st.subheader(f"Topic: {quiz_topic}")
51
+
52
+ # Process PDF and create vector database
53
+ if st.button('Generate Quiz'):
54
+ st.session_state['vectorDB'] = processing(pdf_content)
55
+ st.success('PDF Processed and Vector Database Created')
56
+
57
+ # Generate Quiz Questions
58
+ if st.session_state.vectorDB:
59
+ # Retrieve quiz data from the vector database
60
+ generated_quiz_data = retrieve_quiz_data(st.session_state.vectorDB, num_questions)
61
+
62
+ # Display retrieved questions, options, and correct answers
63
+ for i, (question, options, correct_answer) in enumerate(generated_quiz_data):
64
+ st.subheader(f"Question {i + 1}")
65
+ st.write(f"Retrieved Question: {question}")
66
+ st.write(f"Options: {', '.join(options)}")
67
+ st.write(f"Correct Answer: {correct_answer}")
68
+
69
+ # Save button to store vector database
70
+ if st.session_state.vectorDB:
71
+ if st.button('Save Vector Database'):
72
+ st.success('Vector Database Saved')
73
+
74
+ # Replace this function with your logic to query the vector database and get quiz data
75
+ def query_vector_database(vectorDB, num_questions):
76
+ # Implement your logic to query the vector database and retrieve quiz data
77
+ # This is a placeholder, replace it with your actual implementation
78
+ 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)]
79
+
80
+ if __name__ =='__main__':
81
+ st.set_page_config(page_title="CB Quiz Generator", page_icon="📝")
82
+ st.title('CB Quiz Generator')
83
+
84
+ # User inputs
85
+ quiz_name = st.text_input('Enter Quiz Name:')
86
+ quiz_topic = st.text_input('Enter Quiz Topic:')
87
+ num_questions = st.number_input('Enter Number of Questions:', min_value=1, value=1, step=1)
88
+ pdf_content = st.file_uploader("Upload PDF Content for Questions:", type='pdf')
89
+
90
+ # Generate quiz if all inputs are provided
91
+ if quiz_name and quiz_topic and num_questions and pdf_content:
92
+ generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content)