Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,15 @@ from PIL import Image
|
|
4 |
from fastapi import FastAPI
|
5 |
from starlette.responses import RedirectResponse
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
9 |
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
10 |
|
11 |
-
# FastAPI app
|
12 |
app = FastAPI()
|
13 |
|
14 |
def analyze_input(file, question=None):
|
@@ -17,22 +21,25 @@ def analyze_input(file, question=None):
|
|
17 |
|
18 |
filename = file.name.lower()
|
19 |
|
|
|
20 |
if filename.endswith((".png", ".jpg", ".jpeg")):
|
21 |
image = Image.open(file)
|
22 |
caption = image_captioner(image)[0]['generated_text']
|
23 |
return f"π· Image Interpretation:\n{caption}"
|
24 |
|
|
|
25 |
elif filename.endswith((".pdf", ".docx", ".pptx", ".xlsx")):
|
26 |
-
|
27 |
import docx
|
28 |
import pptx
|
29 |
import pandas as pd
|
30 |
|
31 |
try:
|
32 |
text = ""
|
|
|
33 |
if filename.endswith(".pdf"):
|
34 |
-
|
35 |
-
|
36 |
|
37 |
elif filename.endswith(".docx"):
|
38 |
doc = docx.Document(file)
|
@@ -50,7 +57,7 @@ def analyze_input(file, question=None):
|
|
50 |
text = "\n".join([df[sheet].to_string() for sheet in df])
|
51 |
|
52 |
if not text.strip():
|
53 |
-
return "Could not extract meaningful text from the document."
|
54 |
|
55 |
summary = summarizer(text[:3000], max_length=200, min_length=30, do_sample=False)
|
56 |
return f"π Document Summary:\n{summary[0]['summary_text']}"
|
@@ -59,9 +66,9 @@ def analyze_input(file, question=None):
|
|
59 |
return f"β Error processing document: {str(e)}"
|
60 |
|
61 |
else:
|
62 |
-
return "Unsupported file type. Please upload a valid image or document."
|
63 |
|
64 |
-
# Gradio Interface
|
65 |
iface = gr.Interface(
|
66 |
fn=analyze_input,
|
67 |
inputs=gr.File(label="Upload Document or Image"),
|
@@ -70,12 +77,13 @@ iface = gr.Interface(
|
|
70 |
description="Upload a document (PDF, DOCX, PPTX, XLSX) to get a summary or an image to get a caption. Runs fully on CPU."
|
71 |
)
|
72 |
|
73 |
-
# Wrap in
|
74 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
75 |
|
76 |
-
# Mount to FastAPI app
|
77 |
app = gr.mount_gradio_app(app, demo, path="/")
|
78 |
|
|
|
79 |
@app.get("/")
|
80 |
def home():
|
81 |
return RedirectResponse(url="/")
|
|
|
4 |
from fastapi import FastAPI
|
5 |
from starlette.responses import RedirectResponse
|
6 |
|
7 |
+
# β
Patch for Pydantic v2 schema compatibility (important for FastAPI + Gradio)
|
8 |
+
from pydantic import BaseModel
|
9 |
+
BaseModel.model_config = {"arbitrary_types_allowed": True}
|
10 |
+
|
11 |
+
# β
Load models
|
12 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
13 |
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
14 |
|
15 |
+
# β
Initialize FastAPI app
|
16 |
app = FastAPI()
|
17 |
|
18 |
def analyze_input(file, question=None):
|
|
|
21 |
|
22 |
filename = file.name.lower()
|
23 |
|
24 |
+
# π· Image Processing
|
25 |
if filename.endswith((".png", ".jpg", ".jpeg")):
|
26 |
image = Image.open(file)
|
27 |
caption = image_captioner(image)[0]['generated_text']
|
28 |
return f"π· Image Interpretation:\n{caption}"
|
29 |
|
30 |
+
# π Document Processing
|
31 |
elif filename.endswith((".pdf", ".docx", ".pptx", ".xlsx")):
|
32 |
+
import pdfplumber
|
33 |
import docx
|
34 |
import pptx
|
35 |
import pandas as pd
|
36 |
|
37 |
try:
|
38 |
text = ""
|
39 |
+
|
40 |
if filename.endswith(".pdf"):
|
41 |
+
with pdfplumber.open(file) as pdf:
|
42 |
+
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
43 |
|
44 |
elif filename.endswith(".docx"):
|
45 |
doc = docx.Document(file)
|
|
|
57 |
text = "\n".join([df[sheet].to_string() for sheet in df])
|
58 |
|
59 |
if not text.strip():
|
60 |
+
return "β Could not extract meaningful text from the document."
|
61 |
|
62 |
summary = summarizer(text[:3000], max_length=200, min_length=30, do_sample=False)
|
63 |
return f"π Document Summary:\n{summary[0]['summary_text']}"
|
|
|
66 |
return f"β Error processing document: {str(e)}"
|
67 |
|
68 |
else:
|
69 |
+
return "β Unsupported file type. Please upload a valid image or document."
|
70 |
|
71 |
+
# β
Gradio Interface
|
72 |
iface = gr.Interface(
|
73 |
fn=analyze_input,
|
74 |
inputs=gr.File(label="Upload Document or Image"),
|
|
|
77 |
description="Upload a document (PDF, DOCX, PPTX, XLSX) to get a summary or an image to get a caption. Runs fully on CPU."
|
78 |
)
|
79 |
|
80 |
+
# β
Wrap in a Tabbed Interface
|
81 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
82 |
|
83 |
+
# β
Mount to FastAPI app
|
84 |
app = gr.mount_gradio_app(app, demo, path="/")
|
85 |
|
86 |
+
# β
Redirect base URL to Gradio app
|
87 |
@app.get("/")
|
88 |
def home():
|
89 |
return RedirectResponse(url="/")
|