ikraamkb commited on
Commit
3fb07d9
Β·
verified Β·
1 Parent(s): 40485d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -42
app.py CHANGED
@@ -1,43 +1,49 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import RedirectResponse
 
 
3
  import fitz # PyMuPDF
4
  import docx
5
- import openpyxl
6
  import pptx
 
7
  import io
8
- from PIL import Image
9
  import gradio as gr
10
- from transformers import pipeline
11
 
12
- # Load models
13
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
14
  image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
15
 
 
16
  app = FastAPI()
17
 
18
  # -------------------------
19
- # File Extraction Helpers
20
  # -------------------------
21
- def extract_text_from_pdf(file_obj):
 
22
  try:
23
- file_obj.seek(0)
24
- with fitz.open(stream=file_obj.read(), filetype="pdf") as doc:
 
25
  return "\n".join([page.get_text() for page in doc])
26
  except Exception as e:
27
  return f"❌ PDF extraction error: {e}"
28
 
29
- def extract_text_from_docx(file_obj):
30
  try:
31
- file_obj.seek(0)
32
- doc = docx.Document(io.BytesIO(file_obj.read()))
 
33
  return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
34
  except Exception as e:
35
  return f"❌ DOCX extraction error: {e}"
36
 
37
- def extract_text_from_pptx(file_obj):
38
  try:
39
- file_obj.seek(0)
40
- prs = pptx.Presentation(io.BytesIO(file_obj.read()))
 
41
  text = []
42
  for slide in prs.slides:
43
  for shape in slide.shapes:
@@ -47,10 +53,11 @@ def extract_text_from_pptx(file_obj):
47
  except Exception as e:
48
  return f"❌ PPTX extraction error: {e}"
49
 
50
- def extract_text_from_xlsx(file_obj):
51
  try:
52
- file_obj.seek(0)
53
- wb = openpyxl.load_workbook(io.BytesIO(file_obj.read()))
 
54
  text = []
55
  for sheet in wb.sheetnames:
56
  ws = wb[sheet]
@@ -61,23 +68,29 @@ def extract_text_from_xlsx(file_obj):
61
  return f"❌ XLSX extraction error: {e}"
62
 
63
  # -------------------------
64
- # Main Logic
65
  # -------------------------
66
- def summarize_document(file):
67
- name = getattr(file, "name", "").lower()
68
- if name.endswith(".pdf"):
69
- text = extract_text_from_pdf(file)
70
- elif name.endswith(".docx"):
71
- text = extract_text_from_docx(file)
72
- elif name.endswith(".pptx"):
73
- text = extract_text_from_pptx(file)
74
- elif name.endswith(".xlsx"):
75
- text = extract_text_from_xlsx(file)
 
 
 
 
 
 
76
  else:
77
- return "❌ Unsupported file format."
78
 
79
- if not text or not isinstance(text, str) or not text.strip():
80
- return "❗ No extractable text found."
81
 
82
  try:
83
  summary = summarizer(text[:3000], max_length=150, min_length=30, do_sample=False)
@@ -86,34 +99,34 @@ def summarize_document(file):
86
  return f"⚠️ Summarization error: {e}"
87
 
88
  def interpret_image(image):
 
 
89
  try:
90
  return f"πŸ–ΌοΈ Caption:\n{image_captioner(image)[0]['generated_text']}"
91
  except Exception as e:
92
  return f"⚠️ Image captioning error: {e}"
93
 
94
  # -------------------------
95
- # Gradio Interfaces
96
  # -------------------------
97
- doc_summary = gr.Interface(
 
98
  fn=summarize_document,
99
- inputs=gr.File(label="Upload a Document"),
100
- outputs="text",
101
  title="πŸ“„ Document Summarizer"
102
  )
103
 
104
- img_caption = gr.Interface(
105
  fn=interpret_image,
106
  inputs=gr.Image(type="pil", label="Upload an Image"),
107
- outputs="text",
108
  title="πŸ–ΌοΈ Image Interpreter"
109
  )
110
 
111
- # -------------------------
112
- # Launch with FastAPI
113
- # -------------------------
114
- demo = gr.TabbedInterface([doc_summary, img_caption], ["Document Summary", "Image Captioning"])
115
  app = gr.mount_gradio_app(app, demo, path="/")
116
 
117
  @app.get("/")
