Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# Import necessary libraries
|
2 |
import streamlit as st
|
3 |
from langchain_community.document_loaders import PyPDFLoader
|
4 |
import openai
|
@@ -7,19 +6,22 @@ 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
|
@@ -44,22 +46,46 @@ def generate_pdf(response, filename="response.pdf"):
|
|
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 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
## Prompt Template for Summary
|
64 |
prompt_1 = ChatPromptTemplate.from_messages(
|
65 |
[
|
@@ -99,35 +125,55 @@ if openai_api_key:
|
|
99 |
output_parser = StrOutputParser()
|
100 |
chain_3 = prompt_3 | llm_qa | output_parser
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
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 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
else:
|
133 |
-
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
|
|
|
6 |
from langchain.chat_models import ChatOpenAI
|
7 |
from fpdf import FPDF
|
8 |
import os
|
9 |
+
from datetime import datetime, timedelta
|
10 |
+
import speech_recognition as sr
|
11 |
+
import calendar
|
12 |
|
13 |
# Set up Streamlit UI
|
14 |
st.title('Educational Assistant')
|
15 |
st.header('Summary, Quiz Generator, and Q&A')
|
16 |
+
st.sidebar.title('Drop your PDF or Audio file here')
|
17 |
|
18 |
# Input OpenAI API key from keyboard
|
19 |
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
|
20 |
|
21 |
+
user_file_upload = st.sidebar.file_uploader(label='', type=['pdf', 'mp3'])
|
22 |
|
23 |
# Sidebar option selection for Summary, Quiz, or Q&A
|
24 |
+
option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Daily Study Plan'))
|
25 |
|
26 |
# Input for asking questions (only visible when "Ask a Question" is selected)
|
27 |
question_input = None
|
|
|
46 |
# Return the file path
|
47 |
return filename
|
48 |
|
49 |
+
# Function to handle voice-to-text conversion
|
50 |
+
def convert_audio_to_text(audio_file):
|
51 |
+
recognizer = sr.Recognizer()
|
52 |
+
|
53 |
+
with sr.AudioFile(audio_file) as source:
|
54 |
+
audio = recognizer.record(source)
|
55 |
+
|
56 |
+
try:
|
57 |
+
text = recognizer.recognize_google(audio)
|
58 |
+
return text
|
59 |
+
except sr.UnknownValueError:
|
60 |
+
return "Sorry, I couldn't understand the audio."
|
61 |
+
except sr.RequestError:
|
62 |
+
return "Sorry, there was an error with the speech-to-text service."
|
63 |
+
|
64 |
if openai_api_key:
|
65 |
# Set OpenAI API key
|
66 |
openai.api_key = openai_api_key
|
67 |
|
68 |
if user_file_upload:
|
69 |
+
# Check if the file is PDF or Audio
|
70 |
+
if user_file_upload.type == 'application/pdf':
|
71 |
+
# Read the uploaded PDF file
|
72 |
+
pdf_data = user_file_upload.read()
|
73 |
+
|
74 |
+
# Save the uploaded PDF to a temporary location
|
75 |
+
with open("temp_pdf_file.pdf", "wb") as f:
|
76 |
+
f.write(pdf_data)
|
77 |
+
|
78 |
+
# Load the temporary PDF file
|
79 |
+
loader = PyPDFLoader("temp_pdf_file.pdf")
|
80 |
+
data = loader.load_and_split()
|
81 |
+
|
82 |
+
elif user_file_upload.type == 'audio/mpeg' or user_file_upload.type == 'audio/wav' or user_file_upload.type == 'audio/mp3':
|
83 |
+
# Convert audio file to text
|
84 |
+
text_data = convert_audio_to_text(user_file_upload)
|
85 |
+
data = text_data
|
86 |
+
st.write("Audio converted to text: ")
|
87 |
+
st.write(data)
|
88 |
+
|
89 |
## Prompt Template for Summary
|
90 |
prompt_1 = ChatPromptTemplate.from_messages(
|
91 |
[
|
|
|
125 |
output_parser = StrOutputParser()
|
126 |
chain_3 = prompt_3 | llm_qa | output_parser
|
127 |
|
128 |
+
## Daily Study Plan
|
129 |
+
def generate_study_plan(data, exam_date, num_days=30):
|
130 |
+
today = datetime.today()
|
131 |
+
study_plan = []
|
132 |
+
day_gap = (exam_date - today).days // num_days
|
133 |
+
for i in range(num_days):
|
134 |
+
day_start = today + timedelta(days=i * day_gap)
|
135 |
+
study_plan.append({
|
136 |
+
'day': day_start.strftime("%Y-%m-%d"),
|
137 |
+
'topic': data[i % len(data)] # Simple round-robin of topics for now
|
138 |
+
})
|
139 |
+
return study_plan
|
140 |
+
|
141 |
+
if option == 'Generate Summary':
|
142 |
+
# Generate summary
|
143 |
+
summary_response = chain_1.invoke({'data': data})
|
144 |
+
st.write(summary_response)
|
145 |
+
|
146 |
+
# Generate PDF for the summary and offer it as a download
|
147 |
+
pdf_filename = generate_pdf(summary_response, filename="summary_response.pdf")
|
148 |
+
st.download_button("Download Summary as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
elif option == 'Generate Quiz':
|
151 |
+
# Generate quiz
|
152 |
+
quiz_response = chain_2.invoke({'data': data})
|
153 |
+
st.write(quiz_response)
|
154 |
+
|
155 |
+
# Generate PDF for the quiz and offer it as a download
|
156 |
+
pdf_filename = generate_pdf(quiz_response, filename="quiz_response.pdf")
|
157 |
+
st.download_button("Download Quiz as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
158 |
+
|
159 |
+
elif option == 'Ask a Question' and question_input:
|
160 |
+
# Add a "Generate Answer" button
|
161 |
+
generate_answer = st.button("Generate Answer")
|
162 |
+
|
163 |
+
if generate_answer:
|
164 |
+
# Generate answer for the user's question
|
165 |
+
question_answer_response = chain_3.invoke({'data': data, 'question': question_input})
|
166 |
+
st.write(question_answer_response)
|
167 |
+
|
168 |
+
# Generate PDF for the question answer and offer it as a download
|
169 |
+
pdf_filename = generate_pdf(question_answer_response, filename="question_answer_response.pdf")
|
170 |
+
st.download_button("Download Answer as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
|
171 |
+
|
172 |
+
elif option == 'Daily Study Plan':
|
173 |
+
# Assume exam date is 30 days from today for simplicity
|
174 |
+
exam_date = datetime.today() + timedelta(days=30)
|
175 |
+
study_plan = generate_study_plan(data, exam_date)
|
176 |
+
for plan in study_plan:
|
177 |
+
st.write(f"Day: {plan['day']} - Study Topic: {plan['topic']}")
|
178 |
else:
|
179 |
+
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
|