-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_app.py
132 lines (102 loc) Β· 6.51 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
from helpers.youtube_utils import extract_video_id_from_url, get_transcript_text
from helpers.openai_utils import get_quiz_data
from helpers.quiz_utils import string_to_list, get_randomized_options
from helpers.toast_messages import get_random_toast
st.set_page_config(
page_title="Quizzler",
page_icon="π§ ",
layout="centered",
initial_sidebar_state="collapsed"
)
# Check if user is new or returning using session state.
# If user is new, show the toast message.
if 'first_time' not in st.session_state:
message, icon = get_random_toast()
st.toast(message, icon=icon)
st.session_state.first_time = False
with st.sidebar:
st.header("π¨βπ» About the Author")
st.write("""
**Mobashshir Ali** is a passionate Data Science and Generative AI enthusiast, as well as an accomplished coder. With a profound love for Generative AI, Mobashshir has dedicated himself to creating innovative platforms that make learning both interactive and enjoyable. His work is driven by a commitment to excellence and a desire to empower others through cutting-edge technology. Whether he's developing advanced algorithms or crafting engaging educational experiences, Mobashshir's goal is to inspire and educate the next generation of tech enthusiasts.
Connect, contribute, or just say hi!
""")
st.divider()
st.subheader("π Connect with Me", anchor=False)
st.markdown(
"""
- [π LinkedIn](https://www.linkedin.com/in/mobashshir-ali-028603221/)
"""
)
#st.divider()
#st.subheader("π Streamlit Hackathon 2023", anchor=False)
#st.write("QuizTube proudly stands as Sven's innovative entry for the Streamlit Hackathon held in September 2023. A testament to the power of imagination and code!")
st.divider()
st.write("Made with β₯ in Bhopal, India")
st.title(":red[Quizzler] β Watch. Learn. Quiz. π§ ", anchor=False)
st.write("""
Ever watched a YouTube video and wondered how well you understood its content? Here's a fun twist: Instead of just watching on YouTube, come to **Quizzler** and test your comprehension!
**How does it work?** π€
1. Paste the YouTube video URL of your recently watched video.
2. Enter your [OpenAI API Key](https://platform.openai.com/account/api-keys).
β οΈ Important: The video **must** have English captions for the tool to work.
Once you've input the details, voilΓ ! Dive deep into questions crafted just for you, ensuring you've truly grasped the content of the video. Let's put your knowledge to the test!
""")
#with st.expander("π‘ Video Tutorial"):
#with st.spinner("Loading video.."):
#st.video("https://youtu.be/yzBr3L2BIto", format="video/mp4", start_time=0)
with st.form("user_input"):
YOUTUBE_URL = st.text_input("Enter the YouTube video link:", value="https://youtu.be/bcYwiwsDfGE?si=qQ0nvkmKkzHJom2y")
OPENAI_API_KEY = st.text_input("Enter your OpenAI API Key:", placeholder="sk-XXXX", type='password')
submitted = st.form_submit_button("Craft my quiz!")
if submitted or ('quiz_data_list' in st.session_state):
if not YOUTUBE_URL:
st.info("Please provide a valid YouTube video link. Head over to [YouTube](https://www.youtube.com/) to fetch one.")
st.stop()
elif not OPENAI_API_KEY:
st.info("Please fill out the OpenAI API Key to proceed. If you don't have one, you can obtain it [here](https://platform.openai.com/account/api-keys).")
st.stop()
with st.spinner("Crafting your quiz...π€"):
if submitted:
video_id = extract_video_id_from_url(YOUTUBE_URL)
video_transcription = get_transcript_text(video_id)
quiz_data_str = get_quiz_data(video_transcription, OPENAI_API_KEY)
st.session_state.quiz_data_list = string_to_list(quiz_data_str)
if 'user_answers' not in st.session_state:
st.session_state.user_answers = [None for _ in st.session_state.quiz_data_list]
if 'correct_answers' not in st.session_state:
st.session_state.correct_answers = []
if 'randomized_options' not in st.session_state:
st.session_state.randomized_options = []
for q in st.session_state.quiz_data_list:
options, correct_answer = get_randomized_options(q[1:])
st.session_state.randomized_options.append(options)
st.session_state.correct_answers.append(correct_answer)
with st.form(key='quiz_form'):
st.subheader("π§ Quiz Time: Test Your Knowledge!", anchor=False)
for i, q in enumerate(st.session_state.quiz_data_list):
options = st.session_state.randomized_options[i]
default_index = st.session_state.user_answers[i] if st.session_state.user_answers[i] is not None else 0
response = st.radio(q[0], options, index=default_index)
user_choice_index = options.index(response)
st.session_state.user_answers[i] = user_choice_index # Update the stored answer right after fetching it
results_submitted = st.form_submit_button(label='Unveil My Score!')
if results_submitted:
score = sum([ua == st.session_state.randomized_options[i].index(ca) for i, (ua, ca) in enumerate(zip(st.session_state.user_answers, st.session_state.correct_answers))])
st.success(f"Your score: {score}/{len(st.session_state.quiz_data_list)}")
if score == len(st.session_state.quiz_data_list): # Check if all answers are correct
st.balloons()
else:
incorrect_count = len(st.session_state.quiz_data_list) - score
if incorrect_count == 1:
st.warning(f"Almost perfect! You got 1 question wrong. Let's review it:")
else:
st.warning(f"Almost there! You got {incorrect_count} questions wrong. Let's review them:")
for i, (ua, ca, q, ro) in enumerate(zip(st.session_state.user_answers, st.session_state.correct_answers, st.session_state.quiz_data_list, st.session_state.randomized_options)):
with st.expander(f"Question {i + 1}", expanded=False):
if ro[ua] != ca:
st.info(f"Question: {q[0]}")
st.error(f"Your answer: {ro[ua]}")
st.success(f"Correct answer: {ca}")
# - [π Source Code]()
#- [π Personal Website]()