raahinaez commited on
Commit
2aa380f
Β·
verified Β·
1 Parent(s): d8432f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -1,42 +1,38 @@
 
1
  import gradio as gr
2
- from PIL import Image
3
- from mlx_vlm import load, stream_generate
4
- from mlx_vlm.prompt_utils import apply_chat_template
5
- from mlx_vlm.utils import load_config
6
- from pdf2image import convert_from_path
7
 
8
- MODEL_PATH = "ibm-granite/granite-docling-258M-mlx"
 
 
 
9
 
10
- # Load model once on startup
11
- model, processor = load(MODEL_PATH)
12
- config = load_config(MODEL_PATH)
 
 
 
 
 
 
13
 
14
- def process_file(file):
15
- # Convert PDF to images if needed
16
- if file.name.endswith(".pdf"):
17
- pages = convert_from_path(file.name)
18
- else:
19
- pages = [Image.open(file)]
20
-
21
- results = []
22
- for page in pages:
23
- prompt = "Convert this page to Markdown."
24
- formatted_prompt = apply_chat_template(processor, config, prompt, num_images=1)
25
- output = ""
26
- for token in stream_generate(model, processor, formatted_prompt, [page], max_tokens=4096, verbose=False):
27
- output += token.text
28
- if "</doctag>" in token.text:
29
- break
30
- results.append(output)
31
-
32
- return "\n\n".join(results)
33
 
34
- iface = gr.Interface(
35
- fn=process_file,
36
- inputs=gr.File(file_types=[".pdf", ".png", ".jpg", ".jpeg"]),
37
- outputs=gr.Textbox(lines=30),
38
- title="Docling PDF/Image to Markdown",
39
- description="Upload a PDF or image. The model converts it to Markdown/structured format."
 
40
  )
41
 
42
- iface.launch()
 
1
+ # app.py
2
  import gradio as gr
3
+ import pdfplumber
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
 
5
 
6
+ # Load the model
7
+ model_name = "ibm-granite/granite-docling-258m-demo"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
10
 
11
+ # Function to extract text from PDF
12
+ def extract_text_from_pdf(pdf_file):
13
+ text = ""
14
+ with pdfplumber.open(pdf_file.name) as pdf:
15
+ for page in pdf.pages:
16
+ page_text = page.extract_text()
17
+ if page_text:
18
+ text += page_text + "\n"
19
+ return text
20
 
21
+ # Function to generate JSON from text
22
+ def pdf_to_json(pdf_file):
23
+ text = extract_text_from_pdf(pdf_file)
24
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=2048)
25
+ outputs = model.generate(**inputs, max_new_tokens=1024)
26
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Gradio interface
30
+ interface = gr.Interface(
31
+ fn=pdf_to_json,
32
+ inputs=gr.File(file_types=[".pdf"]),
33
+ outputs=gr.Textbox(label="Generated JSON"),
34
+ title="PDF to JSON using Granite DocLing",
35
+ description="Upload a PDF and get a JSON output using the ibm-granite/granite-docling-258m-demo model."
36
  )
37
 
38
+ interface.launch()