Spaces:
Running
Running
import os | |
import streamlit as st | |
import arxiv | |
import requests | |
import datetime | |
import networkx as nx | |
import matplotlib.pyplot as plt | |
# ------------------------------- | |
# Groq API Client | |
# ------------------------------- | |
from groq import Groq | |
client = Groq( | |
api_key=os.environ.get("GROQ_API_KEY"), | |
) | |
# ------------------------------- | |
# Helper Functions (Groq-based) | |
# ------------------------------- | |
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 in detail:\n\n{text}"} | |
], | |
model="llama-3.3-70b-versatile", | |
) | |
return response.choices[0].message.content.strip() | |
# ------------------------------- | |
# Trust & Relevance Scores | |
# ------------------------------- | |
def get_citation_metadata(arxiv_id): | |
"""Fetch trust & relevance scores from external sources.""" | |
metadata = {"citations": 0, "trust_score": 0, "relevance_score": 0, "links": {}} | |
# Fetch citation data from scite.ai | |
scite_url = f"https://api.scite.ai/papers/{arxiv_id}" | |
response = requests.get(scite_url) | |
if response.status_code == 200: | |
scite_data = response.json() | |
metadata["citations"] = scite_data.get("citation_count", 0) | |
metadata["trust_score"] = scite_data.get("trust_score", 0) | |
# Generate Connected Papers & Litmaps links | |
metadata["links"]["Connected Papers"] = f"https://www.connectedpapers.com/main/{arxiv_id}" | |
metadata["links"]["Bibliographic Explorer"] = f"https://arxiv.org/bib_explorer/{arxiv_id}" | |
metadata["links"]["Litmaps"] = f"https://www.litmaps.com/publications/{arxiv_id}" | |
# Calculate relevance score | |
metadata["relevance_score"] = metadata["citations"] * 0.8 + metadata["trust_score"] * 0.2 | |
return metadata | |
# ------------------------------- | |
# Retrieve Papers | |
# ------------------------------- | |
def retrieve_papers(query, max_results=5): | |
"""Retrieve academic papers from arXiv & add Trust/Relevance scores.""" | |
search = arxiv.Search(query=query, max_results=max_results) | |
papers = [] | |
for result in search.results(): | |
paper_id = result.entry_id.split("/")[-1] # Extract arXiv ID | |
metadata = get_citation_metadata(paper_id) | |
paper = { | |
"title": result.title, | |
"summary": result.summary, | |
"url": result.pdf_url, | |
"authors": [author.name for author in result.authors], | |
"published": result.published, | |
"citations": metadata["citations"], | |
"trust_score": metadata["trust_score"], | |
"relevance_score": metadata["relevance_score"], | |
"links": metadata["links"], | |
} | |
papers.append(paper) | |
return papers | |
# ------------------------------- | |
# Streamlit Interface | |
# ------------------------------- | |
st.title("π PaperPilot β Intelligent Academic Navigator") | |
# Sidebar: Search & Toggle | |
with st.sidebar: | |
st.header("π Search Parameters") | |
query = st.text_input("Research topic or question:") | |
show_scores = st.checkbox("Enable Trust & Relevance Scores", value=True) | |
if st.button("π Find Articles"): | |
if query.strip(): | |
with st.spinner("Searching arXiv..."): | |
papers = retrieve_papers(query) | |
if papers: | |
st.session_state.papers = papers | |
st.session_state.active_section = "articles" | |
st.success(f"Found {len(papers)} papers!") | |
else: | |
st.error("No papers found. Try different keywords.") | |
else: | |
st.warning("Please enter a search query") | |
# Main Content | |
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']}"): | |
st.markdown(f"**Authors:** {', '.join(paper['authors'])}") | |
st.markdown(f"**Published:** {paper['published']}") | |
st.markdown(f"**Link:** [PDF]({paper['url']})") | |
# Show Trust & Relevance Scores if enabled | |
if show_scores: | |
st.markdown(f"π **Citations:** {paper['citations']}") | |
st.markdown(f"π‘οΈ **Trust Score:** {round(paper['trust_score'], 2)} / 10") | |
st.markdown(f"π **Relevance Score:** {round(paper['relevance_score'], 2)} / 10") | |
# External Links | |
st.markdown(f"[π Connected Papers]({paper['links']['Connected Papers']})") | |
st.markdown(f"[π Bibliographic Explorer]({paper['links']['Bibliographic Explorer']})") | |
st.markdown(f"[π Litmaps]({paper['links']['Litmaps']})") | |
# Display Summary | |
st.markdown("**Abstract:**") | |
st.write(paper['summary']) | |
st.caption("Built with β€οΈ using AI") |