Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,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 +19,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 +100,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 +143,24 @@ 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.")
|
|
|
7 |
from langchain.chat_models import ChatOpenAI
|
8 |
from fpdf import FPDF
|
9 |
import os
|
10 |
+
from datetime import datetime, timedelta
|
11 |
|
12 |
# Set up Streamlit UI
|
13 |
st.title('Educational Assistant')
|
14 |
+
st.header('Summary, Quiz Generator, Q&A, and Topics to be Covered')
|
15 |
st.sidebar.title('Drop your PDF here')
|
16 |
|
17 |
# Input OpenAI API key from keyboard
|
|
|
19 |
|
20 |
user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
|
21 |
|
22 |
+
# Sidebar option selection for Summary, Quiz, Q&A, or Topics to be Covered
|
23 |
+
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Topics to be Covered'))
|
24 |
|
25 |
# Input for asking questions (only visible when "Ask a Question" is selected)
|
26 |
question_input = None
|
|
|
100 |
output_parser = StrOutputParser()
|
101 |
chain_3 = prompt_3 | llm_qa | output_parser
|
102 |
|
103 |
+
## Prompt Template for Topics to be Covered
|
104 |
+
prompt_4 = ChatPromptTemplate.from_messages(
|
105 |
+
[
|
106 |
+
("system", "You are a smart assistant. Analyze the user's PDF and generate 7 topics based on the content for the next 7 days. Be polite."),
|
107 |
+
("user", "{data}")
|
108 |
+
]
|
109 |
+
)
|
110 |
+
|
111 |
+
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
112 |
+
llm_topics = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
113 |
+
output_parser = StrOutputParser()
|
114 |
+
chain_4 = prompt_4 | llm_topics | output_parser
|
115 |
+
|
116 |
if option == 'Generate Summary':
|
117 |
# Generate summary
|
118 |
summary_response = chain_1.invoke({'data': data})
|
|
|
143 |
# Generate PDF for the question answer and offer it as a download
|
144 |
pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf")
|
145 |
st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
146 |
+
|
147 |
+
elif option == 'Topics to be Covered':
|
148 |
+
# Generate topics for the next 7 days
|
149 |
+
topics_response = chain_4.invoke({'data': data})
|
150 |
+
topics = topics_response.split("\n") # Split response into topics
|
151 |
+
|
152 |
+
# Get today's date and create a table for the topics for the next 7 days
|
153 |
+
start_date = datetime.today()
|
154 |
+
table_data = []
|
155 |
+
|
156 |
+
for i in range(7):
|
157 |
+
day_date = start_date + timedelta(days=i)
|
158 |
+
topic = topics[i] if i < len(topics) else "Topic not available"
|
159 |
+
table_data.append([day_date.strftime("%dth %b %Y"), topic])
|
160 |
+
|
161 |
+
# Display the topics table
|
162 |
+
st.write("### Topics to be Covered in the Next 7 Days")
|
163 |
+
st.table(table_data)
|
164 |
+
|
165 |
else:
|
166 |
+
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|