Spaces:
Build error
Build error
Update app.py
Browse files17 unmodified lines181920212223244 unmodified lines29303132333435365 unmodified lines4243444546474849505152537 unmodified lines6162636465666768697071727374757677787980818283848517 unmodified lines# ---------------------# Model Setup# ---------------------MODEL_NAME = "allenai/scibert_scivocab_cased" # Example model for tokenization/embeddingSUMMARIZATION_MODEL = "allenai/led-base-16384" # Example summarization model with a large context window# Load summarization model and tokenizer4 unmodified lines# Utility Functions# ---------------------def extract_text_from_pdf(pdf_file):# This function extracts text from a PDF file. Requires PyPDF2 or similar library.# For Hugging Face Spaces, PyPDF2 often works.try:import PyPDF2reader = PyPDF2.PdfReader(pdf_file)5 unmodified linesreturn f"Error reading PDF: {e}"def clean_text(text):# Basic cleaning functiontext = re.sub(r'\s+', ' ', text).strip()return textdef summarize_text(text):# Summarize the given textinputs = summarizer_tokenizer(text, return_tensors="pt", truncation=True, max_length=16384)with torch.no_grad():summary_ids = summarizer_model.generate(7 unmodified linessummary = summarizer_tokenizer.decode(summary_ids[0], skip_special_tokens=True)return summarydef analyze_text(text):# In a more elaborate system, you might:# 1. Extract main findings using IE or NER.# 2. Identify methods mentioned.# 3. Extract references (regex patterns for citations).# Here we just do a simple summarization.text_clean = clean_text(text)if len(text_clean) < 50:return "Please provide a longer text snippet or PDF."summary = summarize_text(text_clean)# Dummy logic for key methods and references (in a real app, use NLP-based extraction)methods = "Key methods extraction is not yet implemented."references = "Reference extraction is not yet implemented."return summary, methods, referencesdef process_input(pdf_file, text_snippet):# If PDF is provided, extract text from PDFinput_text = ""if pdf_file is not None:input_text = extract_text_from_pdf(pdf_file)17 unmodified lines181920212223244 unmodified lines2930313233345 unmodified lines404142434445464748497 unmodified lines5758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610717 unmodified lines# ---------------------# Model Setup# ---------------------MODEL_NAME = "allenai/scibert_scivocab_cased" # Example tokenizer model (not directly used for summarization)SUMMARIZATION_MODEL = "allenai/led-base-16384" # Example summarization model with a large context window# Load summarization model and tokenizer4 unmodified lines# Utility Functions# ---------------------def extract_text_from_pdf(pdf_file):try:import PyPDF2reader = PyPDF2.PdfReader(pdf_file)5 unmodified linesreturn f"Error reading PDF: {e}"def clean_text(text):text = re.sub(r'\s+', ' ', text).strip()return textdef summarize_text(text):inputs = summarizer_tokenizer(text, return_tensors="pt", truncation=True, max_length=16384)with torch.no_grad():summary_ids = summarizer_model.generate(7 unmodified linessummary = summarizer_tokenizer.decode(summary_ids[0], skip_special_tokens=True)return summarydef extract_methods(text):# A very naive approach: search for paragraphs containing method-related keywords# and return them as "Key Methods".methods_keywords = ["method", "methods", "technique", "procedure", "protocol", "experimental approach"]paragraphs = re.split(r'\n+|\. ', text)method_sentences = [p.strip() for p in paragraphs if any(kw in p.lower() for kw in methods_keywords)]if method_sentences:return " ".join(method_sentences)else:return "No explicit methods found using simple keyword search."def extract_references(text):# A naive approach for references:# Look for patterns like "et al., 20XX", "(Author, Year)", or numeric citations [XX].# This is a heuristic and may produce false positives.# Common patterns:# - Something like "Smith et al., 2020"# - (Smith et al., 2020)# - [1], [2], etc., at the end of sentences.references_pattern = r"([A-Z][a-zA-Z]+ et al\.,?\s?\d{4})|(\(\S+ et al\.,?\s?\d{4}\))|(\[\d+\])"refs_found = re.findall(references_pattern, text)# refs_found will be a list of tuples due to multiple groups, flatten them:flat_refs = []for tup in refs_found:for ref in tup:if ref:flat_refs.append(ref.strip())flat_refs = list(set(flat_refs)) # remove duplicatesif flat_refs:return "Possible References Found:\n" + "\n".join(flat_refs)else:return "No explicit references found using simple pattern search."def analyze_text(text):text_clean = clean_text(text)if len(text_clean) < 50:return "Please provide a longer text snippet or PDF.", "", ""summary = summarize_text(text_clean)methods = extract_methods(text_clean)references = extract_references(text_clean)return summary, methods, referencesdef process_input(pdf_file, text_snippet):# If PDF is provided, extract textinput_text = ""if pdf_file is not None:input_text = extract_text_from_pdf(pdf_file)
app.py+38-16