Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
4 |
+
import re
|
5 |
+
import urllib.request
|
6 |
+
import ssl
|
7 |
+
import certifi
|
8 |
+
import whisper
|
9 |
+
import tempfile
|
10 |
+
import os
|
11 |
+
|
12 |
+
def transcribe_audio(file_path):
|
13 |
+
model = whisper.load_model("base")
|
14 |
+
result = model.transcribe(file_path)
|
15 |
+
return result["text"]
|
16 |
+
|
17 |
+
def get_transcript(url):
|
18 |
+
try:
|
19 |
+
video_id_match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url)
|
20 |
+
if video_id_match:
|
21 |
+
video_id = video_id_match.group(1)
|
22 |
+
else:
|
23 |
+
return "Error: Invalid YouTube URL"
|
24 |
+
|
25 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
26 |
+
transcript_text = ' '.join([entry['text'] for entry in transcript])
|
27 |
+
return transcript_text
|
28 |
+
except Exception as e:
|
29 |
+
return str(e)
|
30 |
+
|
31 |
+
def summarize_text(api_key, text):
|
32 |
+
openai.api_key = api_key
|
33 |
+
response = openai.ChatCompletion.create(
|
34 |
+
model="gpt-3.5-turbo",
|
35 |
+
messages=[
|
36 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
37 |
+
{"role": "user", "content": f"Summarize the following text:\n\n{text}"}
|
38 |
+
],
|
39 |
+
max_tokens=150
|
40 |
+
)
|
41 |
+
summary = response.choices[0]['message']['content'].strip()
|
42 |
+
return summary
|
43 |
+
|
44 |
+
def generate_quiz_questions(api_key, text):
|
45 |
+
openai.api_key = api_key
|
46 |
+
response = openai.ChatCompletion.create(
|
47 |
+
model="gpt-3.5-turbo",
|
48 |
+
messages=[
|
49 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
50 |
+
{"role": "user", "content": f"Generate ten quiz questions and four multiple choice answers for each question from the following text. Mark the correct answer with an asterisk (*) at the beginning:\n\n{text}"}
|
51 |
+
],
|
52 |
+
max_tokens=300
|
53 |
+
)
|
54 |
+
quiz_questions = response.choices[0]['message']['content'].strip()
|
55 |
+
return quiz_questions
|
56 |
+
|
57 |
+
def parse_quiz_questions(quiz_text):
|
58 |
+
questions = []
|
59 |
+
question_blocks = quiz_text.split("\n\n")
|
60 |
+
for block in question_blocks:
|
61 |
+
lines = block.strip().split("\n")
|
62 |
+
if len(lines) >= 5:
|
63 |
+
question = lines[0]
|
64 |
+
choices = [line.replace('*', '').strip() for line in lines[1:5]]
|
65 |
+
correct_answer_lines = [line for line in lines[1:5] if '*' in line]
|
66 |
+
if correct_answer_lines:
|
67 |
+
correct_answer = correct_answer_lines[0].replace('*', '').strip()
|
68 |
+
else:
|
69 |
+
correct_answer = "No correct answer provided"
|
70 |
+
questions.append({"question": question, "choices": choices, "correct_answer": correct_answer})
|
71 |
+
return questions
|
72 |
+
|
73 |
+
def generate_explanation(api_key, question, correct_answer, user_answer):
|
74 |
+
openai.api_key = api_key
|
75 |
+
prompt = f"Explain why the correct answer to the following question is '{correct_answer}' and not '{user_answer}':\n\n{question}"
|
76 |
+
response = openai.ChatCompletion.create(
|
77 |
+
model="gpt-3.5-turbo",
|
78 |
+
messages=[
|
79 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
80 |
+
{"role": "user", "content": prompt}
|
81 |
+
],
|
82 |
+
max_tokens=150
|
83 |
+
)
|
84 |
+
explanation = response.choices[0]['message']['content'].strip()
|
85 |
+
return explanation
|
86 |
+
|
87 |
+
def check_answers(api_key, questions, user_answers):
|
88 |
+
feedback = []
|
89 |
+
correct_count = 0
|
90 |
+
for i, question in enumerate(questions):
|
91 |
+
correct_answer = question['correct_answer']
|
92 |
+
user_answer = user_answers.get(f"question_{i+1}", "")
|
93 |
+
if user_answer == correct_answer:
|
94 |
+
feedback.append({
|
95 |
+
"question": question['question'],
|
96 |
+
"user_answer": user_answer,
|
97 |
+
"correct_answer": correct_answer,
|
98 |
+
"status": "Correct"
|
99 |
+
})
|
100 |
+
correct_count += 1
|
101 |
+
else:
|
102 |
+
explanation = generate_explanation(api_key, question['question'], correct_answer, user_answer)
|
103 |
+
feedback.append({
|
104 |
+
"question": question['question'],
|
105 |
+
"user_answer": user_answer,
|
106 |
+
"correct_answer": correct_answer,
|
107 |
+
"status": "Incorrect",
|
108 |
+
"explanation": explanation
|
109 |
+
})
|
110 |
+
return feedback
|
111 |
+
|
112 |
+
def handle_uploaded_file(uploaded_file):
|
113 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
114 |
+
tmp_file.write(uploaded_file.read())
|
115 |
+
tmp_file_path = tmp_file.name
|
116 |
+
return tmp_file_path
|
117 |
+
|
118 |
+
st.title("YouTube Transcript Quiz Generator")
|
119 |
+
|
120 |
+
st.markdown("**Instructions:** Enter your OpenAI API key and paste a YouTube link or upload a media file to generate a quiz.")
|
121 |
+
|
122 |
+
api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
123 |
+
option = st.selectbox("Choose input type", ("YouTube URL", "Upload audio/video file"))
|
124 |
+
|
125 |
+
if "generated_quiz" not in st.session_state:
|
126 |
+
st.session_state.generated_quiz = False
|
127 |
+
|
128 |
+
if option == "YouTube URL":
|
129 |
+
url = st.text_input("YouTube URL", value="")
|
130 |
+
if api_key and url:
|
131 |
+
if st.button("Generate Quiz"):
|
132 |
+
transcript_text = get_transcript(url)
|
133 |
+
if "Error" not in transcript_text:
|
134 |
+
summary = summarize_text(api_key, transcript_text)
|
135 |
+
quiz_text = generate_quiz_questions(api_key, transcript_text)
|
136 |
+
questions = parse_quiz_questions(quiz_text)
|
137 |
+
|
138 |
+
st.write("## Summary")
|
139 |
+
st.write(summary)
|
140 |
+
|
141 |
+
st.write("## Quiz Questions")
|
142 |
+
st.session_state.questions = questions
|
143 |
+
st.session_state.user_answers = {}
|
144 |
+
st.session_state.generated_quiz = True
|
145 |
+
|
146 |
+
for i, question in enumerate(questions):
|
147 |
+
st.write(f"### Question {i+1}")
|
148 |
+
st.write(question['question'])
|
149 |
+
st.session_state.user_answers[f"question_{i+1}"] = st.radio(
|
150 |
+
label="",
|
151 |
+
options=question['choices'],
|
152 |
+
key=f"question_{i+1}"
|
153 |
+
)
|
154 |
+
|
155 |
+
elif option == "Upload audio/video file":
|
156 |
+
uploaded_file = st.file_uploader("Choose an audio or video file", type=["mp3", "wav", "mp4", "mov"])
|
157 |
+
if uploaded_file and api_key:
|
158 |
+
tmp_file_path = handle_uploaded_file(uploaded_file)
|
159 |
+
transcript_text = transcribe_audio(tmp_file_path)
|
160 |
+
os.remove(tmp_file_path)
|
161 |
+
if "Error" not in transcript_text:
|
162 |
+
summary = summarize_text(api_key, transcript_text)
|
163 |
+
quiz_text = generate_quiz_questions(api_key, transcript_text)
|
164 |
+
questions = parse_quiz_questions(quiz_text)
|
165 |
+
|
166 |
+
st.write("## Summary")
|
167 |
+
st.write(summary)
|
168 |
+
|
169 |
+
st.write("## Quiz Questions")
|
170 |
+
st.session_state.questions = questions
|
171 |
+
st.session_state.user_answers = {}
|
172 |
+
st.session_state.generated_quiz = True
|
173 |
+
|
174 |
+
for i, question in enumerate(questions):
|
175 |
+
st.write(f"### Question {i+1}")
|
176 |
+
st.write(question['question'])
|
177 |
+
st.session_state.user_answers[f"question_{i+1}"] = st.radio(
|
178 |
+
label="",
|
179 |
+
options=question['choices'],
|
180 |
+
key=f"question_{i+1}"
|
181 |
+
)
|
182 |
+
|
183 |
+
if st.session_state.generated_quiz:
|
184 |
+
if st.button("Submit Answers"):
|
185 |
+
if "questions" in st.session_state and st.session_state.questions:
|
186 |
+
with st.spinner('Processing your answers...'):
|
187 |
+
feedback = check_answers(api_key, st.session_state.questions, st.session_state.user_answers)
|
188 |
+
st.write("## Feedback")
|
189 |
+
for i, item in enumerate(feedback):
|
190 |
+
with st.expander(f"Question {i+1} Feedback"):
|
191 |
+
st.write(f"### {item['question']}")
|
192 |
+
st.write(f"**Your answer:** {item['user_answer']}")
|
193 |
+
st.write(f"**Correct answer:** {item['correct_answer']}")
|
194 |
+
if item['status'] == "Incorrect":
|
195 |
+
st.write(f"**Explanation:** {item['explanation']}")
|
196 |
+
else:
|
197 |
+
st.write("Please generate the quiz first.")
|