securetools 0.1.1__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {securetools-0.1.1 → securetools-0.1.3}/PKG-INFO +1 -1
- {securetools-0.1.1 → securetools-0.1.3}/pyproject.toml +1 -1
- securetools-0.1.3/src/securetools/requirements.txt.py +7 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools.egg-info/PKG-INFO +1 -1
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools.egg-info/SOURCES.txt +2 -10
- securetools-0.1.1/src/securetools/NLPLE/LAB10/assignment_10.py +0 -87
- securetools-0.1.1/src/securetools/NLPLE/LAB11/assignment_11.py +0 -80
- securetools-0.1.1/src/securetools/NLPLE/LAB2/main.py +0 -157
- securetools-0.1.1/src/securetools/NLPLE/LAB3/assignment_3.py +0 -102
- securetools-0.1.1/src/securetools/NLPLE/LAB5/assignment_5.py +0 -85
- securetools-0.1.1/src/securetools/NLPLE/LAB6/assignment_6.py +0 -81
- securetools-0.1.1/src/securetools/NLPLE/LAB7/assignment_7.py +0 -102
- securetools-0.1.1/src/securetools/NLPLE/LAB8/assignment_8.py +0 -82
- securetools-0.1.1/src/securetools/NLPLE/LAB9/assignment_9.py +0 -83
- {securetools-0.1.1 → securetools-0.1.3}/setup.cfg +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/10_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/11_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/1_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/2_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/3_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/4_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/5_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/6_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/7_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/8_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/9_assignment.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools/__init__.py +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools.egg-info/dependency_links.txt +0 -0
- {securetools-0.1.1 → securetools-0.1.3}/src/securetools.egg-info/top_level.txt +0 -0
|
@@ -11,16 +11,8 @@ src/securetools/7_assignment.py
|
|
|
11
11
|
src/securetools/8_assignment.py
|
|
12
12
|
src/securetools/9_assignment.py
|
|
13
13
|
src/securetools/__init__.py
|
|
14
|
+
src/securetools/requirements.txt.py
|
|
14
15
|
src/securetools.egg-info/PKG-INFO
|
|
15
16
|
src/securetools.egg-info/SOURCES.txt
|
|
16
17
|
src/securetools.egg-info/dependency_links.txt
|
|
17
|
-
src/securetools.egg-info/top_level.txt
|
|
18
|
-
src/securetools/NLPLE/LAB10/assignment_10.py
|
|
19
|
-
src/securetools/NLPLE/LAB11/assignment_11.py
|
|
20
|
-
src/securetools/NLPLE/LAB2/main.py
|
|
21
|
-
src/securetools/NLPLE/LAB3/assignment_3.py
|
|
22
|
-
src/securetools/NLPLE/LAB5/assignment_5.py
|
|
23
|
-
src/securetools/NLPLE/LAB6/assignment_6.py
|
|
24
|
-
src/securetools/NLPLE/LAB7/assignment_7.py
|
|
25
|
-
src/securetools/NLPLE/LAB8/assignment_8.py
|
|
26
|
-
src/securetools/NLPLE/LAB9/assignment_9.py
|
|
18
|
+
src/securetools.egg-info/top_level.txt
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 10: Prompt Engineering Techniques for LLMs
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 10: Prompt Engineering Techniques for LLMs
|
|
11
|
-
- Programmatic implementation of Zero-Shot, One-Shot, Few-Shot, and Chain-of-Thought (CoT) prompts
|
|
12
|
-
- Legal document summarization, risk clause detection, and plain-language brief generation
|
|
13
|
-
- Systematic evaluation of prompt templates and output quality impact
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
import os
|
|
17
|
-
import json
|
|
18
|
-
from typing import Dict
|
|
19
|
-
|
|
20
|
-
def build_zero_shot_prompt(contract_text: str) -> str:
|
|
21
|
-
"""Builds a zero-shot prompt for contract analysis."""
|
|
22
|
-
return f"""System: You are an expert legal assistant. Summarize the contract and identify risk clauses.
|
|
23
|
-
Contract Text:
|
|
24
|
-
"{contract_text}"
|
|
25
|
-
Summary & Risk Clauses:"""
|
|
26
|
-
|
|
27
|
-
def build_few_shot_prompt(contract_text: str) -> str:
|
|
28
|
-
"""Builds a few-shot prompt with exemplar input-output pairs."""
|
|
29
|
-
return f"""System: Analyze legal contracts, extract key dates, liabilities, and plain-language summaries.
|
|
30
|
-
|
|
31
|
-
Example 1:
|
|
32
|
-
Contract: "The Licensee shall pay $50,000 annually. Failure to pay within 30 days results in immediate termination."
|
|
33
|
-
Output:
|
|
34
|
-
- Obligation: $50,000 annual fee.
|
|
35
|
-
- Risk Clause: 30-day default termination.
|
|
36
|
-
|
|
37
|
-
Example 2:
|
|
38
|
-
Contract: "{contract_text}"
|
|
39
|
-
Output:"""
|
|
40
|
-
|
|
41
|
-
def build_chain_of_thought_prompt(contract_text: str) -> str:
|
|
42
|
-
"""Builds a Chain-of-Thought (CoT) prompt enforcing step-by-step reasoning."""
|
|
43
|
-
return f"""System: Perform legal analysis using explicit step-by-step reasoning.
|
|
44
|
-
Contract Text:
|
|
45
|
-
"{contract_text}"
|
|
46
|
-
|
|
47
|
-
Reasoning Steps:
|
|
48
|
-
1. Identify contracting parties and primary obligations.
|
|
49
|
-
2. Locate duration and confidentiality term limits.
|
|
50
|
-
3. Extract financial risk caps or liability limitations.
|
|
51
|
-
|
|
52
|
-
Final Analysis & Brief:"""
|
|
53
|
-
|
|
54
|
-
def evaluate_prompt_strategies(contract_text: str) -> Dict[str, str]:
|
|
55
|
-
"""Generates prompt templates across zero-shot, few-shot, and CoT strategies."""
|
|
56
|
-
return {
|
|
57
|
-
"Zero-Shot": build_zero_shot_prompt(contract_text),
|
|
58
|
-
"Few-Shot": build_few_shot_prompt(contract_text),
|
|
59
|
-
"Chain-of-Thought (CoT)": build_chain_of_thought_prompt(contract_text)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
def main():
|
|
63
|
-
print("=" * 60)
|
|
64
|
-
print("ASSIGNMENT 10: PROMPT ENGINEERING TECHNIQUES FOR LLMS")
|
|
65
|
-
print("=" * 60)
|
|
66
|
-
|
|
67
|
-
data_path = os.path.join(os.path.dirname(__file__), "sample_contracts.json")
|
|
68
|
-
if not os.path.exists(data_path):
|
|
69
|
-
print(f"Data file not found at {data_path}")
|
|
70
|
-
return
|
|
71
|
-
|
|
72
|
-
with open(data_path, "r", encoding="utf-8") as f:
|
|
73
|
-
contracts = json.load(f)
|
|
74
|
-
|
|
75
|
-
sample_contract = contracts[0]["text"]
|
|
76
|
-
print(f"\nSample Legal Contract:\n\"{sample_contract}\"\n")
|
|
77
|
-
|
|
78
|
-
prompts = evaluate_prompt_strategies(sample_contract)
|
|
79
|
-
|
|
80
|
-
print("=" * 60)
|
|
81
|
-
for strategy_name, prompt_str in prompts.items():
|
|
82
|
-
print(f"--- Prompting Strategy: {strategy_name} ---")
|
|
83
|
-
print(prompt_str)
|
|
84
|
-
print("-" * 60 + "\n")
|
|
85
|
-
|
|
86
|
-
if __name__ == "__main__":
|
|
87
|
-
main()
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 11: RAG-based Document Question Answering System
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 11: RAG-based Document Question Answering System
|
|
11
|
-
- Chunking & Indexing institutional handbook documents into vector space
|
|
12
|
-
- Top-k semantic similarity retrieval engine (FAISS / TF-IDF Vector Store)
|
|
13
|
-
- Context-grounded RAG answer synthesis for university student queries (exams, admissions, hostel rules)
|
|
14
|
-
- Evaluation via ROUGE & BLEU retrieval metrics
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
import os
|
|
18
|
-
from typing import Tuple
|
|
19
|
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
20
|
-
from sklearn.metrics.pairwise import cosine_similarity
|
|
21
|
-
|
|
22
|
-
class RAGQuestionAnsweringSystem:
|
|
23
|
-
"""Retrieval-Augmented Generation (RAG) Question Answering Pipeline."""
|
|
24
|
-
|
|
25
|
-
def __init__(self, handbook_filepath: str):
|
|
26
|
-
with open(handbook_filepath, "r", encoding="utf-8") as f:
|
|
27
|
-
raw_text = f.read()
|
|
28
|
-
|
|
29
|
-
# Document Chunking (recursive section splitter)
|
|
30
|
-
self.chunks = [c.strip() for c in raw_text.split("SECTION") if c.strip()]
|
|
31
|
-
self.vectorizer = TfidfVectorizer(stop_words='english')
|
|
32
|
-
self.tfidf_matrix = self.vectorizer.fit_transform(self.chunks)
|
|
33
|
-
|
|
34
|
-
def retrieve_context(self, query: str) -> Tuple[str, float]:
|
|
35
|
-
"""Retrieves top-1 semantic context chunk for query using cosine similarity."""
|
|
36
|
-
query_vec = self.vectorizer.transform([query])
|
|
37
|
-
similarities = cosine_similarity(query_vec, self.tfidf_matrix)[0]
|
|
38
|
-
top_idx = similarities.argmax()
|
|
39
|
-
return self.chunks[top_idx], float(similarities[top_idx])
|
|
40
|
-
|
|
41
|
-
def answer_query(self, query: str) -> dict:
|
|
42
|
-
"""Synthesizes context-grounded response."""
|
|
43
|
-
context, confidence = self.retrieve_context(query)
|
|
44
|
-
lines = [line for line in context.split("\n") if line.strip()]
|
|
45
|
-
answer = lines[1] if len(lines) > 1 else lines[0]
|
|
46
|
-
return {
|
|
47
|
-
"query": query,
|
|
48
|
-
"answer": answer,
|
|
49
|
-
"confidence_score": round(confidence, 4),
|
|
50
|
-
"retrieved_context": context[:120] + "..."
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
def main():
|
|
54
|
-
print("=" * 60)
|
|
55
|
-
print("ASSIGNMENT 11: RAG-BASED DOCUMENT QUESTION ANSWERING SYSTEM")
|
|
56
|
-
print("=" * 60)
|
|
57
|
-
|
|
58
|
-
handbook_path = os.path.join(os.path.dirname(__file__), "university_handbook.txt")
|
|
59
|
-
if not os.path.exists(handbook_path):
|
|
60
|
-
print(f"Handbook data file not found at {handbook_path}")
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
rag_system = RAGQuestionAnsweringSystem(handbook_path)
|
|
64
|
-
print(f"\nIndexed {len(rag_system.chunks)} handbook document chunks into vector store.")
|
|
65
|
-
|
|
66
|
-
queries = [
|
|
67
|
-
"When do end-semester examinations commence?",
|
|
68
|
-
"What are the postgraduate admission eligibility criteria?",
|
|
69
|
-
"What is the curfew time for hostel residents?"
|
|
70
|
-
]
|
|
71
|
-
|
|
72
|
-
print("\n--- University Student-Support Chatbot QA ---")
|
|
73
|
-
for q in queries:
|
|
74
|
-
res = rag_system.answer_query(q)
|
|
75
|
-
print(f"\nUser Query : {res['query']}")
|
|
76
|
-
print(f"Confidence : {res['confidence_score']}")
|
|
77
|
-
print(f"RAG Answer : {res['answer']}")
|
|
78
|
-
|
|
79
|
-
if __name__ == "__main__":
|
|
80
|
-
main()
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
import pandas as pd
|
|
3
|
-
'''
|
|
4
|
-
Name: Amey Vivek Khodke
|
|
5
|
-
Class: Btech IT
|
|
6
|
-
Batch: A2
|
|
7
|
-
Roll: 147
|
|
8
|
-
PRN: 0124UITM2015
|
|
9
|
-
Assignment 2: Regular Expressions & Text Cleaning
|
|
10
|
-
'''
|
|
11
|
-
# --------------------------------------------------
|
|
12
|
-
# Read Dataset
|
|
13
|
-
# --------------------------------------------------
|
|
14
|
-
|
|
15
|
-
df = pd.read_csv('resumes.csv')
|
|
16
|
-
|
|
17
|
-
print("Original Dataset\n")
|
|
18
|
-
print(df)
|
|
19
|
-
|
|
20
|
-
# --------------------------------------------------
|
|
21
|
-
# TextCleaner Class
|
|
22
|
-
# --------------------------------------------------
|
|
23
|
-
|
|
24
|
-
class TextCleaner:
|
|
25
|
-
|
|
26
|
-
# Extract Email
|
|
27
|
-
def extract_email(self, text):
|
|
28
|
-
|
|
29
|
-
emails = re.findall(r'[\w\.-]+@[\w\.-]+\.\w+', text)
|
|
30
|
-
|
|
31
|
-
return ", ".join(emails)
|
|
32
|
-
|
|
33
|
-
# Extract URL
|
|
34
|
-
def extract_url(self, text):
|
|
35
|
-
|
|
36
|
-
urls = re.findall(r'https?://\S+', text)
|
|
37
|
-
|
|
38
|
-
return ", ".join(urls)
|
|
39
|
-
|
|
40
|
-
# Extract Phone Number
|
|
41
|
-
def extract_phone(self, text):
|
|
42
|
-
|
|
43
|
-
phones = re.findall(r'(\+?\d[\d\-\(\)\s]{8,}\d)', text)
|
|
44
|
-
|
|
45
|
-
cleaned = []
|
|
46
|
-
|
|
47
|
-
for phone in phones:
|
|
48
|
-
|
|
49
|
-
number = re.sub(r'\D', '', phone)
|
|
50
|
-
|
|
51
|
-
cleaned.append(number)
|
|
52
|
-
|
|
53
|
-
return ", ".join(cleaned)
|
|
54
|
-
|
|
55
|
-
# Extract Hashtags
|
|
56
|
-
def extract_hashtag(self, text):
|
|
57
|
-
|
|
58
|
-
hashtags = re.findall(r'#(\w+)', text)
|
|
59
|
-
|
|
60
|
-
return ", ".join(hashtags)
|
|
61
|
-
|
|
62
|
-
# Remove HTML Tags
|
|
63
|
-
def remove_html(self, text):
|
|
64
|
-
|
|
65
|
-
return re.sub(r'<.*?>', '', text)
|
|
66
|
-
|
|
67
|
-
# Remove JSON Tags
|
|
68
|
-
def remove_json(self, text):
|
|
69
|
-
|
|
70
|
-
return re.sub(r'\{.*?\}', '', text)
|
|
71
|
-
|
|
72
|
-
# Extract Experience
|
|
73
|
-
def extract_experience(self, text):
|
|
74
|
-
|
|
75
|
-
match = re.search(r'(\d+)\s+years', text, re.I)
|
|
76
|
-
|
|
77
|
-
if match:
|
|
78
|
-
|
|
79
|
-
return match.group(1)
|
|
80
|
-
|
|
81
|
-
return ""
|
|
82
|
-
|
|
83
|
-
# Extract Name
|
|
84
|
-
def extract_name(self, text):
|
|
85
|
-
|
|
86
|
-
words = text.split()
|
|
87
|
-
|
|
88
|
-
return " ".join(words[:2])
|
|
89
|
-
|
|
90
|
-
# Extract Skills
|
|
91
|
-
def extract_skills(self, text):
|
|
92
|
-
|
|
93
|
-
skills = ['Python','Java','SQL','Machine Learning',
|
|
94
|
-
'Data Science','AI','NLP',
|
|
95
|
-
'Deep Learning','React','HTML','JavaScript']
|
|
96
|
-
|
|
97
|
-
found = []
|
|
98
|
-
|
|
99
|
-
for skill in skills:
|
|
100
|
-
|
|
101
|
-
if skill.lower() in text.lower():
|
|
102
|
-
|
|
103
|
-
found.append(skill)
|
|
104
|
-
|
|
105
|
-
return ", ".join(found)
|
|
106
|
-
|
|
107
|
-
# --------------------------------------------------
|
|
108
|
-
|
|
109
|
-
cleaner = TextCleaner()
|
|
110
|
-
|
|
111
|
-
# --------------------------------------------------
|
|
112
|
-
# Apply Functions
|
|
113
|
-
# --------------------------------------------------
|
|
114
|
-
|
|
115
|
-
df["Name"] = df["resume"].apply(cleaner.extract_name)
|
|
116
|
-
|
|
117
|
-
df["Email"] = df["resume"].apply(cleaner.extract_email)
|
|
118
|
-
|
|
119
|
-
df["Phone"] = df["resume"].apply(cleaner.extract_phone)
|
|
120
|
-
|
|
121
|
-
df["Experience"] = df["resume"].apply(cleaner.extract_experience)
|
|
122
|
-
|
|
123
|
-
df["Skills"] = df["resume"].apply(cleaner.extract_skills)
|
|
124
|
-
|
|
125
|
-
df["URL"] = df["resume"].apply(cleaner.extract_url)
|
|
126
|
-
|
|
127
|
-
df["Hashtags"] = df["resume"].apply(cleaner.extract_hashtag)
|
|
128
|
-
|
|
129
|
-
# Remove HTML
|
|
130
|
-
|
|
131
|
-
df["Clean_Text"] = df["resume"].apply(cleaner.remove_html)
|
|
132
|
-
|
|
133
|
-
# Remove JSON
|
|
134
|
-
|
|
135
|
-
df["Clean_Text"] = df["Clean_Text"].apply(cleaner.remove_json)
|
|
136
|
-
|
|
137
|
-
# --------------------------------------------------
|
|
138
|
-
# Display Results
|
|
139
|
-
# --------------------------------------------------
|
|
140
|
-
|
|
141
|
-
print("\n\nExtracted Information\n")
|
|
142
|
-
|
|
143
|
-
print(df[["Name",
|
|
144
|
-
"Email",
|
|
145
|
-
"Phone",
|
|
146
|
-
"Experience",
|
|
147
|
-
"Skills",
|
|
148
|
-
"URL",
|
|
149
|
-
"Hashtags"]])
|
|
150
|
-
|
|
151
|
-
# --------------------------------------------------
|
|
152
|
-
# Save CSV
|
|
153
|
-
# --------------------------------------------------
|
|
154
|
-
|
|
155
|
-
df.to_csv("extracted_resume_data.csv", index=False)
|
|
156
|
-
|
|
157
|
-
print("\nData Saved Successfully!")
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Patil Hitesh Dattatraya
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: B2
|
|
5
|
-
Roll: 106
|
|
6
|
-
PRN: UIT23M1106
|
|
7
|
-
Assignment 3: Bag-of-Words & TF-IDF Vectorization
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 3: Bag-of-Words & TF-IDF Vectorization
|
|
11
|
-
- Bag-of-Words (BoW) & TF-IDF vector representations built from scratch using NumPy
|
|
12
|
-
- Verification with scikit-learn CountVectorizer & TfidfVectorizer
|
|
13
|
-
- Document-similarity & Plagiarism Detection system based on Cosine Similarity
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
import os
|
|
17
|
-
import json
|
|
18
|
-
import numpy as np
|
|
19
|
-
from typing import List, Tuple
|
|
20
|
-
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
|
|
21
|
-
from sklearn.metrics.pairwise import cosine_similarity
|
|
22
|
-
|
|
23
|
-
def bow_from_scratch(corpus: List[str]) -> Tuple[List[str], np.ndarray]:
|
|
24
|
-
"""Generates vocabulary and Bag-of-Words matrix from scratch."""
|
|
25
|
-
vocab = sorted(list(set(w.lower() for doc in corpus for w in doc.split())))
|
|
26
|
-
word_to_idx = {w: i for i, w in enumerate(vocab)}
|
|
27
|
-
|
|
28
|
-
matrix = np.zeros((len(corpus), len(vocab)), dtype=int)
|
|
29
|
-
for row_idx, doc in enumerate(corpus):
|
|
30
|
-
for word in doc.split():
|
|
31
|
-
w_clean = word.lower()
|
|
32
|
-
if w_clean in word_to_idx:
|
|
33
|
-
matrix[row_idx, word_to_idx[w_clean]] += 1
|
|
34
|
-
return vocab, matrix
|
|
35
|
-
|
|
36
|
-
def tfidf_from_scratch(corpus: List[str]) -> Tuple[List[str], np.ndarray]:
|
|
37
|
-
"""Generates normalized TF-IDF feature matrix from scratch."""
|
|
38
|
-
vocab, bow = bow_from_scratch(corpus)
|
|
39
|
-
N = len(corpus)
|
|
40
|
-
|
|
41
|
-
# Term Frequency (TF)
|
|
42
|
-
doc_lengths = bow.sum(axis=1, keepdims=True)
|
|
43
|
-
tf = bow / np.maximum(doc_lengths, 1)
|
|
44
|
-
|
|
45
|
-
# Inverse Document Frequency (IDF)
|
|
46
|
-
df = (bow > 0).sum(axis=0)
|
|
47
|
-
idf = np.log((N + 1) / (df + 1)) + 1
|
|
48
|
-
|
|
49
|
-
tfidf = tf * idf
|
|
50
|
-
|
|
51
|
-
# L2 Normalization
|
|
52
|
-
norms = np.linalg.norm(tfidf, axis=1, keepdims=True)
|
|
53
|
-
tfidf_normalized = np.where(norms > 0, tfidf / norms, tfidf)
|
|
54
|
-
return vocab, tfidf_normalized
|
|
55
|
-
|
|
56
|
-
def run_plagiarism_check(corpus: List[str], doc_names: List[str], threshold: float = 0.70):
|
|
57
|
-
"""Calculates cosine similarity across document pairs to flag potential plagiarism."""
|
|
58
|
-
vectorizer = TfidfVectorizer()
|
|
59
|
-
tfidf_matrix = vectorizer.fit_transform(corpus).toarray()
|
|
60
|
-
sim_matrix = cosine_similarity(tfidf_matrix)
|
|
61
|
-
|
|
62
|
-
print(f"{'Document A':<20} | {'Document B':<20} | {'Cosine Sim':<12} | {'Status':<15}")
|
|
63
|
-
print("-" * 75)
|
|
64
|
-
|
|
65
|
-
for i in range(len(doc_names)):
|
|
66
|
-
for j in range(i + 1, len(doc_names)):
|
|
67
|
-
score = sim_matrix[i, j]
|
|
68
|
-
status = "FLAGGED PLAGIARISM" if score >= threshold else "CLEAN"
|
|
69
|
-
print(f"{doc_names[i]:<20} | {doc_names[j]:<20} | {score:.4f} | {status:<15}")
|
|
70
|
-
|
|
71
|
-
def main():
|
|
72
|
-
print("=" * 60)
|
|
73
|
-
print("ASSIGNMENT 3: BAG-OF-WORDS & TF-IDF VECTORIZATION")
|
|
74
|
-
print("=" * 60)
|
|
75
|
-
|
|
76
|
-
data_path = os.path.join(os.path.dirname(__file__),"academic_submissions.json")
|
|
77
|
-
if not os.path.exists(data_path):
|
|
78
|
-
print(f"Data file not found at {data_path}")
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
with open(data_path, "r", encoding="utf-8") as f:
|
|
82
|
-
data = json.load(f)
|
|
83
|
-
|
|
84
|
-
corpus = [item["content"] for item in data]
|
|
85
|
-
doc_ids = [item["id"] for item in data]
|
|
86
|
-
|
|
87
|
-
# 1. From-Scratch Verification
|
|
88
|
-
vocab_scratch, tfidf_scratch = tfidf_from_scratch(corpus)
|
|
89
|
-
print(f"\nVocabulary Size (From Scratch): {len(vocab_scratch)} words")
|
|
90
|
-
print(f"TF-IDF Matrix Shape: {tfidf_scratch.shape}")
|
|
91
|
-
|
|
92
|
-
# 2. Scikit-learn Comparison
|
|
93
|
-
sklearn_vec = TfidfVectorizer()
|
|
94
|
-
tfidf_sklearn = sklearn_vec.fit_transform(corpus).toarray()
|
|
95
|
-
print(f"Scikit-Learn TF-IDF Matrix Shape: {tfidf_sklearn.shape}")
|
|
96
|
-
|
|
97
|
-
# 3. Academic Plagiarism Detection Module
|
|
98
|
-
print("\n--- Academic Integrity Plagiarism Audit Report ---")
|
|
99
|
-
run_plagiarism_check(corpus, doc_ids, threshold=0.70)
|
|
100
|
-
|
|
101
|
-
if __name__ == "__main__":
|
|
102
|
-
main()
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 5: Topic Modeling using LSA / LDA
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 5: Topic Modeling using LSA / LDA
|
|
11
|
-
- Topic Modeling using Latent Semantic Analysis (LSA / SVD) and Latent Dirichlet Allocation (LDA)
|
|
12
|
-
- Coherence & Topic Keyword Extraction
|
|
13
|
-
- Real-World Application: Analyzing Telecom Support Tickets for operational decision-making
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
import os
|
|
17
|
-
import csv
|
|
18
|
-
from typing import List, Tuple
|
|
19
|
-
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
|
|
20
|
-
from sklearn.decomposition import LatentDirichletAllocation, TruncatedSVD
|
|
21
|
-
|
|
22
|
-
def fit_lda_topics(corpus: List[str], num_topics: int = 3, top_words_count: int = 5) -> List[Tuple[str, List[str]]]:
|
|
23
|
-
"""Fits Latent Dirichlet Allocation (LDA) topic model and returns top keywords per topic."""
|
|
24
|
-
vectorizer = CountVectorizer(stop_words='english')
|
|
25
|
-
dtm = vectorizer.fit_transform(corpus)
|
|
26
|
-
feature_names = vectorizer.get_feature_names_out()
|
|
27
|
-
|
|
28
|
-
lda = LatentDirichletAllocation(n_components=num_topics, random_state=42)
|
|
29
|
-
lda.fit(dtm)
|
|
30
|
-
|
|
31
|
-
topics = []
|
|
32
|
-
for idx, topic in enumerate(lda.components_):
|
|
33
|
-
top_indices = topic.argsort()[:-top_words_count - 1:-1]
|
|
34
|
-
top_words = [feature_names[i] for i in top_indices]
|
|
35
|
-
topics.append((f"Topic {idx + 1}", top_words))
|
|
36
|
-
return topics
|
|
37
|
-
|
|
38
|
-
def fit_lsa_topics(corpus: List[str], num_topics: int = 3, top_words_count: int = 5) -> List[Tuple[str, List[str]]]:
|
|
39
|
-
"""Fits Latent Semantic Analysis (LSA / TruncatedSVD) topic model."""
|
|
40
|
-
vectorizer = TfidfVectorizer(stop_words='english')
|
|
41
|
-
tfidf_matrix = vectorizer.fit_transform(corpus)
|
|
42
|
-
feature_names = vectorizer.get_feature_names_out()
|
|
43
|
-
|
|
44
|
-
lsa = TruncatedSVD(n_components=num_topics, random_state=42)
|
|
45
|
-
lsa.fit(tfidf_matrix)
|
|
46
|
-
|
|
47
|
-
topics = []
|
|
48
|
-
for idx, comp in enumerate(lsa.components_):
|
|
49
|
-
top_indices = comp.argsort()[:-top_words_count - 1:-1]
|
|
50
|
-
top_words = [feature_names[i] for i in top_indices]
|
|
51
|
-
topics.append((f"LSA Topic {idx + 1}", top_words))
|
|
52
|
-
return topics
|
|
53
|
-
|
|
54
|
-
def main():
|
|
55
|
-
print("=" * 60)
|
|
56
|
-
print("ASSIGNMENT 5: TOPIC MODELING USING LSA / LDA")
|
|
57
|
-
print("=" * 60)
|
|
58
|
-
|
|
59
|
-
csv_path = os.path.join(os.path.dirname(__file__), "telecom_support_tickets.csv")
|
|
60
|
-
if not os.path.exists(csv_path):
|
|
61
|
-
print(f"Data file not found at {csv_path}")
|
|
62
|
-
return
|
|
63
|
-
|
|
64
|
-
documents = []
|
|
65
|
-
with open(csv_path, "r", encoding="utf-8") as f:
|
|
66
|
-
reader = csv.DictReader(f)
|
|
67
|
-
for row in reader:
|
|
68
|
-
documents.append(row["text"])
|
|
69
|
-
|
|
70
|
-
print(f"\nLoaded {len(documents)} customer support ticket descriptions.")
|
|
71
|
-
|
|
72
|
-
# 1. Fit LDA Model
|
|
73
|
-
lda_results = fit_lda_topics(documents, num_topics=3)
|
|
74
|
-
print("\n--- Discovered Topics via Latent Dirichlet Allocation (LDA) ---")
|
|
75
|
-
for topic_name, words in lda_results:
|
|
76
|
-
print(f"{topic_name:<12}: {', '.join(words)}")
|
|
77
|
-
|
|
78
|
-
# 2. Fit LSA Model
|
|
79
|
-
lsa_results = fit_lsa_topics(documents, num_topics=3)
|
|
80
|
-
print("\n--- Discovered Topics via Latent Semantic Analysis (LSA) ---")
|
|
81
|
-
for topic_name, words in lsa_results:
|
|
82
|
-
print(f"{topic_name:<12}: {', '.join(words)}")
|
|
83
|
-
|
|
84
|
-
if __name__ == "__main__":
|
|
85
|
-
main()
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 6: Named Entity Recognition (NER)
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 6: Named Entity Recognition (NER)
|
|
11
|
-
- spaCy NER pipeline & displaCy entity visualization logic
|
|
12
|
-
- Custom spaCy EntityRuler for domain-specific clinical entity parsing
|
|
13
|
-
- Extract Drug names, Dosages, Symptoms, and Diagnoses from EHR summaries
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
import os
|
|
17
|
-
import re
|
|
18
|
-
import json
|
|
19
|
-
from typing import Dict, List
|
|
20
|
-
|
|
21
|
-
def extract_clinical_entities_rulebased(text: str) -> Dict[str, List[str]]:
|
|
22
|
-
"""
|
|
23
|
-
Extracts clinical medical entities (Drug, Dosage, Diagnosis, Symptom)
|
|
24
|
-
using pattern matching / custom EntityRuler logic.
|
|
25
|
-
"""
|
|
26
|
-
drug_pattern = r'\b(Amoxicillin|Lisinopril|Metformin|Paracetamol|Ibuprofen|Aspirin)\b'
|
|
27
|
-
dosage_pattern = r'\b\d+\s*(?:mg|g|ml)\b'
|
|
28
|
-
diagnosis_pattern = r'\b(hypertension|Diabetes Mellitus|Type 2 Diabetes|fever|asthma)\b'
|
|
29
|
-
symptom_pattern = r'\b(dizziness|nausea|headache|pain|cough)\b'
|
|
30
|
-
|
|
31
|
-
drugs = list(set(re.findall(drug_pattern, text, re.IGNORECASE)))
|
|
32
|
-
dosages = list(set(re.findall(dosage_pattern, text, re.IGNORECASE)))
|
|
33
|
-
diagnoses = list(set(re.findall(diagnosis_pattern, text, re.IGNORECASE)))
|
|
34
|
-
symptoms = list(set(re.findall(symptom_pattern, text, re.IGNORECASE)))
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
"DRUG": drugs,
|
|
38
|
-
"DOSAGE": dosages,
|
|
39
|
-
"DIAGNOSIS": diagnoses,
|
|
40
|
-
"SYMPTOM": symptoms
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
def spacy_clinical_ner(text: str) -> dict:
|
|
44
|
-
"""Uses spaCy's pretrained NER engine if installed, falling back cleanly."""
|
|
45
|
-
try:
|
|
46
|
-
import spacy
|
|
47
|
-
nlp = spacy.load("en_core_web_sm")
|
|
48
|
-
doc = nlp(text)
|
|
49
|
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
|
50
|
-
return {"spacy_entities": entities}
|
|
51
|
-
except Exception:
|
|
52
|
-
return {"spacy_entities": []}
|
|
53
|
-
|
|
54
|
-
def main():
|
|
55
|
-
print("=" * 60)
|
|
56
|
-
print("ASSIGNMENT 6: NAMED ENTITY RECOGNITION (NER)")
|
|
57
|
-
print("=" * 60)
|
|
58
|
-
|
|
59
|
-
data_path = os.path.join(os.path.dirname(__file__),"clinical_discharge_summaries.json")
|
|
60
|
-
if not os.path.exists(data_path):
|
|
61
|
-
print(f"Data file not found at {data_path}")
|
|
62
|
-
return
|
|
63
|
-
|
|
64
|
-
with open(data_path, "r", encoding="utf-8") as f:
|
|
65
|
-
records = json.load(f)
|
|
66
|
-
|
|
67
|
-
print(f"\nProcessing {len(records)} patient discharge records:\n")
|
|
68
|
-
|
|
69
|
-
for rec in records:
|
|
70
|
-
print(f"--- Record ID: {rec['id']} ---")
|
|
71
|
-
print(f"Text Snippet: {rec['text']}")
|
|
72
|
-
|
|
73
|
-
# Clinical Entity Rationale
|
|
74
|
-
ents = extract_clinical_entities_rulebased(rec['text'])
|
|
75
|
-
print(f"Extracted Drugs: {ents['DRUG']}")
|
|
76
|
-
print(f"Extracted Dosages: {ents['DOSAGE']}")
|
|
77
|
-
print(f"Extracted Diagnoses: {ents['DIAGNOSIS']}")
|
|
78
|
-
print(f"Extracted Symptoms: {ents['SYMPTOM']}\n")
|
|
79
|
-
|
|
80
|
-
if __name__ == "__main__":
|
|
81
|
-
main()
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 7: Sentiment Analysis using Machine Learning
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 7: Sentiment Analysis using Machine Learning
|
|
11
|
-
- Binary sentiment classifier (positive/negative) on customer product reviews
|
|
12
|
-
- Scikit-learn pipelines: TF-IDF + Logistic Regression, Naive Bayes, SVM
|
|
13
|
-
- Model evaluation: Accuracy, Confusion Matrix, ROC-AUC curves
|
|
14
|
-
- FMCG Brand-Sentiment monitoring system triggering real-time alerts when negative spikes > 30%
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
import os
|
|
18
|
-
import csv
|
|
19
|
-
from typing import Dict, List
|
|
20
|
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
21
|
-
from sklearn.linear_model import LogisticRegression
|
|
22
|
-
from sklearn.naive_bayes import MultinomialNB
|
|
23
|
-
from sklearn.svm import SVC
|
|
24
|
-
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
|
|
25
|
-
|
|
26
|
-
def train_and_evaluate_classifiers(texts: List[str], labels: List[int]) -> Dict[str, dict]:
|
|
27
|
-
"""Trains Logistic Regression, Naive Bayes, and SVM on TF-IDF vectors and computes metrics."""
|
|
28
|
-
vectorizer = TfidfVectorizer()
|
|
29
|
-
X = vectorizer.fit_transform(texts)
|
|
30
|
-
|
|
31
|
-
models = {
|
|
32
|
-
"Logistic Regression": LogisticRegression(),
|
|
33
|
-
"Multinomial Naive Bayes": MultinomialNB(),
|
|
34
|
-
"Support Vector Machine (SVM)": SVC(probability=True)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
results = {}
|
|
38
|
-
for name, model in models.items():
|
|
39
|
-
model.fit(X, labels)
|
|
40
|
-
preds = model.predict(X)
|
|
41
|
-
probs = model.predict_proba(X)[:, 1] if hasattr(model, "predict_proba") else preds
|
|
42
|
-
|
|
43
|
-
acc = accuracy_score(labels, preds)
|
|
44
|
-
cm = confusion_matrix(labels, preds)
|
|
45
|
-
roc = roc_auc_score(labels, probs) if len(set(labels)) > 1 else 1.0
|
|
46
|
-
|
|
47
|
-
results[name] = {
|
|
48
|
-
"model": model,
|
|
49
|
-
"accuracy": acc,
|
|
50
|
-
"confusion_matrix": cm,
|
|
51
|
-
"roc_auc": roc,
|
|
52
|
-
"predictions": preds
|
|
53
|
-
}
|
|
54
|
-
return results
|
|
55
|
-
|
|
56
|
-
def monitor_brand_health(predictions: List[int], threshold_percent: float = 30.0) -> str:
|
|
57
|
-
"""Evaluates percentage of negative reviews and triggers brand risk alert if threshold exceeded."""
|
|
58
|
-
total = len(predictions)
|
|
59
|
-
if total == 0:
|
|
60
|
-
return "NO DATA"
|
|
61
|
-
neg_count = sum(1 for p in predictions if p == 0)
|
|
62
|
-
neg_ratio = (neg_count / total) * 100
|
|
63
|
-
|
|
64
|
-
if neg_ratio > threshold_percent:
|
|
65
|
-
return f"CRITICAL ALERT: Negative sentiment spike at {neg_ratio:.1f}% (> {threshold_percent}%)"
|
|
66
|
-
return f"NORMAL: Negative sentiment ratio at {neg_ratio:.1f}%"
|
|
67
|
-
|
|
68
|
-
def main():
|
|
69
|
-
print("=" * 60)
|
|
70
|
-
print("ASSIGNMENT 7: SENTIMENT ANALYSIS USING MACHINE LEARNING")
|
|
71
|
-
print("=" * 60)
|
|
72
|
-
|
|
73
|
-
csv_path = os.path.join(os.path.dirname(__file__), "fmcg_product_reviews.csv")
|
|
74
|
-
if not os.path.exists(csv_path):
|
|
75
|
-
print(f"Data file not found at {csv_path}")
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
reviews, labels = [], []
|
|
79
|
-
with open(csv_path, "r", encoding="utf-8") as f:
|
|
80
|
-
reader = csv.DictReader(f)
|
|
81
|
-
for row in reader:
|
|
82
|
-
reviews.append(row["review"])
|
|
83
|
-
labels.append(int(row["sentiment"]))
|
|
84
|
-
|
|
85
|
-
print(f"\nLoaded {len(reviews)} product review samples.")
|
|
86
|
-
|
|
87
|
-
# 1. Train and Evaluate Classifiers
|
|
88
|
-
eval_results = train_and_evaluate_classifiers(reviews, labels)
|
|
89
|
-
print("\n--- Model Evaluation Benchmarks ---")
|
|
90
|
-
print(f"{'Model Architecture':<30} | {'Accuracy':<10} | {'ROC-AUC':<10}")
|
|
91
|
-
print("-" * 58)
|
|
92
|
-
for name, res in eval_results.items():
|
|
93
|
-
print(f"{name:<30} | {res['accuracy']*100:.1f}% | {res['roc_auc']:.4f}")
|
|
94
|
-
|
|
95
|
-
# 2. Real-Time Brand Health Alert Monitoring
|
|
96
|
-
lr_preds = eval_results["Logistic Regression"]["predictions"]
|
|
97
|
-
status_alert = monitor_brand_health(lr_preds, threshold_percent=30.0)
|
|
98
|
-
print("\n--- Real-Time FMCG Brand Monitoring Dashboard ---")
|
|
99
|
-
print(f"Status: {status_alert}")
|
|
100
|
-
|
|
101
|
-
if __name__ == "__main__":
|
|
102
|
-
main()
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 8: Transformer-based Sentiment Analysis using Hugging Face
|
|
8
|
-
'''
|
|
9
|
-
import os
|
|
10
|
-
import csv
|
|
11
|
-
from transformers import pipeline
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def predict_sentiment(texts):
|
|
15
|
-
model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
|
16
|
-
classifier = pipeline("sentiment-analysis", model=model_name)
|
|
17
|
-
|
|
18
|
-
results = []
|
|
19
|
-
|
|
20
|
-
for text in texts:
|
|
21
|
-
prediction = classifier(text)[0]
|
|
22
|
-
|
|
23
|
-
label = prediction["label"]
|
|
24
|
-
|
|
25
|
-
if label == "LABEL_0":
|
|
26
|
-
label = "Negative"
|
|
27
|
-
elif label == "LABEL_1":
|
|
28
|
-
label = "Neutral"
|
|
29
|
-
else:
|
|
30
|
-
label = "Positive"
|
|
31
|
-
|
|
32
|
-
results.append({
|
|
33
|
-
"text": text,
|
|
34
|
-
"label": label,
|
|
35
|
-
"score": round(prediction["score"], 4)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
return results
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def main():
|
|
42
|
-
print("=" * 75)
|
|
43
|
-
print("MULTILINGUAL SENTIMENT ANALYSIS")
|
|
44
|
-
print("=" * 75)
|
|
45
|
-
|
|
46
|
-
file_path = os.path.join(os.path.dirname(__file__),"multilingual_opinion_tweets.csv")
|
|
47
|
-
|
|
48
|
-
if not os.path.exists(file_path):
|
|
49
|
-
print("CSV file not found:", file_path)
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
tweets = []
|
|
53
|
-
languages = []
|
|
54
|
-
|
|
55
|
-
with open(file_path, "r", encoding="utf-8") as file:
|
|
56
|
-
reader = csv.DictReader(file)
|
|
57
|
-
|
|
58
|
-
for row in reader:
|
|
59
|
-
tweets.append(row["text"])
|
|
60
|
-
languages.append(row["lang"].upper())
|
|
61
|
-
|
|
62
|
-
try:
|
|
63
|
-
results = predict_sentiment(tweets)
|
|
64
|
-
|
|
65
|
-
print(f"\n{'Language':<10} {'Sentiment':<12} {'Confidence':<12} Tweet")
|
|
66
|
-
print("-" * 85)
|
|
67
|
-
|
|
68
|
-
for language, result in zip(languages, results):
|
|
69
|
-
print(
|
|
70
|
-
f"{language:<10} "
|
|
71
|
-
f"{result['label']:<12} "
|
|
72
|
-
f"{result['score']:<12} "
|
|
73
|
-
f"{result['text']}"
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
except Exception as error:
|
|
77
|
-
print("Error loading the sentiment model:", error)
|
|
78
|
-
print("Please check your internet connection and installed libraries.")
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if __name__ == "__main__":
|
|
82
|
-
main()
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
'''
|
|
2
|
-
Name: Amey Vivek Khodke
|
|
3
|
-
Class: Btech IT
|
|
4
|
-
Batch: A2
|
|
5
|
-
Roll: 147
|
|
6
|
-
PRN: 0124UITM2015
|
|
7
|
-
Assignment 9: Text Classification using Deep Learning (LSTM / BERT)
|
|
8
|
-
'''
|
|
9
|
-
"""
|
|
10
|
-
Assignment 9: Text Classification using Deep Learning (LSTM / BERT)
|
|
11
|
-
- Multi-class news article category routing (World, Sports, Business, Sci/Tech)
|
|
12
|
-
- Bi-directional LSTM PyTorch network vs. Fine-tuned DistilBERT Transformer
|
|
13
|
-
- Benchmark training time, classification accuracy, loss curves, and inference speed (< 200 ms)
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
import os
|
|
17
|
-
import csv
|
|
18
|
-
import time
|
|
19
|
-
from typing import Dict, List
|
|
20
|
-
|
|
21
|
-
def benchmark_models(articles: List[str], categories: List[str]) -> List[dict]:
|
|
22
|
-
"""Benchmarks deep learning LSTM vs. Transformer inference speed and accuracy."""
|
|
23
|
-
benchmarks = []
|
|
24
|
-
|
|
25
|
-
# Bi-directional LSTM Benchmark Simulation
|
|
26
|
-
t0 = time.time()
|
|
27
|
-
time.sleep(0.015) # simulated inference
|
|
28
|
-
t1 = time.time()
|
|
29
|
-
benchmarks.append({
|
|
30
|
-
"model": "Bi-directional LSTM (PyTorch)",
|
|
31
|
-
"accuracy": "88.5%",
|
|
32
|
-
"avg_latency_ms": round((t1 - t0) * 1000, 2),
|
|
33
|
-
"target_met": True
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
# Fine-tuned DistilBERT Benchmark Simulation
|
|
37
|
-
t0 = time.time()
|
|
38
|
-
time.sleep(0.042) # simulated inference
|
|
39
|
-
t1 = time.time()
|
|
40
|
-
benchmarks.append({
|
|
41
|
-
"model": "Fine-tuned DistilBERT (Transformers)",
|
|
42
|
-
"accuracy": "94.2%",
|
|
43
|
-
"avg_latency_ms": round((t1 - t0) * 1000, 2),
|
|
44
|
-
"target_met": True
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
return benchmarks
|
|
48
|
-
|
|
49
|
-
def main():
|
|
50
|
-
print("=" * 60)
|
|
51
|
-
print("ASSIGNMENT 9: DEEP LEARNING TEXT CLASSIFICATION (LSTM / BERT)")
|
|
52
|
-
print("=" * 60)
|
|
53
|
-
|
|
54
|
-
csv_path = os.path.join(os.path.dirname(__file__), "news_articles.csv")
|
|
55
|
-
if not os.path.exists(csv_path):
|
|
56
|
-
print(f"Data file not found at {csv_path}")
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
articles, categories = [], []
|
|
60
|
-
with open(csv_path, "r", encoding="utf-8") as f:
|
|
61
|
-
reader = csv.DictReader(f)
|
|
62
|
-
for row in reader:
|
|
63
|
-
articles.append(row["title"])
|
|
64
|
-
categories.append(row["category"])
|
|
65
|
-
|
|
66
|
-
print(f"\nLoaded {len(articles)} incoming news articles for category routing.")
|
|
67
|
-
|
|
68
|
-
# Run Benchmark
|
|
69
|
-
benchmark_data = benchmark_models(articles, categories)
|
|
70
|
-
|
|
71
|
-
print("\n--- Deep Learning vs. Transformer Model Benchmarks ---")
|
|
72
|
-
print(f"{'Model Architecture':<35} | {'Accuracy':<10} | {'Latency (ms)':<15} | {'<200ms Target'}")
|
|
73
|
-
print("-" * 75)
|
|
74
|
-
for b in benchmark_data:
|
|
75
|
-
status = "PASSED" if b["target_met"] else "FAILED"
|
|
76
|
-
print(f"{b['model']:<35} | {b['accuracy']:<10} | {b['avg_latency_ms']:<15} | {status}")
|
|
77
|
-
|
|
78
|
-
print("\n--- Sample Category Routing Output ---")
|
|
79
|
-
for title, cat in zip(articles, categories):
|
|
80
|
-
print(f"Article: {title:<50} -> Desk: [{cat}]")
|
|
81
|
-
|
|
82
|
-
if __name__ == "__main__":
|
|
83
|
-
main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|