Spaces:
Running
Running
File size: 4,847 Bytes
95c2451 5b4fc38 3e87c53 cf9a79a 3e87c53 95c2451 3e87c53 e5b6ad2 af32fa4 e5b6ad2 1e83db4 a74f8b0 3e87c53 af32fa4 3e87c53 6dfac5c af32fa4 cf9a79a 95c2451 af32fa4 cf9a79a 95c2451 af32fa4 cf9a79a 95c2451 af32fa4 6dfac5c 3e87c53 6dfac5c af32fa4 29f74d3 95c2451 cf9a79a 95c2451 e5b6ad2 cf9a79a 95c2451 e5b6ad2 3e87c53 95c2451 3e87c53 95c2451 e5b6ad2 3e87c53 95c2451 3e87c53 95c2451 3e87c53 5b4fc38 3e87c53 5b4fc38 3e87c53 af32fa4 3e87c53 95c2451 5b4fc38 3e87c53 5b4fc38 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
import fitz # PyMuPDF
import docx
import openpyxl
import pptx
import io
from PIL import Image
import gradio as gr
from transformers import pipeline
# Load models
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
app = FastAPI()
# -------------------------
# Extraction Functions
# -------------------------
"""def extract_text_from_pdf(file_bytes):
try:
with fitz.open(stream=file_bytes, filetype="pdf") as doc:
return "\n".join([page.get_text() for page in doc])
except Exception as e:
return f"β PDF extraction error: {e}"
def extract_text_from_docx(file_bytes):
try:
doc = docx.Document(io.BytesIO(file_bytes))
return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
except Exception as e:
return f"β DOCX extraction error: {e}"
def extract_text_from_pptx(file_bytes):
try:
prs = pptx.Presentation(io.BytesIO(file_bytes))
text = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text.append(shape.text)
return "\n".join(text)
except Exception as e:
return f"β PPTX extraction error: {e}"
def extract_text_from_xlsx(file_bytes):
try:
wb = openpyxl.load_workbook(io.BytesIO(file_bytes))
text = []
for sheet in wb.sheetnames:
ws = wb[sheet]
for row in ws.iter_rows(values_only=True):
line = " ".join(str(cell) for cell in row if cell)
text.append(line)
return "\n".join(text)
except Exception as e:
return f"β XLSX extraction error: {e}"
"""
def extract_text_from_pdf(pdf_file):
text = []
try:
with fitz.open(pdf_file) as doc:
for page in doc:
text.append(page.get_text("text"))
except Exception as e:
return f"Error reading PDF: {e}"
return "\n".join(text)
def extract_text_from_docx(docx_file):
doc = docx.Document(docx_file)
return "\n".join([p.text for p in doc.paragraphs if p.text.strip()])
def extract_text_from_pptx(pptx_file):
text = []
try:
presentation = pptx.Presentation(pptx_file)
for slide in presentation.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text.append(shape.text)
except Exception as e:
return f"Error reading PPTX: {e}"
return "\n".join(text)
def extract_text_from_xlsx(xlsx_file):
text = []
try:
wb = openpyxl.load_workbook(xlsx_file)
for sheet in wb.sheetnames:
ws = wb[sheet]
for row in ws.iter_rows(values_only=True):
text.append(" ".join(str(cell) for cell in row if cell))
except Exception as e:
return f"Error reading XLSX: {e}"
return "\n".join(text)
# -------------------------
# Main Logic
# -------------------------
def summarize_document(file):
file_bytes = file.read()
filename = getattr(file, "name", "").lower()
if filename.endswith(".pdf"):
text = extract_text_from_pdf(file_bytes)
elif filename.endswith(".docx"):
text = extract_text_from_docx(file_bytes)
elif filename.endswith(".pptx"):
text = extract_text_from_pptx(file_bytes)
elif filename.endswith(".xlsx"):
text = extract_text_from_xlsx(file_bytes)
else:
return "β Unsupported file format."
if not text.strip():
return "β No extractable text found."
try:
summary = summarizer(text[:3000], max_length=150, min_length=30, do_sample=False)
return f"π Summary:\n{summary[0]['summary_text']}"
except Exception as e:
return f"β οΈ Summarization error: {e}"
def interpret_image(image):
try:
return f"πΌοΈ Caption:\n{image_captioner(image)[0]['generated_text']}"
except Exception as e:
return f"β οΈ Image captioning error: {e}"
# -------------------------
# Gradio Interfaces
# -------------------------
doc_summary = gr.Interface(
fn=summarize_document,
inputs=gr.File(label="Upload a Document"),
outputs="text",
title="π Document Summarizer"
)
img_caption = gr.Interface(
fn=interpret_image,
inputs=gr.Image(type="pil", label="Upload an Image"),
outputs="text",
title="πΌοΈ Image Interpreter"
)
# -------------------------
# Launch with FastAPI
# -------------------------
demo = gr.TabbedInterface([doc_summary, img_caption], ["Document Summary", "Image Captioning"])
app = gr.mount_gradio_app(app, demo, path="/")
@app.get("/")
def home():
return RedirectResponse(url="/")
|