Chan-Y commited on
Commit
026783f
1 Parent(s): f5d655c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import warnings
2
  warnings.simplefilter(action='ignore', category=FutureWarning)
3
 
 
4
  import gradio as gr
5
  from langchain.prompts import PromptTemplate
6
  from langchain.chains.summarize import load_summarize_chain
@@ -18,11 +19,22 @@ llm = HuggingFaceEndpoint(
18
  )
19
  llm_engine_hf = ChatHuggingFace(llm=llm)
20
 
 
 
 
 
 
 
 
21
  def summarize(file, n_words):
22
  # Read the content of the uploaded file
23
  file_path = file.name
24
- with open(file_path, 'r', encoding='utf-8') as f:
25
- file_content = f.read()
 
 
 
 
26
  document = Document(file_content)
27
  # Generate the summary
28
  text = document.page_content
 
1
  import warnings
2
  warnings.simplefilter(action='ignore', category=FutureWarning)
3
 
4
+ import PyPDF2
5
  import gradio as gr
6
  from langchain.prompts import PromptTemplate
7
  from langchain.chains.summarize import load_summarize_chain
 
19
  )
20
  llm_engine_hf = ChatHuggingFace(llm=llm)
21
 
22
+ def read_pdf(file_path):
23
+ pdf_reader = PyPDF2.PdfReader(file_path)
24
+ text = ""
25
+ for page in range(len(pdf_reader.pages)):
26
+ text += pdf_reader.pages[page].extract_text()
27
+ return text
28
+
29
  def summarize(file, n_words):
30
  # Read the content of the uploaded file
31
  file_path = file.name
32
+ if file_path.endswith('.pdf'):
33
+ file_content = read_pdf(file_path)
34
+ else:
35
+ with open(file_path, 'r', encoding='utf-8') as f:
36
+ file_content = f.read()
37
+
38
  document = Document(file_content)
39
  # Generate the summary
40
  text = document.page_content