File size: 5,267 Bytes
fbec6c3
 
 
a263aa6
d6c42b6
 
 
 
 
 
 
 
 
 
 
fbec6c3
 
a263aa6
fbec6c3
d6c42b6
 
 
 
 
 
 
 
 
a263aa6
d6c42b6
 
a263aa6
d6c42b6
fbec6c3
 
a263aa6
fbec6c3
 
 
 
 
7aa208d
d6c42b6
a263aa6
d6c42b6
a263aa6
 
d6c42b6
 
fbec6c3
 
 
 
d6c42b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a263aa6
d6c42b6
a263aa6
fbec6c3
 
 
 
 
d6c42b6
1220468
d6c42b6
 
 
 
a263aa6
d6c42b6
a263aa6
 
 
 
d6c42b6
a263aa6
d6c42b6
a263aa6
d6c42b6
 
a263aa6
d6c42b6
a263aa6
d6c42b6
a263aa6
d6c42b6
a263aa6
d6c42b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220468
d6c42b6
fbec6c3
42e32f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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")