Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pdfplumber
|
| 3 |
+
import re
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Initialize LLMs
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_models():
|
| 9 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 10 |
+
critic = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 11 |
+
return summarizer, critic
|
| 12 |
+
|
| 13 |
+
summarizer, critic = load_models()
|
| 14 |
+
|
| 15 |
+
# PDF Text Extraction
|
| 16 |
+
def extract_text_from_pdf(pdf_file):
|
| 17 |
+
text = ""
|
| 18 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 19 |
+
for page in pdf.pages:
|
| 20 |
+
page_text = page.extract_text()
|
| 21 |
+
if page_text:
|
| 22 |
+
text += page_text + "\n"
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
# Sectioning Logic
|
| 26 |
+
def split_into_sections(text):
|
| 27 |
+
sections = {}
|
| 28 |
+
headings = ["abstract", "introduction", "methodology", "methods", "results", "discussion", "conclusion", "references"]
|
| 29 |
+
current_section = "others"
|
| 30 |
+
sections[current_section] = ""
|
| 31 |
+
|
| 32 |
+
for line in text.splitlines():
|
| 33 |
+
line_lower = line.lower().strip()
|
| 34 |
+
if any(h in line_lower for h in headings):
|
| 35 |
+
current_section = next((h for h in headings if h in line_lower), "others")
|
| 36 |
+
sections[current_section] = ""
|
| 37 |
+
sections[current_section] += line + "\n"
|
| 38 |
+
|
| 39 |
+
return sections
|
| 40 |
+
|
| 41 |
+
# NLP Functions
|
| 42 |
+
def summarize_section(text, max_len=300):
|
| 43 |
+
return summarizer(text, max_length=max_len, min_length=100, do_sample=False)[0]['summary_text']
|
| 44 |
+
|
| 45 |
+
def critique_section(text):
|
| 46 |
+
prompt = f"Critically evaluate this section:\n{text}\nList strengths, weaknesses, and improvements."
|
| 47 |
+
return critic(prompt, max_length=512, do_sample=False)[0]['generated_text']
|
| 48 |
+
|
| 49 |
+
def identify_research_gaps(text):
|
| 50 |
+
prompt = f"Identify research gaps or unanswered questions in the following study:\n{text}"
|
| 51 |
+
return critic(prompt, max_length=512, do_sample=False)[0]['generated_text']
|
| 52 |
+
|
| 53 |
+
# Streamlit UI
|
| 54 |
+
st.set_page_config(page_title="CritiqueGen", layout="wide")
|
| 55 |
+
st.title("π Research Paper Critique Generator")
|
| 56 |
+
|
| 57 |
+
uploaded_file = st.file_uploader("Upload a research paper (PDF)", type="pdf")
|
| 58 |
+
|
| 59 |
+
if uploaded_file:
|
| 60 |
+
with st.spinner("Reading and analyzing the paper..."):
|
| 61 |
+
full_text = extract_text_from_pdf(uploaded_file)
|
| 62 |
+
sections = split_into_sections(full_text)
|
| 63 |
+
|
| 64 |
+
st.header("π Summary")
|
| 65 |
+
summary = summarize_section(full_text[:3000])
|
| 66 |
+
st.write(summary)
|
| 67 |
+
|
| 68 |
+
st.header("π Section-wise Critique and Suggestions")
|
| 69 |
+
for sec, content in sections.items():
|
| 70 |
+
if content.strip():
|
| 71 |
+
with st.expander(f"π {sec.capitalize()}"):
|
| 72 |
+
st.subheader("Critique")
|
| 73 |
+
st.write(critique_section(content[:1000])) # Limit input size
|
| 74 |
+
st.subheader("Research Gaps")
|
| 75 |
+
st.write(identify_research_gaps(content[:1000]))
|
| 76 |
+
|
| 77 |
+
st.success("Analysis complete β
")
|