Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,108 +2,98 @@ import os
|
|
2 |
import streamlit as st
|
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=[
|
21 |
-
|
22 |
-
|
23 |
model="llama-3.3-70b-versatile",
|
24 |
)
|
25 |
return response.choices[0].message.content.strip()
|
26 |
|
27 |
-
def
|
28 |
response = client.chat.completions.create(
|
29 |
-
messages=[{"role": "user", "content":
|
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 |
-
|
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
|
52 |
"doi": f"https://doi.org/10.48550/arXiv.{paper_id}",
|
53 |
-
"
|
54 |
"litmaps": f"https://app.litmaps.com/preview/{paper_id}",
|
55 |
-
"trust_score":
|
56 |
-
"relevance_score":
|
57 |
}
|
58 |
papers.append(paper)
|
59 |
return papers
|
60 |
|
61 |
-
def
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
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
|
88 |
-
st.
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
st.
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
st.write(
|
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!")
|
|
|
2 |
import streamlit as st
|
3 |
import arxiv
|
4 |
import random
|
5 |
+
import networkx as nx
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
import datetime
|
8 |
from groq import Groq
|
9 |
|
10 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
12 |
def groq_summarize(text: str) -> str:
|
13 |
response = client.chat.completions.create(
|
14 |
+
messages=[
|
15 |
+
{"role": "user", "content": f"Summarize in 250 characters:\n{text}"}
|
16 |
+
],
|
17 |
model="llama-3.3-70b-versatile",
|
18 |
)
|
19 |
return response.choices[0].message.content.strip()
|
20 |
|
21 |
+
def groq_generate(text: str) -> str:
|
22 |
response = client.chat.completions.create(
|
23 |
+
messages=[{"role": "user", "content": text}],
|
|
|
|
|
24 |
model="llama-3.3-70b-versatile",
|
25 |
)
|
26 |
return response.choices[0].message.content.strip()
|
27 |
|
|
|
|
|
|
|
|
|
28 |
def retrieve_papers(query, max_results=5):
|
29 |
search = arxiv.Search(query=query, max_results=max_results)
|
30 |
papers = []
|
31 |
for result in search.results():
|
32 |
+
paper_id = result.entry_id.split("/")[-1]
|
|
|
33 |
paper = {
|
34 |
"title": result.title,
|
35 |
"summary": result.summary,
|
36 |
"url": result.pdf_url,
|
37 |
"authors": [author.name for author in result.authors],
|
38 |
+
"published": result.published,
|
39 |
"doi": f"https://doi.org/10.48550/arXiv.{paper_id}",
|
40 |
+
"bibliographic_explorer": f"https://arxiv.org/abs/{paper_id}",
|
41 |
"litmaps": f"https://app.litmaps.com/preview/{paper_id}",
|
42 |
+
"trust_score": random.randint(60, 100),
|
43 |
+
"relevance_score": random.randint(50, 100)
|
44 |
}
|
45 |
papers.append(paper)
|
46 |
return papers
|
47 |
|
48 |
+
def summarize_text(text):
|
49 |
+
return groq_summarize(text)
|
50 |
+
|
51 |
+
def get_cached_summary(paper_id, text):
|
52 |
+
if 'summaries' not in st.session_state:
|
53 |
+
st.session_state.summaries = {}
|
54 |
+
if paper_id not in st.session_state.summaries:
|
55 |
+
st.session_state.summaries[paper_id] = summarize_text(text)
|
56 |
+
return st.session_state.summaries[paper_id]
|
57 |
+
|
58 |
+
def random_paper_search():
|
59 |
+
topics = ["machine learning", "quantum computing", "climate change", "robotics", "health AI"]
|
60 |
+
return random.choice(topics)
|
61 |
+
|
62 |
+
st.title("π PaperPilot β Intelligent Academic Navigator")
|
63 |
|
64 |
+
st.sidebar.header("π Search Parameters")
|
65 |
+
query = st.sidebar.text_input("Research topic or question:")
|
66 |
+
if st.sidebar.button("π² Random Search"):
|
67 |
+
query = random_paper_search()
|
68 |
+
st.sidebar.text(f"Random Topic: {query}")
|
69 |
+
if st.sidebar.button("π Find Articles"):
|
70 |
+
if query.strip():
|
71 |
+
with st.spinner("Searching arXiv..."):
|
72 |
+
papers = retrieve_papers(query, random.randint(5, 15))
|
73 |
+
if papers:
|
74 |
+
st.session_state.papers = papers
|
75 |
+
st.success(f"Found {len(papers)} papers!")
|
76 |
+
st.session_state.active_section = "articles"
|
77 |
+
else:
|
78 |
+
st.error("No papers found. Try different keywords.")
|
79 |
+
else:
|
80 |
+
st.warning("Please enter a search query")
|
81 |
|
82 |
+
if 'active_section' not in st.session_state:
|
83 |
+
st.session_state.active_section = "none"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
if 'papers' in st.session_state and st.session_state.papers:
|
86 |
+
papers = st.session_state.papers
|
87 |
+
if st.session_state.active_section == "articles":
|
88 |
+
st.header("π Retrieved Papers")
|
89 |
+
for idx, paper in enumerate(papers, 1):
|
90 |
+
with st.expander(f"{idx}. {paper['title']}"):
|
91 |
+
st.markdown(f"**Authors:** {', '.join(paper['authors'])}")
|
92 |
+
pub_date = paper['published'].strftime('%Y-%m-%d') if isinstance(paper['published'], datetime.datetime) else "n.d."
|
93 |
+
st.markdown(f"**Published:** {pub_date}")
|
94 |
+
st.markdown(f"**[PDF Link]({paper['url']}) | [DOI]({paper['doi']}) | [Bibliographic Explorer]({paper['bibliographic_explorer']}) | [Litmaps]({paper['litmaps']})**")
|
95 |
+
st.markdown(f"**Trust Score:** {paper['trust_score']} | **Relevance Score:** {paper['relevance_score']}")
|
96 |
+
summary = get_cached_summary(f"paper_{idx}", paper['summary'])
|
97 |
+
st.write(summary)
|
98 |
+
if st.button(f"π Explain like I'm 5 (ELI5) {idx}"):
|
99 |
+
st.write(groq_generate(f"Explain this in simple terms: {summary}"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|