Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ from fpdf import FPDF
|
|
18 |
import datetime
|
19 |
from concurrent.futures import ThreadPoolExecutor
|
20 |
import hashlib
|
|
|
21 |
|
22 |
nltk.download('punkt', quiet=True)
|
23 |
|
@@ -41,7 +42,11 @@ def clean_text(text: str) -> str:
|
|
41 |
text = re.sub(r'\bPage\s*\d+\b', '', text, flags=re.IGNORECASE)
|
42 |
return text.strip()
|
43 |
|
44 |
-
def
|
|
|
|
|
|
|
|
|
45 |
try:
|
46 |
if file_extension == "pdf":
|
47 |
with fitz.open(file_path) as doc:
|
@@ -50,7 +55,7 @@ def extract_text(file_path: str, file_extension: str):
|
|
50 |
images = [page.get_pixmap() for page in doc]
|
51 |
temp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
52 |
images[0].save(temp_img.name)
|
53 |
-
ocr_result =
|
54 |
os.unlink(temp_img.name)
|
55 |
text = "\n".join(ocr_result) if ocr_result else text
|
56 |
return clean_text(text), ""
|
@@ -70,7 +75,7 @@ def extract_text(file_path: str, file_extension: str):
|
|
70 |
return clean_text("\n".join(text)), ""
|
71 |
|
72 |
elif file_extension in ["jpg", "jpeg", "png"]:
|
73 |
-
ocr_result =
|
74 |
return clean_text("\n".join(ocr_result)), ""
|
75 |
|
76 |
return "", "Unsupported file format"
|
@@ -107,7 +112,7 @@ def generate_summary(text: str, length: str = "medium") -> str:
|
|
107 |
length_params = {
|
108 |
"short": {"max_length": 80, "min_length": 30},
|
109 |
"medium": {"max_length": 200, "min_length": 80},
|
110 |
-
"long": {"max_length":
|
111 |
}
|
112 |
chunks = chunk_text(text)
|
113 |
try:
|
@@ -150,7 +155,6 @@ def create_pdf(summary: str, original_filename: str):
|
|
150 |
pdf.set_font("Arial", 'B', 16)
|
151 |
pdf.cell(200, 10, txt="Document Summary", ln=1, align='C')
|
152 |
pdf.set_font("Arial", size=12)
|
153 |
-
pdf.cell(200, 10, txt=f"Original file: {original_filename}", ln=1)
|
154 |
pdf.cell(200, 10, txt=f"Generated on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=1)
|
155 |
pdf.ln(10)
|
156 |
pdf.multi_cell(0, 10, txt=summary)
|
@@ -167,7 +171,9 @@ def summarize_document(file, summary_length: str, enable_tts: bool = True):
|
|
167 |
file_path = file.name
|
168 |
file_extension = file_path.split(".")[-1].lower()
|
169 |
original_filename = os.path.basename(file_path)
|
170 |
-
|
|
|
|
|
171 |
if error:
|
172 |
return error, "", None, None
|
173 |
if not text or len(text.split()) < 30:
|
@@ -181,7 +187,7 @@ def summarize_document(file, summary_length: str, enable_tts: bool = True):
|
|
181 |
return f"Summarization error: {str(e)}", "", None, None
|
182 |
|
183 |
with gr.Blocks(title="Document Summarizer", theme=gr.themes.Soft()) as demo:
|
184 |
-
gr.Markdown("#
|
185 |
gr.Markdown("Upload a document to generate a summary with audio and optional PDF download")
|
186 |
|
187 |
with gr.Row():
|
|
|
18 |
import datetime
|
19 |
from concurrent.futures import ThreadPoolExecutor
|
20 |
import hashlib
|
21 |
+
import asyncio
|
22 |
|
23 |
nltk.download('punkt', quiet=True)
|
24 |
|
|
|
42 |
text = re.sub(r'\bPage\s*\d+\b', '', text, flags=re.IGNORECASE)
|
43 |
return text.strip()
|
44 |
|
45 |
+
async def async_ocr(path):
|
46 |
+
loop = asyncio.get_event_loop()
|
47 |
+
return await loop.run_in_executor(executor, lambda: reader.readtext(path, detail=0))
|
48 |
+
|
49 |
+
async def extract_text(file_path: str, file_extension: str):
|
50 |
try:
|
51 |
if file_extension == "pdf":
|
52 |
with fitz.open(file_path) as doc:
|
|
|
55 |
images = [page.get_pixmap() for page in doc]
|
56 |
temp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
57 |
images[0].save(temp_img.name)
|
58 |
+
ocr_result = await async_ocr(temp_img.name)
|
59 |
os.unlink(temp_img.name)
|
60 |
text = "\n".join(ocr_result) if ocr_result else text
|
61 |
return clean_text(text), ""
|
|
|
75 |
return clean_text("\n".join(text)), ""
|
76 |
|
77 |
elif file_extension in ["jpg", "jpeg", "png"]:
|
78 |
+
ocr_result = await async_ocr(file_path)
|
79 |
return clean_text("\n".join(ocr_result)), ""
|
80 |
|
81 |
return "", "Unsupported file format"
|
|
|
112 |
length_params = {
|
113 |
"short": {"max_length": 80, "min_length": 30},
|
114 |
"medium": {"max_length": 200, "min_length": 80},
|
115 |
+
"long": {"max_length": 300, "min_length": 210}
|
116 |
}
|
117 |
chunks = chunk_text(text)
|
118 |
try:
|
|
|
155 |
pdf.set_font("Arial", 'B', 16)
|
156 |
pdf.cell(200, 10, txt="Document Summary", ln=1, align='C')
|
157 |
pdf.set_font("Arial", size=12)
|
|
|
158 |
pdf.cell(200, 10, txt=f"Generated on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=1)
|
159 |
pdf.ln(10)
|
160 |
pdf.multi_cell(0, 10, txt=summary)
|
|
|
171 |
file_path = file.name
|
172 |
file_extension = file_path.split(".")[-1].lower()
|
173 |
original_filename = os.path.basename(file_path)
|
174 |
+
|
175 |
+
loop = asyncio.get_event_loop()
|
176 |
+
text, error = loop.run_until_complete(extract_text(file_path, file_extension))
|
177 |
if error:
|
178 |
return error, "", None, None
|
179 |
if not text or len(text.split()) < 30:
|
|
|
187 |
return f"Summarization error: {str(e)}", "", None, None
|
188 |
|
189 |
with gr.Blocks(title="Document Summarizer", theme=gr.themes.Soft()) as demo:
|
190 |
+
gr.Markdown("# \ud83d\udcc4 Advanced Document Summarizer")
|
191 |
gr.Markdown("Upload a document to generate a summary with audio and optional PDF download")
|
192 |
|
193 |
with gr.Row():
|