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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -18,14 +18,13 @@ 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
- 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"):
@@ -100,7 +99,7 @@ if openai_api_key:
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)
@@ -109,7 +108,7 @@ if openai_api_key:
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)
@@ -118,13 +117,17 @@ if openai_api_key:
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.")
 
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"):
 
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)
 
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)
 
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.")