Spaces:
Running
Running
File size: 3,989 Bytes
08f3d12 da10ca7 08f3d12 6f78a44 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 08f3d12 7839da1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# app_logic.py
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import fitz, docx, pptx, openpyxl, re, nltk, tempfile, os, easyocr, hashlib, datetime
from nltk.tokenize import sent_tokenize
from fpdf import FPDF
from gtts import gTTS
nltk.download('punkt', quiet=True)
# Load once
MODEL_NAME = "facebook/bart-large-cnn"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer, device=-1, batch_size=4)
reader = easyocr.Reader(['en'], gpu=False)
summary_cache = {}
def clean_text(text):
text = re.sub(r'\s+', ' ', text)
text = re.sub(r'\u2022\s*|\d\.\s+', '', text)
text = re.sub(r'\[.*?\]|\(.*?\)', '', text)
text = re.sub(r'\bPage\s*\d+\b', '', text, flags=re.IGNORECASE)
return text.strip()
def extract_text(file_path, file_extension):
try:
if file_extension in ["pdf"]:
with fitz.open(file_path) as doc:
text = "\n".join(page.get_text("text") for page in doc)
if len(text.strip()) < 50:
images = [page.get_pixmap() for page in doc]
temp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
images[0].save(temp_img.name)
ocr_result = reader.readtext(temp_img.name, detail=0)
os.unlink(temp_img.name)
text = "\n".join(ocr_result) if ocr_result else text
elif file_extension in ["docx"]:
doc = docx.Document(file_path)
text = "\n".join(p.text for p in doc.paragraphs)
elif file_extension in ["pptx"]:
prs = pptx.Presentation(file_path)
text = "\n".join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text"))
elif file_extension in ["xlsx"]:
wb = openpyxl.load_workbook(file_path, read_only=True)
text = "\n".join([" ".join(str(cell) for cell in row if cell) for sheet in wb.sheetnames for row in wb[sheet].iter_rows(values_only=True)])
else:
return "", "Unsupported file type"
return clean_text(text), ""
except Exception as e:
return "", f"Extraction error: {e}"
def chunk_text(text, max_tokens=950):
sentences = sent_tokenize(text)
chunks, current_chunk = [], ""
for sentence in sentences:
if len(tokenizer.encode(current_chunk + " " + sentence)) <= max_tokens:
current_chunk += " " + sentence
else:
chunks.append(current_chunk.strip())
current_chunk = sentence
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def generate_summary(text, length="medium"):
cache_key = hashlib.md5((text + length).encode()).hexdigest()
if cache_key in summary_cache:
return summary_cache[cache_key]
params = {"short": (30, 80), "medium": (80, 200), "long": (210, 300)}[length]
min_len, max_len = params
chunks = chunk_text(text)
summaries = summarizer(chunks, max_length=max_len, min_length=min_len, do_sample=False)
final_summary = " ".join(s['summary_text'] for s in summaries)
summary_cache[cache_key] = final_summary
return final_summary
def text_to_speech(text):
try:
tts = gTTS(text)
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(temp_audio.name)
return temp_audio.name
except:
return ""
def create_pdf(summary, original_filename):
try:
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", 'B', 16)
pdf.cell(200, 10, "Summary", ln=True, align='C')
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, summary)
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
pdf.output(temp_pdf.name)
return temp_pdf.name
except:
return ""
|