Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_parse import LlamaParse
|
| 2 |
+
from llama_index.core import SimpleDirectoryReader
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv()
|
| 6 |
+
import tempfile
|
| 7 |
+
import requests
|
| 8 |
+
import streamlit as st
|
| 9 |
+
|
| 10 |
+
read_file_path = "160/task_for_you.pdf"
|
| 11 |
+
def check_pdf(read_file_path):
|
| 12 |
+
try:
|
| 13 |
+
parser = LlamaParse(result_type="markdown", api_key=os.environ['LLAMA_CLOUD_API_KEY'], ignore_errors=False)
|
| 14 |
+
file_extractor = {".pdf": parser}
|
| 15 |
+
markdown_data = SimpleDirectoryReader(input_files=[read_file_path], file_extractor=file_extractor).load_data()
|
| 16 |
+
if markdown_data == []:
|
| 17 |
+
raise Exception("No markdown data found")
|
| 18 |
+
return True
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"An error occurred: {e}")
|
| 21 |
+
return False
|
| 22 |
+
|
| 23 |
+
def download_file_from_url(url, filename):
|
| 24 |
+
print(f"Downloading file from {url} to {filename}")
|
| 25 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 26 |
+
response = requests.get(url, stream=True)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
with open(filename, 'wb') as file:
|
| 29 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 30 |
+
file.write(chunk)
|
| 31 |
+
print(f"File downloaded and saved as {filename}")
|
| 32 |
+
return True
|
| 33 |
+
else:
|
| 34 |
+
print(f"Failed to download file. Status code: {response.status_code}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
url = st.text_input("Enter URL", key="url")
|
| 38 |
+
|
| 39 |
+
if url:
|
| 40 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 41 |
+
|
| 42 |
+
if download_file_from_url(url, os.path.join(temp_dir, "task_for_you.pdf")):
|
| 43 |
+
if check_pdf(os.path.join(temp_dir, "task_for_you.pdf")):
|
| 44 |
+
st.success("File downloaded successfully")
|
| 45 |
+
else:
|
| 46 |
+
st.error("File is corrupted or not a PDF file")
|
| 47 |
+
else:
|
| 48 |
+
st.error("Failed to download file")
|