118
- def home():
119
  return RedirectResponse(url="/")
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import RedirectResponse
3
+ from transformers import pipeline
4
+ from PIL import Image
5
  import fitz # PyMuPDF
6
  import docx
 
7
  import pptx
8
+ import openpyxl
9
  import io
10
+
11
  import gradio as gr
 
12
 
13
+ # Initialize models
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
+ # FastAPI app
18
  app = FastAPI()
19
 
20
  # -------------------------
21
+ # Helper Functions
22
  # -------------------------
23
+
24
+ def extract_text_from_pdf(upload):
25
  try:
26
+ file_bytes = upload.read()
27
+ stream = io.BytesIO(file_bytes)
28
+ with fitz.open(stream=stream, filetype="pdf") as doc:
29
  return "\n".join([page.get_text() for page in doc])
30
  except Exception as e:
31
  return f"❌ PDF extraction error: {e}"
32
 
33
+ def extract_text_from_docx(upload):
34
  try:
35
+ file_bytes = upload.read()
36
+ stream = io.BytesIO(file_bytes)
37
+ doc = docx.Document(stream)
38
  return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
39
  except Exception as e:
40
  return f"❌ DOCX extraction error: {e}"
41
 
42
+ def extract_text_from_pptx(upload):
43
  try:
44
+ file_bytes = upload.read()
45
+ stream = io.BytesIO(file_bytes)
46
+ prs = pptx.Presentation(stream)
47
  text = []
48
  for slide in prs.slides:
49
  for shape in slide.shapes:
 
53
  except Exception as e:
54
  return f"❌ PPTX extraction error: {e}"
55
 
56
+ def extract_text_from_xlsx(upload):
57
  try:
58
+ file_bytes = upload.read()
59
+ stream = io.BytesIO(file_bytes)
60
+ wb = openpyxl.load_workbook(stream)
61
  text = []
62
  for sheet in wb.sheetnames:
63
  ws = wb[sheet]
 
68
  return f"❌ XLSX extraction error: {e}"
69
 
70
  # -------------------------
71
+ # Core Functions
72
  # -------------------------
73
+
74
+ def summarize_document(upload):
75
+ if not upload:
76
+ return "⚠️ No file uploaded."
77
+
78
+ ext = upload.name.lower()
79
+ upload.seek(0)
80
+
81
+ if ext.endswith(".pdf"):
82
+ text = extract_text_from_pdf(upload)
83
+ elif ext.endswith(".docx"):
84
+ text = extract_text_from_docx(upload)
85
+ elif ext.endswith(".pptx"):
86
+ text = extract_text_from_pptx(upload)
87
+ elif ext.endswith(".xlsx"):
88
+ text = extract_text_from_xlsx(upload)
89
  else:
90
+ return "❌ Unsupported file type."
91
 
92
+ if not text or not text.strip() or text.startswith("❌"):
93
+ return text if text.startswith("❌") else "❗ No extractable text found."
94
 
95
  try:
96
  summary = summarizer(text[:3000], max_length=150, min_length=30, do_sample=False)
 
99
  return f"⚠️ Summarization error: {e}"
100
 
101
  def interpret_image(image):
102
+ if not image:
103
+ return "⚠️ No image uploaded."
104
  try:
105
  return f"πŸ–ΌοΈ Caption:\n{image_captioner(image)[0]['generated_text']}"
106
  except Exception as e:
107
  return f"⚠️ Image captioning error: {e}"
108
 
109
  # -------------------------
110
+ # Gradio Interface
111
  # -------------------------
112
+
113
+ doc_ui = gr.Interface(
114
  fn=summarize_document,
115
+ inputs=gr.File(label="Upload a Document (PDF, DOCX, PPTX, XLSX)"),
116
+ outputs=gr.Textbox(label="Summary"),
117
  title="πŸ“„ Document Summarizer"
118
  )
119
 
120
+ img_ui = gr.Interface(
121
  fn=interpret_image,
122
  inputs=gr.Image(type="pil", label="Upload an Image"),
123
+ outputs=gr.Textbox(label="Caption"),
124
  title="πŸ–ΌοΈ Image Interpreter"
125
  )
126
 
127
+ demo = gr.TabbedInterface([doc_ui, img_ui], ["Document Summarization", "Image Captioning"])
 
 
 
128
  app = gr.mount_gradio_app(app, demo, path="/")
129
 
130
  @app.get("/")
131
+ def redirect_to_ui():
132
  return RedirectResponse(url="/")