Spaces:
Build error
Build error
remove function
Browse files
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
import arxiv
|
| 6 |
-
import networkx as nx
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import datetime
|
| 9 |
|
| 10 |
# -------------------------------
|
|
@@ -20,42 +16,36 @@ client = Groq(
|
|
| 20 |
# Helper Functions (Groq-based)
|
| 21 |
# -------------------------------
|
| 22 |
def groq_summarize(text: str) -> str:
|
| 23 |
-
"""
|
| 24 |
-
Summarize the given text using Groq's chat completion API.
|
| 25 |
-
Adjust the prompt or model as needed.
|
| 26 |
-
"""
|
| 27 |
response = client.chat.completions.create(
|
| 28 |
messages=[
|
| 29 |
-
{
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
],
|
| 34 |
model="llama-3.3-70b-versatile",
|
| 35 |
)
|
| 36 |
return response.choices[0].message.content.strip()
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
"""
|
| 40 |
-
Generate text (e.g., research proposals) using Groq's chat completion API.
|
| 41 |
-
Adjust the prompt or model as needed.
|
| 42 |
-
"""
|
| 43 |
response = client.chat.completions.create(
|
| 44 |
messages=[
|
| 45 |
-
{
|
| 46 |
-
"role": "user",
|
| 47 |
-
"content": text
|
| 48 |
-
}
|
| 49 |
],
|
| 50 |
model="llama-3.3-70b-versatile",
|
| 51 |
)
|
| 52 |
return response.choices[0].message.content.strip()
|
| 53 |
|
| 54 |
# -------------------------------
|
| 55 |
-
#
|
| 56 |
# -------------------------------
|
| 57 |
def retrieve_papers(query, max_results=5):
|
| 58 |
-
"""Retrieve academic papers from arXiv."""
|
| 59 |
search = arxiv.Search(query=query, max_results=max_results)
|
| 60 |
papers = []
|
| 61 |
for result in search.results():
|
|
@@ -69,74 +59,16 @@ def retrieve_papers(query, max_results=5):
|
|
| 69 |
papers.append(paper)
|
| 70 |
return papers
|
| 71 |
|
| 72 |
-
def summarize_text(text):
|
| 73 |
-
"""
|
| 74 |
-
Wrap the groq_summarize function so it's easy to switch
|
| 75 |
-
implementations if needed.
|
| 76 |
-
"""
|
| 77 |
-
return groq_summarize(text)
|
| 78 |
-
|
| 79 |
-
def generate_concept_map(papers):
|
| 80 |
-
"""Create a concept map (graph) based on author connections."""
|
| 81 |
-
G = nx.Graph()
|
| 82 |
-
for paper in papers:
|
| 83 |
-
G.add_node(paper['title'])
|
| 84 |
-
for i in range(len(papers)):
|
| 85 |
-
for j in range(i + 1, len(papers)):
|
| 86 |
-
if set(papers[i]['authors']) & set(papers[j]['authors']):
|
| 87 |
-
G.add_edge(papers[i]['title'], papers[j]['title'])
|
| 88 |
-
return G
|
| 89 |
-
|
| 90 |
-
def generate_citation(paper):
|
| 91 |
-
"""Generate APA-style citation for a paper."""
|
| 92 |
-
authors = ", ".join(paper['authors'])
|
| 93 |
-
if isinstance(paper['published'], datetime.datetime):
|
| 94 |
-
year = paper['published'].year
|
| 95 |
-
else:
|
| 96 |
-
year = "n.d."
|
| 97 |
-
return f"{authors} ({year}). {paper['title']}. Retrieved from {paper['url']}"
|
| 98 |
-
|
| 99 |
-
def generate_proposal_suggestions(text):
|
| 100 |
-
"""
|
| 101 |
-
Generate novel research proposal suggestions based on text,
|
| 102 |
-
wrapping the groq_generate function.
|
| 103 |
-
"""
|
| 104 |
-
prompt = (
|
| 105 |
-
f"Based on this research summary:\n\n{text}\n\n"
|
| 106 |
-
"Propose novel research directions:"
|
| 107 |
-
)
|
| 108 |
-
return groq_generate(prompt)
|
| 109 |
-
|
| 110 |
-
def get_cached_summary(paper_id, text):
|
| 111 |
-
"""
|
| 112 |
-
Retrieve or create a cached summary for a given paper.
|
| 113 |
-
This ensures each paper's summary is generated only once.
|
| 114 |
-
"""
|
| 115 |
-
if 'summaries' not in st.session_state:
|
| 116 |
-
st.session_state.summaries = {}
|
| 117 |
-
if paper_id not in st.session_state.summaries:
|
| 118 |
-
st.session_state.summaries[paper_id] = summarize_text(text)
|
| 119 |
-
return st.session_state.summaries[paper_id]
|
| 120 |
-
|
| 121 |
# -------------------------------
|
| 122 |
# Streamlit Interface
|
| 123 |
# -------------------------------
|
| 124 |
st.title("📚 PaperPilot – Intelligent Academic Navigator")
|
| 125 |
|
| 126 |
-
# Add the Overview subheading
|
| 127 |
st.write("""
|
| 128 |
-
PaperPilot
|
| 129 |
-
|
| 130 |
-
comprehensive toolkit to explore them in depth. You can read a quick summary of each article,
|
| 131 |
-
view a visual concept map to see how different papers are interlinked, generate properly
|
| 132 |
-
formatted citations, and even receive suggestions for novel research proposals. By integrating
|
| 133 |
-
state-of-the-art AI models, PaperPilot streamlines the entire literature review process—making
|
| 134 |
-
it easier to stay organized, discover new insights, and advance your academic endeavors.
|
| 135 |
""")
|
| 136 |
|
| 137 |
-
# ---------------------------------
|
| 138 |
-
# Sidebar: Search & Navigation
|
| 139 |
-
# ---------------------------------
|
| 140 |
with st.sidebar:
|
| 141 |
st.header("🔍 Search Parameters")
|
| 142 |
query = st.text_input("Research topic or question:")
|
|
@@ -148,120 +80,37 @@ with st.sidebar:
|
|
| 148 |
if papers:
|
| 149 |
st.session_state.papers = papers
|
| 150 |
st.success(f"Found {len(papers)} papers!")
|
| 151 |
-
|
| 152 |
-
st.session_state.active_section = "articles"
|
| 153 |
else:
|
| 154 |
st.error("No papers found. Try different keywords.")
|
| 155 |
else:
|
| 156 |
st.warning("Please enter a search query")
|
| 157 |
|
| 158 |
-
# Navigation buttons (only relevant if we have papers in session)
|
| 159 |
-
if 'papers' in st.session_state and st.session_state.papers:
|
| 160 |
-
st.header("🔀 Navigation")
|
| 161 |
-
if st.button("📑 Show Articles"):
|
| 162 |
-
st.session_state.active_section = "articles"
|
| 163 |
-
if st.button("📚 Literature Review & Summary"):
|
| 164 |
-
st.session_state.active_section = "review"
|
| 165 |
-
if st.button("🔍 Concept & Visual Graph"):
|
| 166 |
-
st.session_state.active_section = "graph"
|
| 167 |
-
if st.button("📝 Formatted Citations"):
|
| 168 |
-
st.session_state.active_section = "citations"
|
| 169 |
-
if st.button("💡 Research Proposal"):
|
| 170 |
-
st.session_state.active_section = "proposal"
|
| 171 |
-
|
| 172 |
-
# ---------------------------------
|
| 173 |
-
# Main Content Area
|
| 174 |
-
# ---------------------------------
|
| 175 |
-
if 'active_section' not in st.session_state:
|
| 176 |
-
st.session_state.active_section = "none"
|
| 177 |
-
|
| 178 |
if 'papers' in st.session_state and st.session_state.papers:
|
| 179 |
papers = st.session_state.papers
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# ---------------------------------
|
| 184 |
-
if st.session_state.active_section == "articles":
|
| 185 |
-
st.header("📑 Retrieved Papers")
|
| 186 |
for idx, paper in enumerate(papers, 1):
|
| 187 |
with st.expander(f"{idx}. {paper['title']}"):
|
| 188 |
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
|
| 189 |
-
if isinstance(paper['published'], datetime.datetime)
|
| 190 |
-
pub_date = paper['published'].strftime('%Y-%m-%d')
|
| 191 |
-
else:
|
| 192 |
-
pub_date = "n.d."
|
| 193 |
st.markdown(f"**Published:** {pub_date}")
|
| 194 |
-
st.markdown(f"**Link:** [PDF
|
| 195 |
-
|
| 196 |
-
st.
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
summary = get_cached_summary(paper_id, paper['summary'])
|
| 210 |
-
st.write(summary)
|
| 211 |
-
combined_summary += summary + "\n\n"
|
| 212 |
-
|
| 213 |
-
st.session_state.combined_summary = combined_summary
|
| 214 |
-
|
| 215 |
-
# ---------------------------------
|
| 216 |
-
# 3) Concept & Visual Graph
|
| 217 |
-
# ---------------------------------
|
| 218 |
-
elif st.session_state.active_section == "graph":
|
| 219 |
-
st.header("🔍 Concept & Visual Graph")
|
| 220 |
-
st.write(
|
| 221 |
-
"Below is a concept map that visualizes how the authors are "
|
| 222 |
-
"connected across the retrieved articles. Each node represents a paper, "
|
| 223 |
-
"and edges indicate shared authors."
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
with st.spinner("Generating concept map..."):
|
| 227 |
-
G = generate_concept_map(papers)
|
| 228 |
-
if G.nodes():
|
| 229 |
-
fig, ax = plt.subplots(figsize=(10, 8))
|
| 230 |
-
pos = nx.spring_layout(G, k=0.5, seed=42)
|
| 231 |
-
nx.draw_networkx_nodes(G, pos, node_color='skyblue', node_size=2000, ax=ax)
|
| 232 |
-
nx.draw_networkx_edges(G, pos, edge_color='#666666', ax=ax)
|
| 233 |
-
nx.draw_networkx_labels(G, pos, font_size=10, ax=ax)
|
| 234 |
-
ax.axis('off')
|
| 235 |
-
st.pyplot(fig)
|
| 236 |
-
else:
|
| 237 |
-
st.info("No significant connections found between papers.")
|
| 238 |
-
|
| 239 |
-
# ---------------------------------
|
| 240 |
-
# 4) Formatted Citations
|
| 241 |
-
# ---------------------------------
|
| 242 |
-
elif st.session_state.active_section == "citations":
|
| 243 |
-
st.header("📝 Formatted Citations (APA Style)")
|
| 244 |
-
for paper in papers:
|
| 245 |
-
st.markdown(f"- {generate_citation(paper)}")
|
| 246 |
-
|
| 247 |
-
# ---------------------------------
|
| 248 |
-
# 5) Research Proposal
|
| 249 |
-
# ---------------------------------
|
| 250 |
-
elif st.session_state.active_section == "proposal":
|
| 251 |
-
st.header("💡 Research Proposal Suggestions")
|
| 252 |
-
|
| 253 |
-
# Make sure we have a combined summary for the proposals
|
| 254 |
-
if 'combined_summary' not in st.session_state:
|
| 255 |
-
with st.spinner("Synthesizing research overview..."):
|
| 256 |
-
full_text = "\n".join([p['summary'] for p in papers])
|
| 257 |
-
st.session_state.combined_summary = summarize_text(full_text)
|
| 258 |
-
|
| 259 |
-
with st.spinner("Generating innovative ideas..."):
|
| 260 |
-
proposal = generate_proposal_suggestions(st.session_state.combined_summary[:2000])
|
| 261 |
-
st.write(proposal)
|
| 262 |
-
|
| 263 |
-
else:
|
| 264 |
-
st.info("Please select an option from the sidebar to begin.")
|
| 265 |
else:
|
| 266 |
st.info("Enter a query in the sidebar and click 'Find Articles' to get started.")
|
| 267 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import arxiv
|
|
|
|
|
|
|
| 4 |
import datetime
|
| 5 |
|
| 6 |
# -------------------------------
|
|
|
|
| 16 |
# Helper Functions (Groq-based)
|
| 17 |
# -------------------------------
|
| 18 |
def groq_summarize(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
response = client.chat.completions.create(
|
| 20 |
messages=[
|
| 21 |
+
{"role": "user", "content": f"Summarize the following text concisely:\n\n{text}"}
|
| 22 |
+
],
|
| 23 |
+
model="llama-3.3-70b-versatile",
|
| 24 |
+
)
|
| 25 |
+
return response.choices[0].message.content.strip()
|
| 26 |
+
|
| 27 |
+
def groq_eli5(text: str) -> str:
|
| 28 |
+
response = client.chat.completions.create(
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "user", "content": f"Explain this like I'm 5 years old:\n\n{text}"}
|
| 31 |
],
|
| 32 |
model="llama-3.3-70b-versatile",
|
| 33 |
)
|
| 34 |
return response.choices[0].message.content.strip()
|
| 35 |
|
| 36 |
+
def groq_key_takeaways(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
response = client.chat.completions.create(
|
| 38 |
messages=[
|
| 39 |
+
{"role": "user", "content": f"List the key takeaways from this research:\n\n{text}"}
|
|
|
|
|
|
|
|
|
|
| 40 |
],
|
| 41 |
model="llama-3.3-70b-versatile",
|
| 42 |
)
|
| 43 |
return response.choices[0].message.content.strip()
|
| 44 |
|
| 45 |
# -------------------------------
|
| 46 |
+
# Paper Retrieval & Processing
|
| 47 |
# -------------------------------
|
| 48 |
def retrieve_papers(query, max_results=5):
|
|
|
|
| 49 |
search = arxiv.Search(query=query, max_results=max_results)
|
| 50 |
papers = []
|
| 51 |
for result in search.results():
|
|
|
|
| 59 |
papers.append(paper)
|
| 60 |
return papers
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# -------------------------------
|
| 63 |
# Streamlit Interface
|
| 64 |
# -------------------------------
|
| 65 |
st.title("📚 PaperPilot – Intelligent Academic Navigator")
|
| 66 |
|
|
|
|
| 67 |
st.write("""
|
| 68 |
+
PaperPilot helps you quickly analyze research papers by summarizing them, highlighting key takeaways, and explaining complex topics in simple terms.
|
| 69 |
+
Enter a query and get structured insights instantly!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
""")
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
with st.sidebar:
|
| 73 |
st.header("🔍 Search Parameters")
|
| 74 |
query = st.text_input("Research topic or question:")
|
|
|
|
| 80 |
if papers:
|
| 81 |
st.session_state.papers = papers
|
| 82 |
st.success(f"Found {len(papers)} papers!")
|
| 83 |
+
st.session_state.active_section = "review"
|
|
|
|
| 84 |
else:
|
| 85 |
st.error("No papers found. Try different keywords.")
|
| 86 |
else:
|
| 87 |
st.warning("Please enter a search query")
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if 'papers' in st.session_state and st.session_state.papers:
|
| 90 |
papers = st.session_state.papers
|
| 91 |
+
|
| 92 |
+
if st.session_state.active_section == "review":
|
| 93 |
+
st.header("📚 Literature Review & Summary")
|
|
|
|
|
|
|
|
|
|
| 94 |
for idx, paper in enumerate(papers, 1):
|
| 95 |
with st.expander(f"{idx}. {paper['title']}"):
|
| 96 |
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
|
| 97 |
+
pub_date = paper['published'].strftime('%Y-%m-%d') if isinstance(paper['published'], datetime.datetime) else "n.d."
|
|
|
|
|
|
|
|
|
|
| 98 |
st.markdown(f"**Published:** {pub_date}")
|
| 99 |
+
st.markdown(f"**Link:** [PDF]({paper['url']})")
|
| 100 |
+
|
| 101 |
+
with st.spinner("Generating insights..."):
|
| 102 |
+
short_description = groq_summarize(paper['summary'])
|
| 103 |
+
key_takeaways = groq_key_takeaways(paper['summary'])
|
| 104 |
+
eli5_explanation = groq_eli5(paper['summary'])
|
| 105 |
+
|
| 106 |
+
st.subheader("Short Description")
|
| 107 |
+
st.write(short_description)
|
| 108 |
+
|
| 109 |
+
st.subheader("Key Takeaways")
|
| 110 |
+
st.write(key_takeaways)
|
| 111 |
+
|
| 112 |
+
st.subheader("Explain Like I'm 5 (ELI5)")
|
| 113 |
+
st.write(eli5_explanation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
st.info("Enter a query in the sidebar and click 'Find Articles' to get started.")
|
| 116 |
|