Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# Import necessary libraries
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
import openai
|
|
@@ -7,10 +6,11 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
| 8 |
from fpdf import FPDF
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
# Set up Streamlit UI
|
| 12 |
st.title('Educational Assistant')
|
| 13 |
-
st.header('Summary, Quiz Generator,
|
| 14 |
st.sidebar.title('Drop your PDF here')
|
| 15 |
|
| 16 |
# Input OpenAI API key from keyboard
|
|
@@ -18,8 +18,8 @@ openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="passwo
|
|
| 18 |
|
| 19 |
user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
|
| 20 |
|
| 21 |
-
# Sidebar option selection for Summary, Quiz,
|
| 22 |
-
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question'))
|
| 23 |
|
| 24 |
# Input for asking questions (only visible when "Ask a Question" is selected)
|
| 25 |
question_input = None
|
|
@@ -99,6 +99,19 @@ if openai_api_key:
|
|
| 99 |
output_parser = StrOutputParser()
|
| 100 |
chain_3 = prompt_3 | llm_qa | output_parser
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if option == 'Generate Summary':
|
| 103 |
# Generate summary
|
| 104 |
summary_response = chain_1.invoke({'data': data})
|
|
@@ -129,5 +142,27 @@ if openai_api_key:
|
|
| 129 |
# Generate PDF for the question answer and offer it as a download
|
| 130 |
pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf")
|
| 131 |
st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
else:
|
| 133 |
-
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
import openai
|
|
|
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
| 7 |
from fpdf import FPDF
|
| 8 |
import os
|
| 9 |
+
import pandas as pd
|
| 10 |
|
| 11 |
# Set up Streamlit UI
|
| 12 |
st.title('Educational Assistant')
|
| 13 |
+
st.header('Summary, Quiz Generator, Q&A, and Study Plan')
|
| 14 |
st.sidebar.title('Drop your PDF here')
|
| 15 |
|
| 16 |
# Input OpenAI API key from keyboard
|
|
|
|
| 18 |
|
| 19 |
user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
|
| 20 |
|
| 21 |
+
# Sidebar option selection for Summary, Quiz, Q&A, or Study Plan
|
| 22 |
+
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Generate Study Plan'))
|
| 23 |
|
| 24 |
# Input for asking questions (only visible when "Ask a Question" is selected)
|
| 25 |
question_input = None
|
|
|
|
| 99 |
output_parser = StrOutputParser()
|
| 100 |
chain_3 = prompt_3 | llm_qa | output_parser
|
| 101 |
|
| 102 |
+
## Prompt Template for Study Plan
|
| 103 |
+
prompt_4 = ChatPromptTemplate.from_messages(
|
| 104 |
+
[
|
| 105 |
+
("system", "You are a smart assistant. Based on the content of the user's PDF, generate a 7-day study plan. Divide the content into 7 topics and assign each topic to a day. Please make it logical and balanced."),
|
| 106 |
+
("user", "{data}")
|
| 107 |
+
]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
| 111 |
+
llm_study_plan = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
| 112 |
+
output_parser = StrOutputParser()
|
| 113 |
+
chain_4 = prompt_4 | llm_study_plan | output_parser
|
| 114 |
+
|
| 115 |
if option == 'Generate Summary':
|
| 116 |
# Generate summary
|
| 117 |
summary_response = chain_1.invoke({'data': data})
|
|
|
|
| 142 |
# Generate PDF for the question answer and offer it as a download
|
| 143 |
pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf")
|
| 144 |
st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
| 145 |
+
|
| 146 |
+
elif option == 'Generate Study Plan':
|
| 147 |
+
# Generate study plan
|
| 148 |
+
study_plan_response = chain_4.invoke({'data': data})
|
| 149 |
+
st.write(study_plan_response)
|
| 150 |
+
|
| 151 |
+
# Extract the study plan and convert it into a structured format
|
| 152 |
+
study_plan = study_plan_response.strip().split("\n")
|
| 153 |
+
|
| 154 |
+
# Assuming the study plan is a list of 7 days with topics
|
| 155 |
+
days = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7"]
|
| 156 |
+
topics = [plan.split(":")[1].strip() if ":" in plan else "" for plan in study_plan]
|
| 157 |
+
|
| 158 |
+
# Create a DataFrame to display the study plan in tabular form
|
| 159 |
+
study_plan_df = pd.DataFrame(list(zip(days, topics)), columns=["Day", "Topics to Study"])
|
| 160 |
+
|
| 161 |
+
st.table(study_plan_df)
|
| 162 |
+
|
| 163 |
+
# Generate PDF for the study plan and offer it as a download
|
| 164 |
+
pdf_filename = generate_pdf(study_plan_response, filename="study_plan_response.pdf")
|
| 165 |
+
st.download_button("Download Study Plan as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
| 166 |
+
|
| 167 |
else:
|
| 168 |
+
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|