ikraamkb commited on
Commit
5e30a65
Β·
verified Β·
1 Parent(s): ea282f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -42
app.py CHANGED
@@ -1,7 +1,7 @@
1
- from fastapi import FastAPI, UploadFile, File, Form
2
  from fastapi.responses import RedirectResponse
3
  import gradio as gr
4
- from transformers import pipeline
5
  import tempfile
6
  import os
7
  from PIL import Image
@@ -11,13 +11,27 @@ import openpyxl
11
  from pptx import Presentation
12
  import easyocr
13
 
14
- # Initialize models
15
- summarizer = pipeline("text2text-generation", model="FeruzaBoynazarovaas/my_awesome_billsum_model")
16
- captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
17
- reader = easyocr.Reader(['en']) # For OCR
18
-
19
  app = FastAPI()
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def extract_text_from_file(file_path: str, file_type: str):
22
  """Extract text from different document formats"""
23
  try:
@@ -39,59 +53,53 @@ def extract_text_from_file(file_path: str, file_type: str):
39
  return f"Error reading file: {str(e)}"
40
 
41
  def process_document(file):
42
- """Handle document upload and summarization"""
43
- # Save temp file
44
- file_ext = os.path.splitext(file.name)[1][1:].lower()
45
- with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file_ext}") as tmp:
46
- tmp.write(file.read())
47
- tmp_path = tmp.name
48
-
49
- # Extract and summarize
50
- text = extract_text_from_file(tmp_path, file_ext)
51
- summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['generated_text']
52
-
53
- # Cleanup
54
- os.unlink(tmp_path)
55
- return summary
56
 
57
  def process_image(image):
58
- """Handle image captioning and OCR"""
59
- img = Image.open(image)
60
-
61
- # Get caption
62
- caption = captioner(img)[0]['generated_text']
63
-
64
- # Get OCR text
65
- ocr_result = reader.readtext(img)
66
- ocr_text = " ".join([res[1] for res in ocr_result])
67
-
68
- return {
69
- "caption": caption,
70
- "ocr_text": ocr_text if ocr_text else "No readable text found"
71
- }
72
 
73
  # Gradio Interface
74
  with gr.Blocks() as demo:
75
- gr.Markdown("# πŸ“„ Document & Image Analysis Web Service")
76
 
77
  with gr.Tab("Document Summarization"):
78
- doc_input = gr.File(label="Upload Document (PDF, DOCX, PPTX, XLSX)")
79
  doc_output = gr.Textbox(label="Summary")
80
  doc_button = gr.Button("Summarize")
81
 
82
  with gr.Tab("Image Analysis"):
83
  img_input = gr.Image(type="filepath", label="Upload Image")
84
- with gr.Accordion("Results", open=True):
85
- caption_output = gr.Textbox(label="Image Caption")
86
- ocr_output = gr.Textbox(label="Extracted Text")
87
  img_button = gr.Button("Analyze")
88
 
89
  doc_button.click(process_document, inputs=doc_input, outputs=doc_output)
90
  img_button.click(process_image, inputs=img_input, outputs=[caption_output, ocr_output])
91
 
92
- # Mount Gradio app
93
  app = gr.mount_gradio_app(app, demo, path="/")
94
 
95
  @app.get("/")
96
- def redirect_to_gradio():
97
  return RedirectResponse(url="/")
 
1
+ from fastapi import FastAPI, UploadFile, File
2
  from fastapi.responses import RedirectResponse
3
  import gradio as gr
4
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
5
  import tempfile
6
  import os
7
  from PIL import Image
 
11
  from pptx import Presentation
12
  import easyocr
13
 
 
 
 
 
 
14
  app = FastAPI()
15
 
16
+ # Initialize models with error handling
17
+ try:
18
+ # Load summarization model directly with tokenizer
19
+ tokenizer = AutoTokenizer.from_pretrained("FeruzaBoynazarovaas/my_awesome_billsum_model", use_fast=False)
20
+ model = AutoModelForSeq2SeqLM.from_pretrained("FeruzaBoynazarovaas/my_awesome_billsum_model")
21
+ summarizer = pipeline(
22
+ "text2text-generation",
23
+ model=model,
24
+ tokenizer=tokenizer
25
+ )
26
+ except Exception as e:
27
+ print(f"Error loading summarizer: {e}")
28
+ # Fallback to a default model if custom fails
29
+ summarizer = pipeline("text2text-generation", model="t5-small")
30
+
31
+ # Other models (these should work fine)
32
+ captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
33
+ reader = easyocr.Reader(['en'])
34
+
35
  def extract_text_from_file(file_path: str, file_type: str):
36
  """Extract text from different document formats"""
37
  try:
 
53
  return f"Error reading file: {str(e)}"
54
 
55
  def process_document(file):
56
+ try:
57
+ file_ext = os.path.splitext(file.name)[1][1:].lower()
58
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file_ext}") as tmp:
59
+ tmp.write(file.read())
60
+ tmp_path = tmp.name
61
+
62
+ text = extract_text_from_file(tmp_path, file_ext)
63
+ summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['generated_text']
64
+
65
+ os.unlink(tmp_path)
66
+ return summary
67
+ except Exception as e:
68
+ return f"Processing error: {str(e)}"
 
69
 
70
  def process_image(image):
71
+ try:
72
+ img = Image.open(image)
73
+ caption = captioner(img)[0]['generated_text']
74
+ ocr_result = reader.readtext(img)
75
+ ocr_text = " ".join([res[1] for res in ocr_result])
76
+ return {
77
+ "caption": caption,
78
+ "ocr_text": ocr_text if ocr_text else "No readable text found"
79
+ }
80
+ except Exception as e:
81
+ return {"error": str(e)}
 
 
 
82
 
83
  # Gradio Interface
84
  with gr.Blocks() as demo:
85
+ gr.Markdown("# πŸ“„ Document & Image Analysis")
86
 
87
  with gr.Tab("Document Summarization"):
88
+ doc_input = gr.File(label="Upload Document")
89
  doc_output = gr.Textbox(label="Summary")
90
  doc_button = gr.Button("Summarize")
91
 
92
  with gr.Tab("Image Analysis"):
93
  img_input = gr.Image(type="filepath", label="Upload Image")
94
+ caption_output = gr.Textbox(label="Image Caption")
95
+ ocr_output = gr.Textbox(label="Extracted Text")
 
96
  img_button = gr.Button("Analyze")
97
 
98
  doc_button.click(process_document, inputs=doc_input, outputs=doc_output)
99
  img_button.click(process_image, inputs=img_input, outputs=[caption_output, ocr_output])
100
 
 
101
  app = gr.mount_gradio_app(app, demo, path="/")
102
 
103
  @app.get("/")
104
+ def redirect():
105
  return RedirectResponse(url="/")