Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,85 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 4 |
-
import
|
| 5 |
-
import fitz # PyMuPDF
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
model_name = "
|
| 9 |
-
model = AutoModelForTokenClassification.from_pretrained(model_name
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 11 |
|
| 12 |
-
# Function to extract text from PDF
|
| 13 |
-
def extract_text_from_pdf(
|
| 14 |
-
doc = fitz.open(
|
| 15 |
text = ""
|
| 16 |
for page in doc:
|
| 17 |
text += page.get_text()
|
| 18 |
return text.strip()
|
| 19 |
|
| 20 |
-
# Function to map
|
| 21 |
-
def map_labels(
|
| 22 |
-
for
|
| 23 |
-
if
|
| 24 |
-
return
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def process_text(file, labels):
|
| 29 |
# Extract text from the PDF file
|
| 30 |
text = extract_text_from_pdf(file.name)
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
inputs = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True, truncation=True, padding="max_length", max_length=512)
|
| 34 |
-
|
| 35 |
-
# Make predictions
|
| 36 |
-
with torch.no_grad():
|
| 37 |
-
outputs = model(**inputs)
|
| 38 |
-
predictions = torch.argmax(outputs.logits, dim=-1)
|
| 39 |
-
|
| 40 |
-
# Custom label mapping (enhanced prediction)
|
| 41 |
label_map = {
|
| 42 |
-
"Name": ["
|
| 43 |
-
"Organization": ["
|
| 44 |
-
"Location": ["
|
| 45 |
-
"Project": ["
|
| 46 |
-
"Education": ["
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# If the current word is complete and matches the label, append it
|
| 71 |
-
if last_label and mapped_label in extracted_info:
|
| 72 |
-
extracted_info[mapped_label].append(current_word)
|
| 73 |
-
current_word = "" # Reset current word after adding
|
| 74 |
-
|
| 75 |
-
# Add the last word if it's valid
|
| 76 |
-
if current_word and last_label in extracted_info:
|
| 77 |
-
extracted_info[last_label].append(current_word)
|
| 78 |
-
|
| 79 |
-
# Prepare the final output
|
| 80 |
output = ""
|
| 81 |
-
for label,
|
| 82 |
-
if
|
| 83 |
-
|
| 84 |
-
cleaned_words = ' '.join(words).replace(" ", " ") # Ensures correct spacing
|
| 85 |
-
output += f"{label}: {cleaned_words}\n"
|
| 86 |
else:
|
| 87 |
-
output += f"{label}: No information found
|
| 88 |
-
|
| 89 |
return output.strip()
|
| 90 |
|
| 91 |
# Create Gradio components
|
| 92 |
-
file_input = gr.File(label="Upload a file")
|
| 93 |
-
label_input = gr.Textbox(label="Enter labels (comma-separated)")
|
| 94 |
-
output_text = gr.Textbox(label="Extracted
|
| 95 |
|
| 96 |
# Create the Gradio interface
|
| 97 |
iface = gr.Interface(
|
| 98 |
fn=process_text,
|
| 99 |
inputs=[file_input, label_input],
|
| 100 |
outputs=output_text,
|
| 101 |
-
title="NER with Custom Labels"
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# Launch the interface
|
| 105 |
-
iface.launch()
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 4 |
+
import fitz # PyMuPDF for PDF handling
|
|
|
|
| 5 |
|
| 6 |
+
# Load a pre-trained NER model
|
| 7 |
+
model_name = "dbmdz/bert-large-cased-finetuned-conll03-english"
|
| 8 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 11 |
|
| 12 |
+
# Function to extract text from a PDF file
|
| 13 |
+
def extract_text_from_pdf(file_path):
|
| 14 |
+
doc = fitz.open(file_path)
|
| 15 |
text = ""
|
| 16 |
for page in doc:
|
| 17 |
text += page.get_text()
|
| 18 |
return text.strip()
|
| 19 |
|
| 20 |
+
# Function to map recognized entities to custom labels
|
| 21 |
+
def map_labels(entity_label, label_map):
|
| 22 |
+
for custom_label, ner_labels in label_map.items():
|
| 23 |
+
if entity_label in ner_labels:
|
| 24 |
+
return custom_label
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
+
# Function to process the text and extract entities based on custom labels
|
| 28 |
def process_text(file, labels):
|
| 29 |
# Extract text from the PDF file
|
| 30 |
text = extract_text_from_pdf(file.name)
|
| 31 |
+
|
| 32 |
+
# Define the custom label mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
label_map = {
|
| 34 |
+
"Name": ["PER"],
|
| 35 |
+
"Organization": ["ORG"],
|
| 36 |
+
"Location": ["LOC"],
|
| 37 |
+
"Project": ["MISC"],
|
| 38 |
+
"Education": ["MISC"],
|
| 39 |
}
|
| 40 |
+
|
| 41 |
+
# Split the custom labels provided by the user
|
| 42 |
+
requested_labels = [label.strip() for label in labels.split(",")]
|
| 43 |
+
|
| 44 |
+
# Perform NER on the extracted text
|
| 45 |
+
ner_results = ner_pipeline(text)
|
| 46 |
+
|
| 47 |
+
# Initialize a dictionary to hold the extracted information
|
| 48 |
+
extracted_info = {label: [] for label in requested_labels}
|
| 49 |
+
|
| 50 |
+
# Process the NER results
|
| 51 |
+
for entity in ner_results:
|
| 52 |
+
# Remove subword tokens (##) and map the entity to the custom labels
|
| 53 |
+
entity_text = entity['word'].replace("##", "")
|
| 54 |
+
mapped_label = map_labels(entity['entity_group'], label_map)
|
| 55 |
+
|
| 56 |
+
# If the mapped label is in the requested labels, store the entity
|
| 57 |
+
if mapped_label in extracted_info:
|
| 58 |
+
extracted_info[mapped_label].append(entity_text)
|
| 59 |
+
|
| 60 |
+
# Format the output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
output = ""
|
| 62 |
+
for label, entities in extracted_info.items():
|
| 63 |
+
if entities:
|
| 64 |
+
output += f"{label}: {', '.join(sorted(set(entities)))}\n"
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
+
output += f"{label}: No information found.\n"
|
| 67 |
+
|
| 68 |
return output.strip()
|
| 69 |
|
| 70 |
# Create Gradio components
|
| 71 |
+
file_input = gr.File(label="Upload a PDF file")
|
| 72 |
+
label_input = gr.Textbox(label="Enter labels to extract (comma-separated)")
|
| 73 |
+
output_text = gr.Textbox(label="Extracted Information")
|
| 74 |
|
| 75 |
# Create the Gradio interface
|
| 76 |
iface = gr.Interface(
|
| 77 |
fn=process_text,
|
| 78 |
inputs=[file_input, label_input],
|
| 79 |
outputs=output_text,
|
| 80 |
+
title="NER with Custom Labels from PDF",
|
| 81 |
+
description="Upload a PDF file and extract entities based on custom labels."
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Launch the Gradio interface
|
| 85 |
+
iface.launch()
|