Update app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,53 @@
|
|
1 |
-
# Import necessary libraries
|
2 |
import streamlit as st
|
3 |
from langchain_community.document_loaders import PyPDFLoader
|
4 |
import openai
|
5 |
from langchain.prompts import ChatPromptTemplate
|
6 |
from langchain_core.output_parsers import StrOutputParser
|
7 |
from langchain.chat_models import ChatOpenAI
|
8 |
-
|
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
|
18 |
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
|
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
|
27 |
-
if option == 'Ask a Question':
|
28 |
-
question_input = st.text_input("Enter your question about the document:")
|
29 |
-
|
30 |
-
# Function to generate a PDF and allow download
|
31 |
-
def generate_pdf(response, filename="response.pdf"):
|
32 |
-
pdf = FPDF()
|
33 |
-
pdf.add_page()
|
34 |
-
|
35 |
-
# Adding a Unicode-compatible font (like Arial Unicode MS or other compatible font)
|
36 |
-
pdf.add_font('ArialUnicode', '', 'arialuni.ttf', uni=True) # Path to font, make sure this is correct for your system
|
37 |
-
pdf.set_font('ArialUnicode', '', 12)
|
38 |
-
|
39 |
-
# Add the response text
|
40 |
-
pdf.multi_cell(0, 10, response)
|
41 |
-
|
42 |
-
# Save to a temporary file
|
43 |
-
pdf.output(filename)
|
44 |
-
|
45 |
-
# Return the file path
|
46 |
-
return filename
|
47 |
-
|
48 |
if openai_api_key:
|
49 |
-
# Set OpenAI API key
|
50 |
openai.api_key = openai_api_key
|
51 |
|
52 |
if user_file_upload:
|
53 |
-
# Read the uploaded file
|
54 |
pdf_data = user_file_upload.read()
|
55 |
-
|
56 |
-
# Save the uploaded file to a temporary location
|
57 |
with open("temp_pdf_file.pdf", "wb") as f:
|
58 |
f.write(pdf_data)
|
59 |
-
|
60 |
-
# Load the temporary PDF file
|
61 |
loader = PyPDFLoader("temp_pdf_file.pdf")
|
62 |
data = loader.load_and_split()
|
63 |
|
64 |
-
## Prompt Template for Summary
|
65 |
-
prompt_1 = ChatPromptTemplate.from_messages(
|
66 |
-
[
|
67 |
-
("system", "You are a smart assistant. Give a summary of the user's PDF. Be polite."),
|
68 |
-
("user", "{data}")
|
69 |
-
]
|
70 |
-
)
|
71 |
-
|
72 |
-
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
73 |
-
llm_summary = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
74 |
-
output_parser = StrOutputParser()
|
75 |
-
chain_1 = prompt_1 | llm_summary | output_parser
|
76 |
-
|
77 |
-
## Prompt Template for Quiz
|
78 |
-
prompt_2 = ChatPromptTemplate.from_messages(
|
79 |
-
[
|
80 |
-
("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."),
|
81 |
-
("user", "{data}")
|
82 |
-
]
|
83 |
-
)
|
84 |
-
|
85 |
-
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
86 |
-
llm_quiz = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
87 |
-
output_parser = StrOutputParser()
|
88 |
-
chain_2 = prompt_2 | llm_quiz | output_parser
|
89 |
-
|
90 |
-
## Prompt Template for Question-Answering
|
91 |
-
prompt_3 = ChatPromptTemplate.from_messages(
|
92 |
-
[
|
93 |
-
("system", "You are a smart assistant. Answer the user's question based on the content of the PDF. Be polite."),
|
94 |
-
("user", "{data}\n\nUser's question: {question}")
|
95 |
-
]
|
96 |
-
)
|
97 |
-
|
98 |
-
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
99 |
-
llm_qa = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
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
|
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 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
# Generate PDF for the summary and offer it as a download
|
122 |
-
pdf_filename = generate_pdf(summary_response, filename="summary_response.pdf")
|
123 |
-
st.download_button("Download Summary as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
124 |
-
|
125 |
-
elif option == 'Generate Quiz':
|
126 |
-
# Generate quiz
|
127 |
-
quiz_response = chain_2.invoke({'data': data})
|
128 |
-
st.write(quiz_response)
|
129 |
-
|
130 |
-
# Generate PDF for the quiz and offer it as a download
|
131 |
-
pdf_filename = generate_pdf(quiz_response, filename="quiz_response.pdf")
|
132 |
-
st.download_button("Download Quiz as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
133 |
-
|
134 |
-
elif option == 'Ask a Question' and question_input:
|
135 |
-
# Add a "Generate Answer" button
|
136 |
-
generate_answer = st.button("Generate Answer")
|
137 |
-
|
138 |
-
if generate_answer:
|
139 |
-
# Generate answer for the user's question
|
140 |
-
question_answer_response = chain_3.invoke({'data': data, 'question': question_input})
|
141 |
-
st.write(question_answer_response)
|
142 |
-
|
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 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
else:
|
166 |
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
|
4 |
from langchain.prompts import ChatPromptTemplate
|
5 |
from langchain_core.output_parsers import StrOutputParser
|
6 |
from langchain.chat_models import ChatOpenAI
|
7 |
+
import pandas as pd
|
|
|
8 |
from datetime import datetime, timedelta
|
9 |
|
|
|
10 |
st.title('Educational Assistant')
|
11 |
st.header('Summary, Quiz Generator, Q&A, and Topics to be Covered')
|
12 |
st.sidebar.title('Drop your PDF here')
|
13 |
|
|
|
14 |
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
|
|
|
15 |
user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
|
|
|
|
|
16 |
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Topics to be Covered'))
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if openai_api_key:
|
|
|
19 |
openai.api_key = openai_api_key
|
20 |
|
21 |
if user_file_upload:
|
|
|
22 |
pdf_data = user_file_upload.read()
|
|
|
|
|
23 |
with open("temp_pdf_file.pdf", "wb") as f:
|
24 |
f.write(pdf_data)
|
|
|
|
|
25 |
loader = PyPDFLoader("temp_pdf_file.pdf")
|
26 |
data = loader.load_and_split()
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
prompt_4 = ChatPromptTemplate.from_messages(
|
29 |
[
|
30 |
+
("system", "You are a smart assistant. Analyze the user's PDF and generate 7 topics with detailed themes for the next 7 days. Output format: 'Day X: Task\tTheme'"),
|
31 |
("user", "{data}")
|
32 |
]
|
33 |
)
|
34 |
+
llm_topics = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key)
|
|
|
|
|
35 |
output_parser = StrOutputParser()
|
36 |
chain_4 = prompt_4 | llm_topics | output_parser
|
37 |
|
38 |
+
if option == 'Topics to be Covered':
|
39 |
+
topics_response = chain_4.invoke({'data': data})
|
40 |
+
topics_list = topics_response.split("\n")
|
41 |
+
start_date = datetime.today()
|
42 |
+
table_data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
for i in range(7):
|
45 |
+
day_date = start_date + timedelta(days=i)
|
46 |
+
topic_parts = topics_list[i].split('\t') if i < len(topics_list) else [f"Day {i+1}: you will figure out my llm program", "you will figure out my llm program"]
|
47 |
+
table_data.append([day_date.strftime("%d-%b-%y"), topic_parts[0], topic_parts[1]])
|
48 |
|
49 |
+
df = pd.DataFrame(table_data, columns=["Day", "Tasks", "Theme"])
|
50 |
+
st.write("### Topics to be Covered in the Next 7 Days")
|
51 |
+
st.table(df)
|
|
|
52 |
else:
|
53 |
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|