Spaces:
Sleeping
Sleeping
removed uneeded scripts for ease of use
Browse files
dl_bin.py
DELETED
@@ -1,32 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
from typing import Optional
|
3 |
-
|
4 |
-
def download_binary_file(url: str, file_path: Optional[str] = None) -> None:
|
5 |
-
"""
|
6 |
-
Download a binary file from a given URL and save it to the specified path.
|
7 |
-
|
8 |
-
:param url: URL of the binary file to be downloaded.
|
9 |
-
:param file_path: Local path to save the file. If None, the file will be saved with its original name.
|
10 |
-
"""
|
11 |
-
try:
|
12 |
-
response = requests.get(url, stream=True)
|
13 |
-
response.raise_for_status()
|
14 |
-
|
15 |
-
# If no specific file path is provided, extract the file name from the URL
|
16 |
-
if file_path is None:
|
17 |
-
file_path = url.split('/')[-1]
|
18 |
-
|
19 |
-
with open(file_path, 'wb') as file:
|
20 |
-
for chunk in response.iter_content(chunk_size=8192):
|
21 |
-
file.write(chunk)
|
22 |
-
|
23 |
-
print(f"File downloaded successfully: {file_path}")
|
24 |
-
except requests.exceptions.RequestException as e:
|
25 |
-
print(f"Error downloading file: {e}")
|
26 |
-
|
27 |
-
# Example usage
|
28 |
-
url = "https://llamahack.slack.com/files/U069A8NRB9T/F068ZTLK9KR/anthem_hsa_medical_insurance_benefit_booklet.pdf"
|
29 |
-
# download_binary_file(url)
|
30 |
-
|
31 |
-
import urllib.request
|
32 |
-
urllib.request.urlretrieve(url, "filename.pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ingest.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
from llama_index import SimpleDirectoryReader, VectorStoreIndex, ServiceContext
|
2 |
-
from llama_index.text_splitter import SentenceSplitter
|
3 |
-
import dotenv
|
4 |
-
import os
|
5 |
-
|
6 |
-
dotenv.load_dotenv()
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
documents = SimpleDirectoryReader("./data").load_data()
|
11 |
-
|
12 |
-
text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=10)
|
13 |
-
service_context = ServiceContext.from_defaults(text_splitter=text_splitter)
|
14 |
-
|
15 |
-
index = VectorStoreIndex.from_documents(
|
16 |
-
documents, service_context=service_context
|
17 |
-
)
|
18 |
-
|
19 |
-
query_engine = index.as_query_engine()
|
20 |
-
|
21 |
-
|
22 |
-
# from llama_index.query import QueryBuilder
|
23 |
-
|
24 |
-
# Define the query text
|
25 |
-
query_text = "How does the weather affect crop growth?"
|
26 |
-
|
27 |
-
data = query_engine.query(query_text)
|
28 |
-
|
29 |
-
# Preprocess the query text
|
30 |
-
# query_builder = QueryBuilder(service_context)
|
31 |
-
# query = query_builder.build_query(query_text)
|
32 |
-
|
33 |
-
# # Search for similar documents or retrieve relevant information
|
34 |
-
# results = index.search(query)
|
35 |
-
|
36 |
-
# Process the search results
|
37 |
-
for result in results:
|
38 |
-
document_id = result.document_id
|
39 |
-
score = result.score
|
40 |
-
document = documents[document_id]
|
41 |
-
# Process the retrieved document or display the relevant information
|
42 |
-
print(f"Document ID: {document_id}, Score: {score}")
|
43 |
-
print(f"Document Text: {document.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils.py
DELETED
@@ -1,54 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
import os
|
3 |
-
from typing import Optional
|
4 |
-
from urllib.parse import urlparse, unquote
|
5 |
-
import gdrive
|
6 |
-
|
7 |
-
def get_filename_from_url(url: str, cd: Optional[str]) -> str:
|
8 |
-
"""
|
9 |
-
Extracts and returns the filename from the URL or content-disposition header.
|
10 |
-
"""
|
11 |
-
if cd:
|
12 |
-
fname = [x.strip() for x in cd.split(';') if x.strip().startswith('filename=')]
|
13 |
-
if fname:
|
14 |
-
return unquote(fname[0].split('=')[1].strip('"'))
|
15 |
-
|
16 |
-
# Fallback to extracting filename from URL
|
17 |
-
parsed_url = urlparse(url)
|
18 |
-
return os.path.basename(parsed_url.path)
|
19 |
-
|
20 |
-
def download_file(url: str, save_dir: Optional[str] = None, save_name: Optional[str] = None) -> None:
|
21 |
-
"""
|
22 |
-
Downloads a file from the given URL and saves it in the specified directory.
|
23 |
-
If the directory does not exist, it will be created.
|
24 |
-
"""
|
25 |
-
try:
|
26 |
-
response = requests.get(url, stream=True)
|
27 |
-
response.raise_for_status()
|
28 |
-
|
29 |
-
filename = save_name if save_name else get_filename_from_url(url, response.headers.get('content-disposition'))
|
30 |
-
|
31 |
-
if save_dir:
|
32 |
-
os.makedirs(save_dir, exist_ok=True)
|
33 |
-
file_path = os.path.join(save_dir, filename)
|
34 |
-
else:
|
35 |
-
file_path = filename
|
36 |
-
|
37 |
-
with open(file_path, 'wb') as file:
|
38 |
-
for chunk in response.iter_content(chunk_size=8192):
|
39 |
-
if chunk:
|
40 |
-
file.write(chunk)
|
41 |
-
|
42 |
-
print(f"File downloaded and saved as: {file_path}")
|
43 |
-
|
44 |
-
except requests.exceptions.HTTPError as err:
|
45 |
-
print(f"HTTP Error: {err}")
|
46 |
-
except Exception as e:
|
47 |
-
print(f"Error: {e}")
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
if __name__ == "__main__":
|
52 |
-
# Example Usage
|
53 |
-
url = "https://llamahack.slack.com/files/U069A8NRB9T/F068ZTLK9KR/anthem_hsa_medical_insurance_benefit_booklet.pdf"
|
54 |
-
download_file(url,"data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|