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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -4,11 +4,15 @@ from PIL import Image
4
  from fastapi import FastAPI
5
  from starlette.responses import RedirectResponse
6
 
7
- # Load models
 
 
 
 
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
- from PyPDF2 import PdfReader
27
  import docx
28
  import pptx
29
  import pandas as pd
30
 
31
  try:
32
  text = ""
 
33
  if filename.endswith(".pdf"):
34
- reader = PdfReader(file)
35
- text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
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 TabbedInterface (even if only one for now)
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="/")