|
import streamlit as st |
|
from langchain_community.document_loaders import PyPDFLoader |
|
import openai |
|
from langchain.prompts import ChatPromptTemplate |
|
from langchain_core.output_parsers import StrOutputParser |
|
from langchain.chat_models import ChatOpenAI |
|
from fpdf import FPDF |
|
import os |
|
from datetime import datetime, timedelta |
|
import speech_recognition as sr |
|
import calendar |
|
|
|
|
|
st.title('Educational Assistant') |
|
st.header('Summary, Quiz Generator, and Q&A') |
|
st.sidebar.title('Drop your PDF or Audio file here') |
|
|
|
|
|
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password") |
|
|
|
user_file_upload = st.sidebar.file_uploader(label='', type=['pdf', 'mp3']) |
|
|
|
|
|
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Daily Study Plan')) |
|
|
|
|
|
question_input = None |
|
if option == 'Ask a Question': |
|
question_input = st.text_input("Enter your question about the document:") |
|
|
|
|
|
def generate_pdf(response, filename="response.pdf"): |
|
pdf = FPDF() |
|
pdf.add_page() |
|
|
|
|
|
pdf.add_font('ArialUnicode', '', 'arialuni.ttf', uni=True) |
|
pdf.set_font('ArialUnicode', '', 12) |
|
|
|
|
|
pdf.multi_cell(0, 10, response) |
|
|
|
|
|
pdf.output(filename) |
|
|
|
|
|
return filename |
|
|
|
|
|
def convert_audio_to_text(audio_file): |
|
recognizer = sr.Recognizer() |
|
|
|
with sr.AudioFile(audio_file) as source: |
|
audio = recognizer.record(source) |
|
|
|
try: |
|
text = recognizer.recognize_google(audio) |
|
return text |
|
except sr.UnknownValueError: |
|
return "Sorry, I couldn't understand the audio." |
|
except sr.RequestError: |
|
return "Sorry, there was an error with the speech-to-text service." |
|
|
|
if openai_api_key: |
|
|
|
openai.api_key = openai_api_key |
|
|
|
if user_file_upload: |
|
|
|
if user_file_upload.type == 'application/pdf': |
|
|
|
pdf_data = user_file_upload.read() |
|
|
|
|
|
with open("temp_pdf_file.pdf", "wb") as f: |
|
f.write(pdf_data) |
|
|
|
|
|
loader = PyPDFLoader("temp_pdf_file.pdf") |
|
data = loader.load_and_split() |
|
|
|
elif user_file_upload.type == 'audio/mpeg' or user_file_upload.type == 'audio/wav' or user_file_upload.type == 'audio/mp3': |
|
|
|
text_data = convert_audio_to_text(user_file_upload) |
|
data = text_data |
|
st.write("Audio converted to text: ") |
|
st.write(data) |
|
|
|
|
|
prompt_1 = ChatPromptTemplate.from_messages( |
|
[ |
|
("system", "You are a smart assistant. Give a summary of the user's PDF. Be polite."), |
|
("user", "{data}") |
|
] |
|
) |
|
|
|
|
|
llm_summary = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) |
|
output_parser = StrOutputParser() |
|
chain_1 = prompt_1 | llm_summary | output_parser |
|
|
|
|
|
prompt_2 = ChatPromptTemplate.from_messages( |
|
[ |
|
("system", "You are a smart assistant. Generate 10 multiple-choice quiz questions with 4 options each (including correct and incorrect options) from the user's PDF. Please also include the correct answer in your response. Be polite."), |
|
("user", "{data}") |
|
] |
|
) |
|
|
|
|
|
llm_quiz = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) |
|
output_parser = StrOutputParser() |
|
chain_2 = prompt_2 | llm_quiz | output_parser |
|
|
|
|
|
prompt_3 = ChatPromptTemplate.from_messages( |
|
[ |
|
("system", "You are a smart assistant. Answer the user's question based on the content of the PDF. Be polite."), |
|
("user", "{data}\n\nUser's question: {question}") |
|
] |
|
) |
|
|
|
|
|
llm_qa = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) |
|
output_parser = StrOutputParser() |
|
chain_3 = prompt_3 | llm_qa | output_parser |
|
|
|
|
|
def generate_study_plan(data, exam_date, num_days=30): |
|
today = datetime.today() |
|
study_plan = [] |
|
day_gap = (exam_date - today).days // num_days |
|
for i in range(num_days): |
|
day_start = today + timedelta(days=i * day_gap) |
|
study_plan.append({ |
|
'day': day_start.strftime("%Y-%m-%d"), |
|
'topic': data[i % len(data)] |
|
}) |
|
return study_plan |
|
|
|
if option == 'Generate Summary': |
|
|
|
summary_response = chain_1.invoke({'data': data}) |
|
st.write(summary_response) |
|
|
|
|
|
pdf_filename = generate_pdf(summary_response, filename="summary_response.pdf") |
|
st.download_button("Download Summary as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf") |
|
|
|
elif option == 'Generate Quiz': |
|
|
|
quiz_response = chain_2.invoke({'data': data}) |
|
st.write(quiz_response) |
|
|
|
|
|
pdf_filename = generate_pdf(quiz_response, filename="quiz_response.pdf") |
|
st.download_button("Download Quiz as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf") |
|
|
|
elif option == 'Ask a Question' and question_input: |
|
|
|
generate_answer = st.button("Generate Answer") |
|
|
|
if generate_answer: |
|
|
|
question_answer_response = chain_3.invoke({'data': data, 'question': question_input}) |
|
st.write(question_answer_response) |
|
|
|
|
|
pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf") |
|
st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf") |
|
|
|
elif option == 'Daily Study Plan': |
|
|
|
exam_date = datetime.today() + timedelta(days=30) |
|
study_plan = generate_study_plan(data, exam_date) |
|
for plan in study_plan: |
|
st.write(f"Day: {plan['day']} - Study Topic: {plan['topic']}") |
|
else: |
|
st.sidebar.warning("Please enter your OpenAI API Key to proceed.") |