Engineer786 commited on
Commit
2d00bb4
·
verified ·
1 Parent(s): 438e917

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py CHANGED
@@ -16,6 +16,73 @@ embedding_model = SentenceTransformer('distilbert-base-uncased')
16
  # Streamlit UI
17
  st.title("RAG-based Quiz App")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # File Upload
20
  uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
21
  if uploaded_file is not None:
 
16
  # Streamlit UI
17
  st.title("RAG-based Quiz App")
18
 
19
+ # File Upload
20
+ uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
21
+ if uploaded_file is not None:
22
+ # Extract Text from PDF
23
+ pdf_reader = PdfReader(uploaded_file)
24
+ text = " ".join([page.extract_text() for page in pdf_reader.pages])
25
+
26
+ # Chunking Text
27
+ st.write("Processing the PDF...")
28
+ chunk_size = 500
29
+ chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
30
+
31
+ # Create Embeddings
32
+ embeddings = embedding_model.encode(chunks)
33
+ embeddings = np.array(embeddings, dtype="float32")
34
+
35
+ # FAISS Index
36
+ index = faiss.IndexFlatL2(embeddings.shape[1])
37
+ index.add(embeddings)
38
+
39
+ st.success("PDF Processed! Embeddings Created.")
40
+
41
+ # Generate Questions
42
+ st.write("Generating Quiz Questions...")
43
+ questions = []
44
+ for chunk in chunks[:3]: # Process fewer chunks to improve performance
45
+ response = client.chat.completions.create(
46
+ messages=[{"role": "user", "content": f"Create a multiple-choice quiz question from this text: {chunk}"}],
47
+ model="llama3-8b-8192"
48
+ )
49
+ question = response.choices[0].message.content
50
+ questions.append(question)
51
+
52
+ st.success("Quiz Questions Generated!")
53
+
54
+ # Display Quiz
55
+ for idx, question in enumerate(questions):
56
+ st.write(f"**Question {idx+1}:** {question}")
57
+
58
+ # Parse Question to Extract Correct Answer (Assuming the API formats it consistently)
59
+ # Example format: "Question: ... Options: A) ..., B) ..., C) ..., D) ... Correct: A"
60
+ lines = question.split("\n")
61
+ options = [line.split(") ")[1] for line in lines if line.strip().startswith(("A", "B", "C", "D"))]
62
+ correct_option_line = [line for line in lines if "Correct:" in line]
63
+ correct_option = correct_option_line[0].split(": ")[1].strip() if correct_option_line else None
64
+
65
+ selected_option = st.radio(f"Select your answer for Question {idx+1}", options, key=idx)
66
+
67
+ if st.button(f"Submit Answer for Question {idx+1}", key=f"submit_{idx}"):
68
+ if selected_option == correct_option:
69
+ st.success("Correct Answer!")
70
+ else:
71
+ st.error(f"Wrong Answer! Correct Answer: {correct_option}")
72
+
73
+ # Highlight Correct and Selected Options
74
+ st.write(f"**Correct Option:** {correct_option}")
75
+ st.write(f"**Your Selection:** {selected_option}")
76
+
77
+ # Footer
78
+ st.write("App developed and deployed using Hugging Face Spaces.")
79
+
80
+ # Initialize Embedding Model
81
+ embedding_model = SentenceTransformer('distilbert-base-uncased')
82
+
83
+ # Streamlit UI
84
+ st.title("RAG-based Quiz App")
85
+
86
  # File Upload
87
  uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
88
  if uploaded_file is not None: