Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load summarization and image captioning models
|
6 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
7 |
+
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
8 |
+
|
9 |
+
def analyze_input(file, question=None):
|
10 |
+
if file is None:
|
11 |
+
return "Please upload a document or image."
|
12 |
+
|
13 |
+
filename = file.name.lower()
|
14 |
+
|
15 |
+
if filename.endswith((".png", ".jpg", ".jpeg")):
|
16 |
+
image = Image.open(file)
|
17 |
+
caption = image_captioner(image)[0]['generated_text']
|
18 |
+
return f"π· Image Interpretation:\n{caption}"
|
19 |
+
|
20 |
+
elif filename.endswith((".pdf", ".docx", ".pptx", ".xlsx")):
|
21 |
+
from PyPDF2 import PdfReader
|
22 |
+
import docx
|
23 |
+
import pptx
|
24 |
+
import pandas as pd
|
25 |
+
|
26 |
+
try:
|
27 |
+
text = ""
|
28 |
+
if filename.endswith(".pdf"):
|
29 |
+
reader = PdfReader(file)
|
30 |
+
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
31 |
+
|
32 |
+
elif filename.endswith(".docx"):
|
33 |
+
doc = docx.Document(file)
|
34 |
+
text = "\n".join([p.text for p in doc.paragraphs if p.text.strip()])
|
35 |
+
|
36 |
+
elif filename.endswith(".pptx"):
|
37 |
+
prs = pptx.Presentation(file)
|
38 |
+
for slide in prs.slides:
|
39 |
+
for shape in slide.shapes:
|
40 |
+
if hasattr(shape, "text"):
|
41 |
+
text += shape.text + "\n"
|
42 |
+
|
43 |
+
elif filename.endswith(".xlsx"):
|
44 |
+
df = pd.read_excel(file, sheet_name=None)
|
45 |
+
text = "\n".join([df[sheet].to_string() for sheet in df])
|
46 |
+
|
47 |
+
if not text.strip():
|
48 |
+
return "Could not extract meaningful text from the document."
|
49 |
+
|
50 |
+
summary = summarizer(text[:3000], max_length=200, min_length=30, do_sample=False)
|
51 |
+
return f"π Document Summary:\n{summary[0]['summary_text']}"
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
return f"β Error processing document: {str(e)}"
|
55 |
+
|
56 |
+
else:
|
57 |
+
return "Unsupported file type. Please upload a valid image or document."
|
58 |
+
|
59 |
+
iface = gr.Interface(
|
60 |
+
fn=analyze_input,
|
61 |
+
inputs=gr.File(label="Upload Document or Image"),
|
62 |
+
outputs=gr.Textbox(label="Result", lines=10),
|
63 |
+
title="Document & Image Analysis Web Service",
|
64 |
+
description="Upload a document (PDF, DOCX, PPTX, XLSX) to get a summary or an image to get a caption. Runs fully on CPU."
|
65 |
+
)
|
66 |
+
|
67 |
+
demo = gr.TabbedInterface(iface,"Docs and Images")
|
68 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
69 |
+
|
70 |
+
@app.get("/")
|
71 |
+
def home():
|
72 |
+
return RedirectResponse(url="/")
|