shukdevdatta123 commited on
Commit
5285f2f
·
verified ·
1 Parent(s): 524ec8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -3
app.py CHANGED
@@ -6,10 +6,15 @@ from langchain_core.output_parsers import StrOutputParser
6
  from langchain.chat_models import ChatOpenAI
7
  from fpdf import FPDF
8
  import os
 
 
 
 
 
9
 
10
  # Set up Streamlit UI
11
  st.title('Educational Assistant')
12
- st.header('Summary, Quiz Generator, Q&A, and Study Plan')
13
  st.sidebar.title('Drop your PDF here')
14
 
15
  # Input OpenAI API key from keyboard
@@ -17,8 +22,8 @@ openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="passwo
17
 
18
  user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
19
 
20
- # Sidebar option selection for Summary, Quiz, Q&A, or Study Plan
21
- option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Generate Study Plan'))
22
 
23
  # Input for asking questions (only visible when "Ask a Question" is selected)
24
  question_input = None
@@ -43,6 +48,28 @@ def generate_pdf(response, filename="response.pdf"):
43
  # Return the file path
44
  return filename
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  if openai_api_key:
47
  # Set OpenAI API key
48
  openai.api_key = openai_api_key
@@ -151,5 +178,27 @@ if openai_api_key:
151
  pdf_filename = generate_pdf(study_plan_response, filename="study_plan_response.pdf")
152
  st.download_button("Download Study Plan as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  else:
155
  st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
 
6
  from langchain.chat_models import ChatOpenAI
7
  from fpdf import FPDF
8
  import os
9
+ import spacy
10
+ from collections import Counter
11
+
12
+ # Load SpaCy model for NLP tasks (install 'spacy' and 'en_core_web_sm' first)
13
+ nlp = spacy.load("en_core_web_sm")
14
 
15
  # Set up Streamlit UI
16
  st.title('Educational Assistant')
17
+ st.header('Summary, Quiz Generator, Q&A, Study Plan, Vocabulary Builder & Topic Extraction')
18
  st.sidebar.title('Drop your PDF here')
19
 
20
  # Input OpenAI API key from keyboard
 
22
 
23
  user_file_upload = st.sidebar.file_uploader(label='', type='pdf')
24
 
25
+ # Sidebar option selection for Summary, Quiz, Q&A, Study Plan, Vocabulary Builder, or Topic Extraction
26
+ option = st.sidebar.radio("Choose an option", ('Generate Summary', 'Generate Quiz', 'Ask a Question', 'Generate Study Plan', 'Vocabulary Builder', 'Topic Extraction'))
27
 
28
  # Input for asking questions (only visible when "Ask a Question" is selected)
29
  question_input = None
 
48
  # Return the file path
49
  return filename
50
 
51
+ def extract_key_terms(text):
52
+ """
53
+ Extract key terms (nouns) from the text using spaCy.
54
+ """
55
+ doc = nlp(text)
56
+ key_terms = [token.text for token in doc if token.pos_ == "NOUN" and not token.is_stop]
57
+
58
+ # Count frequency of terms (for demonstration purposes, we show the top 10 most frequent terms)
59
+ term_frequency = Counter(key_terms)
60
+ most_common_terms = term_frequency.most_common(10)
61
+
62
+ return most_common_terms
63
+
64
+ def extract_topics(text):
65
+ """
66
+ Extract main topics (entities) using spaCy's Named Entity Recognition (NER).
67
+ """
68
+ doc = nlp(text)
69
+ topics = [ent.text for ent in doc.ents] # Extract named entities (topics)
70
+
71
+ return set(topics) # Return unique entities as topics
72
+
73
  if openai_api_key:
74
  # Set OpenAI API key
75
  openai.api_key = openai_api_key
 
178
  pdf_filename = generate_pdf(study_plan_response, filename="study_plan_response.pdf")
179
  st.download_button("Download Study Plan as PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
180
 
181
+ elif option == 'Vocabulary Builder':
182
+ # Extract key terms from the document
183
+ key_terms = extract_key_terms(' '.join([page['text'] for page in data]))
184
+ st.write("Key Terms and Definitions:")
185
+ for term, frequency in key_terms:
186
+ st.write(f"{term} - Frequency: {frequency}")
187
+
188
+ # Generate PDF for the key terms and offer it as a download
189
+ pdf_filename = generate_pdf(str(key_terms), filename="key_terms.pdf")
190
+ st.download_button("Download Vocabulary Builder PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
191
+
192
+ elif option == 'Topic Extraction':
193
+ # Extract topics (named entities) from the document
194
+ topics = extract_topics(' '.join([page['text'] for page in data]))
195
+ st.write("Extracted Topics:")
196
+ for topic in topics:
197
+ st.write(topic)
198
+
199
+ # Generate PDF for the topics and offer it as a download
200
+ pdf_filename = generate_pdf(str(topics), filename="topics.pdf")
201
+ st.download_button("Download Topics PDF", data=open(pdf_filename, "rb").read(), file_name=pdf_filename, mime="application/pdf")
202
+
203
  else:
204
  st.sidebar.warning("Please enter your OpenAI API Key to proceed.")