Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
|
| 4 |
+
# Set the title of the app
|
| 5 |
+
st.title("Math Quiz Generation Bot")
|
| 6 |
+
|
| 7 |
+
# Initialize the language model with the provided API key
|
| 8 |
+
llm = ChatGoogleGenerativeAI(
|
| 9 |
+
model="gemini-pro",
|
| 10 |
+
google_api_key=st.secrets["GOOGLE_API_KEY"]
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def generate_math_quiz(role, topic, difficulty, num_questions):
|
| 14 |
+
"""
|
| 15 |
+
Generates a math quiz based on the role, topic, difficulty, and number of questions.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
role (str): The role of the user (e.g., student, teacher).
|
| 19 |
+
topic (str): The topic for the quiz (e.g., Algebra, Calculus).
|
| 20 |
+
difficulty (str): The difficulty level (e.g., Easy, Medium, Hard).
|
| 21 |
+
num_questions (int): The number of questions to generate.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
str: The generated quiz questions.
|
| 25 |
+
"""
|
| 26 |
+
# Construct the prompt for the model
|
| 27 |
+
prompt = (
|
| 28 |
+
f"Create a {num_questions}-question math quiz for a {role} on the topic of {topic}. "
|
| 29 |
+
f"The questions should be of {difficulty} difficulty level. "
|
| 30 |
+
"Provide answers and make sure the questions are clear and varied."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Generate the response from the language model
|
| 34 |
+
response = llm.invoke(prompt)
|
| 35 |
+
|
| 36 |
+
# Extract the content from the response
|
| 37 |
+
return response.content
|
| 38 |
+
|
| 39 |
+
# Create a form for user input
|
| 40 |
+
with st.form("quiz_form"):
|
| 41 |
+
# Dropdown for selecting the role
|
| 42 |
+
role = st.selectbox("Select Role", ["Student", "Teacher", "Researcher"])
|
| 43 |
+
|
| 44 |
+
# Dropdown for selecting the topic
|
| 45 |
+
topic = st.selectbox("Select Topic", ["Algebra", "Calculus", "Geometry", "Statistics"])
|
| 46 |
+
|
| 47 |
+
# Dropdown for selecting the difficulty level
|
| 48 |
+
difficulty = st.selectbox("Select Difficulty", ["Easy", "Medium", "Hard"])
|
| 49 |
+
|
| 50 |
+
# Input for the number of questions
|
| 51 |
+
num_questions = st.number_input("Number of Questions", min_value=1, max_value=20, value=5)
|
| 52 |
+
|
| 53 |
+
# Submit button for the form
|
| 54 |
+
submitted = st.form_submit_button("Generate Quiz")
|
| 55 |
+
|
| 56 |
+
# Handle form submission
|
| 57 |
+
if submitted:
|
| 58 |
+
if role and topic and difficulty:
|
| 59 |
+
# Generate quiz questions based on the user input
|
| 60 |
+
quiz = generate_math_quiz(role, topic, difficulty, num_questions)
|
| 61 |
+
st.info(quiz) # Display the generated quiz questions
|
| 62 |
+
else:
|
| 63 |
+
st.error("Please fill in all fields to generate the quiz.")
|