Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set up API key
|
| 8 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY') # Ensure this is set in the Hugging Face Spaces environment
|
| 9 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 10 |
+
|
| 11 |
+
# Initialize the Generative Model
|
| 12 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 13 |
+
|
| 14 |
+
def extract_text_from_pdf(pdf_file):
|
| 15 |
+
"""Extract text from the given PDF file."""
|
| 16 |
+
try:
|
| 17 |
+
text = ""
|
| 18 |
+
pdf_file.seek(0)
|
| 19 |
+
reader = PyPDF2.PdfReader(io.BytesIO(pdf_file.read()))
|
| 20 |
+
for page in reader.pages:
|
| 21 |
+
text += page.extract_text()
|
| 22 |
+
return text
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error extracting text from PDF: {e}"
|
| 25 |
+
|
| 26 |
+
def generate_quiz(text, difficulty):
|
| 27 |
+
"""Generate a quiz based on the provided text and difficulty, limited to 10 questions."""
|
| 28 |
+
try:
|
| 29 |
+
prompt = (f"Based on the following text, generate a quiz with a maximum of 10 questions. "
|
| 30 |
+
"Include both questions and answers in the format of question and answer pairs. "
|
| 31 |
+
"Adjust the difficulty to {difficulty} level:\n\n"
|
| 32 |
+
f"{text}\n\n"
|
| 33 |
+
"Please provide the quiz in the format of:\n"
|
| 34 |
+
"Q1: Question 1?\n"
|
| 35 |
+
"A1: Answer to Question 1\n"
|
| 36 |
+
"Q2: Question 2?\n"
|
| 37 |
+
"A2: Answer to Question 2\n"
|
| 38 |
+
"... and so on. Limit the total number of questions to 10.")
|
| 39 |
+
response = model.generate_content(prompt)
|
| 40 |
+
|
| 41 |
+
# Limit the response to the first 10 questions
|
| 42 |
+
lines = response.text.split('\n')
|
| 43 |
+
limited_quiz = []
|
| 44 |
+
question_count = 0
|
| 45 |
+
|
| 46 |
+
for line in lines:
|
| 47 |
+
if line.startswith('Q') and question_count >= 10:
|
| 48 |
+
break
|
| 49 |
+
if line.startswith('Q'):
|
| 50 |
+
question_count += 1
|
| 51 |
+
limited_quiz.append(line)
|
| 52 |
+
|
| 53 |
+
return '\n'.join(limited_quiz)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error generating quiz: {e}"
|
| 56 |
+
|
| 57 |
+
def explain_question(question):
|
| 58 |
+
"""Explain the given question and its solution."""
|
| 59 |
+
try:
|
| 60 |
+
prompt = f"Explain the following question and its solution:\n{question}"
|
| 61 |
+
response = model.generate_content(prompt)
|
| 62 |
+
return response.text
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error explaining question: {e}"
|
| 65 |
+
|
| 66 |
+
def process_pdf(pdf_file, difficulty, explain_question_text):
|
| 67 |
+
"""Process the uploaded PDF to generate and explain quiz questions."""
|
| 68 |
+
try:
|
| 69 |
+
# Extract text from the PDF
|
| 70 |
+
extracted_text = extract_text_from_pdf(pdf_file)
|
| 71 |
+
|
| 72 |
+
# Generate a new quiz based on the extracted text
|
| 73 |
+
generated_quiz = generate_quiz(extracted_text, difficulty)
|
| 74 |
+
|
| 75 |
+
# Explain the provided question
|
| 76 |
+
explanation = explain_question(explain_question_text)
|
| 77 |
+
|
| 78 |
+
return generated_quiz, explanation
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"Error processing PDF: {e}", 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 for an explanation of any specific question.")
|
| 85 |
+
|
| 86 |
+
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 87 |
+
difficulty = st.selectbox("Difficulty Level", ['easy', 'medium', 'hard'])
|
| 88 |
+
explain_question_text = st.text_input("Question to Explain", "")
|
| 89 |
+
|
| 90 |
+
if st.button("Generate Quiz"):
|
| 91 |
+
if uploaded_file is not None:
|
| 92 |
+
questions_and_answers, explanation = process_pdf(uploaded_file, difficulty, explain_question_text)
|
| 93 |
+
|
| 94 |
+
st.subheader("Generated Quiz")
|
| 95 |
+
st.text(questions_and_answers)
|
| 96 |
+
|
| 97 |
+
st.subheader("Question Explanation")
|
| 98 |
+
st.text(explanation)
|
| 99 |
+
else:
|
| 100 |
+
st.error("Please upload a PDF file.")
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main()
|