|
|
import openai |
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
api_key = st.secrets["AIML_API_KEY"] |
|
|
|
|
|
|
|
|
openai.api_key = api_key |
|
|
|
|
|
|
|
|
video_links = { |
|
|
"Math": "https://www.youtube.com/watch?v=JT0MmZcJ2Vw", |
|
|
"English": "https://www.youtube.com/watch?v=2n4enj6YMnw", |
|
|
"GK": "https://www.youtube.com/watch?v=F-8KYJpBZTg", |
|
|
} |
|
|
|
|
|
|
|
|
def get_question_and_options(subject): |
|
|
|
|
|
prompt = f"Generate a multiple-choice question with 4 options for the subject: {subject}. Make it suitable for kids." |
|
|
|
|
|
try: |
|
|
|
|
|
response = openai.Completion.create( |
|
|
engine="text-davinci-003", |
|
|
prompt=prompt, |
|
|
max_tokens=150, |
|
|
temperature=0.7 |
|
|
) |
|
|
|
|
|
|
|
|
generated_text = response['choices'][0]['text'].strip() |
|
|
|
|
|
|
|
|
question_parts = generated_text.split("Options:") |
|
|
question = question_parts[0].strip() |
|
|
options_raw = question_parts[1].strip().split("Answer:")[0].strip() |
|
|
options = options_raw.split("\n") |
|
|
|
|
|
|
|
|
correct_answer = generated_text.split("Answer:")[1].strip() |
|
|
|
|
|
return { |
|
|
"question": question, |
|
|
"options": options, |
|
|
"correct_answer": correct_answer |
|
|
} |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"Error generating question: {str(e)}") |
|
|
return None |
|
|
|
|
|
|
|
|
st.title("Quiz App with Subject-Based Videos 🎮") |
|
|
|
|
|
|
|
|
age = st.number_input("Enter your age", min_value=1, max_value=100, step=1) |
|
|
subject = st.selectbox("Select a Subject", ["Math", "English", "GK"]) |
|
|
|
|
|
|
|
|
question_data = get_question_and_options(subject) |
|
|
|
|
|
|
|
|
if question_data: |
|
|
st.subheader(question_data["question"]) |
|
|
options = question_data["options"] |
|
|
|
|
|
|
|
|
user_answer = st.radio("Select your answer", options) |
|
|
|
|
|
|
|
|
if st.button("Submit Answer"): |
|
|
if user_answer == question_data["correct_answer"]: |
|
|
st.success("✅ Correct!") |
|
|
|
|
|
subject_video_link = video_links.get(subject, "https://www.youtube.com/watch?v=DEFAULT_VIDEO") |
|
|
st.video(subject_video_link) |
|
|
else: |
|
|
st.error("❌ Oops! Try again.") |
|
|
st.info(f"The correct answer was: {question_data['correct_answer']}") |
|
|
|
|
|
|
|
|
if st.button("Next Question"): |
|
|
st.experimental_rerun() |
|
|
|