mohammed3536 commited on
Commit
e4c9cfd
1 Parent(s): ac235f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from PyPDF2 import PdfReader
5
+ from langchain_openai import OpenAI, OpenAIEmbeddings
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain.chains import LLMChain
8
+ from langchain_community.vectorstores import FAISS
9
+
10
+ # Load environment variables
11
+ load_dotenv()
12
+ openai_api_key = os.getenv('OPENAI_API_KEY')
13
+
14
+ # Initialize Streamlit session states
15
+ if 'vectorDB' not in st.session_state:
16
+ st.session_state.vectorDB = None
17
+
18
+ # Function to extract text from a PDF file
19
+ def get_pdf_text(pdf):
20
+ text = ""
21
+ pdf_reader = PdfReader(pdf)
22
+ for page in pdf_reader.pages:
23
+ text += page.extract_text()
24
+ return text
25
+
26
+ # Function to create a vector database
27
+ def get_vectorstore(text_chunks):
28
+ embeddings = OpenAIEmbeddings(api_key=openai_api_key)
29
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
30
+ return vectorstore
31
+
32
+ # Function to split text into chunks
33
+ def get_text_chunks(text):
34
+ text_chunks = text.split('\n\n') # Modify this based on your text splitting requirements
35
+ return text_chunks
36
+
37
+ # Function to process PDF and create vector database
38
+ def processing(pdf):
39
+ raw_text = get_pdf_text(pdf)
40
+ text_chunks = get_text_chunks(raw_text)
41
+ vectorDB = get_vectorstore(text_chunks)
42
+ return vectorDB
43
+
44
+ # Function to generate quiz questions
45
+ def generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content):
46
+ st.header(f"Quiz Generator: {quiz_name}")
47
+ st.subheader(f"Topic: {quiz_topic}")
48
+
49
+ # Process PDF and create vector database
50
+ if st.button('Process PDF'):
51
+ st.session_state['vectorDB'] = processing(pdf_content)
52
+ st.success('PDF Processed and Vector Database Created')
53
+
54
+ # Generate Quiz Questions
55
+ for i in range(1, num_questions + 1):
56
+ st.subheader(f"Question {i}")
57
+ question = st.text_input(f"Enter Question {i}:", key=f"question_{i}")
58
+ options = []
59
+ for j in range(1, 5):
60
+ option = st.text_input(f"Option {j}:", key=f"option_{i}_{j}")
61
+ options.append(option)
62
+
63
+ correct_answer = st.selectbox(f"Correct Answer for Question {i}:", options=options, key=f"correct_answer_{i}")
64
+
65
+ # Save question, options, and correct answer in vector database
66
+ if st.session_state.vectorDB:
67
+ # Create a prompt template for question and options
68
+ template = f"Quiz: {quiz_name}\nTopic: {quiz_topic}\nQuestion: {question}\nOptions: {', '.join(options)}\nCorrect Answer: {correct_answer}"
69
+ prompt = PromptTemplate(template=template)
70
+
71
+ # Store question data in vector database
72
+ st.session_state.vectorDB.add(prompt.generate(), embedding=None)
73
+
74
+ # Save button to store vector database
75
+ if st.session_state.vectorDB:
76
+ if st.button('Save Vector Database'):
77
+ st.success('Vector Database Saved')
78
+
79
+ if __name__ =='__main__':
80
+ st.set_page_config(page_title="Quiz Generator", page_icon="📝")
81
+ st.title('Quiz Generator')
82
+
83
+ # User inputs
84
+ quiz_name = st.text_input('Enter Quiz Name:')
85
+ quiz_topic = st.text_input('Enter Quiz Topic:')
86
+ num_questions = st.number_input('Enter Number of Questions:', min_value=1, value=1, step=1)
87
+ pdf_content = st.file_uploader("Upload PDF Content for Questions:", type='pdf')
88
+
89
+ # Generate quiz if all inputs are provided
90
+ if quiz_name and quiz_topic and num_questions and pdf_content:
91
+ generate_quiz(quiz_name, quiz_topic, num_questions, pdf_content)