Spaces:
Sleeping
Sleeping
Latest changes
Browse files
pages/fetch_resources_store_collection.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_community.document_loaders import AsyncHtmlLoader
|
| 3 |
+
from langchain.schema import Document
|
| 4 |
+
import json
|
| 5 |
+
from typing import Iterable
|
| 6 |
+
|
| 7 |
+
# Placeholder for async fetch function (adjust based on actual async handling in your environment)
|
| 8 |
+
async def fetch_documents(urls):
|
| 9 |
+
loader = AsyncHtmlLoader(urls)
|
| 10 |
+
docs = await loader.load()
|
| 11 |
+
return docs
|
| 12 |
+
|
| 13 |
+
def save_docs_to_jsonl(array: Iterable[Document], file_path: str) -> None:
|
| 14 |
+
with open(file_path, 'w') as jsonl_file:
|
| 15 |
+
for doc in array:
|
| 16 |
+
jsonl_file.write(json.dumps(doc.to_dict()) + '\n') # Assuming Document objects have a to_dict method
|
| 17 |
+
|
| 18 |
+
def load_docs_from_jsonl(file_path) -> Iterable[Document]:
|
| 19 |
+
array = []
|
| 20 |
+
with open(file_path, 'r') as jsonl_file:
|
| 21 |
+
for line in jsonl_file:
|
| 22 |
+
data = json.loads(line)
|
| 23 |
+
obj = Document(**data)
|
| 24 |
+
array.append(obj)
|
| 25 |
+
return array
|
| 26 |
+
|
| 27 |
+
def fetch_clean_organize_page():
|
| 28 |
+
st.title("Fetch, Clean, and Organize Documents")
|
| 29 |
+
|
| 30 |
+
# Initialize 'selected_urls' at the start of your app if it doesn't exist
|
| 31 |
+
if 'selected_urls' not in st.session_state:
|
| 32 |
+
st.session_state['selected_urls'] = [] # Default to an empty list
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
urls = st.session_state['selected_urls']
|
| 37 |
+
if st.button("Fetch Documents"):
|
| 38 |
+
# Async fetching operation placeholder
|
| 39 |
+
# Adjust based on your async handling strategy
|
| 40 |
+
docs = fetch_documents(urls) # This needs proper async handling
|
| 41 |
+
st.session_state['docs'] = docs # Assuming docs are fetched and stored correctly
|
| 42 |
+
|
| 43 |
+
if 'docs' in st.session_state:
|
| 44 |
+
st.write(f"Fetched {len(st.session_state['docs'])} documents.")
|
| 45 |
+
|
| 46 |
+
if st.button("Save Documents as JSON"):
|
| 47 |
+
save_docs_to_jsonl(st.session_state['docs'], "documents.jsonl")
|
| 48 |
+
st.success("Documents saved as JSON.")
|
| 49 |
+
|
| 50 |
+
# Provide download link (streamlit >= 0.88.0)
|
| 51 |
+
with open("documents.jsonl", "rb") as file:
|
| 52 |
+
btn = st.download_button(
|
| 53 |
+
label="Download JSON",
|
| 54 |
+
data=file,
|
| 55 |
+
file_name="documents.jsonl",
|
| 56 |
+
mime="application/octet-stream")
|
| 57 |
+
|
| 58 |
+
# Assuming this function is called in your app
|
| 59 |
+
fetch_clean_organize_page()
|
pages/file_web_source_collection.py
CHANGED
|
@@ -19,12 +19,7 @@ def find_linked_urls(url):
|
|
| 19 |
return set()
|
| 20 |
|
| 21 |
def convert_to_absolute_urls(base_url, links):
|
| 22 |
-
|
| 23 |
-
for link in links:
|
| 24 |
-
if not link.startswith('http'):
|
| 25 |
-
link = urljoin(base_url, link)
|
| 26 |
-
absolute_urls.append(link)
|
| 27 |
-
return set(absolute_urls)
|
| 28 |
|
| 29 |
def categorize_links(base_url, links):
|
| 30 |
internal_links, external_links = set(), set()
|
|
@@ -38,20 +33,26 @@ def categorize_links(base_url, links):
|
|
| 38 |
def main():
|
| 39 |
st.title("Data Source Configuration")
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
st.subheader("Scan Websites for URLs")
|
| 42 |
url_input = st.text_area("Enter URLs to scan, separated by new lines:")
|
| 43 |
url_list = [url.strip() for url in url_input.strip().split('\n') if url.strip()] # Splitting and cleaning input
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
selected_urls = []
|
| 54 |
-
for base_url, links in
|
| 55 |
st.write(f"Base URL: {base_url}")
|
| 56 |
include_all_internal = st.checkbox(f"Include all internal links from {base_url}", key=f"all_{base_url}")
|
| 57 |
|
|
@@ -61,21 +62,26 @@ def main():
|
|
| 61 |
selected_internal = [link for link in links["internal"] if st.checkbox(link, key=link)]
|
| 62 |
selected_urls.extend(selected_internal)
|
| 63 |
|
| 64 |
-
# Displaying external links for informational purposes
|
| 65 |
if links["external"]:
|
| 66 |
st.write("External links:")
|
| 67 |
for link in links["external"]:
|
| 68 |
st.write(link)
|
| 69 |
|
| 70 |
-
# Convert selected URLs to a DataFrame and display
|
| 71 |
if selected_urls:
|
| 72 |
df_selected_urls = pd.DataFrame(selected_urls, columns=['Selected URLs'])
|
| 73 |
st.write(df_selected_urls)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
main()
|
|
|
|
| 19 |
return set()
|
| 20 |
|
| 21 |
def convert_to_absolute_urls(base_url, links):
|
| 22 |
+
return {urljoin(base_url, link) if not link.startswith('http') else link for link in links}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def categorize_links(base_url, links):
|
| 25 |
internal_links, external_links = set(), set()
|
|
|
|
| 33 |
def main():
|
| 34 |
st.title("Data Source Configuration")
|
| 35 |
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if 'scanned_urls' not in st.session_state:
|
| 39 |
+
st.session_state['scanned_urls'] = {}
|
| 40 |
+
|
| 41 |
st.subheader("Scan Websites for URLs")
|
| 42 |
url_input = st.text_area("Enter URLs to scan, separated by new lines:")
|
| 43 |
url_list = [url.strip() for url in url_input.strip().split('\n') if url.strip()] # Splitting and cleaning input
|
| 44 |
|
| 45 |
+
scan_button_clicked = st.button("Scan URLs")
|
| 46 |
+
if scan_button_clicked or st.session_state['scanned_urls']:
|
| 47 |
+
if scan_button_clicked:
|
| 48 |
+
for url in url_list:
|
| 49 |
+
unique_urls = find_linked_urls(url)
|
| 50 |
+
absolute_urls = convert_to_absolute_urls(url, unique_urls)
|
| 51 |
+
internal_links, external_links = categorize_links(url, absolute_urls)
|
| 52 |
+
st.session_state['scanned_urls'][url] = {"internal": internal_links, "external": external_links}
|
| 53 |
+
|
| 54 |
selected_urls = []
|
| 55 |
+
for base_url, links in st.session_state['scanned_urls'].items():
|
| 56 |
st.write(f"Base URL: {base_url}")
|
| 57 |
include_all_internal = st.checkbox(f"Include all internal links from {base_url}", key=f"all_{base_url}")
|
| 58 |
|
|
|
|
| 62 |
selected_internal = [link for link in links["internal"] if st.checkbox(link, key=link)]
|
| 63 |
selected_urls.extend(selected_internal)
|
| 64 |
|
|
|
|
| 65 |
if links["external"]:
|
| 66 |
st.write("External links:")
|
| 67 |
for link in links["external"]:
|
| 68 |
st.write(link)
|
| 69 |
|
|
|
|
| 70 |
if selected_urls:
|
| 71 |
df_selected_urls = pd.DataFrame(selected_urls, columns=['Selected URLs'])
|
| 72 |
st.write(df_selected_urls)
|
| 73 |
|
| 74 |
+
st.session_state['selected_urls'] = df_selected_urls
|
| 75 |
+
|
| 76 |
+
# Convert DataFrame to CSV for download
|
| 77 |
+
csv = df_selected_urls.to_csv(index=False).encode('utf-8')
|
| 78 |
+
|
| 79 |
+
st.download_button(
|
| 80 |
+
label="Download selected URLs as CSV",
|
| 81 |
+
data=csv,
|
| 82 |
+
file_name='selected_urls.csv',
|
| 83 |
+
mime='text/csv',
|
| 84 |
+
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
main()
|
selected_urls.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Selected URLs
|
| 2 |
+
https://tlt.rutgers.edu/blog
|
| 3 |
+
https://tlt.rutgers.edu/coursera
|