File size: 2,403 Bytes
4dcec0d
6342db2
4dcec0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6342db2
 
4dcec0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d7f1dd
4dcec0d
 
 
 
 
 
 
 
 
 
1d2f80a
 
6035a41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from warnings import showwarning
from langchain_community.document_loaders import PyMuPDFLoader
from pathlib import Path
import gradio as gr
import requests
import logging
import os
from tqdm import tqdm

# Configure logging with a custom format
logging.basicConfig(filename="gfgnewlog.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s")

# Create a logger instance
logger = logging.getLogger(__name__)

# Create a FileHandler to log to 'logs.log' file
file_handler = logging.FileHandler('logs.log')

# Add the FileHandler to the logger
logger.addHandler(file_handler)


def upload_file(filepath,progress=gr.Progress()):

    d = ""
    name = Path(filepath).name
    loader = PyMuPDFLoader(Path(filepath))
    pages = loader.load()

    for page in progress.tqdm(pages,desc="Loading..."):
      d +=page.page_content

      # Log a warning message using the logger
      logger.warning("pdf extracted")

      # API Fetch
      url = os.getenv("url")
      myobj = {"text": d}

      x = requests.post(url, json = myobj)
      logger.warning("API EXtraction done")

      # write txt file
      output_file = name.split(".")[0]+".txt"
      folder_path = "./data_file"

      # check if folder exists
      if not os.path.exists(folder_path):      
        os.makedirs(folder_path)

      # create and save txt file
      main_txt_path = folder_path+"/"+output_file
      with open(main_txt_path, "w") as file:
        file.write(x.text)  # Write the content to the output 

      
      logger.warning("writing txt done")

    return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {output_file}", value=main_txt_path, visible=True),"Loaded"]

def download_file():
    return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
    

with gr.Blocks() as demo:
    gr.Markdown("First upload a pdf file and and then you'll be able download txt file")
    with gr.Row():
        u = gr.UploadButton("Upload a file", file_count="single",)
        d = gr.DownloadButton("Download the file", visible=False)

    label = gr.Label(label="Loader")
    u.upload(upload_file, u, [u, d,label])
    d.click(download_file, None, [u, d])


if __name__ == "__main__":
    username=os.getenv("username")
    Password = os.getenv("Password")
    demo.launch(auth=(username,Password),auth_message="If you don't have Login Credentials then please contact on IT Team")