Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,131 +3,107 @@ import streamlit as st
|
|
| 3 |
import arxiv
|
| 4 |
import random
|
| 5 |
import datetime
|
|
|
|
| 6 |
|
| 7 |
# -------------------------------
|
| 8 |
-
#
|
| 9 |
# -------------------------------
|
| 10 |
-
from groq import Groq
|
| 11 |
-
|
| 12 |
client = Groq(
|
| 13 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 14 |
)
|
| 15 |
|
| 16 |
# -------------------------------
|
| 17 |
-
# Helper Functions
|
| 18 |
# -------------------------------
|
| 19 |
def groq_summarize(text: str) -> str:
|
| 20 |
-
"""
|
| 21 |
-
Summarize the given text using Groq's chat completion API.
|
| 22 |
-
"""
|
| 23 |
response = client.chat.completions.create(
|
| 24 |
-
messages=[
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"content": f"Summarize this paper in one sentence and provide 3 key takeaways:\n\n{text}"
|
| 28 |
-
}
|
| 29 |
-
],
|
| 30 |
model="llama-3.3-70b-versatile",
|
| 31 |
)
|
| 32 |
return response.choices[0].message.content.strip()
|
| 33 |
|
| 34 |
-
|
| 35 |
def groq_eli5(text: str) -> str:
|
| 36 |
-
"""
|
| 37 |
-
Explain the paper like I'm 5 years old.
|
| 38 |
-
"""
|
| 39 |
response = client.chat.completions.create(
|
| 40 |
-
messages=[
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"content": f"Explain this paper as if I were 5 years old in one sentence:\n\n{text}"
|
| 44 |
-
}
|
| 45 |
-
],
|
| 46 |
model="llama-3.3-70b-versatile",
|
| 47 |
)
|
| 48 |
return response.choices[0].message.content.strip()
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
"""
|
| 53 |
-
Generate trust and relevance scores for a paper.
|
| 54 |
-
"""
|
| 55 |
-
trust_score = random.randint(5, 10) # Placeholder, can be improved with citations data
|
| 56 |
-
relevance_score = random.randint(5, 10) # Placeholder, can use NLP topic matching
|
| 57 |
-
return trust_score, relevance_score
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def retrieve_papers(query=None, max_results=10, random_mode=False):
|
| 61 |
-
"""
|
| 62 |
-
Retrieve academic papers from arXiv, either based on search or randomly.
|
| 63 |
-
"""
|
| 64 |
-
if random_mode:
|
| 65 |
-
query = "" # Empty query fetches random results
|
| 66 |
-
|
| 67 |
search = arxiv.Search(query=query, max_results=max_results)
|
| 68 |
papers = []
|
| 69 |
-
|
| 70 |
for result in search.results():
|
| 71 |
-
|
|
|
|
| 72 |
paper = {
|
| 73 |
"title": result.title,
|
| 74 |
"summary": result.summary,
|
| 75 |
"url": result.pdf_url,
|
| 76 |
"authors": [author.name for author in result.authors],
|
| 77 |
"published": result.published.strftime('%Y-%m-%d') if isinstance(result.published, datetime.datetime) else "n.d.",
|
| 78 |
-
"doi": f"https://doi.org/10.48550/arXiv.{
|
| 79 |
-
"bib_explorer": f"https://arxiv.org/abs/{
|
| 80 |
-
"litmaps": f"https://app.litmaps.com/preview/{
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"relevance_score": relevance_score
|
| 84 |
}
|
| 85 |
papers.append(paper)
|
| 86 |
return papers
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# -------------------------------
|
| 89 |
-
# Streamlit
|
| 90 |
# -------------------------------
|
| 91 |
-
st.title("π PaperPilot β Intelligent
|
| 92 |
|
|
|
|
| 93 |
with st.sidebar:
|
| 94 |
-
st.header("π Search
|
| 95 |
-
query = st.text_input("
|
| 96 |
-
|
| 97 |
-
col1, col2 = st.columns([4, 1])
|
| 98 |
-
with col1:
|
| 99 |
-
search_button = st.button("π Find Articles")
|
| 100 |
-
with col2:
|
| 101 |
-
random_button = st.button("π² Random Papers")
|
| 102 |
-
|
| 103 |
-
if search_button:
|
| 104 |
if query.strip():
|
| 105 |
with st.spinner("Searching arXiv..."):
|
| 106 |
-
st.session_state.papers = retrieve_papers(query
|
| 107 |
st.success(f"Found {len(st.session_state.papers)} papers!")
|
| 108 |
else:
|
| 109 |
st.warning("Please enter a search query")
|
| 110 |
-
|
| 111 |
-
if random_button:
|
| 112 |
with st.spinner("Fetching random papers..."):
|
| 113 |
-
st.session_state.papers =
|
| 114 |
-
st.success(f"
|
| 115 |
|
| 116 |
-
if
|
| 117 |
-
st.header("π
|
| 118 |
-
for
|
| 119 |
-
with st.expander(f"
|
| 120 |
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
|
| 121 |
st.markdown(f"**Published:** {paper['published']}")
|
| 122 |
-
st.markdown(f"**[PDF Link]({paper['url']})** | **[DOI]({paper['doi']})** | **[
|
| 123 |
|
| 124 |
-
with st.spinner("
|
| 125 |
summary = groq_summarize(paper['summary'])
|
| 126 |
-
eli5_summary = groq_eli5(paper['summary'])
|
| 127 |
-
|
| 128 |
st.markdown(f"**Summary:** {summary}")
|
| 129 |
-
st.markdown(f"**ELI5:** {eli5_summary}")
|
| 130 |
|
| 131 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
else:
|
| 133 |
-
st.info("Enter a query or
|
|
|
|
| 3 |
import arxiv
|
| 4 |
import random
|
| 5 |
import datetime
|
| 6 |
+
from groq import Groq
|
| 7 |
|
| 8 |
# -------------------------------
|
| 9 |
+
# API Clients
|
| 10 |
# -------------------------------
|
|
|
|
|
|
|
| 11 |
client = Groq(
|
| 12 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 13 |
)
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# Helper Functions
|
| 17 |
# -------------------------------
|
| 18 |
def groq_summarize(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 19 |
response = client.chat.completions.create(
|
| 20 |
+
messages=[{"role": "user", "content": f"Summarize in 250 characters:
|
| 21 |
+
|
| 22 |
+
{text}"}],
|
|
|
|
|
|
|
|
|
|
| 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=[{"role": "user", "content": f"Explain like I'm 5:
|
| 30 |
+
|
| 31 |
+
{text}"}],
|
|
|
|
|
|
|
|
|
|
| 32 |
model="llama-3.3-70b-versatile",
|
| 33 |
)
|
| 34 |
return response.choices[0].message.content.strip()
|
| 35 |
|
| 36 |
+
def calculate_trust_relevance(paper_title):
|
| 37 |
+
random.seed(hash(paper_title))
|
| 38 |
+
return random.randint(60, 95), random.randint(50, 90)
|
| 39 |
|
| 40 |
+
def retrieve_papers(query, max_results=5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
search = arxiv.Search(query=query, max_results=max_results)
|
| 42 |
papers = []
|
|
|
|
| 43 |
for result in search.results():
|
| 44 |
+
trust, relevance = calculate_trust_relevance(result.title)
|
| 45 |
+
paper_id = result.entry_id.split('/')[-1]
|
| 46 |
paper = {
|
| 47 |
"title": result.title,
|
| 48 |
"summary": result.summary,
|
| 49 |
"url": result.pdf_url,
|
| 50 |
"authors": [author.name for author in result.authors],
|
| 51 |
"published": result.published.strftime('%Y-%m-%d') if isinstance(result.published, datetime.datetime) else "n.d.",
|
| 52 |
+
"doi": f"https://doi.org/10.48550/arXiv.{paper_id}",
|
| 53 |
+
"bib_explorer": f"https://arxiv.org/abs/{paper_id}",
|
| 54 |
+
"litmaps": f"https://app.litmaps.com/preview/{paper_id}",
|
| 55 |
+
"trust_score": trust,
|
| 56 |
+
"relevance_score": relevance
|
|
|
|
| 57 |
}
|
| 58 |
papers.append(paper)
|
| 59 |
return papers
|
| 60 |
|
| 61 |
+
def get_random_papers():
|
| 62 |
+
sample_topics = ["AI ethics", "Quantum computing", "Neuroscience", "Robotics", "Renewable energy", "Space exploration"]
|
| 63 |
+
query = random.choice(sample_topics)
|
| 64 |
+
return retrieve_papers(query, random.randint(5, 15))
|
| 65 |
+
|
| 66 |
# -------------------------------
|
| 67 |
+
# Streamlit UI
|
| 68 |
# -------------------------------
|
| 69 |
+
st.title("π PaperPilot β Intelligent Research Navigator")
|
| 70 |
|
| 71 |
+
# Sidebar Controls
|
| 72 |
with st.sidebar:
|
| 73 |
+
st.header("π Search or Discover")
|
| 74 |
+
query = st.text_input("Search topic:")
|
| 75 |
+
if st.button("π Find Articles"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if query.strip():
|
| 77 |
with st.spinner("Searching arXiv..."):
|
| 78 |
+
st.session_state.papers = retrieve_papers(query)
|
| 79 |
st.success(f"Found {len(st.session_state.papers)} papers!")
|
| 80 |
else:
|
| 81 |
st.warning("Please enter a search query")
|
| 82 |
+
if st.button("π² Random Papers"):
|
|
|
|
| 83 |
with st.spinner("Fetching random papers..."):
|
| 84 |
+
st.session_state.papers = get_random_papers()
|
| 85 |
+
st.success(f"Found {len(st.session_state.papers)} random papers!")
|
| 86 |
|
| 87 |
+
if "papers" in st.session_state and st.session_state.papers:
|
| 88 |
+
st.header("π Research Feed")
|
| 89 |
+
for paper in st.session_state.papers:
|
| 90 |
+
with st.expander(f"π {paper['title']}"):
|
| 91 |
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
|
| 92 |
st.markdown(f"**Published:** {paper['published']}")
|
| 93 |
+
st.markdown(f"**[PDF Link]({paper['url']})** | **[DOI]({paper['doi']})** | **[Bibliographic Explorer]({paper['bib_explorer']})** | **[Litmaps]({paper['litmaps']})**")
|
| 94 |
|
| 95 |
+
with st.spinner("Summarizing..."):
|
| 96 |
summary = groq_summarize(paper['summary'])
|
|
|
|
|
|
|
| 97 |
st.markdown(f"**Summary:** {summary}")
|
|
|
|
| 98 |
|
| 99 |
+
if st.button(f"Explain like I'm 5 π§Έ - {paper['title']}"):
|
| 100 |
+
with st.spinner("Simplifying..."):
|
| 101 |
+
st.write(groq_eli5(paper['summary']))
|
| 102 |
+
|
| 103 |
+
st.markdown("**Trust & Relevance Scores:**")
|
| 104 |
+
st.progress(paper['trust_score'] / 100)
|
| 105 |
+
st.caption(f"πΉ Trust Score: {paper['trust_score']}%")
|
| 106 |
+
st.progress(paper['relevance_score'] / 100)
|
| 107 |
+
st.caption(f"πΉ Relevance Score: {paper['relevance_score']}%")
|
| 108 |
else:
|
| 109 |
+
st.info("Enter a query or use the π² Random Papers button to get started!")
|