ikraamkb commited on
Commit
e8ad5ec
Β·
verified Β·
1 Parent(s): d1b0e84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -4,15 +4,17 @@ from PIL import Image
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,13 +23,13 @@ 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
@@ -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
- # βœ… Gradio Interface
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 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="/")
 
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="/")