Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,28 +3,28 @@ from youtube_transcript_api import YouTubeTranscriptApi
|
|
3 |
import re
|
4 |
import tempfile
|
5 |
import os
|
6 |
-
import
|
|
|
7 |
|
8 |
-
# Set up
|
9 |
-
|
10 |
|
11 |
-
# Supported file types for
|
12 |
SUPPORTED_FILE_TYPES = ["mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm"]
|
13 |
|
14 |
-
# Function to transcribe audio using
|
15 |
def transcribe_audio(file_path):
|
16 |
file_extension = os.path.splitext(file_path)[1][1:]
|
17 |
if file_extension not in SUPPORTED_FILE_TYPES:
|
18 |
return f"Error: Unsupported file type '{file_extension}'. Please upload a valid file."
|
19 |
-
|
20 |
try:
|
21 |
with open(file_path, "rb") as file:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
response_format="json"
|
26 |
)
|
27 |
-
return
|
28 |
except Exception as e:
|
29 |
return f"Error during transcription: {e}"
|
30 |
|
@@ -41,34 +41,36 @@ def get_transcript(url):
|
|
41 |
except Exception as e:
|
42 |
return str(e)
|
43 |
|
44 |
-
# Function to summarize text using
|
45 |
def summarize_text(text):
|
46 |
try:
|
47 |
-
response =
|
48 |
-
model="gpt-3.5-turbo",
|
49 |
messages=[
|
50 |
-
{
|
51 |
-
|
|
|
|
|
52 |
],
|
53 |
-
|
54 |
)
|
55 |
-
summary = response.choices[0].message
|
56 |
return summary
|
57 |
except Exception as e:
|
58 |
return f"Error summarizing text: {e}"
|
59 |
|
60 |
-
# Function to generate quiz questions using
|
61 |
def generate_quiz_questions(text):
|
62 |
try:
|
63 |
-
response =
|
64 |
-
model="gpt-3.5-turbo",
|
65 |
messages=[
|
66 |
-
{
|
67 |
-
|
|
|
|
|
68 |
],
|
69 |
-
|
70 |
)
|
71 |
-
quiz_questions = response.choices[0].message
|
72 |
return quiz_questions
|
73 |
except Exception as e:
|
74 |
return f"Error generating quiz questions: {e}"
|
@@ -76,50 +78,50 @@ def generate_quiz_questions(text):
|
|
76 |
# Function to parse quiz questions from generated text
|
77 |
def parse_quiz_questions(quiz_text):
|
78 |
questions = []
|
|
|
79 |
current_question = None
|
80 |
current_choices = []
|
81 |
correct_answer = None
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
elif line:
|
99 |
-
current_choices.append(line)
|
100 |
|
101 |
# Add the last question if it exists
|
102 |
-
if current_question:
|
103 |
questions.append({
|
104 |
"question": current_question,
|
105 |
"choices": current_choices,
|
106 |
"correct_answer": correct_answer
|
107 |
})
|
108 |
-
|
109 |
return questions
|
110 |
|
111 |
-
# Function to generate explanation for quiz answers using
|
112 |
def generate_explanation(question, correct_answer, user_answer):
|
113 |
try:
|
114 |
-
response =
|
115 |
-
model="gpt-3.5-turbo",
|
116 |
messages=[
|
117 |
-
{
|
118 |
-
|
|
|
|
|
119 |
],
|
120 |
-
|
121 |
)
|
122 |
-
explanation = response.choices[0].message
|
123 |
return explanation
|
124 |
except Exception as e:
|
125 |
return f"Error generating explanation: {e}"
|
@@ -129,7 +131,7 @@ def check_answers(questions, user_answers):
|
|
129 |
feedback = []
|
130 |
correct_count = 0
|
131 |
for i, question in enumerate(questions):
|
132 |
-
correct_answer = question
|
133 |
user_answer = user_answers.get(f"question_{i+1}", "")
|
134 |
if user_answer == correct_answer:
|
135 |
feedback.append({
|
@@ -176,11 +178,11 @@ if option == "YouTube URL":
|
|
176 |
quiz_text = generate_quiz_questions(transcript_text)
|
177 |
questions = parse_quiz_questions(quiz_text)
|
178 |
|
179 |
-
if
|
180 |
st.error("No valid quiz questions could be generated.")
|
181 |
else:
|
182 |
st.session_state.summary = summary
|
183 |
-
st.session_state.questions = questions
|
184 |
st.session_state.user_answers = {}
|
185 |
st.session_state.generated_quiz = True
|
186 |
else:
|
@@ -198,11 +200,11 @@ if option == "Upload audio/video file":
|
|
198 |
quiz_text = generate_quiz_questions(transcript_text)
|
199 |
questions = parse_quiz_questions(quiz_text)
|
200 |
|
201 |
-
if
|
202 |
st.error("No valid quiz questions could be generated.")
|
203 |
else:
|
204 |
st.session_state.summary = summary
|
205 |
-
st.session_state.questions = questions
|
206 |
st.session_state.user_answers = {}
|
207 |
st.session_state.generated_quiz = True
|
208 |
else:
|
|
|
3 |
import re
|
4 |
import tempfile
|
5 |
import os
|
6 |
+
import warnings
|
7 |
+
from groq import Groq
|
8 |
|
9 |
+
# Set up Groq client
|
10 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
11 |
|
12 |
+
# Supported file types for Groq API
|
13 |
SUPPORTED_FILE_TYPES = ["mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm"]
|
14 |
|
15 |
+
# Function to transcribe audio using Groq Whisper API
|
16 |
def transcribe_audio(file_path):
|
17 |
file_extension = os.path.splitext(file_path)[1][1:]
|
18 |
if file_extension not in SUPPORTED_FILE_TYPES:
|
19 |
return f"Error: Unsupported file type '{file_extension}'. Please upload a valid file."
|
20 |
+
|
21 |
try:
|
22 |
with open(file_path, "rb") as file:
|
23 |
+
transcription = client.audio.transcriptions.create(
|
24 |
+
file=(file_path, file.read()),
|
25 |
+
model="whisper-large-v3",
|
|
|
26 |
)
|
27 |
+
return transcription.text
|
28 |
except Exception as e:
|
29 |
return f"Error during transcription: {e}"
|
30 |
|
|
|
41 |
except Exception as e:
|
42 |
return str(e)
|
43 |
|
44 |
+
# Function to summarize text using Groq API
|
45 |
def summarize_text(text):
|
46 |
try:
|
47 |
+
response = client.chat.completions.create(
|
|
|
48 |
messages=[
|
49 |
+
{
|
50 |
+
"role": "user",
|
51 |
+
"content": f"Summarize the following text:\n\n{text}"
|
52 |
+
}
|
53 |
],
|
54 |
+
model="llama3-8b-8192",
|
55 |
)
|
56 |
+
summary = response.choices[0].message.content.strip()
|
57 |
return summary
|
58 |
except Exception as e:
|
59 |
return f"Error summarizing text: {e}"
|
60 |
|
61 |
+
# Function to generate quiz questions using Groq API
|
62 |
def generate_quiz_questions(text):
|
63 |
try:
|
64 |
+
response = client.chat.completions.create(
|
|
|
65 |
messages=[
|
66 |
+
{
|
67 |
+
"role": "user",
|
68 |
+
"content": f"Generate quiz questions for the following text:\n\n{text}"
|
69 |
+
}
|
70 |
],
|
71 |
+
model="llama3-8b-8192",
|
72 |
)
|
73 |
+
quiz_questions = response.choices[0].message.content.strip()
|
74 |
return quiz_questions
|
75 |
except Exception as e:
|
76 |
return f"Error generating quiz questions: {e}"
|
|
|
78 |
# Function to parse quiz questions from generated text
|
79 |
def parse_quiz_questions(quiz_text):
|
80 |
questions = []
|
81 |
+
question_blocks = quiz_text.split("\n\n")
|
82 |
current_question = None
|
83 |
current_choices = []
|
84 |
correct_answer = None
|
85 |
+
|
86 |
+
for block in question_blocks:
|
87 |
+
lines = block.strip().split("\n")
|
88 |
+
if lines:
|
89 |
+
if re.match(r'^\d+\.', lines[0]): # This line is a question number
|
90 |
+
if current_question and current_choices and correct_answer:
|
91 |
+
questions.append({
|
92 |
+
"question": current_question,
|
93 |
+
"choices": current_choices,
|
94 |
+
"correct_answer": correct_answer
|
95 |
+
})
|
96 |
+
current_question = lines[0]
|
97 |
+
current_choices = lines[1:5]
|
98 |
+
correct_answer = lines[-1].split(": ")[-1].strip() if len(lines) > 5 else None
|
99 |
+
else: # This line is an answer
|
100 |
+
correct_answer = lines[-1].split(": ")[-1].strip()
|
|
|
|
|
101 |
|
102 |
# Add the last question if it exists
|
103 |
+
if current_question and current_choices and correct_answer:
|
104 |
questions.append({
|
105 |
"question": current_question,
|
106 |
"choices": current_choices,
|
107 |
"correct_answer": correct_answer
|
108 |
})
|
109 |
+
|
110 |
return questions
|
111 |
|
112 |
+
# Function to generate explanation for quiz answers using Groq API
|
113 |
def generate_explanation(question, correct_answer, user_answer):
|
114 |
try:
|
115 |
+
response = client.chat.completions.create(
|
|
|
116 |
messages=[
|
117 |
+
{
|
118 |
+
"role": "user",
|
119 |
+
"content": f"Explain why the correct answer to the following question is '{correct_answer}' and not '{user_answer}':\n\n{question}"
|
120 |
+
}
|
121 |
],
|
122 |
+
model="llama3-8b-8192",
|
123 |
)
|
124 |
+
explanation = response.choices[0].message.content.strip()
|
125 |
return explanation
|
126 |
except Exception as e:
|
127 |
return f"Error generating explanation: {e}"
|
|
|
131 |
feedback = []
|
132 |
correct_count = 0
|
133 |
for i, question in enumerate(questions):
|
134 |
+
correct_answer = question['correct_answer']
|
135 |
user_answer = user_answers.get(f"question_{i+1}", "")
|
136 |
if user_answer == correct_answer:
|
137 |
feedback.append({
|
|
|
178 |
quiz_text = generate_quiz_questions(transcript_text)
|
179 |
questions = parse_quiz_questions(quiz_text)
|
180 |
|
181 |
+
if not questions:
|
182 |
st.error("No valid quiz questions could be generated.")
|
183 |
else:
|
184 |
st.session_state.summary = summary
|
185 |
+
st.session_state.questions = questions
|
186 |
st.session_state.user_answers = {}
|
187 |
st.session_state.generated_quiz = True
|
188 |
else:
|
|
|
200 |
quiz_text = generate_quiz_questions(transcript_text)
|
201 |
questions = parse_quiz_questions(quiz_text)
|
202 |
|
203 |
+
if not questions:
|
204 |
st.error("No valid quiz questions could be generated.")
|
205 |
else:
|
206 |
st.session_state.summary = summary
|
207 |
+
st.session_state.questions = questions
|
208 |
st.session_state.user_answers = {}
|
209 |
st.session_state.generated_quiz = True
|
210 |
else:
|