Views
No views yet
1from peft import PeftModel, PeftConfig
2from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3import nltk
4from nltk.tokenize import sent_tokenize, word_tokenize
5from nltk.corpus import stopwords
6from nltk.cluster.util import cosine_distance
7import numpy as np
8import networkx as nx
9import pandas as pd
10
11def preprocess_text(text):
12 sentences = sent_tokenize(text)
13 tokenized_sentences = [word_tokenize(sentence.lower()) for sentence in sentences]
14 return tokenized_sentences
15
16def sentence_similarity(sentence1, sentence2):
17 stop_words = set(stopwords.words('english'))
18 filtered_sentence1 = [w for w in sentence1 if w not in stop_words]
19 filtered_sentence2 = [w for w in sentence2 if w not in stop_words]
20 all_words = list(set(filtered_sentence1 + filtered_sentence2))
21 vector1 = [filtered_sentence1.count(word) for word in all_words]
22 vector2 = [filtered_sentence2.count(word) for word in all_words]
23 return 1 - cosine_distance(vector1, vector2)
24
25def build_similarity_matrix(sentences):
26 similarity_matrix = np.zeros((len(sentences), len(sentences)))
27 for i in range(len(sentences)):
28 for j in range(len(sentences)):
29 if i != j:
30 similarity_matrix[i][j] = sentence_similarity(sentences[i], sentences[j])
31 return similarity_matrix
32
33def apply_lexrank(similarity_matrix, damping=0.85, threshold=0.2, max_iter=100):
34 nx_graph = nx.from_numpy_array(similarity_matrix)
35 scores = nx.pagerank(nx_graph, alpha=damping, tol=threshold, max_iter=max_iter)
36 return scores
37
38def get_top_sentences(sentences, scores):
39 ranked_sentences = sorted(((scores[i], sentence) for i, sentence in enumerate(sentences)), reverse=True)
40 top_sentences = [sentence for score, sentence in ranked_sentences]
41 return top_sentences
42
43def extract_important_sentences(text):
44 preprocessed_sentences = preprocess_text(text)
45 similarity_matrix = build_similarity_matrix(preprocessed_sentences)
46 scores = apply_lexrank(similarity_matrix)
47 top_sentences = get_top_sentences(preprocessed_sentences, scores)
48 paragraph = ' '.join([' '.join(sentence) for sentence in top_sentences])
49 return paragraph
50
51def summarize(text, max_tokens):
52
53 peft_model = "Nevidu/LexBartLo_2"
54 config = PeftConfig.from_pretrained(peft_model)
55
56 # load base LLM model and tokenizer
57 model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)
58 tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
59
60 # Load the Lora model
61 model = PeftModel.from_pretrained(model, peft_model)
62
63 sorted_text = extract_important_sentences(text)
64
65 input_ids = tokenizer(sorted_text, return_tensors="pt", truncation=True).input_ids
66 # with torch.inference_mode():
67 outputs = model.generate(input_ids=input_ids, max_new_tokens=max_tokens, do_sample=True, top_p=0.9)
68 summary = tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0]
69 return summary
70
71text = """ Add your patent text"""
72max_tokens = 256
73
74summary = summarize(text, max_tokens)1@inproceedings{jayatilleke2025hybrid,
2 title={A Hybrid Architecture with Efficient Fine Tuning for Abstractive Patent Document Summarization},
3 author={Jayatilleke, Nevidu and Weerasinghe, Ruvan},
4 booktitle={2025 International Research Conference on Smart Computing and Systems Engineering (SCSE)},
5 pages={1--6},
6 year={2025},
7 organization={IEEE}
8}