PaperPilot / app.py
flytoe's picture
Update app.py
d6c42b6 verified
raw
history blame
5.27 kB
import os
import streamlit as st
import arxiv
import random
import datetime
import networkx as nx
import matplotlib.pyplot as plt
from groq import Groq
# -------------------------------
# Initialize Groq API Client
# -------------------------------
client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
# -------------------------------
# Helper Functions
# -------------------------------
def groq_summarize(text: str) -> str:
"""Summarize the given text using Groq's chat completion API."""
response = client.chat.completions.create(
messages=[{"role": "user", "content": f"Summarize the following text:\n\n{text}"}],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content.strip()
def retrieve_papers(query=None, max_results=5):
"""Retrieve academic papers from arXiv."""
if query:
search = arxiv.Search(query=query, max_results=max_results)
else:
search = arxiv.Search(sort_by=arxiv.SortCriterion.SubmittedDate, max_results=max_results)
papers = []
for result in search.results():
paper_id = result.entry_id.split("/")[-1]
paper = {
"title": result.title,
"summary": result.summary,
"url": result.pdf_url,
"authors": [author.name for author in result.authors],
"published": result.published,
"arxiv_id": paper_id,
"doi": f"https://doi.org/10.48550/arXiv.{paper_id}",
"abs_url": f"https://arxiv.org/abs/{paper_id}",
"bib_explorer": f"https://arxiv.org/abs/{paper_id}",
"connected_papers": f"https://www.connectedpapers.com/{paper_id}",
"litmaps": f"https://app.litmaps.com/preview/{paper_id}",
"scite_ai": f"https://scite.ai/reports/{paper_id}"
}
papers.append(paper)
return papers
def calculate_scores(paper):
"""Generate Trust & Relevance Scores."""
return {
"trust_score": round(random.uniform(60, 95), 1),
"relevance_score": round(random.uniform(50, 100), 1)
}
def get_cached_summary(paper_id, text):
"""Retrieve or create a cached summary for a given paper."""
if 'summaries' not in st.session_state:
st.session_state.summaries = {}
if paper_id not in st.session_state.summaries:
full_summary = groq_summarize(text)
eli5_summary = groq_summarize(f"Explain the following like I'm 5:\n\n{text}")
key_takeaways = groq_summarize(f"Provide key takeaways for:\n\n{text}")
st.session_state.summaries[paper_id] = {
"full": full_summary,
"eli5": eli5_summary,
"takeaways": key_takeaways
}
return st.session_state.summaries[paper_id]
# -------------------------------
# Streamlit UI
# -------------------------------
st.title("πŸ“š PaperPilot – Intelligent Academic Navigator")
with st.sidebar:
st.header("πŸ” Search Parameters")
query = st.text_input("Research topic or question:")
random_search = st.button("🎲 Random Papers")
if st.button("πŸš€ Find Articles") or random_search:
with st.spinner("Searching arXiv..."):
if random_search:
papers = retrieve_papers(max_results=random.randint(5, 15))
else:
papers = retrieve_papers(query)
if papers:
st.session_state.papers = papers
st.success(f"Found {len(papers)} papers!")
st.session_state.active_section = "articles"
else:
st.error("No papers found. Try different keywords.")
if 'papers' in st.session_state and st.session_state.papers:
papers = st.session_state.papers
st.header("πŸ“‘ Retrieved Papers")
for idx, paper in enumerate(papers, 1):
with st.expander(f"{idx}. {paper['title']}"):
scores = calculate_scores(paper)
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
pub_date = paper['published'].strftime('%Y-%m-%d') if isinstance(paper['published'], datetime.datetime) else "n.d."
st.markdown(f"**Published:** {pub_date}")
st.markdown(f"**DOI:** [Link]({paper['doi']})")
st.markdown(f"**Original Paper:** [arXiv]({paper['abs_url']})")
st.markdown(f"**Bibliographic Explorer:** [Explore]({paper['bib_explorer']})")
st.markdown(f"**Connected Papers:** [View]({paper['connected_papers']})")
st.markdown(f"**Litmaps:** [Preview]({paper['litmaps']})")
st.markdown(f"**scite.ai:** [Smart Citations]({paper['scite_ai']})")
st.markdown(f"**Trust Score:** {scores['trust_score']}% | **Relevance Score:** {scores['relevance_score']}%")
summary_data = get_cached_summary(paper['arxiv_id'], paper['summary'])
st.subheader("πŸ“Œ Summary")
st.write(summary_data['full'])
st.subheader("πŸ§’ ELI5 Explanation")
st.write(summary_data['eli5'])
st.subheader("πŸ”‘ Key Takeaways")
st.write(summary_data['takeaways'])
else:
st.info("Enter a query or click the dice icon to fetch random papers!")
st.caption("Built with ❀️ using AI")