Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,6 @@ import PyPDF2
|
|
5 |
import gradio as gr
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain.chains.summarize import load_summarize_chain
|
8 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
9 |
-
from langchain_community.document_loaders import DirectoryLoader
|
10 |
from langchain_core.documents import Document
|
11 |
from pathlib import Path
|
12 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
@@ -31,16 +29,11 @@ def summarize(file, n_words):
|
|
31 |
# Read the content of the uploaded file
|
32 |
file_path = file.name
|
33 |
if file_path.endswith('.pdf'):
|
34 |
-
|
35 |
else:
|
36 |
with open(file_path, 'r', encoding='utf-8') as f:
|
37 |
-
|
38 |
|
39 |
-
document = Document(file_content)
|
40 |
-
# Generate the summary
|
41 |
-
text = document.page_content
|
42 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=3000, chunk_overlap=200)
|
43 |
-
chunks = text_splitter.create_documents([text])
|
44 |
template = '''
|
45 |
You are a commentator. Your task is to write a report on an essay.
|
46 |
When presented with the essay, come up with interesting questions to ask, and answer each question.
|
@@ -62,13 +55,18 @@ Generate three distinct and thought-provoking questions that can be asked about
|
|
62 |
## Write a report
|
63 |
Using the essay summary and the answers to the interesting questions, create a comprehensive report in Markdown format.
|
64 |
'''
|
|
|
65 |
prompt = PromptTemplate(
|
66 |
template=template,
|
67 |
input_variables=['TEXT']
|
68 |
)
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
def download_summary(output_text):
|
74 |
if output_text:
|
|
|
5 |
import gradio as gr
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain.chains.summarize import load_summarize_chain
|
|
|
|
|
8 |
from langchain_core.documents import Document
|
9 |
from pathlib import Path
|
10 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
|
|
29 |
# Read the content of the uploaded file
|
30 |
file_path = file.name
|
31 |
if file_path.endswith('.pdf'):
|
32 |
+
text = read_pdf(file_path)
|
33 |
else:
|
34 |
with open(file_path, 'r', encoding='utf-8') as f:
|
35 |
+
text = f.read()
|
36 |
|
|
|
|
|
|
|
|
|
|
|
37 |
template = '''
|
38 |
You are a commentator. Your task is to write a report on an essay.
|
39 |
When presented with the essay, come up with interesting questions to ask, and answer each question.
|
|
|
55 |
## Write a report
|
56 |
Using the essay summary and the answers to the interesting questions, create a comprehensive report in Markdown format.
|
57 |
'''
|
58 |
+
|
59 |
prompt = PromptTemplate(
|
60 |
template=template,
|
61 |
input_variables=['TEXT']
|
62 |
)
|
63 |
+
|
64 |
+
summary = ""
|
65 |
+
while len(summary.split()) < 100:
|
66 |
+
formatted_prompt = prompt.format(TEXT=text)
|
67 |
+
output_summary = llm_engine_hf.invoke(formatted_prompt)
|
68 |
+
summary = output_summary.content
|
69 |
+
return summary
|
70 |
|
71 |
def download_summary(output_text):
|
72 |
if output_text:
|