File size: 5,027 Bytes
5347681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
import streamlit as st
import requests
from datetime import datetime, timedelta
import pandas as pd
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Scopus API key
SCOPUS_API_KEY = "8e94f85eb6044ef1cde06e8b5a426a09"

def search_scopus(query, start_year, end_year, max_results=50):
    base_url = "https://api.elsevier.com/content/search/scopus"
    
    params = {
        "query": query,
        "date": f"{start_year}-{end_year}",
        "count": max_results,
        "sort": "citedby-count desc",
        "field": "title,author,year,publicationName,description,citedby-count,doi,eid"
    }
    
    headers = {
        "X-ELS-APIKey": SCOPUS_API_KEY,
        "Accept": "application/json"
    }
    
    try:
        response = requests.get(base_url, params=params, headers=headers)
        response.raise_for_status()
        return response.json()["search-results"]["entry"]
    except requests.exceptions.RequestException as e:
        st.error(f"An error occurred while searching Scopus: {e}")
        return []

def format_authors(author_info):
    if isinstance(author_info, list):
        return ", ".join([author.get("authname", "") for author in author_info])
    elif isinstance(author_info, dict):
        return author_info.get("authname", "")
    else:
        return "N/A"

def safe_get(dictionary, keys, default="N/A"):
    for key in keys:
        if isinstance(dictionary, dict) and key in dictionary:
            dictionary = dictionary[key]
        else:
            return default
    return dictionary

def get_paper_link(paper):
    doi = safe_get(paper, ["prism:doi"])
    if doi != "N/A":
        return f"https://doi.org/{doi}"
    eid = safe_get(paper, ["eid"])
    if eid != "N/A":
        return f"https://www.scopus.com/record/display.uri?eid={eid}&origin=resultslist"
    return "#"

def main():
    st.set_page_config(page_title="S.H.E.R.L.O.C.K. Research Assistant", page_icon="🔬", layout="wide")
    
    st.sidebar.title("S.H.E.R.L.O.C.K.")
    st.sidebar.markdown("""

    **S**ystematic **H**olistic **E**ducational **R**esource for **L**iterature and **O**ptimizing **C**ognitive **K**nowledge

    

    Enhance your research capabilities with AI-powered literature search and analysis.

    """)
    
    query = st.sidebar.text_input("What topic would you like to research?", "")
    
    current_year = datetime.now().year
    start_year, end_year = st.sidebar.slider(
        "Publication Year Range",
        min_value=1900,
        max_value=current_year,
        value=(current_year-5, current_year)
    )
    
    max_results = st.sidebar.slider("Maximum number of results", 10, 100, 50)
    
    search_button = st.sidebar.button("Search for Research Papers")
    
    st.title("Research Papers and Articles")
    
    if search_button and query:
        with st.spinner("Searching for the most relevant research papers..."):
            results = search_scopus(query, start_year, end_year, max_results)
        
        if results:
            papers = []
            for paper in results:
                papers.append({
                    "Title": safe_get(paper, ["dc:title"]),
                    "Authors": format_authors(safe_get(paper, ["author"])),
                    "Year": safe_get(paper, ["prism:coverDate"])[:4],
                    "Journal": safe_get(paper, ["prism:publicationName"]),
                    "Abstract": safe_get(paper, ["dc:description"]),
                    "Citations": safe_get(paper, ["citedby-count"], "0"),
                    "Link": get_paper_link(paper)
                })
            
            df = pd.DataFrame(papers)
            
            st.markdown(f"### Found {len(results)} papers on '{query}'")
            
            for _, paper in df.iterrows():
                with st.container():
                    col1, col2 = st.columns([3, 1])
                    with col1:
                        st.markdown(f"#### [{paper['Title']}]({paper['Link']})")
                        st.markdown(f"**Authors:** {paper['Authors']}")
                        st.markdown(f"**Published in:** {paper['Journal']} ({paper['Year']})")
                        st.markdown(f"**Abstract:** {paper['Abstract']}")
                    with col2:
                        st.metric("Citations", paper["Citations"])
                    
                    st.markdown("---")
            
            # Download results as CSV
            csv = df.to_csv(index=False).encode('utf-8')
            st.download_button(
                label="Download results as CSV",
                data=csv,
                file_name=f"{query.replace(' ', '_')}_research_papers.csv",
                mime="text/csv",
            )
        else:
            st.warning("No results found. Please try a different search query or adjust the year range.")

if __name__ == "__main__":
    main()