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Prompt # Updated import
|
| 5 |
+
from config import Config
|
| 6 |
+
import yt_dlp
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
# Configure logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
|
| 12 |
+
# Initialize OpenAI client
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def init_openai_client():
|
| 15 |
+
from openai import OpenAI
|
| 16 |
+
return OpenAI(api_key=Config.get_openai_api_key())
|
| 17 |
+
|
| 18 |
+
client = init_openai_client()
|
| 19 |
+
|
| 20 |
+
def download_audio(url):
|
| 21 |
+
ydl_opts = {
|
| 22 |
+
'format': 'bestaudio/best',
|
| 23 |
+
'postprocessors': [{
|
| 24 |
+
'key': 'FFmpegExtractAudio',
|
| 25 |
+
'preferredcodec': 'mp3',
|
| 26 |
+
'preferredquality': '192',
|
| 27 |
+
}],
|
| 28 |
+
'outtmpl': '%(id)s.%(ext)s',
|
| 29 |
+
}
|
| 30 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 31 |
+
info = ydl.extract_info(url, download=True)
|
| 32 |
+
return f"{info['id']}.mp3"
|
| 33 |
+
|
| 34 |
+
def get_transcript_from_file(uploaded_file):
|
| 35 |
+
if uploaded_file is not None:
|
| 36 |
+
with st.spinner("Transcribing... This may take a few minutes."):
|
| 37 |
+
transcript = client.audio.transcriptions.create(
|
| 38 |
+
model="whisper-1",
|
| 39 |
+
file=uploaded_file
|
| 40 |
+
)
|
| 41 |
+
return transcript.text.strip()
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
def get_transcript_from_url(url):
|
| 45 |
+
try:
|
| 46 |
+
if url != '':
|
| 47 |
+
with st.spinner("Downloading audio..."):
|
| 48 |
+
mp3_file = download_audio(url)
|
| 49 |
+
|
| 50 |
+
file_stats = os.stat(mp3_file)
|
| 51 |
+
logging.info(f'Size of audio file in Bytes: {file_stats.st_size}')
|
| 52 |
+
|
| 53 |
+
if file_stats.st_size <= 25000000: # OpenAI has a 25MB limit
|
| 54 |
+
with st.spinner("Transcribing... This may take a few minutes."):
|
| 55 |
+
with open(mp3_file, "rb") as audio_file:
|
| 56 |
+
try:
|
| 57 |
+
transcript = client.audio.transcriptions.create(
|
| 58 |
+
model="whisper-1",
|
| 59 |
+
file=audio_file
|
| 60 |
+
)
|
| 61 |
+
result = transcript.text.strip()
|
| 62 |
+
except Exception as api_error:
|
| 63 |
+
logging.error(f"API Error: {str(api_error)}")
|
| 64 |
+
result = f"API Error: {str(api_error)}"
|
| 65 |
+
|
| 66 |
+
# Clean up the downloaded file
|
| 67 |
+
os.remove(mp3_file)
|
| 68 |
+
|
| 69 |
+
return result
|
| 70 |
+
else:
|
| 71 |
+
os.remove(mp3_file)
|
| 72 |
+
return "Error: The audio file is too large. Please choose a shorter video (up to ~25MB audio file size)."
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logging.error(f"An error occurred: {str(e)}")
|
| 75 |
+
return f"An error occurred: {str(e)}"
|
| 76 |
+
|
| 77 |
+
def generate_notes(transcript):
|
| 78 |
+
# Using the updated Prompt class
|
| 79 |
+
return Model.openai_chatgpt(transcript=transcript, prompt=Prompt.prompt1())
|
| 80 |
+
|
| 81 |
+
def generate_quiz(transcript):
|
| 82 |
+
# Using the updated Prompt class
|
| 83 |
+
raw_quiz = Model.openai_chatgpt(transcript, Prompt.prompt1(ID='quiz'))
|
| 84 |
+
return parse_quiz_content(raw_quiz)
|
| 85 |
+
|
| 86 |
+
def parse_quiz_content(raw_quiz):
|
| 87 |
+
quiz_content = []
|
| 88 |
+
questions = raw_quiz.strip().split("\n\n")
|
| 89 |
+
|
| 90 |
+
for question in questions:
|
| 91 |
+
lines = question.split("\n")
|
| 92 |
+
if len(lines) < 6: # Question + 4 options + correct answer
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
question_text = lines[0].split(".", 1)[-1].strip()
|
| 96 |
+
options = [line.split(")", 1)[-1].strip() for line in lines[1:5]]
|
| 97 |
+
correct_answer = lines[-1].split(":")[-1].strip()
|
| 98 |
+
|
| 99 |
+
quiz_content.append({
|
| 100 |
+
"question": question_text,
|
| 101 |
+
"options": options,
|
| 102 |
+
"correct_answer": correct_answer
|
| 103 |
+
})
|
| 104 |
+
|
| 105 |
+
return quiz_content[:10] # Ensure we return at most 10 questions
|
| 106 |
+
|
| 107 |
+
def main():
|
| 108 |
+
st.title("AI-Powered Teaching Assistant")
|
| 109 |
+
|
| 110 |
+
# Choose input method
|
| 111 |
+
input_method = st.radio("Choose input method:", ("Upload File", "YouTube URL"))
|
| 112 |
+
|
| 113 |
+
transcript = None
|
| 114 |
+
|
| 115 |
+
if input_method == "Upload File":
|
| 116 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=Config.ALLOWED_EXTENSIONS)
|
| 117 |
+
if uploaded_file is not None:
|
| 118 |
+
file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type, "FileSize": uploaded_file.size}
|
| 119 |
+
st.write(file_details)
|
| 120 |
+
if uploaded_file.size <= 25000000: # 25 MB in bytes
|
| 121 |
+
transcript = get_transcript_from_file(uploaded_file)
|
| 122 |
+
else:
|
| 123 |
+
st.error("File size exceeds 25 MB limit. Please upload a smaller file.")
|
| 124 |
+
else:
|
| 125 |
+
url = st.text_input("Enter YouTube URL:")
|
| 126 |
+
if url:
|
| 127 |
+
transcript = get_transcript_from_url(url)
|
| 128 |
+
|
| 129 |
+
if transcript:
|
| 130 |
+
st.subheader("Transcript")
|
| 131 |
+
st.text_area("Generated Transcript", transcript, height=200)
|
| 132 |
+
|
| 133 |
+
if st.button("Generate Notes and Quiz"):
|
| 134 |
+
with st.spinner("Generating notes..."):
|
| 135 |
+
notes = generate_notes(transcript)
|
| 136 |
+
st.subheader("Generated Notes")
|
| 137 |
+
st.markdown(notes)
|
| 138 |
+
|
| 139 |
+
with st.spinner("Generating quiz..."):
|
| 140 |
+
quiz = generate_quiz(transcript)
|
| 141 |
+
st.subheader("Generated Quiz")
|
| 142 |
+
for i, q in enumerate(quiz, 1):
|
| 143 |
+
st.write(f"{i}. {q['question']}")
|
| 144 |
+
for j, option in enumerate(q['options']):
|
| 145 |
+
st.write(f" {chr(65+j)}. {option}")
|
| 146 |
+
st.write(f" Correct Answer: {q['correct_answer']}")
|
| 147 |
+
st.write("")
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
main()
|