Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,133 @@
|
|
|
|
|
| 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 |
-
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
st.title('Educational Assistant')
|
| 11 |
-
st.header('Summary, Quiz Generator, Q&A
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 29 |
[
|
| 30 |
-
("system", "You are a smart assistant.
|
| 31 |
("user", "{data}")
|
| 32 |
]
|
| 33 |
)
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
output_parser = StrOutputParser()
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|
|
|
|
| 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 |
+
from fpdf import FPDF
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Set up Streamlit UI
|
| 12 |
st.title('Educational Assistant')
|
| 13 |
+
st.header('Summary, Quiz Generator, and Q&A')
|
| 14 |
st.sidebar.title('Drop your PDF here')
|
| 15 |
|
| 16 |
+
# Input OpenAI API key from keyboard
|
| 17 |
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
|
| 18 |
+
|
| 19 |
user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
|
| 20 |
+
|
| 21 |
+
# Sidebar option selection for Summary, Quiz, or Q&A
|
| 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
|
| 26 |
+
if option == 'Ask a Question':
|
| 27 |
+
question_input = st.text_input("Enter your question about the document:")
|
| 28 |
+
|
| 29 |
+
# Function to generate a PDF and allow download
|
| 30 |
+
def generate_pdf(response, filename="response.pdf"):
|
| 31 |
+
pdf = FPDF()
|
| 32 |
+
pdf.add_page()
|
| 33 |
+
|
| 34 |
+
# Adding a Unicode-compatible font (like Arial Unicode MS or other compatible font)
|
| 35 |
+
pdf.add_font('ArialUnicode', '', 'arialuni.ttf', uni=True) # Path to font, make sure this is correct for your system
|
| 36 |
+
pdf.set_font('ArialUnicode', '', 12)
|
| 37 |
+
|
| 38 |
+
# Add the response text
|
| 39 |
+
pdf.multi_cell(0, 10, response)
|
| 40 |
+
|
| 41 |
+
# Save to a temporary file
|
| 42 |
+
pdf.output(filename)
|
| 43 |
+
|
| 44 |
+
# Return the file path
|
| 45 |
+
return filename
|
| 46 |
|
| 47 |
if openai_api_key:
|
| 48 |
+
# Set OpenAI API key
|
| 49 |
openai.api_key = openai_api_key
|
| 50 |
|
| 51 |
if user_file_upload:
|
| 52 |
+
# Read the uploaded file
|
| 53 |
pdf_data = user_file_upload.read()
|
| 54 |
+
|
| 55 |
+
# Save the uploaded file to a temporary location
|
| 56 |
with open("temp_pdf_file.pdf", "wb") as f:
|
| 57 |
f.write(pdf_data)
|
| 58 |
+
|
| 59 |
+
# Load the temporary PDF file
|
| 60 |
loader = PyPDFLoader("temp_pdf_file.pdf")
|
| 61 |
data = loader.load_and_split()
|
| 62 |
|
| 63 |
+
## Prompt Template for Summary
|
| 64 |
+
prompt_1 = ChatPromptTemplate.from_messages(
|
| 65 |
[
|
| 66 |
+
("system", "You are a smart assistant. Give a summary of the user's PDF. Be polite."),
|
| 67 |
("user", "{data}")
|
| 68 |
]
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
| 72 |
+
llm_summary = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
| 73 |
output_parser = StrOutputParser()
|
| 74 |
+
chain_1 = prompt_1 | llm_summary | output_parser
|
| 75 |
+
|
| 76 |
+
## Prompt Template for Quiz
|
| 77 |
+
prompt_2 = ChatPromptTemplate.from_messages(
|
| 78 |
+
[
|
| 79 |
+
("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."),
|
| 80 |
+
("user", "{data}")
|
| 81 |
+
]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
| 85 |
+
llm_quiz = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
| 86 |
+
output_parser = StrOutputParser()
|
| 87 |
+
chain_2 = prompt_2 | llm_quiz | output_parser
|
| 88 |
+
|
| 89 |
+
## Prompt Template for Question-Answering
|
| 90 |
+
prompt_3 = ChatPromptTemplate.from_messages(
|
| 91 |
+
[
|
| 92 |
+
("system", "You are a smart assistant. Answer the user's question based on the content of the PDF. Be polite."),
|
| 93 |
+
("user", "{data}\n\nUser's question: {question}")
|
| 94 |
+
]
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Pass the OpenAI API key explicitly to the ChatOpenAI instance
|
| 98 |
+
llm_qa = ChatOpenAI(model="gpt-4o-mini", openai_api_key=openai_api_key) # Pass the key here
|
| 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})
|
| 105 |
+
st.write(summary_response)
|
| 106 |
+
|
| 107 |
+
# Generate PDF for the summary and offer it as a download
|
| 108 |
+
pdf_filename = generate_pdf(summary_response, filename="summary_response.pdf")
|
| 109 |
+
st.download_button("Download Summary as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
| 110 |
+
|
| 111 |
+
elif option == 'Generate Quiz':
|
| 112 |
+
# Generate quiz
|
| 113 |
+
quiz_response = chain_2.invoke({'data': data})
|
| 114 |
+
st.write(quiz_response)
|
| 115 |
+
|
| 116 |
+
# Generate PDF for the quiz and offer it as a download
|
| 117 |
+
pdf_filename = generate_pdf(quiz_response, filename="quiz_response.pdf")
|
| 118 |
+
st.download_button("Download Quiz as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
| 119 |
+
|
| 120 |
+
elif option == 'Ask a Question' and question_input:
|
| 121 |
+
# Add a "Generate Answer" button
|
| 122 |
+
generate_answer = st.button("Generate Answer")
|
| 123 |
+
|
| 124 |
+
if generate_answer:
|
| 125 |
+
# Generate answer for the user's question
|
| 126 |
+
question_answer_response = chain_3.invoke({'data': data, 'question': question_input})
|
| 127 |
+
st.write(question_answer_response)
|
| 128 |
+
|
| 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.")
|