Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import xml.etree.ElementTree as ET
|
| 5 |
+
import unicodedata
|
| 6 |
+
import re
|
| 7 |
+
from collections import Counter
|
| 8 |
+
|
| 9 |
+
def clean_text(text):
|
| 10 |
+
text = text.replace('"', '\\"') # Escape double quotes
|
| 11 |
+
text = text.replace('\n', ' ') # Replace newlines with space
|
| 12 |
+
text = unicodedata.normalize("NFKD", text) # Normalize Unicode characters
|
| 13 |
+
text = re.sub(r'\s+', ' ', text).strip() # Remove extra whitespace
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
def fetch_arxiv_data(search_term, max_results=50):
|
| 17 |
+
url = f"http://export.arxiv.org/api/query?search_query={search_term}&start=0&max_results={max_results}"
|
| 18 |
+
response = requests.get(url)
|
| 19 |
+
response.raise_for_status()
|
| 20 |
+
root = ET.fromstring(response.content)
|
| 21 |
+
|
| 22 |
+
columns = ["ID", "Title", "Authors", "Summary", "Published Date", "Categories", "DOI", "Journal Reference", "Primary Category", "Link"]
|
| 23 |
+
df = pd.DataFrame(columns=columns)
|
| 24 |
+
|
| 25 |
+
for entry in root.findall("{http://www.w3.org/2005/Atom}entry"):
|
| 26 |
+
arxiv_id = entry.find("{http://www.w3.org/2005/Atom}id").text
|
| 27 |
+
title = clean_text(entry.find("{http://www.w3.org/2005/Atom}title").text)
|
| 28 |
+
summary = clean_text(entry.find("{http://www.w3.org/2005/Atom}summary").text)
|
| 29 |
+
published = entry.find("{http://www.w3.org/2005/Atom}published").text
|
| 30 |
+
|
| 31 |
+
authors = [author.find("{http://www.w3.org/2005/Atom}name").text for author in entry.findall("{http://www.w3.org/2005/Atom}author")]
|
| 32 |
+
authors_list = ", ".join(authors)
|
| 33 |
+
|
| 34 |
+
categories = [category.attrib["term"] for category in entry.findall("{http://www.w3.org/2005/Atom}category")]
|
| 35 |
+
categories_list = ", ".join(categories)
|
| 36 |
+
|
| 37 |
+
doi = "N/A"
|
| 38 |
+
for link in entry.findall("{http://www.w3.org/2005/Atom}link"):
|
| 39 |
+
if link.attrib.get("title") == "doi":
|
| 40 |
+
doi = link.attrib.get("href")
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
journal_ref = entry.find("{http://arxiv.org/schemas/atom}journal_ref")
|
| 44 |
+
journal_ref_text = clean_text(journal_ref.text) if journal_ref is not None else "N/A"
|
| 45 |
+
|
| 46 |
+
primary_category = entry.find("{http://arxiv.org/schemas/atom}primary_category").attrib.get("term", "N/A")
|
| 47 |
+
|
| 48 |
+
link = entry.find("{http://www.w3.org/2005/Atom}link").attrib.get("href", "N/A")
|
| 49 |
+
|
| 50 |
+
new_row = {
|
| 51 |
+
"ID": arxiv_id,
|
| 52 |
+
"Title": title,
|
| 53 |
+
"Authors": authors_list,
|
| 54 |
+
"Summary": summary,
|
| 55 |
+
"Published Date": published,
|
| 56 |
+
"Categories": categories_list,
|
| 57 |
+
"DOI": doi,
|
| 58 |
+
"Journal Reference": journal_ref_text,
|
| 59 |
+
"Primary Category": primary_category,
|
| 60 |
+
"Link": link
|
| 61 |
+
}
|
| 62 |
+
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 63 |
+
|
| 64 |
+
return df
|
| 65 |
+
|
| 66 |
+
# Streamlit UI
|
| 67 |
+
st.title("arXiv Search App")
|
| 68 |
+
st.write("Enter a search term to find papers on arXiv.")
|
| 69 |
+
|
| 70 |
+
# User input
|
| 71 |
+
search_term = st.text_input("Search Term", "machine learning")
|
| 72 |
+
max_results = st.slider("Number of Results", min_value=1, max_value=100, value=10)
|
| 73 |
+
|
| 74 |
+
if st.button("Fetch Papers"):
|
| 75 |
+
# Fetch arXiv data
|
| 76 |
+
try:
|
| 77 |
+
df_arxiv = fetch_arxiv_data(search_term, max_results)
|
| 78 |
+
st.write(f"Total Articles Found: {len(df_arxiv)}")
|
| 79 |
+
|
| 80 |
+
# Displaying the data in the table
|
| 81 |
+
st.dataframe(df_arxiv)
|
| 82 |
+
|
| 83 |
+
# Download as CSV
|
| 84 |
+
csv = df_arxiv.to_csv(index=False).encode('utf-8')
|
| 85 |
+
st.download_button(
|
| 86 |
+
label="Download data as CSV",
|
| 87 |
+
data=csv,
|
| 88 |
+
file_name='arxiv_data.csv',
|
| 89 |
+
mime='text/csv',
|
| 90 |
+
)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error(f"An error occurred: {e}")
|