Spaces:
Sleeping
Sleeping
Commit
·
4dcec0d
1
Parent(s):
3c0c722
Add all files
Browse files- app.py +77 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from warnings import showwarning
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
|
| 10 |
+
# Configure logging with a custom format
|
| 11 |
+
logging.basicConfig(filename="gfgnewlog.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s")
|
| 12 |
+
|
| 13 |
+
# Create a logger instance
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
# Create a FileHandler to log to 'logs.log' file
|
| 17 |
+
file_handler = logging.FileHandler('logs.log')
|
| 18 |
+
|
| 19 |
+
# Add the FileHandler to the logger
|
| 20 |
+
logger.addHandler(file_handler)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def upload_file(filepath,progress=gr.Progress()):
|
| 24 |
+
|
| 25 |
+
d = ""
|
| 26 |
+
name = Path(filepath).name
|
| 27 |
+
loader = PyPDFLoader(Path(filepath))
|
| 28 |
+
pages = loader.load_and_split()
|
| 29 |
+
|
| 30 |
+
for page in progress.tqdm(pages,desc="Loading..."):
|
| 31 |
+
d +=page.page_content
|
| 32 |
+
|
| 33 |
+
# Log a warning message using the logger
|
| 34 |
+
logger.warning("pdf extracted")
|
| 35 |
+
|
| 36 |
+
# API Fetch
|
| 37 |
+
url = os.getenv("url")
|
| 38 |
+
myobj = {"text": d}
|
| 39 |
+
|
| 40 |
+
x = requests.post(url, json = myobj)
|
| 41 |
+
logger.warning("API EXtraction done")
|
| 42 |
+
|
| 43 |
+
# write txt file
|
| 44 |
+
output_file = name.split(".")[0]+".txt"
|
| 45 |
+
folder_path = "./data_file"
|
| 46 |
+
|
| 47 |
+
# check if folder exists
|
| 48 |
+
if not os.path.exists(folder_path):
|
| 49 |
+
os.makedirs(folder_path)
|
| 50 |
+
|
| 51 |
+
# create and save txt file
|
| 52 |
+
main_txt_path = folder_path+"/"+output_file
|
| 53 |
+
with open(main_txt_path, "w") as file:
|
| 54 |
+
file.write(x.text) # Write the content to the output
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
logger.warning("writing txt done")
|
| 58 |
+
|
| 59 |
+
return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {output_file}", value=main_txt_path, visible=True),"Loaded"]
|
| 60 |
+
|
| 61 |
+
def download_file():
|
| 62 |
+
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("First upload a file and and then you'll be able download it (but only once!)")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
u = gr.UploadButton("Upload a file", file_count="single",)
|
| 69 |
+
d = gr.DownloadButton("Download the file", visible=False)
|
| 70 |
+
|
| 71 |
+
label = gr.Label(label="Loader")
|
| 72 |
+
u.upload(upload_file, u, [u, d,label])
|
| 73 |
+
d.click(download_file, None, [u, d])
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain-community
|
| 2 |
+
gradio
|
| 3 |
+
pypdf
|
| 4 |
+
tqdm
|