PaperPilot / app.py
flytoe's picture
Update app.py
7f6ef04 verified
raw
history blame
4.29 kB
import os
import streamlit as st
import arxiv
import random
import datetime
from groq import Groq
# -------------------------------
# API Clients
# -------------------------------
client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
# -------------------------------
# Helper Functions
# -------------------------------
def groq_summarize(text: str) -> str:
response = client.chat.completions.create(
messages=[{"role": "user", "content": f"Summarize in 250 characters:
{text}"}],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content.strip()
def groq_eli5(text: str) -> str:
response = client.chat.completions.create(
messages=[{"role": "user", "content": f"Explain like I'm 5:
{text}"}],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content.strip()
def calculate_trust_relevance(paper_title):
random.seed(hash(paper_title))
return random.randint(60, 95), random.randint(50, 90)
def retrieve_papers(query, max_results=5):
search = arxiv.Search(query=query, max_results=max_results)
papers = []
for result in search.results():
trust, relevance = calculate_trust_relevance(result.title)
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.strftime('%Y-%m-%d') if isinstance(result.published, datetime.datetime) else "n.d.",
"doi": f"https://doi.org/10.48550/arXiv.{paper_id}",
"bib_explorer": f"https://arxiv.org/abs/{paper_id}",
"litmaps": f"https://app.litmaps.com/preview/{paper_id}",
"trust_score": trust,
"relevance_score": relevance
}
papers.append(paper)
return papers
def get_random_papers():
sample_topics = ["AI ethics", "Quantum computing", "Neuroscience", "Robotics", "Renewable energy", "Space exploration"]
query = random.choice(sample_topics)
return retrieve_papers(query, random.randint(5, 15))
# -------------------------------
# Streamlit UI
# -------------------------------
st.title("πŸ“š PaperPilot – Intelligent Research Navigator")
# Sidebar Controls
with st.sidebar:
st.header("πŸ” Search or Discover")
query = st.text_input("Search topic:")
if st.button("πŸš€ Find Articles"):
if query.strip():
with st.spinner("Searching arXiv..."):
st.session_state.papers = retrieve_papers(query)
st.success(f"Found {len(st.session_state.papers)} papers!")
else:
st.warning("Please enter a search query")
if st.button("🎲 Random Papers"):
with st.spinner("Fetching random papers..."):
st.session_state.papers = get_random_papers()
st.success(f"Found {len(st.session_state.papers)} random papers!")
if "papers" in st.session_state and st.session_state.papers:
st.header("πŸ“‘ Research Feed")
for paper in st.session_state.papers:
with st.expander(f"πŸ” {paper['title']}"):
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
st.markdown(f"**Published:** {paper['published']}")
st.markdown(f"**[PDF Link]({paper['url']})** | **[DOI]({paper['doi']})** | **[Bibliographic Explorer]({paper['bib_explorer']})** | **[Litmaps]({paper['litmaps']})**")
with st.spinner("Summarizing..."):
summary = groq_summarize(paper['summary'])
st.markdown(f"**Summary:** {summary}")
if st.button(f"Explain like I'm 5 🧸 - {paper['title']}"):
with st.spinner("Simplifying..."):
st.write(groq_eli5(paper['summary']))
st.markdown("**Trust & Relevance Scores:**")
st.progress(paper['trust_score'] / 100)
st.caption(f"πŸ”Ή Trust Score: {paper['trust_score']}%")
st.progress(paper['relevance_score'] / 100)
st.caption(f"πŸ”Ή Relevance Score: {paper['relevance_score']}%")
else:
st.info("Enter a query or use the 🎲 Random Papers button to get started!")