MojoHz commited on
Commit
9a7793f
·
verified ·
1 Parent(s): 4c15f4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -26
app.py CHANGED
@@ -23,7 +23,7 @@ def extract_text_from_pdf(pdf_file):
23
  return f"Error extracting text from PDF: {e}"
24
 
25
  def generate_quiz(text, difficulty, num_questions):
26
- """Generate a quiz based on the provided text and difficulty, limited to a specified number of questions."""
27
  try:
28
  prompt = (f"Based on the following text, generate a quiz with a maximum of {num_questions} questions. "
29
  "Include both questions and answers in the format of question and answer pairs. "
@@ -37,7 +37,7 @@ def generate_quiz(text, difficulty, num_questions):
37
  "... and so on. Limit the total number of questions to {num_questions}.")
38
  response = model.generate_content(prompt)
39
 
40
- # Limit the response to the specified number of questions
41
  lines = response.text.split('\n')
42
  limited_quiz = []
43
  question_count = 0
@@ -67,7 +67,7 @@ def answer_question_from_quiz(question):
67
  return f"Error answering question: {e}"
68
 
69
  def process_pdf(pdf_file, difficulty, num_questions):
70
- """Process the uploaded PDF to generate a quiz."""
71
  try:
72
  # Extract text from the PDF
73
  extracted_text = extract_text_from_pdf(pdf_file)
@@ -79,24 +79,9 @@ def process_pdf(pdf_file, difficulty, num_questions):
79
  except Exception as e:
80
  return f"Error processing PDF: {e}"
81
 
82
- def format_quiz_text(quiz_text):
83
- """Format the quiz text to improve readability."""
84
- formatted_text = ""
85
- lines = quiz_text.split('\n')
86
-
87
- question_number = 1
88
- for line in lines:
89
- if line.startswith('Q'):
90
- formatted_text += f"**{line[2:]}**\n\n" # Bold question text
91
- elif line.startswith('A'):
92
- formatted_text += f"**Answer {question_number}:** {line[2:]}\n\n" # Clear answer text
93
- question_number += 1
94
-
95
- return formatted_text.strip()
96
-
97
  def main():
98
- st.title("Welcome to ExtraQ!")
99
- st.write("Please upload an exam or quiz PDF to generate a new quiz with the same style, while having the freedom to change complexity. You can also ask questions based on the generated quiz. This is a student project, with no funds and professional team to follow it regularly, so apologies if any inconvenience happens, I will try my best to improve this project as the time passes by!")
100
 
101
  if 'generated_quiz' not in st.session_state:
102
  st.session_state.generated_quiz = ""
@@ -110,8 +95,7 @@ def main():
110
  st.session_state.generated_quiz = process_pdf(uploaded_file, difficulty, num_questions)
111
 
112
  st.subheader("Generated Quiz")
113
- formatted_quiz = format_quiz_text(st.session_state.generated_quiz)
114
- st.markdown(formatted_quiz)
115
  else:
116
  st.error("Please upload a PDF file.")
117
 
@@ -125,9 +109,5 @@ def main():
125
  else:
126
  st.error("Please type a question to get an answer.")
127
 
128
- # Add credits section
129
- st.markdown("---")
130
- st.markdown("**Developed for NU Students by Mohamed Hafez**")
131
-
132
  if __name__ == "__main__":
133
  main()
 
23
  return f"Error extracting text from PDF: {e}"
24
 
25
  def generate_quiz(text, difficulty, num_questions):
26
+ """Generate a quiz based on the provided text, difficulty, and number of questions."""
27
  try:
28
  prompt = (f"Based on the following text, generate a quiz with a maximum of {num_questions} questions. "
29
  "Include both questions and answers in the format of question and answer pairs. "
 
37
  "... and so on. Limit the total number of questions to {num_questions}.")
38
  response = model.generate_content(prompt)
39
 
40
+ # Limit the response to the number of questions requested
41
  lines = response.text.split('\n')
42
  limited_quiz = []
43
  question_count = 0
 
67
  return f"Error answering question: {e}"
68
 
69
  def process_pdf(pdf_file, difficulty, num_questions):
70
+ """Process the uploaded PDF to generate and explain quiz questions."""
71
  try:
72
  # Extract text from the PDF
73
  extracted_text = extract_text_from_pdf(pdf_file)
 
79
  except Exception as e:
80
  return f"Error processing PDF: {e}"
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def main():
83
+ st.title("AI Quiz Generator")
84
+ st.write("Upload an exam or quiz PDF to generate a new quiz with the same style and complexity. You can also ask questions based on the generated quiz.")
85
 
86
  if 'generated_quiz' not in st.session_state:
87
  st.session_state.generated_quiz = ""
 
95
  st.session_state.generated_quiz = process_pdf(uploaded_file, difficulty, num_questions)
96
 
97
  st.subheader("Generated Quiz")
98
+ st.text(st.session_state.generated_quiz)
 
99
  else:
100
  st.error("Please upload a PDF file.")
101
 
 
109
  else:
110
  st.error("Please type a question to get an answer.")
111
 
 
 
 
 
112
  if __name__ == "__main__":
113
  main()