shukdevdatta123 commited on
Commit
b99a30f
·
verified ·
1 Parent(s): bbeaed1

Create abc.txt

Browse files
Files changed (1) hide show
  1. abc.txt +130 -0
abc.txt ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ summary_clicked = st.button('Generate Summary')
22
+ quiz_clicked = st.button('Generate Quiz')
23
+
24
+ # Input for asking questions
25
+ question_input = st.text_input("Enter your question about the document:")
26
+
27
+ # Button to trigger question answering, placed after the input
28
+ ask_question_clicked = st.button('Ask a Question')
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
+ if summary_clicked:
104
+ # Generate summary
105
+ summary_response = chain_1.invoke({'data': data})
106
+ st.write(summary_response)
107
+
108
+ # Generate PDF for the summary and offer it as a download
109
+ pdf_filename = generate_pdf(summary_response, filename="summary_response.pdf")
110
+ st.download_button("Download Summary as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
111
+
112
+ elif quiz_clicked:
113
+ # Generate quiz
114
+ quiz_response = chain_2.invoke({'data': data})
115
+ st.write(quiz_response)
116
+
117
+ # Generate PDF for the quiz and offer it as a download
118
+ pdf_filename = generate_pdf(quiz_response, filename="quiz_response.pdf")
119
+ st.download_button("Download Quiz as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
120
+
121
+ elif ask_question_clicked and question_input:
122
+ # Generate answer for the user's question
123
+ question_answer_response = chain_3.invoke({'data': data, 'question': question_input})
124
+ st.write(question_answer_response)
125
+
126
+ # Generate PDF for the question answer and offer it as a download
127
+ pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf")
128
+ st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
129
+ else:
130
+ st.sidebar.warning("Please enter your OpenAI API Key to proceed.")