Spaces:
Sleeping
Sleeping
glitz-dev
commited on
Commit
·
24d708d
0
Parent(s):
Initial commit to HF
Browse files- .gitignore +52 -0
- ReadMe.md +27 -0
- hipaathesis.py +734 -0
- requirements.txt +12 -0
- thesis.pdf +0 -0
- thesis.py +626 -0
.gitignore
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```
|
| 2 |
+
# Byte-compiled / cache files
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.py[cod]
|
| 5 |
+
*.pyo
|
| 6 |
+
*.pyd
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Virtual environment
|
| 10 |
+
venv/
|
| 11 |
+
.env/
|
| 12 |
+
env/
|
| 13 |
+
.venv/
|
| 14 |
+
|
| 15 |
+
# VS Code settings
|
| 16 |
+
.vscode/
|
| 17 |
+
|
| 18 |
+
# Environment / secrets
|
| 19 |
+
*.env
|
| 20 |
+
.env.*
|
| 21 |
+
|
| 22 |
+
# Distribution / packaging
|
| 23 |
+
*.egg
|
| 24 |
+
*.egg-info/
|
| 25 |
+
dist/
|
| 26 |
+
build/
|
| 27 |
+
.eggs/
|
| 28 |
+
|
| 29 |
+
# Logs
|
| 30 |
+
*.log
|
| 31 |
+
*.out
|
| 32 |
+
*.err
|
| 33 |
+
*.enc
|
| 34 |
+
|
| 35 |
+
# Testing
|
| 36 |
+
.coverage
|
| 37 |
+
.tox/
|
| 38 |
+
nosetests.xml
|
| 39 |
+
coverage.xml
|
| 40 |
+
htmlcov/
|
| 41 |
+
|
| 42 |
+
# Jupyter notebooks
|
| 43 |
+
.ipynb_checkpoints
|
| 44 |
+
|
| 45 |
+
# OS files
|
| 46 |
+
.DS_Store
|
| 47 |
+
Thumbs.db
|
| 48 |
+
|
| 49 |
+
# Uvicorn / FastAPI specific
|
| 50 |
+
*.sqlite3
|
| 51 |
+
# Files
|
| 52 |
+
/thesis_1.pdf
|
ReadMe.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Adding to Huggingface
|
| 2 |
+
|
| 3 |
+
1. Check Current status -> git remote -v
|
| 4 |
+
2. To remove an exisitng HF Repo -> git remote remove hf
|
| 5 |
+
3. To add Files to HF Repo -> git remote add hf https://huggingface.co/spaces/Username/SpaceName
|
| 6 |
+
4. Then commit using -> git push hf main OR git push hf main --force
|
| 7 |
+
|
| 8 |
+
#### Not permitting to add files to HF, generate new token with write permission
|
| 9 |
+
|
| 10 |
+
1. Go to https://huggingface.co/settings/tokens
|
| 11 |
+
2. Click **New token**
|
| 12 |
+
3. Name it something like: hf-cli
|
| 13 |
+
4. Set **Role = Write**
|
| 14 |
+
5. Copy the generated token.
|
| 15 |
+
6. logout and login in hf,
|
| 16 |
+
- huggingface-cli logout (deprecated) / hf auth logout
|
| 17 |
+
- huggingface-cli login (deprecated) / hf auth login
|
| 18 |
+
- paste key + enter / $env:HF_TOKEN = "token-no"
|
| 19 |
+
7. confirm identity
|
| 20 |
+
- huggingface-cli whoami (deprecated)/ hf auth whoami
|
| 21 |
+
8. try to push again
|
| 22 |
+
- git push hf main --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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import PyPDF2
|
| 2 |
+
import re
|
| 3 |
+
from collections import Counter
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.stem import WordNetLemmatizer
|
| 8 |
+
import string
|
| 9 |
+
from datetime import datetime, timedelta
|
| 10 |
+
import json
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer, pipeline, BlipProcessor, BlipForConditionalGeneration
|
| 13 |
+
import warnings
|
| 14 |
+
import fitz # PyMuPDF
|
| 15 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 16 |
+
import io
|
| 17 |
+
import base64
|
| 18 |
+
import os
|
| 19 |
+
import pytesseract
|
| 20 |
+
import hashlib
|
| 21 |
+
import logging
|
| 22 |
+
import getpass
|
| 23 |
+
import tempfile
|
| 24 |
+
import shutil
|
| 25 |
+
from fastapi import FastAPI
|
| 26 |
+
from fastapi.staticfiles import StaticFiles
|
| 27 |
+
from pydantic import BaseModel
|
| 28 |
+
from typing import List, Dict, Any, Optional
|
| 29 |
+
|
| 30 |
+
from cryptography.fernet import Fernet
|
| 31 |
+
from cryptography.hazmat.primitives import hashes
|
| 32 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
| 33 |
+
try:
|
| 34 |
+
import cv2
|
| 35 |
+
import numpy as np
|
| 36 |
+
OPENCV_AVAILABLE = True
|
| 37 |
+
except ImportError:
|
| 38 |
+
print("OpenCV not available. Using PIL for image preprocessing.")
|
| 39 |
+
OPENCV_AVAILABLE = False
|
| 40 |
+
import numpy as np
|
| 41 |
+
|
| 42 |
+
warnings.filterwarnings('ignore')
|
| 43 |
+
|
| 44 |
+
app = FastAPI(title='AI (PDF→Summary+QnA+Scores)', version='0.2.1')
|
| 45 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 46 |
+
|
| 47 |
+
class HIPAALogger:
|
| 48 |
+
"""HIPAA-compliant audit logging system"""
|
| 49 |
+
|
| 50 |
+
def __init__(self, log_file="hipaa_audit.log"):
|
| 51 |
+
self.log_file = log_file
|
| 52 |
+
self.setup_logging()
|
| 53 |
+
|
| 54 |
+
def setup_logging(self):
|
| 55 |
+
"""Setup secure audit logging"""
|
| 56 |
+
logging.basicConfig(
|
| 57 |
+
filename=self.log_file,
|
| 58 |
+
level=logging.INFO,
|
| 59 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 60 |
+
datefmt='%Y-%m-%d %H:%M:%S'
|
| 61 |
+
)
|
| 62 |
+
self.logger = logging.getLogger('HIPAA_AUDIT')
|
| 63 |
+
|
| 64 |
+
def log_access(self, user_id, action, resource, success=True):
|
| 65 |
+
"""Log access attempts and actions"""
|
| 66 |
+
status = "SUCCESS" if success else "FAILURE"
|
| 67 |
+
message = f"USER:{user_id} ACTION:{action} RESOURCE:{resource} STATUS:{status}"
|
| 68 |
+
self.logger.info(message)
|
| 69 |
+
|
| 70 |
+
def log_phi_processing(self, user_id, document_hash, action):
|
| 71 |
+
"""Log PHI processing events"""
|
| 72 |
+
message = f"PHI_PROCESSING USER:{user_id} DOC_HASH:{document_hash} ACTION:{action}"
|
| 73 |
+
self.logger.info(message)
|
| 74 |
+
|
| 75 |
+
class SecureFileHandler:
|
| 76 |
+
"""Secure file handling with encryption and secure deletion"""
|
| 77 |
+
|
| 78 |
+
def __init__(self, password=None):
|
| 79 |
+
self.password = password
|
| 80 |
+
self.key = self._derive_key(password) if password else None
|
| 81 |
+
self.fernet = Fernet(self.key) if self.key else None
|
| 82 |
+
|
| 83 |
+
def _derive_key(self, password):
|
| 84 |
+
"""Derive encryption key from password"""
|
| 85 |
+
password_bytes = password.encode()
|
| 86 |
+
kdf = PBKDF2HMAC(
|
| 87 |
+
algorithm=hashes.SHA256(),
|
| 88 |
+
length=32,
|
| 89 |
+
salt=b'hipaa_thesis_analyzer_salt',
|
| 90 |
+
iterations=100000,
|
| 91 |
+
)
|
| 92 |
+
key = base64.urlsafe_b64encode(kdf.derive(password_bytes))
|
| 93 |
+
return key
|
| 94 |
+
|
| 95 |
+
def encrypt_data(self, data):
|
| 96 |
+
"""Encrypt sensitive data"""
|
| 97 |
+
if not self.fernet:
|
| 98 |
+
return data
|
| 99 |
+
|
| 100 |
+
if isinstance(data, str):
|
| 101 |
+
data = data.encode()
|
| 102 |
+
return self.fernet.encrypt(data)
|
| 103 |
+
|
| 104 |
+
def decrypt_data(self, encrypted_data):
|
| 105 |
+
"""Decrypt sensitive data"""
|
| 106 |
+
if not self.fernet:
|
| 107 |
+
return encrypted_data
|
| 108 |
+
|
| 109 |
+
decrypted = self.fernet.decrypt(encrypted_data)
|
| 110 |
+
return decrypted.decode()
|
| 111 |
+
|
| 112 |
+
def secure_save(self, data, filepath):
|
| 113 |
+
"""Save data with encryption"""
|
| 114 |
+
if self.fernet:
|
| 115 |
+
encrypted_data = self.encrypt_data(json.dumps(data))
|
| 116 |
+
with open(filepath + '.enc', 'wb') as f:
|
| 117 |
+
f.write(encrypted_data)
|
| 118 |
+
else:
|
| 119 |
+
with open(filepath, 'w', encoding='utf-8') as f:
|
| 120 |
+
json.dump(data, f, indent=2)
|
| 121 |
+
|
| 122 |
+
def secure_load(self, filepath):
|
| 123 |
+
"""Load encrypted data"""
|
| 124 |
+
if self.fernet and os.path.exists(filepath + '.enc'):
|
| 125 |
+
with open(filepath + '.enc', 'rb') as f:
|
| 126 |
+
encrypted_data = f.read()
|
| 127 |
+
decrypted_data = self.decrypt_data(encrypted_data)
|
| 128 |
+
return json.loads(decrypted_data)
|
| 129 |
+
elif os.path.exists(filepath):
|
| 130 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 131 |
+
return json.load(f)
|
| 132 |
+
return None
|
| 133 |
+
|
| 134 |
+
def secure_delete(self, filepath):
|
| 135 |
+
"""Securely delete files by overwriting"""
|
| 136 |
+
if os.path.exists(filepath):
|
| 137 |
+
# Overwrite file multiple times before deletion
|
| 138 |
+
file_size = os.path.getsize(filepath)
|
| 139 |
+
with open(filepath, 'rb+') as f:
|
| 140 |
+
for _ in range(3): # DoD 5220.22-M standard
|
| 141 |
+
f.seek(0)
|
| 142 |
+
f.write(os.urandom(file_size))
|
| 143 |
+
f.flush()
|
| 144 |
+
os.remove(filepath)
|
| 145 |
+
|
| 146 |
+
# Also check for encrypted version
|
| 147 |
+
if os.path.exists(filepath + '.enc'):
|
| 148 |
+
file_size = os.path.getsize(filepath + '.enc')
|
| 149 |
+
with open(filepath + '.enc', 'rb+') as f:
|
| 150 |
+
for _ in range(3):
|
| 151 |
+
f.seek(0)
|
| 152 |
+
f.write(os.urandom(file_size))
|
| 153 |
+
f.flush()
|
| 154 |
+
os.remove(filepath + '.enc')
|
| 155 |
+
|
| 156 |
+
class HIPAACompliantThesisAnalyzer:
|
| 157 |
+
"""HIPAA-compliant version of the thesis analyzer"""
|
| 158 |
+
|
| 159 |
+
def __init__(self, user_id=None, password=None, session_timeout=30):
|
| 160 |
+
self.user_id = user_id or getpass.getuser()
|
| 161 |
+
self.session_timeout = session_timeout # minutes
|
| 162 |
+
self.session_start = datetime.now()
|
| 163 |
+
self.last_activity = datetime.now()
|
| 164 |
+
|
| 165 |
+
# Initialize HIPAA compliance components
|
| 166 |
+
self.hipaa_logger = HIPAALogger()
|
| 167 |
+
self.secure_handler = SecureFileHandler(password)
|
| 168 |
+
|
| 169 |
+
# Log session start
|
| 170 |
+
self.hipaa_logger.log_access(self.user_id, "SESSION_START", "THESIS_ANALYZER")
|
| 171 |
+
|
| 172 |
+
# Initialize base analyzer components
|
| 173 |
+
self._initialize_analyzer()
|
| 174 |
+
|
| 175 |
+
print(f"HIPAA-Compliant Thesis Analyzer initialized for user: {self.user_id}")
|
| 176 |
+
print(f"Session timeout: {session_timeout} minutes")
|
| 177 |
+
print(f"Encryption enabled: {'Yes' if password else 'No'}")
|
| 178 |
+
|
| 179 |
+
def _initialize_analyzer(self):
|
| 180 |
+
"""Initialize the core analyzer components"""
|
| 181 |
+
try:
|
| 182 |
+
self.lemmatizer = WordNetLemmatizer()
|
| 183 |
+
self.stop_words = set(stopwords.words('english'))
|
| 184 |
+
except LookupError as e:
|
| 185 |
+
print(f"NLTK resource error: {e}")
|
| 186 |
+
self._download_nltk_resources()
|
| 187 |
+
self.lemmatizer = WordNetLemmatizer()
|
| 188 |
+
self.stop_words = set(stopwords.words('english'))
|
| 189 |
+
|
| 190 |
+
self.thesis_text = ""
|
| 191 |
+
self.sentences = []
|
| 192 |
+
self.key_terms = []
|
| 193 |
+
self.extracted_images = []
|
| 194 |
+
self.image_descriptions = []
|
| 195 |
+
self.ocr_results = []
|
| 196 |
+
self.use_ocr = True
|
| 197 |
+
self.use_blip = True
|
| 198 |
+
|
| 199 |
+
# Initialize T5 model
|
| 200 |
+
print("Loading T5-small model (HIPAA-compliant local processing)...")
|
| 201 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 202 |
+
|
| 203 |
+
self.model_name = "t5-small"
|
| 204 |
+
self.tokenizer = T5Tokenizer.from_pretrained(self.model_name)
|
| 205 |
+
self.model = T5ForConditionalGeneration.from_pretrained(self.model_name)
|
| 206 |
+
self.model.to(self.device)
|
| 207 |
+
|
| 208 |
+
# Initialize pipelines
|
| 209 |
+
self.summarizer = pipeline(
|
| 210 |
+
"summarization",
|
| 211 |
+
model=self.model_name,
|
| 212 |
+
tokenizer=self.model_name,
|
| 213 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 214 |
+
max_length=200,
|
| 215 |
+
min_length=150,
|
| 216 |
+
do_sample=True,
|
| 217 |
+
temperature=0.7
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
self.qa_pipeline = pipeline(
|
| 221 |
+
"text2text-generation",
|
| 222 |
+
model=self.model_name,
|
| 223 |
+
tokenizer=self.model_name,
|
| 224 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 225 |
+
max_length=512,
|
| 226 |
+
do_sample=True,
|
| 227 |
+
temperature=0.7
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Initialize BLIP if enabled
|
| 231 |
+
if self.use_blip:
|
| 232 |
+
try:
|
| 233 |
+
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 234 |
+
self.blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 235 |
+
self.blip_model.to(self.device)
|
| 236 |
+
print("BLIP model loaded for local image analysis")
|
| 237 |
+
except Exception as e:
|
| 238 |
+
print(f"BLIP model loading failed: {e}")
|
| 239 |
+
self.use_blip = False
|
| 240 |
+
|
| 241 |
+
# Check OCR availability
|
| 242 |
+
if self.use_ocr:
|
| 243 |
+
try:
|
| 244 |
+
pytesseract.get_tesseract_version()
|
| 245 |
+
print("Tesseract OCR available for local processing")
|
| 246 |
+
except Exception as e:
|
| 247 |
+
print(f"Tesseract OCR not available: {e}")
|
| 248 |
+
self.use_ocr = False
|
| 249 |
+
|
| 250 |
+
def _download_nltk_resources(self):
|
| 251 |
+
"""Download required NLTK resources"""
|
| 252 |
+
resources = [
|
| 253 |
+
('tokenizers/punkt', 'punkt'),
|
| 254 |
+
('tokenizers/punkt_tab', 'punkt_tab'),
|
| 255 |
+
('corpora/stopwords', 'stopwords'),
|
| 256 |
+
('corpora/wordnet', 'wordnet'),
|
| 257 |
+
('corpora/omw-1.4', 'omw-1.4')
|
| 258 |
+
]
|
| 259 |
+
|
| 260 |
+
for resource_path, resource_name in resources:
|
| 261 |
+
try:
|
| 262 |
+
nltk.data.find(resource_path)
|
| 263 |
+
except LookupError:
|
| 264 |
+
try:
|
| 265 |
+
nltk.download(resource_name, quiet=True)
|
| 266 |
+
except Exception as e:
|
| 267 |
+
print(f"Warning: Failed to download {resource_name}: {e}")
|
| 268 |
+
|
| 269 |
+
def check_session_timeout(self):
|
| 270 |
+
"""Check if session has timed out"""
|
| 271 |
+
time_since_start = datetime.now() - self.session_start
|
| 272 |
+
time_since_activity = datetime.now() - self.last_activity
|
| 273 |
+
|
| 274 |
+
if time_since_activity.total_seconds() > (self.session_timeout * 60):
|
| 275 |
+
self.hipaa_logger.log_access(self.user_id, "SESSION_TIMEOUT", "THESIS_ANALYZER")
|
| 276 |
+
raise Exception("Session timed out due to inactivity. Please restart for security.")
|
| 277 |
+
|
| 278 |
+
self.last_activity = datetime.now()
|
| 279 |
+
|
| 280 |
+
def calculate_document_hash(self, content):
|
| 281 |
+
"""Calculate secure hash of document content"""
|
| 282 |
+
return hashlib.sha256(content.encode()).hexdigest()
|
| 283 |
+
|
| 284 |
+
def process_document_securely(self, pdf_path, questions, output_file=None):
|
| 285 |
+
"""Process document with full HIPAA compliance"""
|
| 286 |
+
self.check_session_timeout()
|
| 287 |
+
|
| 288 |
+
# Calculate document hash for audit trail
|
| 289 |
+
with open(pdf_path, 'rb') as f:
|
| 290 |
+
doc_content = f.read()
|
| 291 |
+
doc_hash = hashlib.sha256(doc_content).hexdigest()[:16]
|
| 292 |
+
|
| 293 |
+
self.hipaa_logger.log_phi_processing(self.user_id, doc_hash, "DOCUMENT_LOAD")
|
| 294 |
+
|
| 295 |
+
try:
|
| 296 |
+
# Extract text and images
|
| 297 |
+
text, images = self._extract_text_and_images(pdf_path)
|
| 298 |
+
self.hipaa_logger.log_phi_processing(self.user_id, doc_hash, "TEXT_EXTRACTION")
|
| 299 |
+
|
| 300 |
+
# Perform OCR if enabled
|
| 301 |
+
ocr_results = []
|
| 302 |
+
if self.use_ocr and images:
|
| 303 |
+
ocr_results = self._perform_secure_ocr(images)
|
| 304 |
+
self.hipaa_logger.log_phi_processing(self.user_id, doc_hash, "OCR_PROCESSING")
|
| 305 |
+
|
| 306 |
+
# Analyze images if BLIP enabled
|
| 307 |
+
image_descriptions = []
|
| 308 |
+
if self.use_blip and images:
|
| 309 |
+
image_descriptions = self._analyze_images_securely(images)
|
| 310 |
+
self.hipaa_logger.log_phi_processing(self.user_id, doc_hash, "IMAGE_ANALYSIS")
|
| 311 |
+
|
| 312 |
+
# Combine all text
|
| 313 |
+
ocr_text = " ".join([result['ocr_text'] for result in ocr_results if result.get('ocr_text')])
|
| 314 |
+
combined_text = text + " " + ocr_text
|
| 315 |
+
|
| 316 |
+
# Generate analysis
|
| 317 |
+
sections = self._extract_key_sections(combined_text)
|
| 318 |
+
key_terms = self._extract_key_terms(combined_text)
|
| 319 |
+
summary = self._generate_summary_secure(combined_text)
|
| 320 |
+
question_answers = self._answer_questions_secure(questions, combined_text)
|
| 321 |
+
|
| 322 |
+
self.hipaa_logger.log_phi_processing(self.user_id, doc_hash, "ANALYSIS_COMPLETE")
|
| 323 |
+
|
| 324 |
+
# Compile HIPAA-compliant report
|
| 325 |
+
report = {
|
| 326 |
+
"hipaa_compliance": {
|
| 327 |
+
"processed_locally": True,
|
| 328 |
+
"encrypted_storage": bool(self.secure_handler.fernet),
|
| 329 |
+
"audit_logged": True,
|
| 330 |
+
"user_id": self.user_id,
|
| 331 |
+
"session_id": hashlib.md5(f"{self.user_id}{self.session_start}".encode()).hexdigest()[:8],
|
| 332 |
+
"document_hash": doc_hash,
|
| 333 |
+
"processing_timestamp": datetime.now().isoformat(),
|
| 334 |
+
"no_external_apis": True,
|
| 335 |
+
"local_processing_only": True
|
| 336 |
+
},
|
| 337 |
+
"document_info": {
|
| 338 |
+
"file_path": os.path.basename(pdf_path), # Only filename for privacy
|
| 339 |
+
"analysis_timestamp": datetime.now().isoformat(),
|
| 340 |
+
"total_characters": len(text),
|
| 341 |
+
"total_images": len(images),
|
| 342 |
+
"device_used": str(self.device)
|
| 343 |
+
},
|
| 344 |
+
"text_analysis": {
|
| 345 |
+
"summary": summary,
|
| 346 |
+
"key_terms": key_terms[:15],
|
| 347 |
+
"sections_found": list(sections.keys())
|
| 348 |
+
},
|
| 349 |
+
"image_analysis": {
|
| 350 |
+
"total_images_extracted": len(images),
|
| 351 |
+
"images_with_text": len([r for r in ocr_results if r.get('has_text', False)]),
|
| 352 |
+
"ocr_available": self.use_ocr,
|
| 353 |
+
"blip_available": self.use_blip
|
| 354 |
+
},
|
| 355 |
+
"question_responses": question_answers,
|
| 356 |
+
"statistics": {
|
| 357 |
+
"total_text_characters": len(text),
|
| 358 |
+
"ocr_text_characters": len(ocr_text),
|
| 359 |
+
"questions_processed": len(questions),
|
| 360 |
+
"sections_identified": len(sections),
|
| 361 |
+
"key_terms_extracted": len(key_terms)
|
| 362 |
+
}
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
# Save securely if output file specified
|
| 366 |
+
if output_file:
|
| 367 |
+
self.secure_handler.secure_save(report, output_file)
|
| 368 |
+
self.hipaa_logger.log_access(self.user_id, "REPORT_SAVE", output_file)
|
| 369 |
+
|
| 370 |
+
return report
|
| 371 |
+
|
| 372 |
+
except Exception as e:
|
| 373 |
+
self.hipaa_logger.log_access(self.user_id, "PROCESSING_ERROR", pdf_path, success=False)
|
| 374 |
+
raise e
|
| 375 |
+
|
| 376 |
+
def _extract_text_and_images(self, pdf_path):
|
| 377 |
+
"""Securely extract text and images from PDF"""
|
| 378 |
+
text = ""
|
| 379 |
+
images = []
|
| 380 |
+
|
| 381 |
+
try:
|
| 382 |
+
# Use PyMuPDF for comprehensive extraction
|
| 383 |
+
doc = fitz.open(pdf_path)
|
| 384 |
+
|
| 385 |
+
for page_num in range(len(doc)):
|
| 386 |
+
page = doc.load_page(page_num)
|
| 387 |
+
|
| 388 |
+
# Extract text
|
| 389 |
+
page_text = page.get_text()
|
| 390 |
+
if page_text.strip():
|
| 391 |
+
text += page_text + "\n"
|
| 392 |
+
|
| 393 |
+
# Extract images
|
| 394 |
+
image_list = page.get_images()
|
| 395 |
+
|
| 396 |
+
for img_index, img in enumerate(image_list):
|
| 397 |
+
try:
|
| 398 |
+
xref = img[0]
|
| 399 |
+
pix = fitz.Pixmap(doc, xref)
|
| 400 |
+
|
| 401 |
+
if pix.n - pix.alpha < 4:
|
| 402 |
+
img_data = pix.tobytes("ppm")
|
| 403 |
+
img_pil = Image.open(io.BytesIO(img_data))
|
| 404 |
+
|
| 405 |
+
image_info = {
|
| 406 |
+
'page': page_num + 1,
|
| 407 |
+
'index': img_index,
|
| 408 |
+
'image': img_pil,
|
| 409 |
+
'size': img_pil.size,
|
| 410 |
+
'format': img_pil.format or 'Unknown'
|
| 411 |
+
}
|
| 412 |
+
images.append(image_info)
|
| 413 |
+
|
| 414 |
+
pix = None
|
| 415 |
+
|
| 416 |
+
except Exception as e:
|
| 417 |
+
print(f"Error extracting image {img_index} from page {page_num + 1}: {e}")
|
| 418 |
+
continue
|
| 419 |
+
|
| 420 |
+
doc.close()
|
| 421 |
+
|
| 422 |
+
except Exception as e:
|
| 423 |
+
print(f"Error in secure extraction: {e}")
|
| 424 |
+
|
| 425 |
+
return text, images
|
| 426 |
+
|
| 427 |
+
def _perform_secure_ocr(self, images):
|
| 428 |
+
"""Perform OCR with audit logging"""
|
| 429 |
+
ocr_results = []
|
| 430 |
+
|
| 431 |
+
for i, img_info in enumerate(images):
|
| 432 |
+
try:
|
| 433 |
+
img = img_info['image']
|
| 434 |
+
if img.mode != 'RGB':
|
| 435 |
+
img = img.convert('RGB')
|
| 436 |
+
|
| 437 |
+
# Preprocess for OCR
|
| 438 |
+
if OPENCV_AVAILABLE:
|
| 439 |
+
img_array = np.array(img)
|
| 440 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 441 |
+
denoised = cv2.medianBlur(gray, 3)
|
| 442 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 443 |
+
enhanced = clahe.apply(denoised)
|
| 444 |
+
_, thresh = cv2.threshold(enhanced, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 445 |
+
processed_img = Image.fromarray(thresh)
|
| 446 |
+
else:
|
| 447 |
+
gray = img.convert('L')
|
| 448 |
+
enhancer = ImageEnhance.Contrast(gray)
|
| 449 |
+
enhanced = enhancer.enhance(2.0)
|
| 450 |
+
processed_img = enhanced.filter(ImageFilter.SHARPEN)
|
| 451 |
+
|
| 452 |
+
# Perform OCR locally
|
| 453 |
+
ocr_text = pytesseract.image_to_string(processed_img, config='--psm 6')
|
| 454 |
+
|
| 455 |
+
ocr_result = {
|
| 456 |
+
'page': img_info['page'],
|
| 457 |
+
'image_index': img_info['index'],
|
| 458 |
+
'ocr_text': ocr_text.strip(),
|
| 459 |
+
'has_text': bool(ocr_text.strip()),
|
| 460 |
+
'processing_method': 'Local_OCR'
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
ocr_results.append(ocr_result)
|
| 464 |
+
|
| 465 |
+
except Exception as e:
|
| 466 |
+
ocr_results.append({
|
| 467 |
+
'page': img_info['page'],
|
| 468 |
+
'image_index': img_info['index'],
|
| 469 |
+
'ocr_text': '',
|
| 470 |
+
'has_text': False,
|
| 471 |
+
'error': str(e)
|
| 472 |
+
})
|
| 473 |
+
|
| 474 |
+
return ocr_results
|
| 475 |
+
|
| 476 |
+
def _analyze_images_securely(self, images):
|
| 477 |
+
"""Analyze images locally with BLIP"""
|
| 478 |
+
if not self.use_blip:
|
| 479 |
+
return []
|
| 480 |
+
|
| 481 |
+
descriptions = []
|
| 482 |
+
|
| 483 |
+
for img_info in images:
|
| 484 |
+
try:
|
| 485 |
+
image = img_info['image']
|
| 486 |
+
if image.mode != 'RGB':
|
| 487 |
+
image = image.convert('RGB')
|
| 488 |
+
|
| 489 |
+
inputs = self.blip_processor(image, return_tensors="pt").to(self.device)
|
| 490 |
+
|
| 491 |
+
with torch.no_grad():
|
| 492 |
+
out = self.blip_model.generate(**inputs, max_length=100, num_beams=5)
|
| 493 |
+
|
| 494 |
+
caption = self.blip_processor.decode(out[0], skip_special_tokens=True)
|
| 495 |
+
|
| 496 |
+
description = {
|
| 497 |
+
'page': img_info['page'],
|
| 498 |
+
'image_index': img_info['index'],
|
| 499 |
+
'caption': caption,
|
| 500 |
+
'processing_method': 'Local_BLIP'
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
descriptions.append(description)
|
| 504 |
+
|
| 505 |
+
except Exception as e:
|
| 506 |
+
descriptions.append({
|
| 507 |
+
'page': img_info['page'],
|
| 508 |
+
'image_index': img_info['index'],
|
| 509 |
+
'caption': 'Analysis failed',
|
| 510 |
+
'error': str(e)
|
| 511 |
+
})
|
| 512 |
+
|
| 513 |
+
return descriptions
|
| 514 |
+
|
| 515 |
+
def _extract_key_sections(self, text):
|
| 516 |
+
"""Extract key sections from text"""
|
| 517 |
+
sections = {}
|
| 518 |
+
section_patterns = {
|
| 519 |
+
'abstract': r'abstract\s*:?\s*(.*?)(?=\n\s*(?:introduction|chapter|acknowledgment|table of contents))',
|
| 520 |
+
'introduction': r'introduction\s*:?\s*(.*?)(?=\n\s*(?:literature review|methodology|chapter|background))',
|
| 521 |
+
'methodology': r'(?:methodology|methods)\s*:?\s*(.*?)(?=\n\s*(?:results|findings|analysis|chapter))',
|
| 522 |
+
'results': r'(?:results|findings)\s*:?\s*(.*?)(?=\n\s*(?:discussion|conclusion|chapter))',
|
| 523 |
+
'conclusion': r'conclusion\s*:?\s*(.*?)(?=\n\s*(?:references|bibliography|appendix))'
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
for section_name, pattern in section_patterns.items():
|
| 527 |
+
match = re.search(pattern, text.lower(), re.DOTALL | re.IGNORECASE)
|
| 528 |
+
if match:
|
| 529 |
+
sections[section_name] = match.group(1).strip()[:1000] # Truncate for privacy
|
| 530 |
+
|
| 531 |
+
return sections
|
| 532 |
+
|
| 533 |
+
def _extract_key_terms(self, text):
|
| 534 |
+
"""Extract key terms securely"""
|
| 535 |
+
try:
|
| 536 |
+
words = re.findall(r'\b[a-zA-Z]+\b', text.lower())
|
| 537 |
+
words = [
|
| 538 |
+
self.lemmatizer.lemmatize(word)
|
| 539 |
+
for word in words
|
| 540 |
+
if word not in self.stop_words
|
| 541 |
+
and len(word) > 3
|
| 542 |
+
and word.isalpha()
|
| 543 |
+
]
|
| 544 |
+
|
| 545 |
+
word_freq = Counter(words)
|
| 546 |
+
return [term for term, freq in word_freq.most_common(20)]
|
| 547 |
+
|
| 548 |
+
except Exception as e:
|
| 549 |
+
print(f"Error in key term extraction: {e}")
|
| 550 |
+
return []
|
| 551 |
+
|
| 552 |
+
def _generate_summary_secure(self, text):
|
| 553 |
+
"""Generate summary using local T5 model"""
|
| 554 |
+
try:
|
| 555 |
+
clean_text = re.sub(r'\s+', ' ', text).strip()
|
| 556 |
+
|
| 557 |
+
# Chunk text for processing
|
| 558 |
+
max_length = 1000
|
| 559 |
+
if len(clean_text) > max_length:
|
| 560 |
+
clean_text = clean_text[:max_length]
|
| 561 |
+
|
| 562 |
+
summary = self.summarizer(
|
| 563 |
+
clean_text,
|
| 564 |
+
max_length=200,
|
| 565 |
+
min_length=150,
|
| 566 |
+
do_sample=True,
|
| 567 |
+
temperature=0.7
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
return summary[0]['summary_text']
|
| 571 |
+
|
| 572 |
+
except Exception as e:
|
| 573 |
+
print(f"Error in T5 summarization: {e}")
|
| 574 |
+
# Fallback to extractive summary
|
| 575 |
+
sentences = re.split(r'[.!?]+', text)
|
| 576 |
+
return " ".join(sentences[:3]) + "..."
|
| 577 |
+
|
| 578 |
+
def _answer_questions_secure(self, questions, text):
|
| 579 |
+
"""Answer questions using local T5 model"""
|
| 580 |
+
answers = {}
|
| 581 |
+
|
| 582 |
+
for question in questions:
|
| 583 |
+
try:
|
| 584 |
+
prompt = f"question: {question} context: {text[:1000]}"
|
| 585 |
+
|
| 586 |
+
answer_result = self.qa_pipeline(
|
| 587 |
+
prompt,
|
| 588 |
+
max_length=200,
|
| 589 |
+
min_length=30,
|
| 590 |
+
do_sample=True,
|
| 591 |
+
temperature=0.7,
|
| 592 |
+
num_return_sequences=1
|
| 593 |
+
)
|
| 594 |
+
|
| 595 |
+
answer = answer_result[0]['generated_text']
|
| 596 |
+
answer = re.sub(r'^(answer:|Answer:)', '', answer).strip()
|
| 597 |
+
|
| 598 |
+
answers[question] = {
|
| 599 |
+
'answer': answer,
|
| 600 |
+
'method': 'Local_T5',
|
| 601 |
+
'processed_securely': True
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
except Exception as e:
|
| 605 |
+
answers[question] = {
|
| 606 |
+
'answer': 'Unable to process question securely',
|
| 607 |
+
'error': str(e),
|
| 608 |
+
'method': 'Error'
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
return answers
|
| 612 |
+
|
| 613 |
+
def cleanup_session(self):
|
| 614 |
+
"""Clean up session data securely"""
|
| 615 |
+
self.hipaa_logger.log_access(self.user_id, "SESSION_END", "THESIS_ANALYZER")
|
| 616 |
+
|
| 617 |
+
# Clear sensitive data from memory
|
| 618 |
+
self.thesis_text = ""
|
| 619 |
+
self.extracted_images = []
|
| 620 |
+
self.ocr_results = []
|
| 621 |
+
self.image_descriptions = []
|
| 622 |
+
|
| 623 |
+
# Clear model cache if needed
|
| 624 |
+
if hasattr(torch.cuda, 'empty_cache'):
|
| 625 |
+
torch.cuda.empty_cache()
|
| 626 |
+
|
| 627 |
+
print("Session cleaned up securely")
|
| 628 |
+
|
| 629 |
+
class AnalyzeReq(BaseModel):
|
| 630 |
+
storageKey: str # path to PDF on disk (or adjust to your storage scheme)
|
| 631 |
+
projectId: Optional[str] = None
|
| 632 |
+
documentId: Optional[str] = None
|
| 633 |
+
ocr: bool = False
|
| 634 |
+
blip: bool = False
|
| 635 |
+
userId:str
|
| 636 |
+
password:str
|
| 637 |
+
useEncryption: bool =False
|
| 638 |
+
|
| 639 |
+
@app.post('/analyze')
|
| 640 |
+
def analyze(req: AnalyzeReq):
|
| 641 |
+
"""Main function with HIPAA compliance demonstration"""
|
| 642 |
+
print("HIPAA-COMPLIANT THESIS ANALYZER")
|
| 643 |
+
print("=" * 50)
|
| 644 |
+
|
| 645 |
+
try:
|
| 646 |
+
# Initialize HIPAA-compliant analyzer
|
| 647 |
+
analyzer = HIPAACompliantThesisAnalyzer(
|
| 648 |
+
user_id=req.userId,
|
| 649 |
+
password=req.password,
|
| 650 |
+
session_timeout=30
|
| 651 |
+
)
|
| 652 |
+
|
| 653 |
+
pdf_path = req.storageKey
|
| 654 |
+
|
| 655 |
+
# Sample questions
|
| 656 |
+
questions = [
|
| 657 |
+
"What is the main objective of the research?",
|
| 658 |
+
"What methodology was used in the study?",
|
| 659 |
+
"What are the key findings or results?",
|
| 660 |
+
"What conclusions did the authors draw?",
|
| 661 |
+
"What are the limitations of the study?",
|
| 662 |
+
"What motivated the researchers to conduct this study?",
|
| 663 |
+
"How does this research relate to existing literature?",
|
| 664 |
+
"What are the practical implications of the findings?",
|
| 665 |
+
"What assumptions underlie the research?",
|
| 666 |
+
"What statistical methods were used to analyze the data?",
|
| 667 |
+
"How robust are the study’s findings?",
|
| 668 |
+
"Are there any potential biases in the study design or data collection?",
|
| 669 |
+
"How do the results compare with previous studies on the same topic?",
|
| 670 |
+
"What are the potential future applications of this research?",
|
| 671 |
+
"How could this research be expanded or built upon in future studies?",
|
| 672 |
+
"What new questions have emerged as a result of this study?"
|
| 673 |
+
]
|
| 674 |
+
|
| 675 |
+
# Process document securely
|
| 676 |
+
print("\nProcessing document with HIPAA compliance...")
|
| 677 |
+
report = analyzer.process_document_securely(
|
| 678 |
+
pdf_path=pdf_path,
|
| 679 |
+
questions=questions,
|
| 680 |
+
output_file="hipaa_compliant_analysis"
|
| 681 |
+
)
|
| 682 |
+
|
| 683 |
+
print("\n" + "="*60)
|
| 684 |
+
print("HIPAA-COMPLIANT ANALYSIS COMPLETE")
|
| 685 |
+
print("="*60)
|
| 686 |
+
print(f"✓ Processed locally: {report['hipaa_compliance']['processed_locally']}")
|
| 687 |
+
print(f"✓ Encrypted storage: {report['hipaa_compliance']['encrypted_storage']}")
|
| 688 |
+
print(f"✓ Audit logged: {report['hipaa_compliance']['audit_logged']}")
|
| 689 |
+
print(f"✓ No external APIs: {report['hipaa_compliance']['no_external_apis']}")
|
| 690 |
+
print(f"✓ Session ID: {report['hipaa_compliance']['session_id']}")
|
| 691 |
+
|
| 692 |
+
# Cleanup
|
| 693 |
+
analyzer.cleanup_session()
|
| 694 |
+
|
| 695 |
+
return report
|
| 696 |
+
except Exception as e:
|
| 697 |
+
print(f"Error: {e}")
|
| 698 |
+
print("Ensure all requirements are installed and Tesseract is available.")
|
| 699 |
+
|
| 700 |
+
#if __name__ == "__main__":
|
| 701 |
+
print("""
|
| 702 |
+
HIPAA-COMPLIANT THESIS ANALYZER
|
| 703 |
+
===============================
|
| 704 |
+
|
| 705 |
+
HIPAA COMPLIANCE FEATURES:
|
| 706 |
+
✓ Local processing only - no external API calls
|
| 707 |
+
✓ Encryption at rest with password protection
|
| 708 |
+
✓ Comprehensive audit logging
|
| 709 |
+
✓ Session timeout and access controls
|
| 710 |
+
✓ Secure file deletion
|
| 711 |
+
✓ PHI processing audit trail
|
| 712 |
+
✓ User authentication
|
| 713 |
+
✓ Data integrity verification
|
| 714 |
+
|
| 715 |
+
INSTALLATION:
|
| 716 |
+
pip install torch transformers PyPDF2 nltk PyMuPDF pillow pytesseract cryptography
|
| 717 |
+
|
| 718 |
+
SECURITY FEATURES:
|
| 719 |
+
- All processing happens locally
|
| 720 |
+
- Optional file encryption
|
| 721 |
+
- Secure memory cleanup
|
| 722 |
+
- Audit trail for all operations
|
| 723 |
+
- Session management with timeouts
|
| 724 |
+
- Secure file overwriting for deletion
|
| 725 |
+
|
| 726 |
+
COMPLIANCE NOTES:
|
| 727 |
+
- This tool provides technical safeguards
|
| 728 |
+
- You must implement administrative and physical safeguards
|
| 729 |
+
- Ensure your workstation meets HIPAA requirements
|
| 730 |
+
- Regular security assessments recommended
|
| 731 |
+
|
| 732 |
+
""")
|
| 733 |
+
|
| 734 |
+
#main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cryptography==46.0.1
|
| 2 |
+
fastapi==0.118.0
|
| 3 |
+
fitz==0.0.1.dev2
|
| 4 |
+
nltk==3.9.1
|
| 5 |
+
numpy==2.3.3
|
| 6 |
+
opencv_python==4.12.0.88
|
| 7 |
+
Pillow==11.3.0
|
| 8 |
+
pydantic==2.11.9
|
| 9 |
+
PyPDF2==3.0.1
|
| 10 |
+
pytesseract==0.3.13
|
| 11 |
+
torch==2.8.0
|
| 12 |
+
transformers==4.56.1
|
thesis.pdf
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
thesis.py
ADDED
|
@@ -0,0 +1,626 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import PyPDF2
|
| 2 |
+
import re
|
| 3 |
+
from collections import Counter
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.stem import WordNetLemmatizer
|
| 8 |
+
import string
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import json
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer, pipeline
|
| 13 |
+
import warnings
|
| 14 |
+
|
| 15 |
+
warnings.filterwarnings('ignore')
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Download required NLTK data with improved error handling
|
| 19 |
+
def download_nltk_resources():
|
| 20 |
+
"""Download required NLTK resources with proper error handling"""
|
| 21 |
+
resources = [
|
| 22 |
+
('tokenizers/punkt', 'punkt'),
|
| 23 |
+
('tokenizers/punkt_tab', 'punkt_tab'),
|
| 24 |
+
('corpora/stopwords', 'stopwords'),
|
| 25 |
+
('corpora/wordnet', 'wordnet'),
|
| 26 |
+
('corpora/omw-1.4', 'omw-1.4')
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
for resource_path, resource_name in resources:
|
| 30 |
+
try:
|
| 31 |
+
nltk.data.find(resource_path)
|
| 32 |
+
print(f"✓ {resource_name} already available")
|
| 33 |
+
except LookupError:
|
| 34 |
+
print(f"Downloading {resource_name}...")
|
| 35 |
+
try:
|
| 36 |
+
nltk.download(resource_name, quiet=False)
|
| 37 |
+
print(f"✓ {resource_name} downloaded successfully")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Warning: Failed to download {resource_name}: {e}")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Download NLTK resources
|
| 44 |
+
print("Checking and downloading required NLTK resources...")
|
| 45 |
+
download_nltk_resources()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class ThesisAnalyzer:
|
| 49 |
+
def __init__(self):
|
| 50 |
+
# Initialize NLTK components with error handling
|
| 51 |
+
try:
|
| 52 |
+
self.lemmatizer = WordNetLemmatizer()
|
| 53 |
+
self.stop_words = set(stopwords.words('english'))
|
| 54 |
+
except LookupError as e:
|
| 55 |
+
print(f"NLTK resource error: {e}")
|
| 56 |
+
print("Attempting to download missing resources...")
|
| 57 |
+
download_nltk_resources()
|
| 58 |
+
self.lemmatizer = WordNetLemmatizer()
|
| 59 |
+
self.stop_words = set(stopwords.words('english'))
|
| 60 |
+
|
| 61 |
+
self.thesis_text = ""
|
| 62 |
+
self.sentences = []
|
| 63 |
+
self.key_terms = []
|
| 64 |
+
|
| 65 |
+
# Initialize T5 model and tokenizer
|
| 66 |
+
print("Loading T5-small model and tokenizer...")
|
| 67 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 68 |
+
print(f"Using device: {self.device}")
|
| 69 |
+
|
| 70 |
+
# Load T5 model for text generation
|
| 71 |
+
self.model_name = "t5-small"
|
| 72 |
+
self.tokenizer = T5Tokenizer.from_pretrained(self.model_name)
|
| 73 |
+
self.model = T5ForConditionalGeneration.from_pretrained(self.model_name)
|
| 74 |
+
self.model.to(self.device)
|
| 75 |
+
|
| 76 |
+
# Initialize summarization pipeline
|
| 77 |
+
self.summarizer = pipeline(
|
| 78 |
+
"summarization",
|
| 79 |
+
model=self.model_name,
|
| 80 |
+
tokenizer=self.model_name,
|
| 81 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 82 |
+
max_length=200,
|
| 83 |
+
min_length=150,
|
| 84 |
+
do_sample=True,
|
| 85 |
+
temperature=0.7
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Initialize question answering pipeline
|
| 89 |
+
self.qa_pipeline = pipeline(
|
| 90 |
+
"text2text-generation",
|
| 91 |
+
model=self.model_name,
|
| 92 |
+
tokenizer=self.model_name,
|
| 93 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 94 |
+
max_length=512,
|
| 95 |
+
do_sample=True,
|
| 96 |
+
temperature=0.7
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
print("T5 model loaded successfully!")
|
| 100 |
+
|
| 101 |
+
def extract_text_from_pdf(self, pdf_path):
|
| 102 |
+
"""Extract text content from PDF file"""
|
| 103 |
+
try:
|
| 104 |
+
with open(pdf_path, 'rb') as file:
|
| 105 |
+
reader = PyPDF2.PdfReader(file)
|
| 106 |
+
text = ""
|
| 107 |
+
|
| 108 |
+
for page_num, page in enumerate(reader.pages):
|
| 109 |
+
try:
|
| 110 |
+
text += page.extract_text() + "\n"
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"Error extracting text from page {page_num + 1}: {e}")
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
+
self.thesis_text = text
|
| 116 |
+
return text
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error reading PDF file: {e}")
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
def preprocess_text(self, text):
|
| 123 |
+
"""Clean and preprocess the text"""
|
| 124 |
+
# Remove extra whitespace and normalize
|
| 125 |
+
text = re.sub(r'\s+', ' ', text)
|
| 126 |
+
# Remove page numbers and headers/footers (basic cleaning)
|
| 127 |
+
text = re.sub(r'\n\d+\n', ' ', text)
|
| 128 |
+
# Remove excessive line breaks
|
| 129 |
+
text = re.sub(r'\n+', ' ', text)
|
| 130 |
+
# Remove special characters but keep basic punctuation
|
| 131 |
+
text = re.sub(r'[^\w\s\.\,\;\:\!\?\-\(\)]', ' ', text)
|
| 132 |
+
|
| 133 |
+
return text.strip()
|
| 134 |
+
|
| 135 |
+
def chunk_text(self, text, max_chunk_size=1000):
|
| 136 |
+
"""Split text into chunks for processing with T5"""
|
| 137 |
+
try:
|
| 138 |
+
sentences = sent_tokenize(text)
|
| 139 |
+
except LookupError:
|
| 140 |
+
print("NLTK punkt tokenizer not found. Using basic sentence splitting...")
|
| 141 |
+
# Fallback to basic sentence splitting
|
| 142 |
+
sentences = re.split(r'[.!?]+', text)
|
| 143 |
+
sentences = [s.strip() for s in sentences if s.strip()]
|
| 144 |
+
|
| 145 |
+
chunks = []
|
| 146 |
+
current_chunk = ""
|
| 147 |
+
|
| 148 |
+
for sentence in sentences:
|
| 149 |
+
if len(current_chunk) + len(sentence) <= max_chunk_size:
|
| 150 |
+
current_chunk += sentence + " "
|
| 151 |
+
else:
|
| 152 |
+
if current_chunk:
|
| 153 |
+
chunks.append(current_chunk.strip())
|
| 154 |
+
current_chunk = sentence + " "
|
| 155 |
+
|
| 156 |
+
if current_chunk:
|
| 157 |
+
chunks.append(current_chunk.strip())
|
| 158 |
+
|
| 159 |
+
return chunks
|
| 160 |
+
|
| 161 |
+
def extract_key_sections(self, text):
|
| 162 |
+
"""Extract key sections from the thesis"""
|
| 163 |
+
sections = {}
|
| 164 |
+
|
| 165 |
+
# Common thesis section patterns
|
| 166 |
+
section_patterns = {
|
| 167 |
+
'abstract': r'abstract\s*:?\s*(.*?)(?=\n\s*(?:introduction|chapter|acknowledgment|table of contents))',
|
| 168 |
+
'introduction': r'introduction\s*:?\s*(.*?)(?=\n\s*(?:literature review|methodology|chapter|background))',
|
| 169 |
+
'methodology': r'(?:methodology|methods)\s*:?\s*(.*?)(?=\n\s*(?:results|findings|analysis|chapter))',
|
| 170 |
+
'results': r'(?:results|findings)\s*:?\s*(.*?)(?=\n\s*(?:discussion|conclusion|chapter))',
|
| 171 |
+
'conclusion': r'conclusion\s*:?\s*(.*?)(?=\n\s*(?:references|bibliography|appendix))'
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
for section_name, pattern in section_patterns.items():
|
| 175 |
+
match = re.search(pattern, text.lower(), re.DOTALL | re.IGNORECASE)
|
| 176 |
+
if match:
|
| 177 |
+
sections[section_name] = match.group(1).strip()[:2000] # Increased limit
|
| 178 |
+
|
| 179 |
+
return sections
|
| 180 |
+
|
| 181 |
+
def extract_key_terms(self, text, num_terms=20):
|
| 182 |
+
"""Extract key terms from the thesis using T5"""
|
| 183 |
+
try:
|
| 184 |
+
# Traditional key term extraction with error handling
|
| 185 |
+
try:
|
| 186 |
+
words = word_tokenize(text.lower())
|
| 187 |
+
except LookupError:
|
| 188 |
+
print("NLTK tokenizer not available. Using basic word splitting...")
|
| 189 |
+
words = re.findall(r'\b[a-zA-Z]+\b', text.lower())
|
| 190 |
+
|
| 191 |
+
words = [
|
| 192 |
+
self.lemmatizer.lemmatize(word)
|
| 193 |
+
for word in words
|
| 194 |
+
if word not in self.stop_words
|
| 195 |
+
and word not in string.punctuation
|
| 196 |
+
and len(word) > 3
|
| 197 |
+
and word.isalpha()
|
| 198 |
+
]
|
| 199 |
+
|
| 200 |
+
word_freq = Counter(words)
|
| 201 |
+
traditional_terms = [term for term, freq in word_freq.most_common(num_terms)]
|
| 202 |
+
|
| 203 |
+
# Enhanced key term extraction using T5
|
| 204 |
+
try:
|
| 205 |
+
# Create a prompt for key term extraction
|
| 206 |
+
prompt = f"summarize: Extract key research terms from this academic text: {text[:1000]}"
|
| 207 |
+
|
| 208 |
+
# Use T5 to generate key terms
|
| 209 |
+
inputs = self.tokenizer.encode(prompt, return_tensors='pt', max_length=512, truncation=True)
|
| 210 |
+
inputs = inputs.to(self.device)
|
| 211 |
+
|
| 212 |
+
with torch.no_grad():
|
| 213 |
+
outputs = self.model.generate(
|
| 214 |
+
inputs,
|
| 215 |
+
max_length=100,
|
| 216 |
+
num_return_sequences=1,
|
| 217 |
+
temperature=0.7,
|
| 218 |
+
do_sample=True,
|
| 219 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
t5_terms = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 223 |
+
t5_terms = [term.strip() for term in t5_terms.split(',') if term.strip()]
|
| 224 |
+
|
| 225 |
+
# Combine traditional and T5-generated terms
|
| 226 |
+
self.key_terms = list(set(traditional_terms[:15] + t5_terms[:10]))[:20]
|
| 227 |
+
|
| 228 |
+
except Exception as e:
|
| 229 |
+
print(f"Error in T5 key term extraction: {e}")
|
| 230 |
+
self.key_terms = traditional_terms
|
| 231 |
+
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f"Error in key term extraction: {e}")
|
| 234 |
+
# Very basic fallback
|
| 235 |
+
words = re.findall(r'\b[a-zA-Z]{4,}\b', text.lower())
|
| 236 |
+
word_freq = Counter(words)
|
| 237 |
+
self.key_terms = [term for term, freq in word_freq.most_common(20)]
|
| 238 |
+
|
| 239 |
+
return self.key_terms
|
| 240 |
+
|
| 241 |
+
def generate_summary_with_t5(self, text):
|
| 242 |
+
"""Generate summary using T5 model"""
|
| 243 |
+
try:
|
| 244 |
+
# Preprocess and chunk the text
|
| 245 |
+
clean_text = self.preprocess_text(text)
|
| 246 |
+
chunks = self.chunk_text(clean_text, max_chunk_size=1000)
|
| 247 |
+
|
| 248 |
+
print(f"Processing {len(chunks)} text chunks for summarization...")
|
| 249 |
+
|
| 250 |
+
# Generate summaries for each chunk
|
| 251 |
+
chunk_summaries = []
|
| 252 |
+
for i, chunk in enumerate(chunks[:5]): # Limit to first 5 chunks
|
| 253 |
+
try:
|
| 254 |
+
print(f"Summarizing chunk {i + 1}/{min(len(chunks), 5)}...")
|
| 255 |
+
|
| 256 |
+
# Use the summarization pipeline
|
| 257 |
+
summary = self.summarizer(
|
| 258 |
+
chunk,
|
| 259 |
+
max_length=150,
|
| 260 |
+
min_length=50,
|
| 261 |
+
do_sample=True,
|
| 262 |
+
temperature=0.7
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
chunk_summaries.append(summary[0]['summary_text'])
|
| 266 |
+
|
| 267 |
+
except Exception as e:
|
| 268 |
+
print(f"Error summarizing chunk {i + 1}: {e}")
|
| 269 |
+
continue
|
| 270 |
+
|
| 271 |
+
# Combine chunk summaries
|
| 272 |
+
combined_summary = " ".join(chunk_summaries)
|
| 273 |
+
|
| 274 |
+
# Generate final summary
|
| 275 |
+
if len(combined_summary) > 500:
|
| 276 |
+
try:
|
| 277 |
+
final_summary = self.summarizer(
|
| 278 |
+
combined_summary,
|
| 279 |
+
max_length=200,
|
| 280 |
+
min_length=150,
|
| 281 |
+
do_sample=True,
|
| 282 |
+
temperature=0.7
|
| 283 |
+
)
|
| 284 |
+
return final_summary[0]['summary_text']
|
| 285 |
+
except:
|
| 286 |
+
return combined_summary[:800] + "..."
|
| 287 |
+
else:
|
| 288 |
+
return combined_summary
|
| 289 |
+
|
| 290 |
+
except Exception as e:
|
| 291 |
+
print(f"Error in T5 summarization: {e}")
|
| 292 |
+
return self.fallback_summary(text)
|
| 293 |
+
|
| 294 |
+
def fallback_summary(self, text):
|
| 295 |
+
"""Fallback summary method if T5 fails"""
|
| 296 |
+
try:
|
| 297 |
+
sentences = sent_tokenize(self.preprocess_text(text))
|
| 298 |
+
except LookupError:
|
| 299 |
+
# Basic sentence splitting fallback
|
| 300 |
+
sentences = re.split(r'[.!?]+', self.preprocess_text(text))
|
| 301 |
+
sentences = [s.strip() for s in sentences if s.strip()]
|
| 302 |
+
|
| 303 |
+
key_terms = self.extract_key_terms(text)
|
| 304 |
+
|
| 305 |
+
# Score sentences based on key term frequency
|
| 306 |
+
sentence_scores = {}
|
| 307 |
+
for sentence in sentences:
|
| 308 |
+
try:
|
| 309 |
+
words = word_tokenize(sentence.lower())
|
| 310 |
+
except LookupError:
|
| 311 |
+
words = re.findall(r'\b[a-zA-Z]+\b', sentence.lower())
|
| 312 |
+
|
| 313 |
+
score = sum(1 for word in words if word in key_terms)
|
| 314 |
+
sentence_scores[sentence] = score
|
| 315 |
+
|
| 316 |
+
# Select top sentences
|
| 317 |
+
top_sentences = sorted(sentence_scores.items(), key=lambda x: x[1], reverse=True)
|
| 318 |
+
|
| 319 |
+
summary_text = ""
|
| 320 |
+
word_count = 0
|
| 321 |
+
for sentence, score in top_sentences:
|
| 322 |
+
if word_count >= 180:
|
| 323 |
+
break
|
| 324 |
+
if len(sentence) > 20:
|
| 325 |
+
summary_text += sentence + " "
|
| 326 |
+
word_count += len(sentence.split())
|
| 327 |
+
|
| 328 |
+
return summary_text.strip()
|
| 329 |
+
|
| 330 |
+
def answer_questions_with_t5(self, questions):
|
| 331 |
+
"""Answer questions using T5 model"""
|
| 332 |
+
if not self.thesis_text:
|
| 333 |
+
return "No thesis text loaded. Please extract text first."
|
| 334 |
+
|
| 335 |
+
answers = {}
|
| 336 |
+
clean_text = self.preprocess_text(self.thesis_text)
|
| 337 |
+
|
| 338 |
+
# Limit text length for processing
|
| 339 |
+
text_chunks = self.chunk_text(clean_text, max_chunk_size=1500)
|
| 340 |
+
|
| 341 |
+
for question in questions:
|
| 342 |
+
print(f"Processing question: {question[:50]}...")
|
| 343 |
+
|
| 344 |
+
try:
|
| 345 |
+
# Find the most relevant chunk for this question
|
| 346 |
+
best_chunk = ""
|
| 347 |
+
best_score = 0
|
| 348 |
+
|
| 349 |
+
try:
|
| 350 |
+
question_words = set(word_tokenize(question.lower()))
|
| 351 |
+
except LookupError:
|
| 352 |
+
question_words = set(re.findall(r'\b[a-zA-Z]+\b', question.lower()))
|
| 353 |
+
|
| 354 |
+
for chunk in text_chunks[:3]: # Process first 3 chunks
|
| 355 |
+
try:
|
| 356 |
+
chunk_words = set(word_tokenize(chunk.lower()))
|
| 357 |
+
except LookupError:
|
| 358 |
+
chunk_words = set(re.findall(r'\b[a-zA-Z]+\b', chunk.lower()))
|
| 359 |
+
|
| 360 |
+
overlap = len(question_words.intersection(chunk_words))
|
| 361 |
+
if overlap > best_score:
|
| 362 |
+
best_score = overlap
|
| 363 |
+
best_chunk = chunk
|
| 364 |
+
|
| 365 |
+
# Create T5 prompt for question answering
|
| 366 |
+
prompt = f"question: {question} context: {best_chunk[:1000]}"
|
| 367 |
+
|
| 368 |
+
# Generate answer using T5
|
| 369 |
+
answer_result = self.qa_pipeline(
|
| 370 |
+
prompt,
|
| 371 |
+
max_length=200,
|
| 372 |
+
min_length=30,
|
| 373 |
+
do_sample=True,
|
| 374 |
+
temperature=0.7,
|
| 375 |
+
num_return_sequences=1
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
answer = answer_result[0]['generated_text']
|
| 379 |
+
|
| 380 |
+
# Clean up the answer
|
| 381 |
+
answer = re.sub(r'^(answer:|Answer:)', '', answer).strip()
|
| 382 |
+
|
| 383 |
+
answers[question] = {
|
| 384 |
+
'answer': answer,
|
| 385 |
+
'confidence': min(best_score / len(question_words), 1.0) if question_words else 0.5,
|
| 386 |
+
'method': 'T5-generated',
|
| 387 |
+
'chunk_used': len(best_chunk) > 0
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
except Exception as e:
|
| 391 |
+
print(f"Error processing question with T5: {e}")
|
| 392 |
+
# Fallback to traditional method
|
| 393 |
+
answers[question] = self.fallback_answer(question, clean_text)
|
| 394 |
+
|
| 395 |
+
return answers
|
| 396 |
+
|
| 397 |
+
def fallback_answer(self, question, text):
|
| 398 |
+
"""Fallback answer method if T5 fails"""
|
| 399 |
+
try:
|
| 400 |
+
sentences = sent_tokenize(text)
|
| 401 |
+
except LookupError:
|
| 402 |
+
sentences = re.split(r'[.!?]+', text)
|
| 403 |
+
sentences = [s.strip() for s in sentences if s.strip()]
|
| 404 |
+
|
| 405 |
+
try:
|
| 406 |
+
question_words = [
|
| 407 |
+
word.lower() for word in word_tokenize(question)
|
| 408 |
+
if word.lower() not in self.stop_words and word.isalpha()
|
| 409 |
+
]
|
| 410 |
+
except LookupError:
|
| 411 |
+
question_words = [
|
| 412 |
+
word.lower() for word in re.findall(r'\b[a-zA-Z]+\b', question)
|
| 413 |
+
if word.lower() not in self.stop_words and len(word) > 2
|
| 414 |
+
]
|
| 415 |
+
|
| 416 |
+
relevant_sentences = []
|
| 417 |
+
for sentence in sentences:
|
| 418 |
+
sentence_lower = sentence.lower()
|
| 419 |
+
relevance_score = sum(1 for word in question_words if word in sentence_lower)
|
| 420 |
+
|
| 421 |
+
if relevance_score > 0:
|
| 422 |
+
relevant_sentences.append((sentence, relevance_score))
|
| 423 |
+
|
| 424 |
+
relevant_sentences.sort(key=lambda x: x[1], reverse=True)
|
| 425 |
+
|
| 426 |
+
if relevant_sentences:
|
| 427 |
+
answer_text = " ".join([s[0].strip() for s in relevant_sentences[:2]])
|
| 428 |
+
return {
|
| 429 |
+
'answer': answer_text,
|
| 430 |
+
'confidence': min(relevant_sentences[0][1] / len(question_words), 1.0),
|
| 431 |
+
'method': 'Traditional extraction',
|
| 432 |
+
'chunk_used': True
|
| 433 |
+
}
|
| 434 |
+
else:
|
| 435 |
+
return {
|
| 436 |
+
'answer': "No relevant information found in the thesis text.",
|
| 437 |
+
'confidence': 0.0,
|
| 438 |
+
'method': 'No match',
|
| 439 |
+
'chunk_used': False
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
def generate_report(self, pdf_path, questions, output_file=None):
|
| 443 |
+
"""Generate a complete analysis report using T5"""
|
| 444 |
+
print("Starting advanced thesis analysis with T5-small...")
|
| 445 |
+
|
| 446 |
+
# Extract text from PDF
|
| 447 |
+
text = self.extract_text_from_pdf(pdf_path)
|
| 448 |
+
if not text:
|
| 449 |
+
return "Failed to extract text from PDF."
|
| 450 |
+
|
| 451 |
+
print(f"Extracted {len(text)} characters from PDF.")
|
| 452 |
+
|
| 453 |
+
# Extract key sections and terms
|
| 454 |
+
print("Extracting key sections and terms...")
|
| 455 |
+
sections = self.extract_key_sections(text)
|
| 456 |
+
key_terms = self.extract_key_terms(text)
|
| 457 |
+
|
| 458 |
+
# Generate summary using T5
|
| 459 |
+
print("Generating T5-powered summary...")
|
| 460 |
+
summary = self.generate_summary_with_t5(text)
|
| 461 |
+
|
| 462 |
+
# Answer questions using T5
|
| 463 |
+
print("Answering questions with T5...")
|
| 464 |
+
question_answers = self.answer_questions_with_t5(questions)
|
| 465 |
+
|
| 466 |
+
# Compile report
|
| 467 |
+
report = f"""
|
| 468 |
+
{'=' * 70}
|
| 469 |
+
ADVANCED THESIS ANALYSIS REPORT (T5-Small Enhanced)
|
| 470 |
+
{'=' * 70}
|
| 471 |
+
|
| 472 |
+
Generated on: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
| 473 |
+
Document: {pdf_path}
|
| 474 |
+
Model: T5-Small (Hugging Face Transformers)
|
| 475 |
+
Device: {str(self.device)}
|
| 476 |
+
|
| 477 |
+
{'=' * 70}
|
| 478 |
+
THESIS SUMMARY (AI-Generated)
|
| 479 |
+
{'=' * 70}
|
| 480 |
+
|
| 481 |
+
{summary}
|
| 482 |
+
|
| 483 |
+
Key Terms Identified: {', '.join(key_terms[:15])}
|
| 484 |
+
|
| 485 |
+
Sections Found: {', '.join(sections.keys())}
|
| 486 |
+
|
| 487 |
+
{'=' * 70}
|
| 488 |
+
QUESTION RESPONSES (T5-Enhanced)
|
| 489 |
+
{'=' * 70}
|
| 490 |
+
|
| 491 |
+
"""
|
| 492 |
+
|
| 493 |
+
for i, (question, response) in enumerate(question_answers.items(), 1):
|
| 494 |
+
report += f"""
|
| 495 |
+
Question {i}: {question}
|
| 496 |
+
|
| 497 |
+
Answer: {response['answer']}
|
| 498 |
+
|
| 499 |
+
Confidence Level: {response['confidence']:.2f}
|
| 500 |
+
Generation Method: {response['method']}
|
| 501 |
+
Context Used: {'Yes' if response['chunk_used'] else 'No'}
|
| 502 |
+
|
| 503 |
+
{'-' * 50}
|
| 504 |
+
"""
|
| 505 |
+
|
| 506 |
+
report += f"""
|
| 507 |
+
|
| 508 |
+
{'=' * 70}
|
| 509 |
+
ANALYSIS STATISTICS
|
| 510 |
+
{'=' * 70}
|
| 511 |
+
|
| 512 |
+
Total Characters: {len(text):,}
|
| 513 |
+
Total Sentences: {len(sent_tokenize(text)):,}
|
| 514 |
+
Key Terms Identified: {len(key_terms)}
|
| 515 |
+
Questions Processed: {len(questions)}
|
| 516 |
+
Sections Identified: {len(sections)}
|
| 517 |
+
Model Performance: T5-Small with {str(self.device).upper()} acceleration
|
| 518 |
+
|
| 519 |
+
{'=' * 70}
|
| 520 |
+
TECHNICAL DETAILS
|
| 521 |
+
{'=' * 70}
|
| 522 |
+
|
| 523 |
+
Model: {self.model_name}
|
| 524 |
+
Tokenizer: T5Tokenizer
|
| 525 |
+
Framework: Hugging Face Transformers
|
| 526 |
+
PyTorch Device: {str(self.device)}
|
| 527 |
+
Summarization Pipeline: Enabled
|
| 528 |
+
Question Answering: T5 Text-to-Text Generation
|
| 529 |
+
|
| 530 |
+
{'=' * 70}
|
| 531 |
+
"""
|
| 532 |
+
|
| 533 |
+
# Save to file if specified
|
| 534 |
+
if output_file:
|
| 535 |
+
try:
|
| 536 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 537 |
+
f.write(report)
|
| 538 |
+
print(f"Report saved to: {output_file}")
|
| 539 |
+
except Exception as e:
|
| 540 |
+
print(f"Error saving report: {e}")
|
| 541 |
+
|
| 542 |
+
return report
|
| 543 |
+
|
| 544 |
+
|
| 545 |
+
def main():
|
| 546 |
+
"""Main function to demonstrate usage"""
|
| 547 |
+
try:
|
| 548 |
+
analyzer = ThesisAnalyzer()
|
| 549 |
+
|
| 550 |
+
# Example usage
|
| 551 |
+
pdf_path = "thesis.pdf" # Replace with your PDF path
|
| 552 |
+
|
| 553 |
+
# Enhanced questions for T5 processing
|
| 554 |
+
sample_questions = [
|
| 555 |
+
"What is the main objective of the research?",
|
| 556 |
+
"What methodology was used in the study?",
|
| 557 |
+
"What are the key findings or results?",
|
| 558 |
+
"What conclusions did the authors draw?",
|
| 559 |
+
"What are the limitations of the study?",
|
| 560 |
+
"What motivated the researchers to conduct this study?",
|
| 561 |
+
"How does this research relate to existing literature?",
|
| 562 |
+
"What are the practical implications of the findings?",
|
| 563 |
+
"What assumptions underlie the research?",
|
| 564 |
+
"What statistical methods were used to analyze the data?",
|
| 565 |
+
"How robust are the study’s findings?",
|
| 566 |
+
"Are there any potential biases in the study design or data collection?",
|
| 567 |
+
"How do the results compare with previous studies on the same topic?",
|
| 568 |
+
"What are the potential future applications of this research?",
|
| 569 |
+
"How could this research be expanded or built upon in future studies?",
|
| 570 |
+
"What new questions have emerged as a result of this study?"
|
| 571 |
+
]
|
| 572 |
+
|
| 573 |
+
# Generate report
|
| 574 |
+
report = analyzer.generate_report(
|
| 575 |
+
pdf_path=pdf_path,
|
| 576 |
+
questions=sample_questions,
|
| 577 |
+
output_file="t5_thesis_analysis_report.txt"
|
| 578 |
+
)
|
| 579 |
+
|
| 580 |
+
print("\nT5-ENHANCED ANALYSIS COMPLETE!")
|
| 581 |
+
print("\nSample of generated report:")
|
| 582 |
+
print("=" * 60)
|
| 583 |
+
print(report[:1500] + "...")
|
| 584 |
+
|
| 585 |
+
except FileNotFoundError:
|
| 586 |
+
print(f"PDF file '{pdf_path}' not found. Please check the file path.")
|
| 587 |
+
except Exception as e:
|
| 588 |
+
print(f"An error occurred: {e}")
|
| 589 |
+
print("Make sure you have installed the required packages:")
|
| 590 |
+
print("pip install torch transformers PyPDF2 nltk")
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
if __name__ == "__main__":
|
| 594 |
+
# Instructions for usage
|
| 595 |
+
print("""
|
| 596 |
+
T5-ENHANCED THESIS ANALYZER - SETUP INSTRUCTIONS
|
| 597 |
+
=================================================
|
| 598 |
+
|
| 599 |
+
1. Install required packages:
|
| 600 |
+
pip install torch transformers PyPDF2 nltk
|
| 601 |
+
|
| 602 |
+
2. First run will download T5-small model (~240MB)
|
| 603 |
+
|
| 604 |
+
3. Update the pdf_path variable with your thesis file path
|
| 605 |
+
|
| 606 |
+
4. The program will use GPU if available, CPU otherwise
|
| 607 |
+
|
| 608 |
+
5. Run the script to generate AI-enhanced analysis report
|
| 609 |
+
|
| 610 |
+
NEW FEATURES WITH T5-SMALL:
|
| 611 |
+
- Advanced text summarization using transformer models
|
| 612 |
+
- Intelligent question answering with context understanding
|
| 613 |
+
- Better key term extraction
|
| 614 |
+
- Enhanced natural language generation
|
| 615 |
+
- Confidence scoring for answers
|
| 616 |
+
|
| 617 |
+
The program will:
|
| 618 |
+
- Load T5-small model from Hugging Face
|
| 619 |
+
- Extract and preprocess text from PDF
|
| 620 |
+
- Generate AI-powered summaries (150-200 words)
|
| 621 |
+
- Answer questions using advanced NLP
|
| 622 |
+
- Save detailed report with technical metrics
|
| 623 |
+
|
| 624 |
+
""")
|
| 625 |
+
|
| 626 |
+
main()
|