Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,17 @@ from PIL import Image
|
|
4 |
from fastapi import FastAPI
|
5 |
from starlette.responses import RedirectResponse
|
6 |
|
7 |
-
#
|
|
|
8 |
from pydantic import BaseModel
|
9 |
-
BaseModel
|
|
|
10 |
|
11 |
-
#
|
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 |
-
#
|
16 |
app = FastAPI()
|
17 |
|
18 |
def analyze_input(file, question=None):
|
@@ -21,13 +23,13 @@ def analyze_input(file, question=None):
|
|
21 |
|
22 |
filename = file.name.lower()
|
23 |
|
24 |
-
#
|
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
|
31 |
elif filename.endswith((".pdf", ".docx", ".pptx", ".xlsx")):
|
32 |
import pdfplumber
|
33 |
import docx
|
@@ -68,22 +70,22 @@ def analyze_input(file, question=None):
|
|
68 |
else:
|
69 |
return "β Unsupported file type. Please upload a valid image or document."
|
70 |
|
71 |
-
#
|
72 |
iface = gr.Interface(
|
73 |
fn=analyze_input,
|
74 |
inputs=gr.File(label="Upload Document or Image"),
|
75 |
outputs=gr.Textbox(label="Result", lines=10),
|
76 |
title="Document & Image Analysis Web Service",
|
77 |
-
description="Upload a document (PDF, DOCX, PPTX, XLSX) to get a summary or
|
78 |
)
|
79 |
|
80 |
-
#
|
81 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
82 |
|
83 |
-
#
|
84 |
app = gr.mount_gradio_app(app, demo, path="/")
|
85 |
|
86 |
-
#
|
87 |
@app.get("/")
|
88 |
def home():
|
89 |
return RedirectResponse(url="/")
|
|
|
4 |
from fastapi import FastAPI
|
5 |
from starlette.responses import RedirectResponse
|
6 |
|
7 |
+
# π₯ Fix for Pydantic v2 compatibility with Gradio
|
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 |
+
# π Create FastAPI App
|
18 |
app = FastAPI()
|
19 |
|
20 |
def analyze_input(file, question=None):
|
|
|
23 |
|
24 |
filename = file.name.lower()
|
25 |
|
26 |
+
# πΌοΈ Image
|
27 |
if filename.endswith((".png", ".jpg", ".jpeg")):
|
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
|
|
|
70 |
else:
|
71 |
return "β Unsupported file type. Please upload a valid image or document."
|
72 |
|
73 |
+
# ποΈ Gradio UI
|
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 summary or caption. CPU-friendly."
|
80 |
)
|
81 |
|
82 |
+
# β¨οΈ Wrap in Tabbed UI
|
83 |
demo = gr.TabbedInterface([iface], ["Docs and Images"])
|
84 |
|
85 |
+
# π Mount Gradio to FastAPI
|
86 |
app = gr.mount_gradio_app(app, demo, path="/")
|
87 |
|
88 |
+
# π Base redirect
|
89 |
@app.get("/")
|
90 |
def home():
|
91 |
return RedirectResponse(url="/")
|