Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,49 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.responses import RedirectResponse
|
|
|
|
|
3 |
import fitz # PyMuPDF
|
4 |
import docx
|
5 |
-
import openpyxl
|
6 |
import pptx
|
|
|
7 |
import io
|
8 |
-
|
9 |
import gradio as gr
|
10 |
-
from transformers import pipeline
|
11 |
|
12 |
-
#
|
13 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
14 |
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
15 |
|
|
|
16 |
app = FastAPI()
|
17 |
|
18 |
# -------------------------
|
19 |
-
#
|
20 |
# -------------------------
|
21 |
-
|
|
|
22 |
try:
|
23 |
-
|
24 |
-
|
|
|
25 |
return "\n".join([page.get_text() for page in doc])
|
26 |
except Exception as e:
|
27 |
return f"β PDF extraction error: {e}"
|
28 |
|
29 |
-
def extract_text_from_docx(
|
30 |
try:
|
31 |
-
|
32 |
-
|
|
|
33 |
return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
|
34 |
except Exception as e:
|
35 |
return f"β DOCX extraction error: {e}"
|
36 |
|
37 |
-
def extract_text_from_pptx(
|
38 |
try:
|
39 |
-
|
40 |
-
|
|
|
41 |
text = []
|
42 |
for slide in prs.slides:
|
43 |
for shape in slide.shapes:
|
@@ -47,10 +53,11 @@ def extract_text_from_pptx(file_obj):
|
|
47 |
except Exception as e:
|
48 |
return f"β PPTX extraction error: {e}"
|
49 |
|
50 |
-
def extract_text_from_xlsx(
|
51 |
try:
|
52 |
-
|
53 |
-
|
|
|
54 |
text = []
|
55 |
for sheet in wb.sheetnames:
|
56 |
ws = wb[sheet]
|
@@ -61,23 +68,29 @@ def extract_text_from_xlsx(file_obj):
|
|
61 |
return f"β XLSX extraction error: {e}"
|
62 |
|
63 |
# -------------------------
|
64 |
-
#
|
65 |
# -------------------------
|
66 |
-
|
67 |
-
|
68 |
-
if
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
text =
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
else:
|
77 |
-
return "β Unsupported file
|
78 |
|
79 |
-
if not text or not
|
80 |
-
return "β No extractable text found."
|
81 |
|
82 |
try:
|
83 |
summary = summarizer(text[:3000], max_length=150, min_length=30, do_sample=False)
|
@@ -86,34 +99,34 @@ def summarize_document(file):
|
|
86 |
return f"β οΈ Summarization error: {e}"
|
87 |
|
88 |
def interpret_image(image):
|
|
|
|
|
89 |
try:
|
90 |
return f"πΌοΈ Caption:\n{image_captioner(image)[0]['generated_text']}"
|
91 |
except Exception as e:
|
92 |
return f"β οΈ Image captioning error: {e}"
|
93 |
|
94 |
# -------------------------
|
95 |
-
# Gradio
|
96 |
# -------------------------
|
97 |
-
|
|
|
98 |
fn=summarize_document,
|
99 |
-
inputs=gr.File(label="Upload a Document"),
|
100 |
-
outputs="
|
101 |
title="π Document Summarizer"
|
102 |
)
|
103 |
|
104 |
-
|
105 |
fn=interpret_image,
|
106 |
inputs=gr.Image(type="pil", label="Upload an Image"),
|
107 |
-
outputs="
|
108 |
title="πΌοΈ Image Interpreter"
|
109 |
)
|
110 |
|
111 |
-
|
112 |
-
# Launch with FastAPI
|
113 |
-
# -------------------------
|
114 |
-
demo = gr.TabbedInterface([doc_summary, img_caption], ["Document Summary", "Image Captioning"])
|
115 |
app = gr.mount_gradio_app(app, demo, path="/")
|
116 |
|
117 |
@app.get("/")
|
118 |
-
def
|
119 |
return RedirectResponse(url="/")
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.responses import RedirectResponse
|
3 |
+
from transformers import pipeline
|
4 |
+
from PIL import Image
|
5 |
import fitz # PyMuPDF
|
6 |
import docx
|
|
|
7 |
import pptx
|
8 |
+
import openpyxl
|
9 |
import io
|
10 |
+
|
11 |
import gradio as gr
|
|
|
12 |
|
13 |
+
# Initialize models
|
14 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
15 |
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
16 |
|
17 |
+
# FastAPI app
|
18 |
app = FastAPI()
|
19 |
|
20 |
# -------------------------
|
21 |
+
# Helper Functions
|
22 |
# -------------------------
|
23 |
+
|
24 |
+
def extract_text_from_pdf(upload):
|
25 |
try:
|
26 |
+
file_bytes = upload.read()
|
27 |
+
stream = io.BytesIO(file_bytes)
|
28 |
+
with fitz.open(stream=stream, filetype="pdf") as doc:
|
29 |
return "\n".join([page.get_text() for page in doc])
|
30 |
except Exception as e:
|
31 |
return f"β PDF extraction error: {e}"
|
32 |
|
33 |
+
def extract_text_from_docx(upload):
|
34 |
try:
|
35 |
+
file_bytes = upload.read()
|
36 |
+
stream = io.BytesIO(file_bytes)
|
37 |
+
doc = docx.Document(stream)
|
38 |
return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
|
39 |
except Exception as e:
|
40 |
return f"β DOCX extraction error: {e}"
|
41 |
|
42 |
+
def extract_text_from_pptx(upload):
|
43 |
try:
|
44 |
+
file_bytes = upload.read()
|
45 |
+
stream = io.BytesIO(file_bytes)
|
46 |
+
prs = pptx.Presentation(stream)
|
47 |
text = []
|
48 |
for slide in prs.slides:
|
49 |
for shape in slide.shapes:
|
|
|
53 |
except Exception as e:
|
54 |
return f"β PPTX extraction error: {e}"
|
55 |
|
56 |
+
def extract_text_from_xlsx(upload):
|
57 |
try:
|
58 |
+
file_bytes = upload.read()
|
59 |
+
stream = io.BytesIO(file_bytes)
|
60 |
+
wb = openpyxl.load_workbook(stream)
|
61 |
text = []
|
62 |
for sheet in wb.sheetnames:
|
63 |
ws = wb[sheet]
|
|
|
68 |
return f"β XLSX extraction error: {e}"
|
69 |
|
70 |
# -------------------------
|
71 |
+
# Core Functions
|
72 |
# -------------------------
|
73 |
+
|
74 |
+
def summarize_document(upload):
|
75 |
+
if not upload:
|
76 |
+
return "β οΈ No file uploaded."
|
77 |
+
|
78 |
+
ext = upload.name.lower()
|
79 |
+
upload.seek(0)
|
80 |
+
|
81 |
+
if ext.endswith(".pdf"):
|
82 |
+
text = extract_text_from_pdf(upload)
|
83 |
+
elif ext.endswith(".docx"):
|
84 |
+
text = extract_text_from_docx(upload)
|
85 |
+
elif ext.endswith(".pptx"):
|
86 |
+
text = extract_text_from_pptx(upload)
|
87 |
+
elif ext.endswith(".xlsx"):
|
88 |
+
text = extract_text_from_xlsx(upload)
|
89 |
else:
|
90 |
+
return "β Unsupported file type."
|
91 |
|
92 |
+
if not text or not text.strip() or text.startswith("β"):
|
93 |
+
return text if text.startswith("β") else "β No extractable text found."
|
94 |
|
95 |
try:
|
96 |
summary = summarizer(text[:3000], max_length=150, min_length=30, do_sample=False)
|
|
|
99 |
return f"β οΈ Summarization error: {e}"
|
100 |
|
101 |
def interpret_image(image):
|
102 |
+
if not image:
|
103 |
+
return "β οΈ No image uploaded."
|
104 |
try:
|
105 |
return f"πΌοΈ Caption:\n{image_captioner(image)[0]['generated_text']}"
|
106 |
except Exception as e:
|
107 |
return f"β οΈ Image captioning error: {e}"
|
108 |
|
109 |
# -------------------------
|
110 |
+
# Gradio Interface
|
111 |
# -------------------------
|
112 |
+
|
113 |
+
doc_ui = gr.Interface(
|
114 |
fn=summarize_document,
|
115 |
+
inputs=gr.File(label="Upload a Document (PDF, DOCX, PPTX, XLSX)"),
|
116 |
+
outputs=gr.Textbox(label="Summary"),
|
117 |
title="π Document Summarizer"
|
118 |
)
|
119 |
|
120 |
+
img_ui = gr.Interface(
|
121 |
fn=interpret_image,
|
122 |
inputs=gr.Image(type="pil", label="Upload an Image"),
|
123 |
+
outputs=gr.Textbox(label="Caption"),
|
124 |
title="πΌοΈ Image Interpreter"
|
125 |
)
|
126 |
|
127 |
+
demo = gr.TabbedInterface([doc_ui, img_ui], ["Document Summarization", "Image Captioning"])
|
|
|
|
|
|
|
128 |
app = gr.mount_gradio_app(app, demo, path="/")
|
129 |
|
130 |
@app.get("/")
|
131 |
+
def redirect_to_ui():
|
132 |
return RedirectResponse(url="/")
|