mohammed3536 commited on
Commit
bba13a8
·
verified ·
1 Parent(s): f501194

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PyPDF2
2
+ import chromadb
3
+ import streamlit as st
4
+ from langchain_openai import OpenAI
5
+
6
+ # Function to extract text from PDF
7
+ def extract_text_from_pdf(pdf_path):
8
+ text = ""
9
+ with open(pdf_path, "rb") as file:
10
+ pdf_reader = PyPDF2.PdfFileReader(file)
11
+ for page_num in range(pdf_reader.numPages):
12
+ text += pdf_reader.getPage(page_num).extractText()
13
+ return text
14
+
15
+ # Function to create chunks of text
16
+ def create_text_chunks(text, chunk_size=1000, overlap_size=100):
17
+ chunks = []
18
+ for i in range(0, len(text), chunk_size - overlap_size):
19
+ chunks.append(text[i:i + chunk_size])
20
+ return chunks
21
+
22
+ # Function to save chunks to chromadb vector database
23
+ def save_to_chromadb(chunks, quiz_name, quiz_topic):
24
+ # Assume you have a ChromaDB instance named 'db'
25
+ db = chromadb.ChromaDB("your_chromadb_url")
26
+ for i, chunk in enumerate(chunks):
27
+ vector = langchain_openai.get_vector(chunk)
28
+ db.add_vector(quiz_name, quiz_topic, i, vector)
29
+
30
+ # Function to generate questions using ChatGPT-3.5-turbo-16k
31
+ def generate_questions(topic):
32
+ prompt = f"Generate questions on the topic: {topic}"
33
+ response = langchain_openai.complete(prompt)
34
+ return response.choices[0].text.strip()
35
+
36
+ # Streamlit interface
37
+ def main():
38
+ st.title("Quiz Generator")
39
+
40
+ # User inputs
41
+ quiz_name = st.text_input("Enter Quiz Name:")
42
+ quiz_topic = st.text_input("Enter Quiz Topic:")
43
+ num_questions = st.number_input("Number of Questions:", value=5, min_value=1)
44
+ pdf_path = st.file_uploader("Upload PDF File:", type=["pdf"])
45
+
46
+ if pdf_path:
47
+ # Extract text from PDF
48
+ pdf_text = extract_text_from_pdf(pdf_path)
49
+
50
+ # Create and save text chunks to ChromaDB
51
+ text_chunks = create_text_chunks(pdf_text)
52
+ save_to_chromadb(text_chunks, quiz_name, quiz_topic)
53
+
54
+ # User input for query
55
+ user_query = st.text_input("Enter Query for Question Generation:")
56
+
57
+ # Search for the topic in the vector database
58
+ if quiz_topic in db.get_topics(quiz_name):
59
+ # Generate questions using ChatGPT-3.5-turbo-16k
60
+ generated_questions = generate_questions(user_query)
61
+
62
+ st.subheader("Generated Questions:")
63
+ st.write(generated_questions)
64
+ else:
65
+ st.warning("Specified topic not found in the document.")
66
+
67
+ if __name__ == "__main__":
68
+ main()