Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,112 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
4 |
from fastapi import FastAPI
|
5 |
from starlette.responses import RedirectResponse
|
6 |
|
7 |
-
#
|
8 |
-
import gradio.context
|
9 |
-
from pydantic import BaseModel
|
10 |
-
if not hasattr(BaseModel, "model_fields"): # model_fields was renamed from __fields__ in Pydantic v1 β v2
|
11 |
-
BaseModel.model_fields = BaseModel.__fields__
|
12 |
-
|
13 |
-
# π Load Hugging Face Pipelines
|
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 |
-
#
|
18 |
app = FastAPI()
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if file is None:
|
22 |
return "Please upload a document or image."
|
23 |
|
24 |
filename = file.name.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
image = Image.open(file)
|
29 |
-
caption = image_captioner(image)[0]['generated_text']
|
30 |
-
return f"π· Image Interpretation:\n{caption}"
|
31 |
-
|
32 |
-
# π Document
|
33 |
-
elif filename.endswith((".pdf", ".docx", ".pptx", ".xlsx")):
|
34 |
-
import pdfplumber
|
35 |
-
import docx
|
36 |
-
import pptx
|
37 |
-
import pandas as pd
|
38 |
-
|
39 |
-
try:
|
40 |
-
text = ""
|
41 |
-
|
42 |
-
if filename.endswith(".pdf"):
|
43 |
-
with pdfplumber.open(file) as pdf:
|
44 |
-
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
45 |
-
|
46 |
-
elif filename.endswith(".docx"):
|
47 |
-
doc = docx.Document(file)
|
48 |
-
text = "\n".join([p.text for p in doc.paragraphs if p.text.strip()])
|
49 |
-
|
50 |
-
elif filename.endswith(".pptx"):
|
51 |
-
prs = pptx.Presentation(file)
|
52 |
-
for slide in prs.slides:
|
53 |
-
for shape in slide.shapes:
|
54 |
-
if hasattr(shape, "text"):
|
55 |
-
text += shape.text + "\n"
|
56 |
-
|
57 |
-
elif filename.endswith(".xlsx"):
|
58 |
-
df = pd.read_excel(file, sheet_name=None)
|
59 |
-
text = "\n".join([df[sheet].to_string() for sheet in df])
|
60 |
-
|
61 |
-
if not text.strip():
|
62 |
-
return "β Could not extract meaningful text from the document."
|
63 |
-
|
64 |
-
summary = summarizer(text[:3000], max_length=200, min_length=30, do_sample=False)
|
65 |
-
return f"π Document Summary:\n{summary[0]['summary_text']}"
|
66 |
-
|
67 |
-
except Exception as e:
|
68 |
-
return f"β Error processing document: {str(e)}"
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
#
|
74 |
iface = gr.Interface(
|
75 |
fn=analyze_input,
|
76 |
inputs=gr.File(label="Upload Document or Image"),
|
77 |
outputs=gr.Textbox(label="Result", lines=10),
|
78 |
title="Document & Image Analysis Web Service",
|
79 |
-
description="Upload a document (PDF, DOCX, PPTX, XLSX) or image to get a
|
80 |
)
|
81 |
|
82 |
-
# β¨οΈ Wrap in Tabbed UI
|
83 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
84 |
|
85 |
-
#
|
86 |
app = gr.mount_gradio_app(app, demo, path="/")
|
87 |
|
88 |
-
# π Base redirect
|
89 |
@app.get("/")
|
90 |
-
def
|
91 |
return RedirectResponse(url="/")
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
+
import fitz # PyMuPDF for PDF
|
5 |
+
import docx
|
6 |
+
import pptx
|
7 |
+
import openpyxl
|
8 |
+
import easyocr
|
9 |
from fastapi import FastAPI
|
10 |
from starlette.responses import RedirectResponse
|
11 |
|
12 |
+
# Initialize models
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
14 |
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
15 |
+
reader = easyocr.Reader(['en', 'fr'])
|
16 |
|
17 |
+
# FastAPI app
|
18 |
app = FastAPI()
|
19 |
|
20 |
+
# Text extraction functions
|
21 |
+
def extract_text_from_pdf(file_path):
|
22 |
+
try:
|
23 |
+
doc = fitz.open(file_path)
|
24 |
+
return "\n".join([page.get_text() for page in doc])
|
25 |
+
except Exception as e:
|
26 |
+
return f"β Error reading PDF: {e}"
|
27 |
+
|
28 |
+
def extract_text_from_docx(file):
|
29 |
+
try:
|
30 |
+
doc = docx.Document(file)
|
31 |
+
return "\n".join([p.text for p in doc.paragraphs if p.text.strip()])
|
32 |
+
except Exception as e:
|
33 |
+
return f"β Error reading DOCX: {e}"
|
34 |
+
|
35 |
+
def extract_text_from_pptx(file):
|
36 |
+
try:
|
37 |
+
text = []
|
38 |
+
prs = pptx.Presentation(file)
|
39 |
+
for slide in prs.slides:
|
40 |
+
for shape in slide.shapes:
|
41 |
+
if hasattr(shape, "text"):
|
42 |
+
text.append(shape.text)
|
43 |
+
return "\n".join(text)
|
44 |
+
except Exception as e:
|
45 |
+
return f"β Error reading PPTX: {e}"
|
46 |
+
|
47 |
+
def extract_text_from_xlsx(file):
|
48 |
+
try:
|
49 |
+
wb = openpyxl.load_workbook(file)
|
50 |
+
text = []
|
51 |
+
for sheet in wb.sheetnames:
|
52 |
+
ws = wb[sheet]
|
53 |
+
for row in ws.iter_rows(values_only=True):
|
54 |
+
text.append(" ".join(str(cell) for cell in row if cell))
|
55 |
+
return "\n".join(text)
|
56 |
+
except Exception as e:
|
57 |
+
return f"β Error reading XLSX: {e}"
|
58 |
+
|
59 |
+
def extract_text_from_image(file):
|
60 |
+
try:
|
61 |
+
image = Image.open(file).convert("RGB")
|
62 |
+
return "\n".join([text[1] for text in reader.readtext(np.array(image))])
|
63 |
+
except Exception as e:
|
64 |
+
return f"β Error reading image with OCR: {e}"
|
65 |
+
|
66 |
+
# Main processing function
|
67 |
+
def analyze_input(file):
|
68 |
if file is None:
|
69 |
return "Please upload a document or image."
|
70 |
|
71 |
filename = file.name.lower()
|
72 |
+
ext = filename.split('.')[-1]
|
73 |
+
|
74 |
+
if ext in ["jpg", "jpeg", "png"]:
|
75 |
+
caption = image_captioner(Image.open(file))[0]['generated_text']
|
76 |
+
ocr_text = extract_text_from_image(file)
|
77 |
+
return f"π· Image Caption:\n{caption}\n\nπ OCR Text:\n{ocr_text}"
|
78 |
+
|
79 |
+
elif ext == "pdf":
|
80 |
+
text = extract_text_from_pdf(file.name)
|
81 |
+
elif ext == "docx":
|
82 |
+
text = extract_text_from_docx(file)
|
83 |
+
elif ext == "pptx":
|
84 |
+
text = extract_text_from_pptx(file)
|
85 |
+
elif ext == "xlsx":
|
86 |
+
text = extract_text_from_xlsx(file)
|
87 |
+
else:
|
88 |
+
return "Unsupported file type. Please upload PDF, DOCX, PPTX, XLSX, or an image."
|
89 |
|
90 |
+
if not text.strip():
|
91 |
+
return "β No text could be extracted from the document."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
summary = summarizer(text[:3000], max_length=200, min_length=30, do_sample=False)
|
94 |
+
return f"π Document Summary:\n{summary[0]['summary_text']}"
|
95 |
|
96 |
+
# Gradio Interface
|
97 |
iface = gr.Interface(
|
98 |
fn=analyze_input,
|
99 |
inputs=gr.File(label="Upload Document or Image"),
|
100 |
outputs=gr.Textbox(label="Result", lines=10),
|
101 |
title="Document & Image Analysis Web Service",
|
102 |
+
description="Upload a document (PDF, DOCX, PPTX, XLSX) to get a summary or an image to get a caption. OCR and AI-powered."
|
103 |
)
|
104 |
|
|
|
105 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
106 |
|
107 |
+
# Mount to FastAPI
|
108 |
app = gr.mount_gradio_app(app, demo, path="/")
|
109 |
|
|
|
110 |
@app.get("/")
|
111 |
+
def root():
|
112 |
return RedirectResponse(url="/")
|