Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,38 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
|
|
|
| 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()
|